diff options
Diffstat (limited to 'django/utils/functional.py')
| -rw-r--r-- | django/utils/functional.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/django/utils/functional.py b/django/utils/functional.py index 2830cc47ab..f674a9bba2 100644 --- a/django/utils/functional.py +++ b/django/utils/functional.py @@ -249,6 +249,8 @@ class LazyObject(object): _wrapped = None def __init__(self): + # Note: if a subclass overrides __init__(), it will likely need to + # override __copy__() and __deepcopy__() as well. self._wrapped = empty __getattr__ = new_method_proxy(getattr) @@ -301,6 +303,15 @@ class LazyObject(object): def __getstate__(self): return {} + def __copy__(self): + if self._wrapped is empty: + # If uninitialized, copy the wrapper. Use type(self), not + # self.__class__, because the latter is proxied. + return type(self)() + else: + # If initialized, return a copy of the wrapped object. + return copy.copy(self._wrapped) + def __deepcopy__(self, memo): if self._wrapped is empty: # We have to use type(self), not self.__class__, because the @@ -377,6 +388,15 @@ class SimpleLazyObject(LazyObject): repr_attr = self._wrapped return '<%s: %r>' % (type(self).__name__, repr_attr) + def __copy__(self): + if self._wrapped is empty: + # If uninitialized, copy the wrapper. Use SimpleLazyObject, not + # self.__class__, because the latter is proxied. + return SimpleLazyObject(self._setupfunc) + else: + # If initialized, return a copy of the wrapped object. + return copy.copy(self._wrapped) + def __deepcopy__(self, memo): if self._wrapped is empty: # We have to use SimpleLazyObject, not self.__class__, because the |
