mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
331: Param.normalize() no longer raises a TypeError when value in not a basestring
This commit is contained in:
parent
95abdcd714
commit
d56f4c643b
@ -105,6 +105,38 @@ class Param(plugable.ReadOnly):
|
|||||||
self.rules = (type_.validate,) + rules
|
self.rules = (type_.validate,) + rules
|
||||||
lock(self)
|
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):
|
def __convert_scalar(self, value, index=None):
|
||||||
if value is None:
|
if value is None:
|
||||||
raise TypeError('value cannot be 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, 0),) # tuple
|
||||||
return self.__convert_scalar(value)
|
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):
|
def __validate_scalar(self, value, index=None):
|
||||||
if type(value) is not self.type.type:
|
if type(value) is not self.type.type:
|
||||||
|
@ -202,7 +202,7 @@ class test_Option(ClassChecker):
|
|||||||
for v in (u'Hello', u'hello', 'Hello'): # Okay
|
for v in (u'Hello', u'hello', 'Hello'): # Okay
|
||||||
assert o.normalize(v) == 'hello'
|
assert o.normalize(v) == 'hello'
|
||||||
for v in [None, 42, (u'Hello',)]: # Not basestring
|
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
|
# Scenario 3: multivalue=True, normalize=None
|
||||||
o = self.cls(name, t, multivalue=True)
|
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
|
for value in [(u'Hello',), (u'hello',), 'Hello', ['Hello']]: # Okay
|
||||||
assert o.normalize(value) == (u'hello',)
|
assert o.normalize(value) == (u'hello',)
|
||||||
fail = 42 # Not basestring
|
fail = 42 # Not basestring
|
||||||
for v in [fail, [fail], (u'Hello', fail)]: # Non unicode member
|
for v in [[fail], (u'hello', fail)]: # Non unicode member
|
||||||
check_TypeError(fail, basestring, 'value', o.normalize, v)
|
assert o.normalize(v) == tuple(v)
|
||||||
|
|
||||||
def test_validate(self):
|
def test_validate(self):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user