Skip to content

Commit 415cad6

Browse files
committed
py3: Extend table.encode() to handle both str and bytes
Signed-off-by: Justin Israel <[email protected]>
1 parent b9a2928 commit 415cad6

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

puka/table.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ def encode(table):
120120
Traceback (most recent call last):
121121
...
122122
AssertionError: Unsupported value type during encoding set([]) (<type 'set'>)
123+
124+
Checking the handling of unicode and byte values
125+
126+
>>> encode({u'd': u'x'})
127+
'\x00\x00\x00\x08\x01dS\x00\x00\x00\x01x'
128+
>>> encode({b'd': b'x'})
129+
'\x00\x00\x00\x08\x01dS\x00\x00\x00\x01x'
123130
"""
124131
pieces = []
125132
if table is None:
@@ -143,6 +150,10 @@ def encode_value(pieces, value):
143150
pieces.append(struct.pack('>cI', b'S', len(value)))
144151
pieces.append(compat.as_bytes(value))
145152
return 5 + len(value)
153+
elif isinstance(value, futils.binary_type):
154+
pieces.append(struct.pack('>cI', b'S', len(value)))
155+
pieces.append(value)
156+
return 5 + len(value)
146157
elif isinstance(value, bool):
147158
pieces.append(struct.pack('>cB', b't', int(value)))
148159
return 2

0 commit comments

Comments
 (0)