diff options
author | Victor Stinner <vstinner@redhat.com> | 2016-03-08 10:06:31 +0100 |
---|---|---|
committer | Victor Stinner <vstinner@redhat.com> | 2016-03-08 11:21:06 +0100 |
commit | 4a57d81b4a066bebf4268755d07c85154b536e7d (patch) | |
tree | f9fec1a3f7e7534b6b870979046172cca4fd8476 | |
parent | 30bb63a6f5347752e406857c86d5524d0199132f (diff) | |
download | wsme-4a57d81b4a066bebf4268755d07c85154b536e7d.tar.gz |
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
-rw-r--r-- | wsme/tests/test_types.py | 13 | ||||
-rw-r--r-- | wsme/types.py | 2 |
2 files changed, 14 insertions, 1 deletions
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: |