Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""":mod:`wand.cdefs.structures` --- MagickWand C typedefs 

2~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

3 

4.. versionadded:: 0.5.0 

5""" 

6import ctypes 

7import os 

8import platform 

9import sys 

10 

11__all__ = ('c_magick_char_p', 'c_magick_real_t', 'c_magick_size_t', 

12 'c_ssize_t') 

13 

14 

15class c_magick_char_p(ctypes.c_char_p): 

16 """This subclass prevents the automatic conversion behavior of 

17 :class:`ctypes.c_char_p`, allowing memory to be properly freed in the 

18 destructor. It must only be used for non-const character pointers 

19 returned by ImageMagick functions. 

20 

21 """ 

22 

23 def __del__(self): 

24 """Relinquishes memory allocated by ImageMagick. 

25 We don't need to worry about checking for ``NULL`` because 

26 :c:func:`MagickRelinquishMemory` does that for us. 

27 Note alslo that :class:`ctypes.c_char_p` has no 

28 :meth:`~object.__del__` method, so we don't need to 

29 (and indeed can't) call the superclass destructor. 

30 

31 """ 

32 try: 

33 from wand.api import library # Lazy load global library 

34 library.MagickRelinquishMemory(self) 

35 except ImportError: 

36 # Python my be shutting down; and such, ``sys.meta_path`` 

37 # may not be available. 

38 pass 

39 

40 

41if not hasattr(ctypes, 'c_ssize_t'): 

42 if ctypes.sizeof(ctypes.c_uint) == ctypes.sizeof(ctypes.c_void_p): 

43 ctypes.c_ssize_t = ctypes.c_int 

44 elif ctypes.sizeof(ctypes.c_ulong) == ctypes.sizeof(ctypes.c_void_p): 

45 ctypes.c_ssize_t = ctypes.c_long 

46 elif ctypes.sizeof(ctypes.c_ulonglong) == ctypes.sizeof(ctypes.c_void_p): 

47 ctypes.c_ssize_t = ctypes.c_longlong 

48c_ssize_t = ctypes.c_ssize_t 

49 

50 

51env_real = os.getenv('WAND_REAL_TYPE', 'auto') 

52if env_real in ('double', 'c_double'): 

53 c_magick_real_t = ctypes.c_double 

54elif env_real in ('longdouble', 'c_longdouble'): 

55 c_magick_real_t = ctypes.c_longdouble 

56else: 

57 # Attempt to guess MagickRealType size 

58 if sys.maxsize > 2**32: 

59 c_magick_real_t = ctypes.c_double 

60 else: 

61 c_magick_real_t = ctypes.c_longdouble 

62del env_real 

63 

64 

65# FIXME: Might need to rewrite to check against c_void_p size; 

66# like `c_ssize_t` above, and not against window platform. 

67if sys.maxsize > 2**32: 

68 c_magick_size_t = ctypes.c_size_t 

69elif platform.system() == "Windows": 

70 c_magick_size_t = ctypes.c_ulonglong 

71else: 

72 c_magick_size_t = ctypes.c_size_t