From 680bf7c54863452263b9cd324ede444fd438436d Mon Sep 17 00:00:00 2001 From: Rob Crittenden Date: Wed, 4 Nov 2009 09:41:48 -0500 Subject: [PATCH] _convert_scalar() should throw an error if passed a tuple or list A parameter needs to have multivalue set in order to work on lists/tuples and even then _convert_scalar() will be sent one value at a time. --- ipalib/parameters.py | 12 ++++++++++++ tests/test_ipalib/test_parameters.py | 8 +++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/ipalib/parameters.py b/ipalib/parameters.py index 0630915cb..227757d63 100644 --- a/ipalib/parameters.py +++ b/ipalib/parameters.py @@ -227,6 +227,9 @@ class Param(ReadOnly): # Subclasses should override this with something more specific: type_error = _('incorrect type') + # _convert_scalar operates only on scalar values + scalar_error = _('Only one value is allowed') + kwargs = ( ('cli_name', str, None), ('cli_short_name', str, None), @@ -810,6 +813,9 @@ class Bool(Param): return True if value in self.falsehoods: return False + if type(value) in (tuple, list): + raise ConversionError(name=self.name, index=index, + error=ugettext(self.scalar_error)) raise ConversionError(name=self.name, index=index, error=ugettext(self.type_error), ) @@ -873,6 +879,9 @@ class Number(Param): return self.type(value) except ValueError: pass + if type(value) in (tuple, list): + raise ConversionError(name=self.name, index=index, + error=ugettext(self.scalar_error)) raise ConversionError(name=self.name, index=index, error=ugettext(self.type_error), ) @@ -1106,6 +1115,9 @@ class Str(Data): return value if type(value) in (int, float): return self.type(value) + if type(value) in (tuple, list): + raise ConversionError(name=self.name, index=index, + error=ugettext(self.scalar_error)) raise ConversionError(name=self.name, index=index, error=ugettext(self.type_error), ) diff --git a/tests/test_ipalib/test_parameters.py b/tests/test_ipalib/test_parameters.py index 44286ac07..f6bc66f4e 100644 --- a/tests/test_ipalib/test_parameters.py +++ b/tests/test_ipalib/test_parameters.py @@ -923,7 +923,7 @@ class test_Str(ClassChecker): mthd = o._convert_scalar for value in (u'Hello', 42, 1.2, unicode_str): assert mthd(value) == unicode(value) - bad = [True, 'Hello', (u'Hello',), [42.3], dict(one=1), utf8_bytes] + bad = [True, 'Hello', dict(one=1), utf8_bytes] for value in bad: e = raises(errors.ConversionError, mthd, value) assert e.name == 'my_str' @@ -933,6 +933,12 @@ class test_Str(ClassChecker): assert e.name == 'my_str' assert e.index == 18 assert_equal(e.error, u'must be Unicode text') + bad = [(u'Hello',), [42.3]] + for value in bad: + e = raises(errors.ConversionError, mthd, value) + assert e.name == 'my_str' + assert e.index is None + assert_equal(e.error, u'Only one value is allowed') assert o.convert(None) is None def test_rule_minlength(self):