331: Param.normalize() no longer raises a TypeError when value in not a basestring

This commit is contained in:
Jason Gerard DeRose 2008-09-24 06:11:46 +00:00
parent 95abdcd714
commit d56f4c643b
2 changed files with 35 additions and 18 deletions

View File

@ -105,6 +105,38 @@ class Param(plugable.ReadOnly):
self.rules = (type_.validate,) + rules
lock(self)
def __normalize_scalar(self, value):
if not isinstance(value, basestring):
return value
try:
return self.__normalize(value)
except StandardError:
return value
def normalize(self, value):
"""
Normalize ``value`` using normalize callback.
If this `Param` instance does not have a normalize callback,
``value`` is returned unchanged.
If this `Param` instance has a normalize callback and ``value`` is
a basestring, the normalize callback is called and its return value
is returned.
If ``value`` is not a basestring, or if an exception is caught
when calling the normalize callback, ``value`` is returned unchanged.
:param value: A proposed value for this parameter.
"""
if self.__normalize is None:
return value
if self.multivalue:
if type(value) in (tuple, list):
return tuple(self.__normalize_scalar(v) for v in value)
return (self.__normalize_scalar(value),) # tuple
return self.__normalize_scalar(value)
def __convert_scalar(self, value, index=None):
if value is None:
raise TypeError('value cannot be None')
@ -124,22 +156,7 @@ class Param(plugable.ReadOnly):
return (self.__convert_scalar(value, 0),) # tuple
return self.__convert_scalar(value)
def __normalize_scalar(self, value):
if not isinstance(value, basestring):
raise_TypeError(value, basestring, 'value')
try:
return self.__normalize(value)
except Exception:
return value
def normalize(self, value):
if self.__normalize is None:
return value
if self.multivalue:
if type(value) in (tuple, list):
return tuple(self.__normalize_scalar(v) for v in value)
return (self.__normalize_scalar(value),) # tuple
return self.__normalize_scalar(value)
def __validate_scalar(self, value, index=None):
if type(value) is not self.type.type:

View File

@ -202,7 +202,7 @@ class test_Option(ClassChecker):
for v in (u'Hello', u'hello', 'Hello'): # Okay
assert o.normalize(v) == 'hello'
for v in [None, 42, (u'Hello',)]: # Not basestring
check_TypeError(v, basestring, 'value', o.normalize, v)
assert o.normalize(v) is v
# Scenario 3: multivalue=True, normalize=None
o = self.cls(name, t, multivalue=True)
@ -215,8 +215,8 @@ class test_Option(ClassChecker):
for value in [(u'Hello',), (u'hello',), 'Hello', ['Hello']]: # Okay
assert o.normalize(value) == (u'hello',)
fail = 42 # Not basestring
for v in [fail, [fail], (u'Hello', fail)]: # Non unicode member
check_TypeError(fail, basestring, 'value', o.normalize, v)
for v in [[fail], (u'hello', fail)]: # Non unicode member
assert o.normalize(v) == tuple(v)
def test_validate(self):
"""