[Freeipa-devel] [PATCH] Reading Int parameter class should respect radix prefix

John Dennis jdennis at redhat.com
Thu Nov 19 16:39:49 UTC 2009


Signed-off-by: John Dennis <jdennis at redhat.com>

Reading INT parameter class should respect radix prefix

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).

---

 ipalib/parameters.py |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)
-------------- next part --------------
diff --git a/ipalib/parameters.py b/ipalib/parameters.py
index 227757d..00862f0 100644
--- a/ipalib/parameters.py
+++ b/ipalib/parameters.py
@@ -909,6 +909,28 @@ class Int(Number):
                     self.nice, self.minvalue, self.maxvalue)
             )
 
+    def _convert_scalar(self, value, index=None):
+        """
+        Convert a single scalar value.
+        """
+        if type(value) is int:
+            return value
+        if type(value) is unicode:
+            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.


More information about the Freeipa-devel mailing list