From 7bd978ffaa61f1f89e512c6d8f4fb112bf264a7f Mon Sep 17 00:00:00 2001 From: Joel Bender Date: Tue, 19 Mar 2019 22:43:19 -0400 Subject: [PATCH] a little too aggressive copy/paste (#258) --- py25/bacpypes/primitivedata.py | 16 ++++++++++------ py27/bacpypes/primitivedata.py | 12 ++++++++---- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/py25/bacpypes/primitivedata.py b/py25/bacpypes/primitivedata.py index 6718c61..668c99a 100755 --- a/py25/bacpypes/primitivedata.py +++ b/py25/bacpypes/primitivedata.py @@ -598,13 +598,17 @@ class Unsigned(Atomic): _high_limit = None def __init__(self, arg=None): - self.value = 0 + self.value = 0L if arg is None: pass elif isinstance(arg, Tag): self.decode(arg) elif isinstance(arg, int): + if not self.is_valid(arg): + raise ValueError("value out of range") + self.value = long(arg) + elif isinstance(arg, long): if not self.is_valid(arg): raise ValueError("value out of range") self.value = arg @@ -617,10 +621,10 @@ class Unsigned(Atomic): def encode(self, tag): # rip apart the number - data = bytearray(struct.pack('>L', self.value)) + data = struct.pack('>L', self.value) # reduce the value to the smallest number of octets - while (len(data) > 1) and (data[0] == 0): + while (len(data) > 1) and (data[0] == '\x00'): del data[0] # encode the tag @@ -633,9 +637,9 @@ class Unsigned(Atomic): raise InvalidTag("invalid tag length") # get the data - rslt = 0 + rslt = 0L for c in tag.tagData: - rslt = (rslt << 8) + c + rslt = (rslt << 8) + ord(c) # save the result self.value = rslt @@ -643,7 +647,7 @@ class Unsigned(Atomic): @classmethod def is_valid(cls, arg): """Return True if arg is valid value for the class.""" - if not isinstance(arg, int) or isinstance(arg, bool): + if not isinstance(arg, (int, long)) or isinstance(arg, bool): return False if (arg < cls._low_limit): return False diff --git a/py27/bacpypes/primitivedata.py b/py27/bacpypes/primitivedata.py index 53034e0..43c09e9 100755 --- a/py27/bacpypes/primitivedata.py +++ b/py27/bacpypes/primitivedata.py @@ -602,13 +602,17 @@ class Unsigned(Atomic): _high_limit = None def __init__(self, arg=None): - self.value = 0 + self.value = 0L if arg is None: pass elif isinstance(arg, Tag): self.decode(arg) elif isinstance(arg, int): + if not self.is_valid(arg): + raise ValueError("value out of range") + self.value = long(arg) + elif isinstance(arg, long): if not self.is_valid(arg): raise ValueError("value out of range") self.value = arg @@ -637,9 +641,9 @@ class Unsigned(Atomic): raise InvalidTag("invalid tag length") # get the data - rslt = 0 + rslt = 0L for c in tag.tagData: - rslt = (rslt << 8) + c + rslt = (rslt << 8) + ord(c) # save the result self.value = rslt @@ -647,7 +651,7 @@ class Unsigned(Atomic): @classmethod def is_valid(cls, arg): """Return True if arg is valid value for the class.""" - if not isinstance(arg, int) or isinstance(arg, bool): + if not isinstance(arg, (int, long)) or isinstance(arg, bool): return False if (arg < cls._low_limit): return False