From 4a57d81b4a066bebf4268755d07c85154b536e7d Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 8 Mar 2016 10:06:31 +0100 Subject: [PATCH] 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 --- wsme/tests/test_types.py | 13 +++++++++++++ wsme/types.py | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/wsme/tests/test_types.py b/wsme/tests/test_types.py index c11ba34..fc41417 100644 --- a/wsme/tests/test_types.py +++ b/wsme/tests/test_types.py @@ -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 diff --git a/wsme/types.py b/wsme/types.py index 77404e5..eca915c 100644 --- a/wsme/types.py +++ b/wsme/types.py @@ -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: