From 4a08514fd38302f59b55524aec427c0d9b082aab Mon Sep 17 00:00:00 2001 From: Vanya Sergeev Date: Mon, 6 Apr 2015 01:56:28 -0700 Subject: [PATCH] use xrange in array and map unpacking and alias it to range under python 3. Resolves #11. --- test_umsgpack.py | 2 +- umsgpack.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/test_umsgpack.py b/test_umsgpack.py index 0d60d62..977b24b 100644 --- a/test_umsgpack.py +++ b/test_umsgpack.py @@ -357,7 +357,7 @@ def test_namespacing(self): # Get a list of global variables from umsgpack module exported_vars = list(filter(lambda x: not x.startswith("_"), dir(umsgpack))) # Ignore imports - exported_vars = list(filter(lambda x: x != "struct" and x != "collections" and x != "sys" and x != "io", exported_vars)) + exported_vars = list(filter(lambda x: x != "struct" and x != "collections" and x != "sys" and x != "io" and x != "xrange", exported_vars)) self.assertTrue(len(exported_vars) == len(exported_vars_test_vector)) for var in exported_vars_test_vector: diff --git a/umsgpack.py b/umsgpack.py index 0cdc83e..78d18a8 100644 --- a/umsgpack.py +++ b/umsgpack.py @@ -600,7 +600,7 @@ def _unpack_array(code, fp): else: raise Exception("logic error, not array: 0x%02x" % ord(code)) - return [_unpack(fp) for i in range(length)] + return [_unpack(fp) for i in xrange(length)] def _deep_list_to_tuple(obj): if isinstance(obj, list): @@ -618,7 +618,7 @@ def _unpack_map(code, fp): raise Exception("logic error, not map: 0x%02x" % ord(code)) d = {} - for i in range(length): + for i in xrange(length): # Unpack key k = _unpack(fp) @@ -793,6 +793,7 @@ def __init(): global compatibility global _float_size global _unpack_dispatch_table + global xrange # Compatibility mode for handling strings/bytes with the old specification compatibility = False @@ -813,6 +814,7 @@ def __init(): unpackb = _unpackb3 load = _unpack3 loads = _unpackb3 + xrange = range else: pack = _pack2 packb = _packb2