From: astaric Date: Thu, 12 Jun 2014 07:33:36 +0000 (+0200) Subject: Add __copy__ and __deepcopy__ to LazyProxy. X-Git-Tag: dev-2a51c9b95d06~38^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F100%2Fhead;p=thirdparty%2Fbabel.git Add __copy__ and __deepcopy__ to LazyProxy. Python's copy.copy and copy.deepcopy do not call objects __init__, resulting in endless recursion. --- diff --git a/babel/support.py b/babel/support.py index c720c747..5ab97a5b 100644 --- a/babel/support.py +++ b/babel/support.py @@ -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): diff --git a/tests/test_support.py b/tests/test_support.py index 8c182fc7..4647f6b1 100644 --- a/tests/test_support.py +++ b/tests/test_support.py @@ -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')