diff --git a/src/dependency_injector/providers/base.pyx b/src/dependency_injector/providers/base.pyx index 05049f20..eb4e2859 100644 --- a/src/dependency_injector/providers/base.pyx +++ b/src/dependency_injector/providers/base.pyx @@ -120,7 +120,6 @@ cdef class Provider(object): if not is_provider(provider): provider = Object(provider) - print(self.__overridden, provider) self.__overridden += (provider,) self.__overridden_len += 1 diff --git a/src/dependency_injector/providers/callables.pxd b/src/dependency_injector/providers/callables.pxd index 357a038e..f529526b 100644 --- a/src/dependency_injector/providers/callables.pxd +++ b/src/dependency_injector/providers/callables.pxd @@ -7,6 +7,8 @@ from .base cimport Provider from .injections cimport ( PositionalInjection, NamedInjection, + __provide_positional_args, + __provide_keyword_args, ) @@ -22,6 +24,19 @@ cdef class Callable(Provider): cpdef object _provide(self, tuple args, dict kwargs) + cdef inline object __provide(self, tuple args, dict kwargs): + cdef tuple positional_args + cdef dict keyword_args + + positional_args = __provide_positional_args(args, + self.__args, + self.__args_len) + keyword_args = __provide_keyword_args(kwargs, + self.__kwargs, + self.__kwargs_len) + + return self.__provides(*positional_args, **keyword_args) + cdef class DelegatedCallable(Callable): pass diff --git a/src/dependency_injector/providers/callables.pyx b/src/dependency_injector/providers/callables.pyx index 2ca4d897..370f6619 100644 --- a/src/dependency_injector/providers/callables.pyx +++ b/src/dependency_injector/providers/callables.pyx @@ -11,8 +11,6 @@ from .injections cimport ( NamedInjection, parse_positional_injections, parse_named_injections, - __provide_positional_args, - __provide_keyword_args, ) from .utils import represent_provider @@ -180,17 +178,7 @@ cdef class Callable(Provider): cpdef object _provide(self, tuple args, dict kwargs): """Return result of provided callable's call.""" - cdef tuple positional_args - cdef dict keyword_args - - positional_args = __provide_positional_args(args, - self.__args, - self.__args_len) - keyword_args = __provide_keyword_args(kwargs, - self.__kwargs, - self.__kwargs_len) - - return self.__provides(*positional_args, **keyword_args) + return self.__provide(args, kwargs) cdef class DelegatedCallable(Callable): diff --git a/src/dependency_injector/providers/factories.pxd b/src/dependency_injector/providers/factories.pxd index 28e3d354..89399e17 100644 --- a/src/dependency_injector/providers/factories.pxd +++ b/src/dependency_injector/providers/factories.pxd @@ -4,16 +4,30 @@ Powered by Cython. """ from .base cimport Provider +from .callables cimport Callable +from .injections cimport __inject_attributes cdef class Factory(Provider): - cdef object __instantiator + cdef Callable __instantiator cdef tuple __attributes cdef int __attributes_len cpdef object _provide(self, tuple args, dict kwargs) + cdef inline object __provide(self, tuple args, dict kwargs): + cdef object instance + + instance = self.__instantiator.__provide(args, kwargs) + + if self.__attributes_len > 0: + __inject_attributes(instance, + self.__attributes, + self.__attributes_len) + + return instance + cdef class DelegatedFactory(Factory): pass diff --git a/src/dependency_injector/providers/factories.pyx b/src/dependency_injector/providers/factories.pyx index 607f9319..c54ee9ac 100644 --- a/src/dependency_injector/providers/factories.pyx +++ b/src/dependency_injector/providers/factories.pyx @@ -8,13 +8,8 @@ from dependency_injector.errors import Error from .base cimport Provider from .callables cimport Callable from .injections cimport ( - PositionalInjection, NamedInjection, - parse_positional_injections, parse_named_injections, - __provide_positional_args, - __provide_keyword_args, - __inject_attributes, ) from .utils import represent_provider @@ -88,14 +83,11 @@ cdef class Factory(Provider): raise Error('{0} can provide only {1} instances'.format( self.__class__, self.__class__.provided_type)) - self.__instantiator = Callable(provides) + self.__instantiator = Callable(provides, *args, **kwargs) self.__attributes = tuple() self.__attributes_len = 0 - self.set_args(*args) - self.set_kwargs(**kwargs) - super(Factory, self).__init__() @@ -236,16 +228,7 @@ cdef class Factory(Provider): cpdef object _provide(self, tuple args, dict kwargs): """Return new instance.""" - cdef object instance - - instance = self.__instantiator._provide(args, kwargs) - - if self.__attributes_len > 0: - __inject_attributes(instance, - self.__attributes, - self.__attributes_len) - - return instance + return self.__provide(args, kwargs) cdef class DelegatedFactory(Factory): diff --git a/src/dependency_injector/providers/injections.pxd b/src/dependency_injector/providers/injections.pxd index 66a9ed2b..87a243a2 100644 --- a/src/dependency_injector/providers/injections.pxd +++ b/src/dependency_injector/providers/injections.pxd @@ -53,7 +53,7 @@ cdef inline tuple __provide_positional_args(tuple args, positional_args = list() for index in range(inj_args_len): injection = inj_args[index] - positional_args.append(injection.get_value()) + positional_args.append(injection.__get_value()) positional_args.extend(args) return tuple(positional_args) @@ -66,20 +66,19 @@ cdef inline dict __provide_keyword_args(dict kwargs, int inj_kwargs_len): cdef int index cdef object name - cdef int kwargs_len cdef NamedInjection kw_injection - kwargs_len = len(kwargs) - if kwargs_len == 0: + if len(kwargs) == 0: for index in range(inj_kwargs_len): kw_injection = inj_kwargs[index] - kwargs[kw_injection.get_name()] = kw_injection.get_value() + name = kw_injection.__get_name() + kwargs[name] = kw_injection.__get_value() else: for index in range(inj_kwargs_len): kw_injection = inj_kwargs[index] - name = kw_injection.get_name() + name = kw_injection.__get_name() if name not in kwargs: - kwargs[name] = kw_injection.get_value() + kwargs[name] = kw_injection.__get_value() return kwargs @@ -93,8 +92,8 @@ cdef inline object __inject_attributes(object instance, for index in range(attributes_len): attr_injection = attributes[index] setattr(instance, - attr_injection.get_name(), - attr_injection.get_value()) + attr_injection.__get_name(), + attr_injection.__get_value()) cpdef tuple parse_positional_injections(tuple args) diff --git a/src/dependency_injector/providers/injections.pyx b/src/dependency_injector/providers/injections.pyx index 39e41593..6b6a71dc 100644 --- a/src/dependency_injector/providers/injections.pyx +++ b/src/dependency_injector/providers/injections.pyx @@ -23,7 +23,8 @@ cdef class PositionalInjection(Injection): self.__value = value self.__is_provider = is_provider(value) self.__is_delegated = is_delegated(value) - self.__call = self.__is_provider == 1 and self.__is_delegated == 0 + self.__call = (self.__is_provider == 1 and + self.__is_delegated == 0) def get_value(self): """Return injection value.""" @@ -43,7 +44,8 @@ cdef class NamedInjection(Injection): self.__value = value self.__is_provider = is_provider(value) self.__is_delegated = is_delegated(value) - self.__call = self.__is_provider == 1 and self.__is_delegated == 0 + self.__call = (self.__is_provider == 1 and + self.__is_delegated == 0) def get_name(self): """Return injection value."""