[pypy-svn] r29783 - in pypy/dist/pypy/config: . test
cfbolz at codespeak.net
cfbolz at codespeak.net
Sat Jul 8 10:40:05 CEST 2006
Author: cfbolz
Date: Sat Jul 8 10:40:01 2006
New Revision: 29783
Modified:
pypy/dist/pypy/config/config.py
pypy/dist/pypy/config/test/test_config.py
Log:
more option stuff:
* add an IntOption and a FloatOption
* prevent modification of options after freezing
* being able to loop over options
* remove the ListOption
Modified: pypy/dist/pypy/config/config.py
==============================================================================
--- pypy/dist/pypy/config/config.py (original)
+++ pypy/dist/pypy/config/config.py Sat Jul 8 10:40:01 2006
@@ -73,6 +73,11 @@
def __ne__(self, other):
return not self == other
+ def __iter__(self):
+ for child in self._descr._children:
+ if isinstance(child, Option):
+ yield child._name, getattr(self, child._name)
+
class Option(object):
def validate(self, value):
raise NotImplementedError('abstract base class')
@@ -111,28 +116,62 @@
subconfig.require(name, reqvalue)
super(BoolOption, self).setoption(config, value)
+class IntOption(Option):
+ def __init__(self, name, doc, default=True):
+ self._name = name
+ self.doc = doc
+ self.default = default
+
+ def validate(self, value):
+ try:
+ int(value)
+ except TypeError:
+ return False
+ return True
+
+ def setoption(self, config, value):
+ super(IntOption, self).setoption(config, int(value))
+
-class ListOption(Option):
- def __init__(self, name, doc, allowed_values, default=None):
+class IntOption(Option):
+ def __init__(self, name, doc, default=0):
self._name = name
self.doc = doc
- self.allowed_values = allowed_values
- self.default = default or []
+ self.default = default
def validate(self, value):
- assert isinstance(value, list)
- for item in value:
- if item not in self.allowed_values:
- return False
+ try:
+ int(value)
+ except TypeError:
+ return False
return True
- def getdefault(self):
- return self.default[:]
+ def setoption(self, config, value):
+ try:
+ super(IntOption, self).setoption(config, int(value))
+ except TypeError, e:
+ raise ValueError(*e.args)
+
+
+class FloatOption(Option):
+ def __init__(self, name, doc, default=0.0):
+ self._name = name
+ self.doc = doc
+ self.default = default
+
+ def validate(self, value):
+ try:
+ float(value)
+ except TypeError:
+ return False
+ return True
+
+ def setoption(self, config, value):
+ try:
+ super(FloatOption, self).setoption(config, float(value))
+ except TypeError, e:
+ raise ValueError(*e.args)
- def getkey(self, value):
- key = value[:]
- value.sort()
- return tuple(value)
class OptionDescription(object):
def __init__(self, name, children):
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 Sat Jul 8 10:40:01 2006
@@ -3,18 +3,19 @@
def make_description():
gcoption = ChoiceOption('name', 'GC name', ['ref', 'framework'], 'ref')
+ gcdummy = BoolOption('dummy', 'dummy', default=False)
objspaceoption = ChoiceOption('objspace', 'Object space',
['std', 'logic'], 'std')
- booloption = BoolOption('bool', 'Test Boolean option')
- listoption = ListOption('list', 'Test list valued option', ['foo', 'bar'],
- ['foo'])
+ booloption = BoolOption('bool', 'Test boolean option')
+ intoption = IntOption('int', 'Test int option')
+ floatoption = FloatOption('float', 'Test float option', default=2.3)
wantref_option = BoolOption('wantref', 'Test requires', default=False,
requires=[('gc.name', 'ref')])
- gcgroup = OptionDescription('gc', [gcoption])
+ gcgroup = OptionDescription('gc', [gcoption, gcdummy, floatoption])
descr = OptionDescription('pypy', [gcgroup, booloption, objspaceoption,
- listoption, wantref_option])
+ wantref_option, intoption])
return descr
@@ -30,10 +31,12 @@
config.objspace = 'logic'
assert config.objspace == 'logic'
-
- assert config.list == ['foo']
- config.list = ['bar']
- assert config.list == ['bar']
+ assert config.gc.float == 2.3
+ assert config.int == 0
+ config.gc.float = 3.4
+ config.int = 123
+ assert config.gc.float == 3.4
+ assert config.int == 123
assert not config.wantref
@@ -41,7 +44,8 @@
py.test.raises(ValueError, 'config.gc.name = "foo"')
py.test.raises(ValueError, 'config.gc.foo = "bar"')
py.test.raises(ValueError, 'config.bool = 123')
- py.test.raises(ValueError, 'config.list = ["baz"]')
+ py.test.raises(ValueError, 'config.int = "hello"')
+ py.test.raises(ValueError, 'config.gc.float = None')
# test whether the gc.name is set to 'ref' when wantref is true (note that
# the current value of gc.name is 'framework')
@@ -87,3 +91,12 @@
assert conf1 == conf2
assert hash(conf1) == hash(conf2)
assert conf1.getkey() == conf2.getkey()
+
+def test_loop():
+ descr = make_description()
+ conf = Config(descr)
+ for (name, value), (gname, gvalue) in \
+ zip(conf.gc, [("name", "ref"), ("dummy", False)]):
+ assert name == gname
+ assert value == gvalue
+
More information about the Pypy-commit
mailing list