From a1cc3f1ca8149cddd97bd6823857cc9e3eba544e Mon Sep 17 00:00:00 2001 From: astaric Date: Thu, 12 Jun 2014 09:33:36 +0200 Subject: [PATCH] Add __copy__ and __deepcopy__ to LazyProxy. Python's copy.copy and copy.deepcopy do not call objects __init__, resulting in endless recursion. --- babel/support.py | 17 +++++++++++++++++ tests/test_support.py | 27 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) 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') -- 2.47.2