Skip to content

Commit

Permalink
add use_tuple unpacking option to unpack arrays into tuples
Browse files Browse the repository at this point in the history
resolves #43.
  • Loading branch information
vsergeev committed Mar 1, 2020
1 parent 027bc3f commit 6d514f9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
19 changes: 19 additions & 0 deletions test_umsgpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@
["float precision double", 2.5, b"\xcb\x40\x04\x00\x00\x00\x00\x00\x00"],
]

tuple_test_vectors = [
["nested array", [0x01, [b"\x80", [[u"a", u"b", u"c"], True]]],
b"\x92\x01\x92\xc4\x01\x80\x92\x93\xa1a\xa1b\xa1c\xc3",
(0x01, (b"\x80", ((u"a", u"b", u"c"), True)))],
]

naive_timestamp_test_vectors = [
["32-bit timestamp (naive)", datetime.datetime(2000, 1, 1, 10, 5, 2, 0, umsgpack._utc_tzinfo),
b"\xd6\xff\x38\x6d\xd1\x4e",
Expand Down Expand Up @@ -518,6 +524,19 @@ def test_unpack_ordered_dict(self):
self.assertTrue(isinstance(unpacked, OrderedDict))
self.assertEqual(unpacked, obj)

def test_unpack_tuple(self):
# Use tuple test vector
(_, obj, data, obj_tuple) = tuple_test_vectors[0]

# Unpack with default options (list)
self.assertEqual(umsgpack.unpackb(data), obj)

# Unpack with use_tuple=False (list)
self.assertEqual(umsgpack.unpackb(data, use_tuple=False), obj)

# Unpack with use_tuple=True (tuple)
self.assertEqual(umsgpack.unpackb(data, use_tuple=True), obj_tuple)

def test_ext_exceptions(self):
with self.assertRaises(TypeError):
_ = umsgpack.Ext(5.0, b"")
Expand Down
11 changes: 11 additions & 0 deletions umsgpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,9 @@ def _unpack_array(code, fp, options):
else:
raise Exception("logic error, not array: 0x%02x" % ord(code))

if options.get('use_tuple'):
return tuple((_unpack(fp, options) for i in xrange(length)))

return [_unpack(fp, options) for i in xrange(length)]


Expand Down Expand Up @@ -878,6 +881,8 @@ def _unpack2(fp, **options):
Ext into an object
use_ordered_dict (bool): unpack maps into OrderedDict, instead of
unordered dict (default False)
use_tuple (bool): unpacks arrays into tuples, instead of lists (default
False)
allow_invalid_utf8 (bool): unpack invalid strings into instances of
InvalidString, for access to the bytes
(default False)
Expand Down Expand Up @@ -922,6 +927,8 @@ def _unpack3(fp, **options):
Ext into an object
use_ordered_dict (bool): unpack maps into OrderedDict, instead of
unordered dict (default False)
use_tuple (bool): unpacks arrays into tuples, instead of lists (default
False)
allow_invalid_utf8 (bool): unpack invalid strings into instances of
InvalidString, for access to the bytes
(default False)
Expand Down Expand Up @@ -967,6 +974,8 @@ def _unpackb2(s, **options):
Ext into an object
use_ordered_dict (bool): unpack maps into OrderedDict, instead of
unordered dict (default False)
use_tuple (bool): unpacks arrays into tuples, instead of lists (default
False)
allow_invalid_utf8 (bool): unpack invalid strings into instances of
InvalidString, for access to the bytes
(default False)
Expand Down Expand Up @@ -1015,6 +1024,8 @@ def _unpackb3(s, **options):
Ext into an object
use_ordered_dict (bool): unpack maps into OrderedDict, instead of
unordered dict (default False)
use_tuple (bool): unpacks arrays into tuples, instead of lists (default
False)
allow_invalid_utf8 (bool): unpack invalid strings into instances of
InvalidString, for access to the bytes
(default False)
Expand Down

0 comments on commit 6d514f9

Please sign in to comment.