Skip to content

Commit feec062

Browse files
sblondonmethane
andauthored
Drop python2 support (#519)
The PR removes python2 references and cases. Close #518 Co-authored-by: Inada Naoki <[email protected]>
1 parent 45f8486 commit feec062

17 files changed

+58
-143
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ and `raw=True` options.
220220

221221
```pycon
222222
>>> import msgpack
223-
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=False), raw=True)
223+
>>> msgpack.unpackb(msgpack.packb([b'spam', 'eggs'], use_bin_type=False), raw=True)
224224
[b'spam', b'eggs']
225-
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=True), raw=False)
225+
>>> msgpack.unpackb(msgpack.packb([b'spam', 'eggs'], use_bin_type=True), raw=False)
226226
[b'spam', 'eggs']
227227
```
228228

docs/conf.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
master_doc = "index"
4141

4242
# General information about the project.
43-
project = u"msgpack"
44-
copyright = u"Inada Naoki"
43+
project = "msgpack"
44+
copyright = "Inada Naoki"
4545

4646
# The version info for the project you're documenting, acts as replacement for
4747
# |version| and |release|, also used in various other places throughout the
@@ -181,7 +181,7 @@
181181
# Grouping the document tree into LaTeX files. List of tuples
182182
# (source start file, target name, title, author, documentclass [howto/manual]).
183183
latex_documents = [
184-
("index", "msgpack.tex", u"msgpack Documentation", u"Author", "manual"),
184+
("index", "msgpack.tex", "msgpack Documentation", "Author", "manual"),
185185
]
186186

187187
# The name of an image file (relative to this directory) to place at the top of
@@ -209,7 +209,7 @@
209209

210210
# One entry per manual page. List of tuples
211211
# (source start file, name, description, authors, manual section).
212-
man_pages = [("index", "msgpack", u"msgpack Documentation", [u"Author"], 1)]
212+
man_pages = [("index", "msgpack", "msgpack Documentation", ["Author"], 1)]
213213

214214
# If true, show URL addresses after external links.
215215
# man_show_urls = False
@@ -224,8 +224,8 @@
224224
(
225225
"index",
226226
"msgpack",
227-
u"msgpack Documentation",
228-
u"Author",
227+
"msgpack Documentation",
228+
"Author",
229229
"msgpack",
230230
"One line description of project.",
231231
"Miscellaneous",
@@ -245,10 +245,10 @@
245245
# -- Options for Epub output ---------------------------------------------------
246246

247247
# Bibliographic Dublin Core info.
248-
epub_title = u"msgpack"
249-
epub_author = u"Author"
250-
epub_publisher = u"Author"
251-
epub_copyright = u"2013, Author"
248+
epub_title = "msgpack"
249+
epub_author = "Author"
250+
epub_publisher = "Author"
251+
epub_copyright = "2013, Author"
252252

253253
# The language of the text. It defaults to the language option
254254
# or en if the language is not set.

msgpack/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
__version__ = "1.0.5"
1111

1212

13-
if os.environ.get("MSGPACK_PUREPYTHON") or sys.version_info[0] == 2:
13+
if os.environ.get("MSGPACK_PUREPYTHON"):
1414
from .fallback import Packer, unpackb, Unpacker
1515
else:
1616
try:

msgpack/_packer.pyx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ cdef class Packer(object):
9898
If set to true, datetime with tzinfo is packed into Timestamp type.
9999
Note that the tzinfo is stripped in the timestamp.
100100
You can get UTC datetime with `timestamp=3` option of the Unpacker.
101-
(Python 2 is not supported).
102101
103102
:param str unicode_errors:
104103
The error handler for encoding unicode. (default: 'strict')

msgpack/_unpacker.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ cdef class Unpacker(object):
236236
0 - Timestamp
237237
1 - float (Seconds from the EPOCH)
238238
2 - int (Nanoseconds from the EPOCH)
239-
3 - datetime.datetime (UTC). Python 2 is not supported.
239+
3 - datetime.datetime (UTC).
240240
241241
:param bool strict_map_key:
242242
If true (default), only str or bytes are accepted for map (dict) keys.

msgpack/ext.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,6 @@
55
import struct
66

77

8-
PY2 = sys.version_info[0] == 2
9-
10-
if PY2:
11-
int_types = (int, long)
12-
_utc = None
13-
else:
14-
int_types = int
15-
try:
16-
_utc = datetime.timezone.utc
17-
except AttributeError:
18-
_utc = datetime.timezone(datetime.timedelta(0))
19-
20-
218
class ExtType(namedtuple("ExtType", "code data")):
229
"""ExtType represents ext type in msgpack."""
2310

@@ -55,9 +42,9 @@ def __init__(self, seconds, nanoseconds=0):
5542
5643
Note: Negative times (before the UNIX epoch) are represented as negative seconds + positive ns.
5744
"""
58-
if not isinstance(seconds, int_types):
45+
if not isinstance(seconds, int):
5946
raise TypeError("seconds must be an integer")
60-
if not isinstance(nanoseconds, int_types):
47+
if not isinstance(nanoseconds, int):
6148
raise TypeError("nanoseconds must be an integer")
6249
if not (0 <= nanoseconds < 10**9):
6350
raise ValueError(
@@ -174,20 +161,17 @@ def to_unix_nano(self):
174161
def to_datetime(self):
175162
"""Get the timestamp as a UTC datetime.
176163
177-
Python 2 is not supported.
178-
179164
:rtype: datetime.
180165
"""
181-
return datetime.datetime.fromtimestamp(0, _utc) + datetime.timedelta(
166+
utc = datetime.timezone.utc
167+
return datetime.datetime.fromtimestamp(0, utc) + datetime.timedelta(
182168
seconds=self.to_unix()
183169
)
184170

185171
@staticmethod
186172
def from_datetime(dt):
187173
"""Create a Timestamp from datetime with tzinfo.
188174
189-
Python 2 is not supported.
190-
191175
:rtype: Timestamp
192176
"""
193177
return Timestamp.from_unix(dt.timestamp())

msgpack/fallback.py

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@
44
import struct
55

66

7-
PY2 = sys.version_info[0] == 2
8-
if PY2:
9-
int_types = (int, long)
10-
11-
def dict_iteritems(d):
12-
return d.iteritems()
13-
14-
else:
15-
int_types = int
16-
unicode = str
17-
xrange = range
18-
19-
def dict_iteritems(d):
20-
return d.items()
21-
22-
237
if sys.version_info < (3, 5):
248
# Ugly hack...
259
RecursionError = RuntimeError
@@ -134,15 +118,6 @@ def unpackb(packed, **kwargs):
134118
return ret
135119

136120

137-
if sys.version_info < (2, 7, 6):
138-
139-
def _unpack_from(f, b, o=0):
140-
"""Explicit type cast for legacy struct.unpack_from"""
141-
return struct.unpack_from(f, bytes(b), o)
142-
143-
else:
144-
_unpack_from = struct.unpack_from
145-
146121
_NO_FORMAT_USED = ""
147122
_MSGPACK_HEADERS = {
148123
0xC4: (1, _NO_FORMAT_USED, TYPE_BIN),
@@ -202,7 +177,7 @@ class Unpacker(object):
202177
0 - Timestamp
203178
1 - float (Seconds from the EPOCH)
204179
2 - int (Nanoseconds from the EPOCH)
205-
3 - datetime.datetime (UTC). Python 2 is not supported.
180+
3 - datetime.datetime (UTC).
206181
207182
:param bool strict_map_key:
208183
If true (default), only str or bytes are accepted for map (dict) keys.
@@ -477,7 +452,7 @@ def _read_header(self):
477452
size, fmt, typ = _MSGPACK_HEADERS[b]
478453
self._reserve(size)
479454
if len(fmt) > 0:
480-
n = _unpack_from(fmt, self._buffer, self._buff_i)[0]
455+
n = struct.unpack_from(fmt, self._buffer, self._buff_i)[0]
481456
else:
482457
n = self._buffer[self._buff_i]
483458
self._buff_i += size
@@ -487,7 +462,7 @@ def _read_header(self):
487462
elif 0xC7 <= b <= 0xC9:
488463
size, fmt, typ = _MSGPACK_HEADERS[b]
489464
self._reserve(size)
490-
L, n = _unpack_from(fmt, self._buffer, self._buff_i)
465+
L, n = struct.unpack_from(fmt, self._buffer, self._buff_i)
491466
self._buff_i += size
492467
if L > self._max_ext_len:
493468
raise ValueError("%s exceeds max_ext_len(%s)" % (L, self._max_ext_len))
@@ -496,7 +471,7 @@ def _read_header(self):
496471
size, fmt = _MSGPACK_HEADERS[b]
497472
self._reserve(size)
498473
if len(fmt) > 0:
499-
obj = _unpack_from(fmt, self._buffer, self._buff_i)[0]
474+
obj = struct.unpack_from(fmt, self._buffer, self._buff_i)[0]
500475
else:
501476
obj = self._buffer[self._buff_i]
502477
self._buff_i += size
@@ -507,13 +482,13 @@ def _read_header(self):
507482
"%s exceeds max_ext_len(%s)" % (size, self._max_ext_len)
508483
)
509484
self._reserve(size + 1)
510-
n, obj = _unpack_from(fmt, self._buffer, self._buff_i)
485+
n, obj = struct.unpack_from(fmt, self._buffer, self._buff_i)
511486
self._buff_i += size + 1
512487
elif 0xD9 <= b <= 0xDB:
513488
size, fmt, typ = _MSGPACK_HEADERS[b]
514489
self._reserve(size)
515490
if len(fmt) > 0:
516-
(n,) = _unpack_from(fmt, self._buffer, self._buff_i)
491+
(n,) = struct.unpack_from(fmt, self._buffer, self._buff_i)
517492
else:
518493
n = self._buffer[self._buff_i]
519494
self._buff_i += size
@@ -523,7 +498,7 @@ def _read_header(self):
523498
elif 0xDC <= b <= 0xDD:
524499
size, fmt, typ = _MSGPACK_HEADERS[b]
525500
self._reserve(size)
526-
(n,) = _unpack_from(fmt, self._buffer, self._buff_i)
501+
(n,) = struct.unpack_from(fmt, self._buffer, self._buff_i)
527502
self._buff_i += size
528503
if n > self._max_array_len:
529504
raise ValueError(
@@ -532,7 +507,7 @@ def _read_header(self):
532507
elif 0xDE <= b <= 0xDF:
533508
size, fmt, typ = _MSGPACK_HEADERS[b]
534509
self._reserve(size)
535-
(n,) = _unpack_from(fmt, self._buffer, self._buff_i)
510+
(n,) = struct.unpack_from(fmt, self._buffer, self._buff_i)
536511
self._buff_i += size
537512
if n > self._max_map_len:
538513
raise ValueError("%s exceeds max_map_len(%s)" % (n, self._max_map_len))
@@ -554,38 +529,38 @@ def _unpack(self, execute=EX_CONSTRUCT):
554529
# TODO should we eliminate the recursion?
555530
if typ == TYPE_ARRAY:
556531
if execute == EX_SKIP:
557-
for i in xrange(n):
532+
for i in range(n):
558533
# TODO check whether we need to call `list_hook`
559534
self._unpack(EX_SKIP)
560535
return
561536
ret = newlist_hint(n)
562-
for i in xrange(n):
537+
for i in range(n):
563538
ret.append(self._unpack(EX_CONSTRUCT))
564539
if self._list_hook is not None:
565540
ret = self._list_hook(ret)
566541
# TODO is the interaction between `list_hook` and `use_list` ok?
567542
return ret if self._use_list else tuple(ret)
568543
if typ == TYPE_MAP:
569544
if execute == EX_SKIP:
570-
for i in xrange(n):
545+
for i in range(n):
571546
# TODO check whether we need to call hooks
572547
self._unpack(EX_SKIP)
573548
self._unpack(EX_SKIP)
574549
return
575550
if self._object_pairs_hook is not None:
576551
ret = self._object_pairs_hook(
577552
(self._unpack(EX_CONSTRUCT), self._unpack(EX_CONSTRUCT))
578-
for _ in xrange(n)
553+
for _ in range(n)
579554
)
580555
else:
581556
ret = {}
582-
for _ in xrange(n):
557+
for _ in range(n):
583558
key = self._unpack(EX_CONSTRUCT)
584-
if self._strict_map_key and type(key) not in (unicode, bytes):
559+
if self._strict_map_key and type(key) not in (str, bytes):
585560
raise ValueError(
586561
"%s is not allowed for map key" % str(type(key))
587562
)
588-
if not PY2 and type(key) is str:
563+
if type(key) is str:
589564
key = sys.intern(key)
590565
ret[key] = self._unpack(EX_CONSTRUCT)
591566
if self._object_hook is not None:
@@ -698,7 +673,6 @@ class Packer(object):
698673
If set to true, datetime with tzinfo is packed into Timestamp type.
699674
Note that the tzinfo is stripped in the timestamp.
700675
You can get UTC datetime with `timestamp=3` option of the Unpacker.
701-
(Python 2 is not supported).
702676
703677
:param str unicode_errors:
704678
The error handler for encoding unicode. (default: 'strict')
@@ -743,8 +717,6 @@ def __init__(
743717
self._autoreset = autoreset
744718
self._use_bin_type = use_bin_type
745719
self._buffer = StringIO()
746-
if PY2 and datetime:
747-
raise ValueError("datetime is not supported in Python 2")
748720
self._datetime = bool(datetime)
749721
self._unicode_errors = unicode_errors or "strict"
750722
if default is not None:
@@ -774,7 +746,7 @@ def _pack(
774746
if obj:
775747
return self._buffer.write(b"\xc3")
776748
return self._buffer.write(b"\xc2")
777-
if check(obj, int_types):
749+
if check(obj, int):
778750
if 0 <= obj < 0x80:
779751
return self._buffer.write(struct.pack("B", obj))
780752
if -0x20 <= obj < 0:
@@ -806,7 +778,7 @@ def _pack(
806778
raise ValueError("%s is too large" % type(obj).__name__)
807779
self._pack_bin_header(n)
808780
return self._buffer.write(obj)
809-
if check(obj, unicode):
781+
if check(obj, str):
810782
obj = obj.encode("utf-8", self._unicode_errors)
811783
n = len(obj)
812784
if n >= 2**32:
@@ -855,13 +827,11 @@ def _pack(
855827
if check(obj, list_types):
856828
n = len(obj)
857829
self._pack_array_header(n)
858-
for i in xrange(n):
830+
for i in range(n):
859831
self._pack(obj[i], nest_limit - 1)
860832
return
861833
if check(obj, dict):
862-
return self._pack_map_pairs(
863-
len(obj), dict_iteritems(obj), nest_limit - 1
864-
)
834+
return self._pack_map_pairs(len(obj), obj.items(), nest_limit - 1)
865835

866836
if self._datetime and check(obj, _DateTime) and obj.tzinfo is not None:
867837
obj = Timestamp.from_datetime(obj)
@@ -1004,7 +974,7 @@ def reset(self):
1004974

1005975
def getbuffer(self):
1006976
"""Return view of internal buffer."""
1007-
if USING_STRINGBUILDER or PY2:
977+
if USING_STRINGBUILDER:
1008978
return memoryview(self.bytes())
1009979
else:
1010980
return self._buffer.getbuffer()

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111

1212
PYPY = hasattr(sys, "pypy_version_info")
13-
PY2 = sys.version_info[0] == 2
1413

1514

1615
class NoCython(Exception):
@@ -79,7 +78,7 @@ def __init__(self, *args, **kwargs):
7978
macros = [("__LITTLE_ENDIAN__", "1")]
8079

8180
ext_modules = []
82-
if not PYPY and not PY2 and not os.environ.get("MSGPACK_PUREPYTHON"):
81+
if not PYPY and not os.environ.get("MSGPACK_PUREPYTHON"):
8382
ext_modules.append(
8483
Extension(
8584
"msgpack._cmsgpack",

test/test_buffer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from msgpack import packb, unpackb
77

88

9-
@pytest.mark.skipif(sys.version_info[0] == 2, reason="Python 2 is not supported")
109
def test_unpack_buffer():
1110
from array import array
1211

test/test_case.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,4 @@ def test_match():
134134

135135

136136
def test_unicode():
137-
assert unpackb(packb(u"foobar"), use_list=1) == u"foobar"
137+
assert unpackb(packb("foobar"), use_list=1) == "foobar"

0 commit comments

Comments
 (0)