Fix LazyItemsDict.__deepcopy__() for python 2.7.
authorZac Medico <zmedico@gentoo.org>
Fri, 29 Oct 2010 23:29:37 +0000 (16:29 -0700)
committerZac Medico <zmedico@gentoo.org>
Sat, 30 Oct 2010 03:40:32 +0000 (20:40 -0700)
In python-2.7, changes in deepcopy() make LazyItemsDict.__deepcopy__()
fail in some cases. Thanks to Diego E. Pettenò <flameeyes@g.o> for
reporting.

pym/portage/tests/ebuild/test_config.py
pym/portage/util/__init__.py

index 9e2d2eaaa6edd1019ef1187c3198f087871ae17a..9b6b13bab8ff224ef1cebbfa26ddb05f50f3ed45 100644 (file)
@@ -10,6 +10,28 @@ from portage.tests.resolver.ResolverPlayground import ResolverPlayground, Resolv
 
 class ConfigTestCase(TestCase):
 
+       def testClone(self):
+               """
+               Test the clone via constructor.
+               """
+
+               ebuilds = {
+                       "dev-libs/A-1": { },
+               }
+
+               playground = ResolverPlayground(ebuilds=ebuilds)
+               try:
+                       settings = config(clone=playground.settings)
+                       result = playground.run(["=dev-libs/A-1"])
+                       pkg, existing_node = result.depgraph._select_package(
+                               playground.root, "=dev-libs/A-1")
+                       settings.setcpv(pkg)
+
+                       # clone after setcpv tests deepcopy of LazyItemsDict
+                       settings2 = config(clone=settings)
+               finally:
+                       playground.cleanup()
+
        def testFeaturesMutation(self):
                """
                Test whether mutation of config.features updates the FEATURES
index 0b0e0435e7777f34eb1cc5bee20a8d562b21b9e9..d766690b309c3d2ff7f2e3c77566aaddbf441d13 100644 (file)
@@ -1314,15 +1314,12 @@ class LazyItemsDict(UserDict):
                        k_copy = deepcopy(k, memo)
                        if k in self.lazy_items:
                                lazy_item = self.lazy_items[k]
-                               try:
-                                       result.lazy_items[k_copy] = deepcopy(lazy_item, memo)
-                               except TypeError:
-                                       if not lazy_item.singleton:
-                                               raise
-                                       UserDict.__setitem__(result,
-                                               k_copy, deepcopy(self[k], memo))
-                               else:
-                                       UserDict.__setitem__(result, k_copy, None)
+                               if not lazy_item.singleton:
+                                       raise TypeError("LazyItemsDict deepcopy is " + \
+                                               "unsafe with lazy items that " + \
+                                               "are not singletons: %s" % (lazy_item,))
+                               UserDict.__setitem__(result,
+                                       k_copy, deepcopy(self[k], memo))
                        else:
                                UserDict.__setitem__(result, k_copy, deepcopy(self[k], memo))
                return result