diff --git a/test_umsgpack.py b/test_umsgpack.py index 86d4362..31c01e1 100644 --- a/test_umsgpack.py +++ b/test_umsgpack.py @@ -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", @@ -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"") diff --git a/umsgpack.py b/umsgpack.py index be21e7c..ec11cc6 100644 --- a/umsgpack.py +++ b/umsgpack.py @@ -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)] @@ -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) @@ -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) @@ -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) @@ -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)