diff --git a/dependency_injector/catalogs/utils.py b/dependency_injector/catalogs/utils.py index 367e1b5f..150c2ffc 100644 --- a/dependency_injector/catalogs/utils.py +++ b/dependency_injector/catalogs/utils.py @@ -2,8 +2,7 @@ import six -from copy import deepcopy - +from dependency_injector.utils import _copy_providers from dependency_injector.errors import UndefinedProviderError @@ -40,7 +39,7 @@ def copy(catalog): else: memo[id(source_provider)] = provider - copied_catalog.bind_providers(deepcopy(catalog.providers, memo), + copied_catalog.bind_providers(_copy_providers(catalog.providers, memo), force=True) return copied_catalog diff --git a/dependency_injector/utils.py b/dependency_injector/utils.py index c677d162..15941129 100644 --- a/dependency_injector/utils.py +++ b/dependency_injector/utils.py @@ -1,6 +1,8 @@ """Utils module.""" import sys +import copy +import types import threading import six @@ -20,6 +22,12 @@ if _IS_PYPY or six.PY3: # pragma: no cover else: # pragma: no cover _OBJECT_INIT = None +if six.PY2: # pragma: no cover + copy._deepcopy_dispatch[types.MethodType] = \ + lambda obj, memo: type(obj)(obj.im_func, + copy.deepcopy(obj.im_self, memo), + obj.im_class) + def is_provider(instance): """Check if instance is provider instance. @@ -245,3 +253,8 @@ def fetch_cls_init(cls): return None else: return cls_init + + +def _copy_providers(providers, memo=None): + """Make full copy of providers dictionary.""" + return copy.deepcopy(providers, memo)