summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2009-01-13 18:29:45 -0700
committerJason Gerard DeRose <jderose@redhat.com>2009-01-13 18:29:45 -0700
commita0fb215a2c5f2dfaf26b06f93c3a651f2420083d (patch)
treeade2015b98d1832ad2c0b9905c4465a32bf25458
parent10747103fa3748677e6e1948977de1313fe25bc9 (diff)
downloadfreeipa-a0fb215a2c5f2dfaf26b06f93c3a651f2420083d.tar.gz
freeipa-a0fb215a2c5f2dfaf26b06f93c3a651f2420083d.tar.xz
freeipa-a0fb215a2c5f2dfaf26b06f93c3a651f2420083d.zip
New Param: updated Str._convert_scalar() so it raises a ConversionError
-rw-r--r--ipalib/parameter.py42
-rw-r--r--tests/test_ipalib/test_parameter.py18
2 files changed, 41 insertions, 19 deletions
diff --git a/ipalib/parameter.py b/ipalib/parameter.py
index 3ec7579a..6c942cab 100644
--- a/ipalib/parameter.py
+++ b/ipalib/parameter.py
@@ -409,8 +409,8 @@ class Param(ReadOnly):
multivalue parameter. For example:
>>> multi = Str('my_multi', multivalue=True)
- >>> multi.convert([True, '', 17, None, False])
- (u'True', u'17', u'False')
+ >>> multi.convert([1.5, '', 17, None, u'Hello'])
+ (u'1.5', u'17', u'Hello')
>>> multi.convert([None, u'']) is None # Filters to an empty list
True
@@ -419,8 +419,8 @@ class Param(ReadOnly):
>>> multi.convert(42) # Called with a scalar value
(u'42',)
- >>> multi.convert([True, False]) # Called with a list value
- (u'True', u'False')
+ >>> multi.convert([0, 1]) # Called with a list value
+ (u'0', u'1')
Note that how values are converted (and from what types they will be
converted) completely depends upon how a subclass implements its
@@ -436,7 +436,7 @@ class Param(ReadOnly):
value = (value,)
values = tuple(
self._convert_scalar(v, i) for (i, v) in filter(
- lambda tup: tup[1] not in NULLS, enumerate(value)
+ lambda iv: iv[1] not in NULLS, enumerate(value)
)
)
if len(values) == 0:
@@ -608,13 +608,13 @@ class Param(ReadOnly):
class Bool(Param):
"""
-
+ A parameter for boolean values (stored in the ``bool`` type).
"""
class Int(Param):
"""
- A parameter for integer values (stored in Python ``int`` type).
+ A parameter for integer values (stored in the ``int`` type).
"""
type = int
@@ -623,7 +623,7 @@ class Int(Param):
class Float(Param):
"""
-
+ A parameter for floating-point values (stored in the ``float`` type).
"""
type = float
@@ -631,7 +631,12 @@ class Float(Param):
class Bytes(Param):
"""
- A parameter for binary data (stored in Python ``str`` type).
+ A parameter for binary data (stored in the ``str`` type).
+
+ This class is named *Bytes* instead of *Str* so it's aligned with the
+ Python v3 ``(str, unicode) => (bytes, str)`` clean-up. See:
+
+ http://docs.python.org/3.0/whatsnew/3.0.html
"""
type = str
@@ -710,20 +715,31 @@ class Bytes(Param):
class Str(Bytes):
"""
- A parameter for character data (stored in Python ``unicode`` type).
+ A parameter for Unicode text (stored in the``unicode`` type).
+
+ This class is named *Str* instead of *Unicode* so it's aligned with the
+ Python v3 ``(str, unicode) => (bytes, str)`` clean-up. See:
+
+ http://docs.python.org/3.0/whatsnew/3.0.html
"""
type = unicode
+ type_error = _('must be Unicode text')
kwargs = Bytes.kwargs[:-1] + (
('pattern', unicode, None),
)
def _convert_scalar(self, value, index=None):
- if type(value) in (self.type, int, float, bool):
+ """
+ Convert a single scalar value.
+ """
+ if type(value) is self.type:
+ return value
+ if type(value) in (int, float):
return self.type(value)
- raise TypeError(
- 'Can only implicitly convert int, float, or bool; got %r' % value
+ raise ConversionError(name=self.name, index=index,
+ error=ugettext(self.type_error),
)
def _rule_minlength(self, _, value):
diff --git a/tests/test_ipalib/test_parameter.py b/tests/test_ipalib/test_parameter.py
index 27d348b9..47cea89d 100644
--- a/tests/test_ipalib/test_parameter.py
+++ b/tests/test_ipalib/test_parameter.py
@@ -667,12 +667,18 @@ class test_Str(ClassChecker):
Test the `ipalib.parameter.Str._convert_scalar` method.
"""
o = self.cls('my_str')
- for value in (u'Hello', 42, 1.2, True):
- assert o._convert_scalar(value) == unicode(value)
- for value in ('Hello', (None,), [u'42', '42'], dict(hello=u'world')):
- e = raises(TypeError, o._convert_scalar, value)
- assert str(e) == \
- 'Can only implicitly convert int, float, or bool; got %r' % value
+ mthd = o._convert_scalar
+ for value in (u'Hello', 42, 1.2):
+ assert mthd(value) == unicode(value)
+ for value in [True, 'Hello', (u'Hello',), [42.3], dict(one=1)]:
+ e = raises(errors2.ConversionError, mthd, value)
+ assert e.name == 'my_str'
+ assert e.index is None
+ assert_equal(e.error, u'must be Unicode text')
+ e = raises(errors2.ConversionError, mthd, value, index=18)
+ assert e.name == 'my_str'
+ assert e.index == 18
+ assert_equal(e.error, u'must be Unicode text')
def test_rule_minlength(self):
"""