|
4 | 4 | import struct |
5 | 5 | import warnings |
6 | 6 |
|
7 | | -if sys.version_info[0] == 3: |
8 | | - PY3 = True |
| 7 | + |
| 8 | +if sys.version_info[0] == 2: |
| 9 | + PY2 = True |
| 10 | + int_types = (int, long) |
| 11 | + def dict_iteritems(d): |
| 12 | + return d.iteritems() |
| 13 | +else: |
| 14 | + PY2 = False |
9 | 15 | int_types = int |
10 | | - Unicode = str |
| 16 | + unicode = str |
11 | 17 | xrange = range |
12 | 18 | def dict_iteritems(d): |
13 | 19 | return d.items() |
14 | | -else: |
15 | | - PY3 = False |
16 | | - int_types = (int, long) |
17 | | - Unicode = unicode |
18 | | - def dict_iteritems(d): |
19 | | - return d.iteritems() |
20 | 20 |
|
21 | 21 | if sys.version_info < (3, 5): |
22 | 22 | # Ugly hack... |
@@ -97,7 +97,7 @@ def _get_data_from_buffer(obj): |
97 | 97 | view = memoryview(obj) |
98 | 98 | except TypeError: |
99 | 99 | # try to use legacy buffer protocol if 2.7, otherwise re-raise |
100 | | - if not PY3: |
| 100 | + if PY2: |
101 | 101 | view = memoryview(buffer(obj)) |
102 | 102 | warnings.warn("using old buffer interface to unpack %s; " |
103 | 103 | "this leads to unpacking errors if slicing is used and " |
@@ -639,7 +639,7 @@ def _unpack(self, execute=EX_CONSTRUCT): |
639 | 639 | ret = {} |
640 | 640 | for _ in xrange(n): |
641 | 641 | key = self._unpack(EX_CONSTRUCT) |
642 | | - if self._strict_map_key and type(key) not in (Unicode, bytes): |
| 642 | + if self._strict_map_key and type(key) not in (unicode, bytes): |
643 | 643 | raise ValueError("%s is not allowed for map key" % str(type(key))) |
644 | 644 | ret[key] = self._unpack(EX_CONSTRUCT) |
645 | 645 | if self._object_hook is not None: |
@@ -819,7 +819,7 @@ def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT, |
819 | 819 | raise ValueError("%s is too large" % type(obj).__name__) |
820 | 820 | self._pack_bin_header(n) |
821 | 821 | return self._buffer.write(obj) |
822 | | - if check(obj, Unicode): |
| 822 | + if check(obj, unicode): |
823 | 823 | if self._encoding is None: |
824 | 824 | raise TypeError( |
825 | 825 | "Can't encode unicode string: " |
@@ -1006,7 +1006,7 @@ def reset(self): |
1006 | 1006 |
|
1007 | 1007 | def getbuffer(self): |
1008 | 1008 | """Return view of internal buffer.""" |
1009 | | - if USING_STRINGBUILDER or not PY3: |
| 1009 | + if USING_STRINGBUILDER or PY2: |
1010 | 1010 | return memoryview(self.bytes()) |
1011 | 1011 | else: |
1012 | 1012 | return self._buffer.getbuffer() |
0 commit comments