From 74eb33b789005860d9e31de1843515d0f14a96a5 Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Thu, 15 Aug 2019 15:42:43 +0200 Subject: [PATCH] fixes in datatypes - Enum.__call__(self, value) should raise BadValueError when value is not hashable - added TextType.copy (DataType.copy will not work here) Change-Id: I6c460991fa1e6595a939fb0814f490d2188c97b4 Reviewed-on: https://forge.frm2.tum.de/review/21015 Tested-by: JenkinsCodeReview Reviewed-by: Markus Zolliker --- secop/datatypes.py | 6 +++++- test/test_datatypes.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/secop/datatypes.py b/secop/datatypes.py index c1c73ac..8fd0f9e 100644 --- a/secop/datatypes.py +++ b/secop/datatypes.py @@ -339,7 +339,7 @@ class EnumType(DataType): """return the validated (internal) value or raise""" try: return self._enum[value] - except KeyError: + except (KeyError, TypeError): # TypeError will be raised when value is not hashable raise BadValueError(u'%r is not a member of enum %r' % (value, self._enum)) def from_string(self, text): @@ -469,6 +469,10 @@ class TextType(StringType): def __repr__(self): return u'TextType(%d, %d)' % (self.minsize, self.maxsize) + def copy(self): + # DataType.copy will not work, because it is exported as 'string' + return TextType(self.maxsize) + # Bool is a special enum class BoolType(DataType): diff --git a/test/test_datatypes.py b/test/test_datatypes.py index a34bc64..4f1f497 100644 --- a/test/test_datatypes.py +++ b/test/test_datatypes.py @@ -174,7 +174,7 @@ def test_EnumType(): dt(-9) with pytest.raises(ValueError): dt(u'XX') - with pytest.raises(TypeError): + with pytest.raises(ValueError): dt([19, u'X']) assert dt(u'a') == 3