[svn r57756] * create new contrib directory
* move py/green to contrib/pygreen, fix tests and code to pass --HG-- branch : trunk
This commit is contained in:
29
contrib/pygreen/msgstruct.py
Normal file
29
contrib/pygreen/msgstruct.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from struct import pack, unpack, calcsize
|
||||
|
||||
|
||||
def message(tp, *values):
|
||||
strtype = type('')
|
||||
typecodes = ['']
|
||||
for v in values:
|
||||
if type(v) is strtype:
|
||||
typecodes.append('%ds' % len(v))
|
||||
elif 0 <= v < 256:
|
||||
typecodes.append('B')
|
||||
else:
|
||||
typecodes.append('l')
|
||||
typecodes = ''.join(typecodes)
|
||||
assert len(typecodes) < 256
|
||||
return pack(("!B%dsc" % len(typecodes)) + typecodes,
|
||||
len(typecodes), typecodes, tp, *values)
|
||||
|
||||
def decodemessage(data):
|
||||
if data:
|
||||
limit = ord(data[0]) + 1
|
||||
if len(data) >= limit:
|
||||
typecodes = "!c" + data[1:limit]
|
||||
end = limit + calcsize(typecodes)
|
||||
if len(data) >= end:
|
||||
return unpack(typecodes, data[limit:end]), data[end:]
|
||||
#elif end > 1000000:
|
||||
# raise OverflowError
|
||||
return None, data
|
||||
Reference in New Issue
Block a user