summaryrefslogtreecommitdiffstats
path: root/ipalib/parameters.py
diff options
context:
space:
mode:
authorJohn Dennis <jdennis@redhat.com>2009-11-20 13:41:44 -0500
committerRob Crittenden <rcritten@redhat.com>2009-11-23 16:53:43 -0500
commit5d2bbf53256c212ad26505bae6827f6739c73f22 (patch)
treeabb951adf6a64dc75ff27a3a299a5ee71bdf3863 /ipalib/parameters.py
parent55422cb7b9319eaabc929151a9f4506e5d85600e (diff)
downloadfreeipa-5d2bbf53256c212ad26505bae6827f6739c73f22.tar.gz
freeipa-5d2bbf53256c212ad26505bae6827f6739c73f22.tar.xz
freeipa-5d2bbf53256c212ad26505bae6827f6739c73f22.zip
Reading INT parameter class should respect radix prefix
This modifies the original patch by including a unit test, handling floats when passed as unicode, and handling large magnitude values beyond maxint. The INT parameter class was not respecting any radix prefix (e.g. 0x) the user may have supplied. This patch implements _convert_scalar method for the Int class so that we can pass the special radix base of zero to the int constructor telling it to determine the radix from the prefix (if present).
Diffstat (limited to 'ipalib/parameters.py')
-rw-r--r--ipalib/parameters.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/ipalib/parameters.py b/ipalib/parameters.py
index 227757d63..8a1aede96 100644
--- a/ipalib/parameters.py
+++ b/ipalib/parameters.py
@@ -909,6 +909,35 @@ class Int(Number):
self.nice, self.minvalue, self.maxvalue)
)
+ def _convert_scalar(self, value, index=None):
+ """
+ Convert a single scalar value.
+ """
+ if type(value) in (int, long):
+ return value
+ if type(value) is unicode:
+ # permit floating point strings
+ if value.find(u'.') >= 0:
+ try:
+ return int(float(value))
+ except ValueError:
+ pass
+ else:
+ try:
+ # 2nd arg is radix base, 2nd arg only accepted for strings.
+ # Zero means determine radix base from prefix (e.g. 0x for hex)
+ return int(value, 0)
+ except ValueError:
+ pass
+ if type(value) is float:
+ try:
+ return int(value)
+ except ValueError:
+ pass
+ raise ConversionError(name=self.name, index=index,
+ error=ugettext(self.type_error),
+ )
+
def _rule_minvalue(self, _, value):
"""
Check min constraint.