From ecaa9e813b23febb0a884a5814fdeed55382b778 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 30 Mar 2007 14:25:25 +0000 Subject: [PATCH] Fix #30. --- src/libprs500/prstypes.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/libprs500/prstypes.py b/src/libprs500/prstypes.py index 7daba91761..efb9078acd 100755 --- a/src/libprs500/prstypes.py +++ b/src/libprs500/prstypes.py @@ -128,13 +128,20 @@ class TransferBuffer(list): def pack(self, val, fmt=DWORD, start=0): """ - Encode C{val} and write it to buffer. + Encode C{val} and write it to buffer. For fmt==WORD val is + adjusted to be in the range 0 <= val < 256**2. @param fmt: See U{struct} @param start: Position in buffer at which to write encoded data """ - self[start:start+struct.calcsize(fmt)] = [ ord(i) for i in struct.pack(fmt, val) ] - + # struct.py is fussy about packing values into a WORD. The value must be + # between 0 and 65535 or a DeprecationWarning is raised. In the future + # this may become an error, so it's best to take care of wrapping here. + if fmt == WORD: + val = val % 256**2 + self[start:start+struct.calcsize(fmt)] = \ + [ ord(i) for i in struct.pack(fmt, val) ] + def _normalize(self): """ Replace negative bytes in C{self} by 256 + byte """ for i in range(len(self)):