[pypy-svn] r34010 - in pypy/dist/pypy/config: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Wed Nov 1 13:24:12 CET 2006


Author: cfbolz
Date: Wed Nov  1 13:24:11 2006
New Revision: 34010

Modified:
   pypy/dist/pypy/config/config.py
   pypy/dist/pypy/config/test/test_config.py
Log:
(cfbolz, pedronis and arigo discussing)
add copy method


Modified: pypy/dist/pypy/config/config.py
==============================================================================
--- pypy/dist/pypy/config/config.py	(original)
+++ pypy/dist/pypy/config/config.py	Wed Nov  1 13:24:11 2006
@@ -31,6 +31,25 @@
             homeconfig, name = self._cfgimpl_get_home_by_path(name)
             homeconfig.setoption(name, value, 'default')
 
+    def copy(self, as_default=False, parent=None):
+        result = Config.__new__(self.__class__)
+        result._cfgimpl_descr = self._cfgimpl_descr
+        result._cfgimpl_value_owners = owners = {}
+        result._cfgimpl_parent = parent
+        result._cfgimpl_values = v = {}
+        for child in self._cfgimpl_descr._children:
+            if isinstance(child, Option):
+                v[child._name] = self._cfgimpl_values[child._name]
+                if as_default:
+                    owners[child._name] = 'default'
+                else:
+                    owners[child._name] = (
+                        self._cfgimpl_value_owners[child._name])
+            elif isinstance(child, OptionDescription):
+                v[child._name] = self._cfgimpl_values[child._name].copy(
+                    as_default, parent=result)
+        return result
+
     def __setattr__(self, name, value):
         if self._cfgimpl_frozen and getattr(self, name) != value:
             raise TypeError("trying to change a frozen option object")

Modified: pypy/dist/pypy/config/test/test_config.py
==============================================================================
--- pypy/dist/pypy/config/test/test_config.py	(original)
+++ pypy/dist/pypy/config/test/test_config.py	Wed Nov  1 13:24:11 2006
@@ -424,3 +424,23 @@
     d = make_dict(config)
     assert d == {"s1.a": True, "int": 43}
 
+def test_copy():
+    descr = OptionDescription("opt", "", [
+        OptionDescription("s1", "", [
+            BoolOption("a", "", default=False)]),
+        IntOption("int", "", default=42)])
+    c1 = Config(descr)
+    c1.int = 43
+    c2 = c1.copy()
+    assert c2.int == 43
+    assert not c2.s1.a
+    c2.s1.a = True
+    assert c2.s1.a
+    py.test.raises(ValueError, "c2.int = 44")
+    c2 = c1.copy(as_default=True)
+    assert c2.int == 43
+    assert not c2.s1.a
+    c2.s1.a = True
+    assert c2.s1.a
+    c2.int = 44 # does not crash
+



More information about the Pypy-commit mailing list