[pypy-svn] r33389 - in pypy/branch/even-more-config3/pypy/config: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Tue Oct 17 21:40:39 CEST 2006


Author: cfbolz
Date: Tue Oct 17 21:40:38 2006
New Revision: 33389

Modified:
   pypy/branch/even-more-config3/pypy/config/config.py
   pypy/branch/even-more-config3/pypy/config/test/test_config.py
Log:
(pedronis, cfbolz): add an arbitrary option


Modified: pypy/branch/even-more-config3/pypy/config/config.py
==============================================================================
--- pypy/branch/even-more-config3/pypy/config/config.py	(original)
+++ pypy/branch/even-more-config3/pypy/config/config.py	Tue Oct 17 21:40:38 2006
@@ -20,7 +20,7 @@
     def _cfgimpl_build(self, overrides):
         for child in self._cfgimpl_descr._children:
             if isinstance(child, Option):
-                self._cfgimpl_values[child._name] = child.default
+                self._cfgimpl_values[child._name] = child.getdefault()
                 self._cfgimpl_value_owners[child._name] = 'default'
             elif isinstance(child, OptionDescription):
                 self._cfgimpl_values[child._name] = Config(child, parent=self)
@@ -332,6 +332,25 @@
                                    action='callback', type='str',
                                    callback=_callback, *argnames)
 
+class ArbitraryOption(Option):
+    def __init__(self, name, doc, default=None, defaultfactory=None):
+        super(ArbitraryOption, self).__init__(name, doc, cmdline=None)
+        self.default = default
+        self.defaultfactory = defaultfactory
+        if defaultfactory is not None:
+            assert default is None
+
+    def validate(self, value):
+        return True
+
+    def add_optparse_option(self, *args, **kwargs):
+        return
+
+    def getdefault(self):
+        if self.defaultfactory is not None:
+            return self.defaultfactory()
+        return self.default
+
 class OptionDescription(object):
     def __init__(self, name, doc, children, cmdline=DEFAULT_OPTION_NAME):
         self._name = name

Modified: pypy/branch/even-more-config3/pypy/config/test/test_config.py
==============================================================================
--- pypy/branch/even-more-config3/pypy/config/test/test_config.py	(original)
+++ pypy/branch/even-more-config3/pypy/config/test/test_config.py	Tue Oct 17 21:40:38 2006
@@ -63,6 +63,24 @@
     py.test.raises(ValueError, 'config.gc.name = "ref"')
     config.gc.name = "framework"
 
+def test_arbitrary_option():
+    descr = OptionDescription("top", "", [
+        ArbitraryOption("a", "no help", default=None)
+    ])
+    config = Config(descr)
+    config.a = []
+    config.a.append(1)
+    assert config.a == [1]
+
+    descr = OptionDescription("top", "", [
+        ArbitraryOption("a", "no help", defaultfactory=list)
+    ])
+    c1 = Config(descr)
+    c2 = Config(descr)
+    c1.a.append(1)
+    assert c2.a == []
+    assert c1.a == [1]
+
 def test_annotator_folding():
     from pypy.translator.interactive import Translation
 



More information about the Pypy-commit mailing list