/usr/lib/python3/dist-packages/wand/resource.py is in python3-wand 0.3.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | """:mod:`wand.resource` --- Global resource management
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There is the global resource to manage in MagickWand API. This module
implements automatic global resource management through reference counting.
"""
import contextlib
import ctypes
import warnings
from .api import library
from .exceptions import TYPE_MAP, WandException
__all__ = ('genesis', 'terminus', 'increment_refcount', 'decrement_refcount',
'Resource', 'DestroyedResourceError')
def genesis():
"""Instantiates the MagickWand API.
.. warning::
Don't call this function directly. Use :func:`increment_refcount()` and
:func:`decrement_refcount()` functions instead.
"""
library.MagickWandGenesis()
def terminus():
"""Cleans up the MagickWand API.
.. warning::
Don't call this function directly. Use :func:`increment_refcount()` and
:func:`decrement_refcount()` functions instead.
"""
library.MagickWandTerminus()
#: (:class:`numbers.Integral`) The internal integer value that maintains
#: the number of referenced objects.
#:
#: .. warning::
#:
#: Don't touch this global variable. Use :func:`increment_refcount()` and
#: :func:`decrement_refcount()` functions instead.
#:
reference_count = 0
def increment_refcount():
"""Increments the :data:`reference_count` and instantiates the MagickWand
API if it is the first use.
"""
global reference_count
if reference_count:
reference_count += 1
else:
genesis()
reference_count = 1
def decrement_refcount():
"""Decrements the :data:`reference_count` and cleans up the MagickWand
API if it will be no more used.
"""
global reference_count
if not reference_count:
raise RuntimeError('wand.resource.reference_count is already zero')
reference_count -= 1
if not reference_count:
terminus()
class Resource(object):
"""Abstract base class for MagickWand object that requires resource
management. Its all subclasses manage the resource semiautomatically
and support :keyword:`with` statement as well::
with Resource() as resource:
# use the resource...
pass
It doesn't implement constructor by itself, so subclasses should
implement it. Every constructor should assign the pointer of its
resource data into :attr:`resource` attribute inside of :keyword:`with`
:meth:`allocate()` context. For example::
class Pizza(Resource):
'''My pizza yummy.'''
def __init__(self):
with self.allocate():
self.resource = library.NewPizza()
.. versionadded:: 0.1.2
"""
#: (:class:`ctypes.CFUNCTYPE`) The :mod:`ctypes` predicate function
#: that returns whether the given pointer (that contains a resource data
#: usuaully) is a valid resource.
#:
#: .. note::
#:
#: It is an abstract attribute that has to be implemented
#: in the subclass.
c_is_resource = NotImplemented
#: (:class:`ctypes.CFUNCTYPE`) The :mod:`ctypes` function that destroys
#: the :attr:`resource`.
#:
#: .. note::
#:
#: It is an abstract attribute that has to be implemented
#: in the subclass.
c_destroy_resource = NotImplemented
#: (:class:`ctypes.CFUNCTYPE`) The :mod:`ctypes` function that gets
#: an exception from the :attr:`resource`.
#:
#: .. note::
#:
#: It is an abstract attribute that has to be implemented
#: in the subclass.
c_get_exception = NotImplemented
#: (:class:`ctypes.CFUNCTYPE`) The :mod:`ctypes` function that clears
#: an exception of the :attr:`resource`.
#:
#: .. note::
#:
#: It is an abstract attribute that has to be implemented
#: in the subclass.
c_clear_exception = NotImplemented
@property
def resource(self):
"""Internal pointer to the resource instance. It may raise
:exc:`DestroyedResourceError` when the resource has destroyed already.
"""
if getattr(self, 'c_resource', None) is None:
raise DestroyedResourceError(repr(self) + ' is destroyed already')
return self.c_resource
@resource.setter
def resource(self, resource):
# Delete the existing resource if there is one
if getattr(self, 'c_resource', None):
self.destroy()
if self.c_is_resource(resource):
self.c_resource = resource
else:
raise TypeError(repr(resource) + ' is an invalid resource')
increment_refcount()
@resource.deleter
def resource(self):
self.c_destroy_resource(self.resource)
self.c_resource = None
@contextlib.contextmanager
def allocate(self):
"""Allocates the memory for the resource explicitly. Its subclasses
should assign the created resource into :attr:`resource` attribute
inside of this context. For example::
with resource.allocate():
resource.resource = library.NewResource()
"""
increment_refcount()
try:
yield self
except:
decrement_refcount()
raise
def destroy(self):
"""Cleans up the resource explicitly. If you use the resource in
:keyword:`with` statement, it was called implicitly so have not to
call it.
"""
del self.resource
decrement_refcount()
def get_exception(self):
"""Gets a current exception instance.
:returns: a current exception. it can be ``None`` as well if any
errors aren't occurred
:rtype: :class:`wand.exceptions.WandException`
"""
severity = ctypes.c_int()
desc = self.c_get_exception(self.resource, ctypes.byref(severity))
if severity.value == 0:
return
self.c_clear_exception(self.wand)
exc_cls = TYPE_MAP[severity.value]
return exc_cls(desc.value)
def raise_exception(self, stacklevel=1):
"""Raises an exception or warning if it has occurred."""
e = self.get_exception()
if isinstance(e, Warning):
warnings.warn(e, stacklevel=stacklevel + 1)
elif isinstance(e, Exception):
raise e
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.destroy()
def __del__(self):
try:
self.destroy()
except DestroyedResourceError:
pass
class DestroyedResourceError(WandException, ReferenceError, AttributeError):
"""An error that rises when some code tries access to an already
destroyed resource.
.. versionchanged:: 0.3.0
It becomes a subtype of :exc:`wand.exceptions.WandException`.
"""
|