PROTON-2038: A simple fix for preventing the initialization of unsigned proton types with negative values. A test has also been added.
diff --git a/python/proton/_data.py b/python/proton/_data.py
index 0bfd4ec..b133d5a 100644
--- a/python/proton/_data.py
+++ b/python/proton/_data.py
@@ -69,6 +69,11 @@
class ulong(long):
+ def __init__(self, l):
+ if (l < 0):
+ raise AssertionError("initializing ulong with negative value")
+ super(ulong, self).__new__(ulong, l)
+
def __repr__(self):
return "ulong(%s)" % long.__repr__(self)
@@ -111,18 +116,33 @@
class ubyte(int):
+ def __init__(self, i):
+ if (i < 0):
+ raise AssertionError("initializing ubyte with negative value")
+ super(ubyte, self).__new__(ubyte, i)
+
def __repr__(self):
return "ubyte(%s)" % int.__repr__(self)
class ushort(int):
+ def __init__(self, i):
+ if (i < 0):
+ raise AssertionError("initializing ushort with negative value")
+ super(ushort, self).__new__(ushort, i)
+
def __repr__(self):
return "ushort(%s)" % int.__repr__(self)
class uint(long):
+ def __init__(self, l):
+ if (l < 0):
+ raise AssertionError("initializing uint with negative value")
+ super(uint, self).__new__(uint, l)
+
def __repr__(self):
return "uint(%s)" % long.__repr__(self)
diff --git a/python/tests/proton_tests/codec.py b/python/tests/proton_tests/codec.py
index e7cade7..5a81ec5 100644
--- a/python/tests/proton_tests/codec.py
+++ b/python/tests/proton_tests/codec.py
@@ -260,13 +260,21 @@
self._test(itype, *self.int_values(itype))
def testByte(self): self._test_int("byte")
- def testUbyte(self): self._test_int("ubyte")
+ def testUbyte(self):
+ self._test_int("ubyte")
+ self.assertRaises(AssertionError, ubyte, -1)
def testShort(self): self._test_int("short")
- def testUshort(self): self._test("ushort")
+ def testUshort(self):
+ self._test("ushort")
+ self.assertRaises(AssertionError, ushort, -1)
def testInt(self): self._test_int("int")
- def testUint(self): self._test_int("uint")
+ def testUint(self):
+ self._test_int("uint")
+ self.assertRaises(AssertionError, uint, -1)
def testLong(self): self._test_int("long")
- def testUlong(self): self._test_int("ulong")
+ def testUlong(self):
+ self._test_int("ulong")
+ self.assertRaises(AssertionError, ulong, -1)
def testString(self):
self._test("string", "one", "two", "three", "this is a test", "")