diff options
| author | Ben Kraft <benjaminjkraft@gmail.com> | 2016-01-23 18:02:15 -0800 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-01-26 06:56:21 -0500 |
| commit | 13023ba86746980aace2341ba32a9419e7567751 (patch) | |
| tree | 77ee5f8c4583904508bdabbdcaf638756d800e0f /django/utils/functional.py | |
| parent | cfda1fa3f8d95f0f4a369da9021dbd770e5fa44a (diff) | |
Fixed #26122 -- Fixed copying a LazyObject
Shallow copying of `django.utils.functional.LazyObject` or its subclasses has
been broken in a couple of different ways in the past, most recently due to
35355a4.
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 |
