Transfer utils module to Cython

This commit is contained in:
Roman Mogilatov 2016-11-04 11:12:37 +02:00
parent 74aea6f1dc
commit 7bcb882425
5 changed files with 48 additions and 21 deletions

View File

@ -10,6 +10,7 @@ follows `Semantic versioning`_
Development version Development version
------------------- -------------------
- Add ``dependency_injector.injections`` module (C extension). - Add ``dependency_injector.injections`` module (C extension).
- Transfer ``dependency_injector.utils`` module to Cython (C extension).
- Transfer ``dependency_injector.errors`` module to Cython (C extension). - Transfer ``dependency_injector.errors`` module to Cython (C extension).
- Remove ``@inject`` decorator. - Remove ``@inject`` decorator.
- Add makefile (``clean``, ``test``, ``build``, ``install``, ``uninstall`` - Add makefile (``clean``, ``test``, ``build``, ``install``, ``uninstall``

View File

@ -50,6 +50,10 @@ setup(name='dependency-injector',
['src/dependency_injector/injections.c'], ['src/dependency_injector/injections.c'],
define_macros=defined_macros, define_macros=defined_macros,
extra_compile_args=['-O2']), extra_compile_args=['-O2']),
Extension('dependency_injector.utils',
['src/dependency_injector/utils.c'],
define_macros=defined_macros,
extra_compile_args=['-O2']),
Extension('dependency_injector.errors', Extension('dependency_injector.errors',
['src/dependency_injector/errors.c'], ['src/dependency_injector/errors.c'],
define_macros=defined_macros, define_macros=defined_macros,

View File

@ -5,7 +5,7 @@ Powered by Cython.
cimport cython cimport cython
from .utils import ( from .utils cimport (
is_provider, is_provider,
is_delegated, is_delegated,
) )

View File

@ -0,0 +1,16 @@
"""Dependency injector utils.
Powered by Cython.
"""
cpdef bint is_provider(object instance)
cpdef object ensure_is_provider(object instance)
cpdef bint is_delegated(object instance)
cpdef bint is_container(object instance)
cpdef str represent_provider(object provider, object provides)
cpdef object deepcopy(object instance, dict memo=*)

View File

@ -1,13 +1,16 @@
"""Dependency injector utils module.""" """Dependency injector utils.
Powered by Cython.
"""
cimport cpython.version
from dependency_injector cimport errors
import copy as _copy import copy as _copy
import types import types
import threading import threading
import six
from dependency_injector import errors
GLOBAL_LOCK = threading.RLock() GLOBAL_LOCK = threading.RLock()
"""Dependency injector global reentrant lock. """Dependency injector global reentrant lock.
@ -15,14 +18,19 @@ GLOBAL_LOCK = threading.RLock()
:type: :py:class:`threading.RLock` :type: :py:class:`threading.RLock`
""" """
if six.PY2: # pragma: no cover if cpython.version.PY_MAJOR_VERSION < 3: # pragma: no cover
CLASS_TYPES = (type, types.ClassType)
_copy._deepcopy_dispatch[types.MethodType] = \ _copy._deepcopy_dispatch[types.MethodType] = \
lambda obj, memo: type(obj)(obj.im_func, lambda obj, memo: type(obj)(obj.im_func,
_copy.deepcopy(obj.im_self, memo), _copy.deepcopy(obj.im_self, memo),
obj.im_class) obj.im_class)
else: # pragma: no cover
CLASS_TYPES = (type,)
def is_provider(instance):
cpdef bint is_provider(object instance):
"""Check if instance is provider instance. """Check if instance is provider instance.
:param instance: Instance to be checked. :param instance: Instance to be checked.
@ -30,12 +38,11 @@ def is_provider(instance):
:rtype: bool :rtype: bool
""" """
return (not isinstance(instance, six.class_types) and return (not isinstance(instance, CLASS_TYPES) and
hasattr(instance, '__IS_PROVIDER__') and getattr(instance, '__IS_PROVIDER__', False) is True)
getattr(instance, '__IS_PROVIDER__') is True)
def ensure_is_provider(instance): cpdef object ensure_is_provider(object instance):
"""Check if instance is provider instance and return it. """Check if instance is provider instance and return it.
:param instance: Instance to be checked. :param instance: Instance to be checked.
@ -52,7 +59,7 @@ def ensure_is_provider(instance):
return instance return instance
def is_delegated(instance): cpdef bint is_delegated(object instance):
"""Check if instance is delegated provider. """Check if instance is delegated provider.
:param instance: Instance to be checked. :param instance: Instance to be checked.
@ -60,12 +67,11 @@ def is_delegated(instance):
:rtype: bool :rtype: bool
""" """
return (not isinstance(instance, six.class_types) and return (not isinstance(instance, CLASS_TYPES) and
hasattr(instance, '__IS_DELEGATED__') and getattr(instance, '__IS_DELEGATED__', False) is True)
getattr(instance, '__IS_DELEGATED__') is True)
def is_container(instance): cpdef bint is_container(object instance):
"""Check if instance is container instance. """Check if instance is container instance.
:param instance: Instance to be checked. :param instance: Instance to be checked.
@ -73,11 +79,10 @@ def is_container(instance):
:rtype: bool :rtype: bool
""" """
return (hasattr(instance, '__IS_CONTAINER__') and return getattr(instance, '__IS_CONTAINER__', False) is True
getattr(instance, '__IS_CONTAINER__', False) is True)
def represent_provider(provider, provides): cpdef str represent_provider(object provider, object provides):
"""Return string representation of provider. """Return string representation of provider.
:param provider: Provider object :param provider: Provider object
@ -96,6 +101,7 @@ def represent_provider(provider, provides):
address=hex(id(provider))) address=hex(id(provider)))
def deepcopy(instance, memo=None): cpdef object deepcopy(object instance, dict memo=None):
"""Make full copy of instance.""" """Make full copy of instance."""
return _copy.deepcopy(instance, memo) return _copy.deepcopy(instance, memo)