_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.
This commit is contained in:
Rob Crittenden 2009-11-04 09:41:48 -05:00 committed by Jason Gerard DeRose
parent 1d1d82fda4
commit 680bf7c548
2 changed files with 19 additions and 1 deletions

View File

@ -227,6 +227,9 @@ class Param(ReadOnly):
# Subclasses should override this with something more specific: # Subclasses should override this with something more specific:
type_error = _('incorrect type') type_error = _('incorrect type')
# _convert_scalar operates only on scalar values
scalar_error = _('Only one value is allowed')
kwargs = ( kwargs = (
('cli_name', str, None), ('cli_name', str, None),
('cli_short_name', str, None), ('cli_short_name', str, None),
@ -810,6 +813,9 @@ class Bool(Param):
return True return True
if value in self.falsehoods: if value in self.falsehoods:
return False 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, raise ConversionError(name=self.name, index=index,
error=ugettext(self.type_error), error=ugettext(self.type_error),
) )
@ -873,6 +879,9 @@ class Number(Param):
return self.type(value) return self.type(value)
except ValueError: except ValueError:
pass 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, raise ConversionError(name=self.name, index=index,
error=ugettext(self.type_error), error=ugettext(self.type_error),
) )
@ -1106,6 +1115,9 @@ class Str(Data):
return value return value
if type(value) in (int, float): if type(value) in (int, float):
return self.type(value) 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, raise ConversionError(name=self.name, index=index,
error=ugettext(self.type_error), error=ugettext(self.type_error),
) )

View File

@ -923,7 +923,7 @@ class test_Str(ClassChecker):
mthd = o._convert_scalar mthd = o._convert_scalar
for value in (u'Hello', 42, 1.2, unicode_str): for value in (u'Hello', 42, 1.2, unicode_str):
assert mthd(value) == unicode(value) 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: for value in bad:
e = raises(errors.ConversionError, mthd, value) e = raises(errors.ConversionError, mthd, value)
assert e.name == 'my_str' assert e.name == 'my_str'
@ -933,6 +933,12 @@ class test_Str(ClassChecker):
assert e.name == 'my_str' assert e.name == 'my_str'
assert e.index == 18 assert e.index == 18
assert_equal(e.error, u'must be Unicode text') 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 assert o.convert(None) is None
def test_rule_minlength(self): def test_rule_minlength(self):