From 78cb4296cceff6494205b15cf0030f7ed3bb8e7c Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Fri, 4 Nov 2016 10:41:40 +0200 Subject: [PATCH] Add is_delegated() method to utils --- src/dependency_injector/injections.pxd | 46 ++++++++++++++++++-------- src/dependency_injector/injections.pyx | 26 ++++++++++++--- src/dependency_injector/utils.py | 13 ++++++++ 3 files changed, 66 insertions(+), 19 deletions(-) diff --git a/src/dependency_injector/injections.pxd b/src/dependency_injector/injections.pxd index 5069d1ff..d6969714 100644 --- a/src/dependency_injector/injections.pxd +++ b/src/dependency_injector/injections.pxd @@ -3,8 +3,7 @@ Cython optimized code. """ -cpdef tuple parse_positional_injections(tuple args) -cpdef tuple parse_named_injections(dict kwargs) +cimport cython cdef class Injection: @@ -39,25 +38,44 @@ cdef class NamedInjection(Injection): return self.__value() +@cython.boundscheck(False) +@cython.wraparound(False) cdef inline tuple __provide_positional_args(tuple inj_args, int inj_args_len, tuple args): + cdef int index + cdef list positional_args cdef PositionalInjection injection - if inj_args_len > 0: - positional_args = list() - for index in range(inj_args_len): - injection = inj_args[index] - positional_args.append(injection.get_value()) - positional_args.extend(args) - args = positional_args - return args + if inj_args_len == 0: + return args + + positional_args = list() + for index in range(inj_args_len): + injection = inj_args[index] + positional_args.append(injection.get_value()) + positional_args.extend(args) + + return positional_args + + +@cython.boundscheck(False) +@cython.wraparound(False) cdef inline dict __provide_keyword_args(tuple inj_kwargs, int inj_kwargs_len, dict kwargs): + cdef int index cdef NamedInjection kw_injection - if inj_kwargs_len > 0: - for index in range(inj_kwargs_len): - kw_injection = inj_kwargs[index] - kwargs[kw_injection.get_name()] = kw_injection.get_value() + + if inj_kwargs_len == 0: + return kwargs + + for index in range(inj_kwargs_len): + kw_injection = inj_kwargs[index] + kwargs[kw_injection.get_name()] = kw_injection.get_value() + return kwargs + + +cpdef tuple parse_positional_injections(tuple args) +cpdef tuple parse_named_injections(dict kwargs) diff --git a/src/dependency_injector/injections.pyx b/src/dependency_injector/injections.pyx index b17f718f..1afd99c7 100644 --- a/src/dependency_injector/injections.pyx +++ b/src/dependency_injector/injections.pyx @@ -3,8 +3,12 @@ Cython optimized code. """ -# TODO: replace to cimport -from .utils import is_provider +cimport cython + +from .utils import ( + is_provider, + is_delegated, +) cdef class Injection: @@ -18,13 +22,17 @@ cdef class PositionalInjection(Injection): """Initializer.""" self.__value = value self.__is_provider = is_provider(value) - self.__is_delegated = 0 # TODO: use utils.is_delegated() + self.__is_delegated = is_delegated(value) self.__call = self.__is_provider == 1 and self.__is_delegated == 0 def get_value(self): """Return injection value.""" return self.__get_value() + def get_original_value(self): + """Return original value.""" + return self.__value + cdef class NamedInjection(Injection): """Keyword injection class.""" @@ -34,7 +42,7 @@ cdef class NamedInjection(Injection): self.__name = name self.__value = value self.__is_provider = is_provider(value) - self.__is_delegated = 0 # TODO: use utils.is_delegated() + self.__is_delegated = is_delegated(value) self.__call = self.__is_provider == 1 and self.__is_delegated == 0 def get_name(self): @@ -45,14 +53,20 @@ cdef class NamedInjection(Injection): """Return injection value.""" return self.__get_value() + def get_original_value(self): + """Return original value.""" + return self.__value + +@cython.boundscheck(False) +@cython.wraparound(False) cpdef tuple parse_positional_injections(tuple args): """Parse positional injections.""" cdef list injections = list() cdef int args_len = len(args) - cdef object arg cdef int index + cdef object arg cdef PositionalInjection injection for index in range(args_len): @@ -63,6 +77,8 @@ cpdef tuple parse_positional_injections(tuple args): return tuple(injections) +@cython.boundscheck(False) +@cython.wraparound(False) cpdef tuple parse_named_injections(dict kwargs): """Parse named injections.""" cdef list injections = list() diff --git a/src/dependency_injector/utils.py b/src/dependency_injector/utils.py index cfe8b31e..550fdcc1 100644 --- a/src/dependency_injector/utils.py +++ b/src/dependency_injector/utils.py @@ -52,6 +52,19 @@ def ensure_is_provider(instance): return instance +def is_delegated(instance): + """Check if instance is delegated provider. + + :param instance: Instance to be checked. + :type instance: object + + :rtype: bool + """ + return (not isinstance(instance, six.class_types) and + hasattr(instance, '__IS_DELEGATED__') and + getattr(instance, '__IS_DELEGATED__') is True) + + def is_container(instance): """Check if instance is container instance.