Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def safe_copy(ptr):
"""Safely cast memory address to char pointer, convert to python string,
and immediately free resources.
:param ptr: The memory address to convert to text string.
:type ptr: :class:`ctypes.c_void_p`
:returns: :class:`tuple` (:class:`ctypes.c_void_p`, :class:`str`)
.. versionadded:: 0.5.3
"""
string = ''
if bool(ptr):
string = ctypes.cast(ptr, ctypes.c_char_p).value
ptr = library.MagickRelinquishMemory(ptr)
return ptr, text(string)
def __iter__(self):
image = self.image
num = ctypes.c_size_t()
props_p = library.MagickGetImageProperties(image.wand, b'', num)
props = [text(props_p[i]) for i in xrange(num.value)]
library.MagickRelinquishMemory(props_p)
return iter(props)
def __del__(self):
"""Relinquishes memory allocated by ImageMagick.
We don't need to worry about checking for ``NULL`` because
:c:func:`MagickRelinquishMemory` does that for us.
Note alslo that :class:`ctypes.c_char_p` has no
:meth:`~object.__del__` method, so we don't need to
(and indeed can't) call the superclass destructor.
"""
try:
from wand.api import library # Lazy load global library
library.MagickRelinquishMemory(self)
except ImportError:
# Python my be shutting down; and such, ``sys.meta_path``
# may not be available.
pass
.. versionadded:: 0.4.0
.. versionchanged: 0.4.1
Safely release allocated memory with
:c:func:`MagickRelinquishMemory` instead of :c:func:`libc.free`.
"""
number_elements = ctypes.c_size_t(0)
dash_array_p = library.DrawGetStrokeDashArray(
self.resource, ctypes.byref(number_elements)
)
dash_array = []
if dash_array_p is not None:
dash_array = [float(dash_array_p[i])
for i in xrange(number_elements.value)]
library.MagickRelinquishMemory(dash_array_p)
return dash_array
"""
if format is not None:
with self.convert(format) as converted:
return converted.make_blob()
library.MagickResetIterator(self.wand)
length = ctypes.c_size_t()
blob_p = None
if len(self.sequence) > 1:
blob_p = library.MagickGetImagesBlob(self.wand,
ctypes.byref(length))
else:
blob_p = library.MagickGetImageBlob(self.wand,
ctypes.byref(length))
if blob_p and length.value:
blob = ctypes.string_at(blob_p, length.value)
library.MagickRelinquishMemory(blob_p)
return blob
self.raise_exception()
def _repr_png_(self): # pragma: no cover
library.MagickResetIterator(self.image.wand)
repr_wand = library.MagickAppendImages(self.image.wand, 1)
length = ctypes.c_size_t()
blob_p = library.MagickGetImagesBlob(repr_wand,
ctypes.byref(length))
if blob_p and length.value:
blob = ctypes.string_at(blob_p, length.value)
library.MagickRelinquishMemory(blob_p)
return blob
else:
return None
def __len__(self):
image = self.image
num = ctypes.c_size_t()
props_p = library.MagickGetImageProperties(image.wand, b'', num)
library.MagickRelinquishMemory(props_p)
return num.value
font_metrics_f = library.MagickQueryFontMetrics
if isinstance(text, text_type):
if self.text_encoding:
text = text.encode(self.text_encoding)
else:
text = binary(text)
result = font_metrics_f(image.wand, self.resource, text)
if not result: # pragma: no cover
# Error on drawing context
self.raise_exception()
# Or error on image canvas
image.raise_exception()
# Generate a generic error if ImageMagick couldn't emit one.
raise ValueError('Unable to render text with current font.')
args = [result[i] for i in xrange(13)]
library.MagickRelinquishMemory(result)
return FontMetrics(*args)