Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 support #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions python/tinypacks/tinypacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def pack(obj, use_double=False):
return chr(TP_NONE)
elif isinstance(obj, bool):
return chr(TP_BOOLEAN|1) + '\x01' if obj else chr(TP_BOOLEAN)
elif isinstance(obj, (int, long)):
elif isinstance(obj, int):
bit_length = bit_len(obj)
if bit_length == 0:
return chr(TP_INTEGER)
Expand All @@ -76,8 +76,8 @@ def pack(obj, use_double=False):
return struct.pack(">Bd", TP_REAL|8, obj)
else:
return struct.pack(">Bf", TP_REAL|4, obj)
elif isinstance(obj, (str, unicode)):
content = obj.encode('utf_8') if isinstance(obj, unicode) else obj
elif isinstance(obj, str):
content = obj.encode('utf_8') if isinstance(obj, str) else obj
byte_length = len(content)
if byte_length <= TP_SMALL_SIZE_MAX:
return struct.pack(">B%is" % byte_length, TP_STRING | byte_length, content)
Expand All @@ -98,7 +98,7 @@ def pack(obj, use_double=False):
else:
raise ValueError("Bytearray too long")
elif isinstance(obj, (list, tuple)):
content = ''.join([pack(value) for value in obj])
content = b''.join([pack(value) for value in obj])
byte_length = len(content)
if byte_length <= TP_SMALL_SIZE_MAX:
return struct.pack(">B%is" % byte_length, TP_LIST | byte_length, content)
Expand All @@ -110,10 +110,10 @@ def pack(obj, use_double=False):
raise ValueError("List too long")
elif isinstance(obj, dict):
elements = []
for item in obj.items():
for item in list(obj.items()):
elements.append(pack(item[0]))
elements.append(pack(item[1]))
content = ''.join(elements)
content = b''.join(elements)
byte_length = len(content)
if byte_length <= TP_SMALL_SIZE_MAX:
return struct.pack(">B%is" % byte_length, TP_MAP | byte_length, content)
Expand All @@ -130,8 +130,8 @@ def pack(obj, use_double=False):
def unpack(bytes):
if len(bytes) == 0:
ValueError("Cannot unpack an empty pack")
content_type = ord(bytes[0]) & TP_TYPE_MASK
content_length = ord(bytes[0]) & TP_SMALL_SIZE_MASK
content_type = ord(chr(bytes[0])) & TP_TYPE_MASK
content_length = ord(chr(bytes[0])) & TP_SMALL_SIZE_MASK
element_length = content_length + 1
if content_length != TP_EXTENDED_SIZE_16:
content_raw = bytes[1 : element_length]
Expand All @@ -148,7 +148,7 @@ def unpack(bytes):
if content_type == TP_NONE:
obj = None
elif content_type == TP_BOOLEAN:
obj = True if (content_length and ord(content_raw[0])) else False
obj = True if (content_length and ord(chr(content_raw[0]))) else False
elif content_type == TP_INTEGER:
if content_length == 0:
obj = 0
Expand Down Expand Up @@ -188,4 +188,4 @@ def unpack(bytes):
obj[key] = value

return (obj, bytes[element_length:])