From 03facf57af25c2c162e7b3762c2e7615ca645c7b Mon Sep 17 00:00:00 2001 From: Hossein Rimaz Date: Thu, 30 Nov 2023 18:28:20 +0100 Subject: [PATCH 1/2] Fix negative xsd edge cases --- basyx/aas/model/datatypes.py | 12 ++++++++---- test/model/test_datatypes.py | 7 +++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/basyx/aas/model/datatypes.py b/basyx/aas/model/datatypes.py index f3960fa0e..31f1ad657 100644 --- a/basyx/aas/model/datatypes.py +++ b/basyx/aas/model/datatypes.py @@ -235,7 +235,8 @@ class Float(float): class Long(int): def __new__(cls, *args, **kwargs): res = int.__new__(cls, *args, **kwargs) - if abs(res) > 2**63-1: + # [-9223372036854775808, 9223372036854775807] + if res > 2**63-1 or res < -2**63: raise ValueError("{} is out of the allowed range for type {}".format(res, cls.__name__)) return res @@ -243,7 +244,8 @@ def __new__(cls, *args, **kwargs): class Int(int): def __new__(cls, *args, **kwargs): res = int.__new__(cls, *args, **kwargs) - if abs(res) > 2**31-1: + # [-2147483648, 2147483647] + if res > 2**31-1 or res < -2**31: raise ValueError("{} is out of the allowed range for type {}".format(res, cls.__name__)) return res @@ -251,7 +253,8 @@ def __new__(cls, *args, **kwargs): class Short(int): def __new__(cls, *args, **kwargs): res = int.__new__(cls, *args, **kwargs) - if abs(res) > 2**15-1: + # [-32768, 32767] + if res > 2**15-1 or res < -2**15: raise ValueError("{} is out of the allowed range for type {}".format(res, cls.__name__)) return res @@ -259,7 +262,8 @@ def __new__(cls, *args, **kwargs): class Byte(int): def __new__(cls, *args, **kwargs): res = int.__new__(cls, *args, **kwargs) - if abs(res) > 2**7-1: + # [-128,127] + if res > 2**7-1 or res < -2**7: raise ValueError("{} is out of the allowed range for type {}".format(res, cls.__name__)) return res diff --git a/test/model/test_datatypes.py b/test/model/test_datatypes.py index bfdedb1c2..e0339fc6c 100644 --- a/test/model/test_datatypes.py +++ b/test/model/test_datatypes.py @@ -21,11 +21,18 @@ def test_parse_int(self) -> None: self.assertEqual(8, model.datatypes.from_xsd("8", model.datatypes.Long)) self.assertEqual(9, model.datatypes.from_xsd("9", model.datatypes.Int)) self.assertEqual(10, model.datatypes.from_xsd("10", model.datatypes.Short)) + self.assertEqual(-123456789012345678901234567890, model.datatypes.from_xsd("-123456789012345678901234567890", model.datatypes.Integer)) + self.assertEqual(2147483647, model.datatypes.from_xsd("2147483647", model.datatypes.Int)) + self.assertEqual(-2147483648, model.datatypes.from_xsd("-2147483648", model.datatypes.Int)) + self.assertEqual(-32768, model.datatypes.from_xsd("-32768", model.datatypes.Short)) + self.assertEqual(-128, model.datatypes.from_xsd("-128", model.datatypes.Byte)) + self.assertEqual(-9223372036854775808, model.datatypes.from_xsd("-9223372036854775808", model.datatypes.Long)) def test_serialize_int(self) -> None: self.assertEqual("5", model.datatypes.xsd_repr(model.datatypes.Integer(5))) self.assertEqual("6", model.datatypes.xsd_repr(model.datatypes.Byte(6))) self.assertEqual("7", model.datatypes.xsd_repr(model.datatypes.NonNegativeInteger(7))) + self.assertEqual("-128", model.datatypes.xsd_repr(model.datatypes.Byte(-128))) def test_range_error(self) -> None: with self.assertRaises(ValueError) as cm: From 8fb522b784b865f5ee3e17c0a0bd00320bc3d0c0 Mon Sep 17 00:00:00 2001 From: Hossein Rimaz Date: Thu, 30 Nov 2023 18:36:17 +0100 Subject: [PATCH 2/2] Fix style --- test/model/test_datatypes.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/model/test_datatypes.py b/test/model/test_datatypes.py index e0339fc6c..f66a04791 100644 --- a/test/model/test_datatypes.py +++ b/test/model/test_datatypes.py @@ -21,12 +21,14 @@ def test_parse_int(self) -> None: self.assertEqual(8, model.datatypes.from_xsd("8", model.datatypes.Long)) self.assertEqual(9, model.datatypes.from_xsd("9", model.datatypes.Int)) self.assertEqual(10, model.datatypes.from_xsd("10", model.datatypes.Short)) - self.assertEqual(-123456789012345678901234567890, model.datatypes.from_xsd("-123456789012345678901234567890", model.datatypes.Integer)) + self.assertEqual(-123456789012345678901234567890, + model.datatypes.from_xsd("-123456789012345678901234567890", model.datatypes.Integer)) self.assertEqual(2147483647, model.datatypes.from_xsd("2147483647", model.datatypes.Int)) self.assertEqual(-2147483648, model.datatypes.from_xsd("-2147483648", model.datatypes.Int)) self.assertEqual(-32768, model.datatypes.from_xsd("-32768", model.datatypes.Short)) self.assertEqual(-128, model.datatypes.from_xsd("-128", model.datatypes.Byte)) - self.assertEqual(-9223372036854775808, model.datatypes.from_xsd("-9223372036854775808", model.datatypes.Long)) + self.assertEqual(-9223372036854775808, + model.datatypes.from_xsd("-9223372036854775808", model.datatypes.Long)) def test_serialize_int(self) -> None: self.assertEqual("5", model.datatypes.xsd_repr(model.datatypes.Integer(5)))