wsattr.__set__() catchs TypeError

On Python 3, comparison between string (str) and integer (int) raises
a TypeError exception. I suggest to catch it to raise an InvalidInput
exception, as we already do for ValueError.

In practice, the TypeError was seen in OpenStack Cue tests on
Python 3, in a test passing a string to an attribute expecting an
integer, attribute having a minimum set (to an integer too).

Change-Id: I74103330ccb5cdc26aa3508fcefcc34310e00c27
This commit is contained in:
Victor Stinner 2016-03-08 10:06:31 +01:00
parent 30bb63a6f5
commit 4a57d81b4a
2 changed files with 14 additions and 1 deletions

View File

@ -206,6 +206,19 @@ Value: 'v3'. Value should be one of: v., v.",
self.assertRaises(exc.InvalidInput, setattr, obj, 'alist', 12)
self.assertRaises(exc.InvalidInput, setattr, obj, 'alist', [2, 'a'])
def test_attribute_validation_minimum(self):
class ATypeInt(object):
attr = types.IntegerType(minimum=1, maximum=5)
types.register_type(ATypeInt)
obj = ATypeInt()
obj.attr = 2
# comparison between 'zero' value and intger minimum (1) raises a
# TypeError which must be wrapped into an InvalidInput exception
self.assertRaises(exc.InvalidInput, setattr, obj, 'attr', 'zero')
def test_text_attribute_conversion(self):
class SType(object):
atext = types.text

View File

@ -487,7 +487,7 @@ class wsattr(object):
def __set__(self, instance, value):
try:
value = validate_value(self.datatype, value)
except ValueError as e:
except (ValueError, TypeError) as e:
raise exc.InvalidInput(self.name, value, six.text_type(e))
dataholder = self._get_dataholder(instance)
if value is Unset: