Signed-off-by: John Dennis <jden...@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(-)
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.
_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to