]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Add __copy__ and __deepcopy__ to LazyProxy. 100/head
authorastaric <anze.staric@gmail.com>
Thu, 12 Jun 2014 07:33:36 +0000 (09:33 +0200)
committerastaric <anze.staric@gmail.com>
Wed, 5 Aug 2015 16:21:00 +0000 (18:21 +0200)
Python's copy.copy and copy.deepcopy do not call objects __init__,
resulting in endless recursion.

babel/support.py
tests/test_support.py

index c720c747cd7e1bd16f349486c99d96ae382ee11c..5ab97a5b23fdcc6a23943aab5dddd8abe00d382d 100644 (file)
@@ -263,6 +263,23 @@ class LazyProxy(object):
     def __setitem__(self, key, value):
         self.value[key] = value
 
+    def __copy__(self):
+        return LazyProxy(
+            self._func,
+            enable_cache=self._is_cache_enabled,
+            *self._args,
+            **self._kwargs
+        )
+
+    def __deepcopy__(self, memo):
+        from copy import deepcopy
+        return LazyProxy(
+            deepcopy(self._func, memo),
+            enable_cache=deepcopy(self._is_cache_enabled, memo),
+            *deepcopy(self._args, memo),
+            **deepcopy(self._kwargs, memo)
+        )
+
 
 class NullTranslations(gettext.NullTranslations, object):
 
index 8c182fc7756ee89ab0b112a64eaf447f9b10637b..4647f6b13be6539fe922070c1a1467d408dbb933 100644 (file)
@@ -243,6 +243,33 @@ class LazyProxyTestCase(unittest.TestCase):
         self.assertEqual(1, proxy.value)
         self.assertEqual(2, proxy.value)
 
+    def test_can_copy_proxy(self):
+        from copy import copy
+
+        numbers = [1,2]
+        def first(xs):
+            return xs[0]
+
+        proxy = support.LazyProxy(first, numbers)
+        proxy_copy = copy(proxy)
+
+        numbers.pop(0)
+        self.assertEqual(2, proxy.value)
+        self.assertEqual(2, proxy_copy.value)
+
+    def test_can_deepcopy_proxy(self):
+        from copy import deepcopy
+        numbers = [1,2]
+        def first(xs):
+            return xs[0]
+
+        proxy = support.LazyProxy(first, numbers)
+        proxy_deepcopy = deepcopy(proxy)
+
+        numbers.pop(0)
+        self.assertEqual(2, proxy.value)
+        self.assertEqual(1, proxy_deepcopy.value)
+
 
 def test_format_date():
     fmt = support.Format('en_US')