Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Prepare codebase for Python 3.7
  • Loading branch information
evgenymarkov committed May 22, 2023
commit 89c02cacf77711a96b905b2bbc41200dfe56724a
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ all: cython
black:
black $(PYTHON_SOURCES)

.PHONY: pyupgrade
pyupgrade:
@find $(PYTHON_SOURCES) -name '*.py' -type f -exec pyupgrade --py37-plus '{}' \;

.PHONY: cython
cython:
cython --cplus msgpack/_cmsgpack.pyx
Expand Down
1 change: 0 additions & 1 deletion msgpack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding: utf-8
from .exceptions import *
from .ext import ExtType, Timestamp

Expand Down
7 changes: 3 additions & 4 deletions msgpack/ext.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding: utf-8
from collections import namedtuple
import datetime
import sys
Expand All @@ -15,10 +14,10 @@ def __new__(cls, code, data):
raise TypeError("data must be bytes")
if not 0 <= code <= 127:
raise ValueError("code must be 0~127")
return super(ExtType, cls).__new__(cls, code, data)
return super().__new__(cls, code, data)


class Timestamp(object):
class Timestamp:
"""Timestamp represents the Timestamp extension type in msgpack.

When built with Cython, msgpack uses C methods to pack and unpack `Timestamp`. When using pure-Python
Expand Down Expand Up @@ -53,7 +52,7 @@ def __init__(self, seconds, nanoseconds=0):

def __repr__(self):
"""String representation of Timestamp."""
return "Timestamp(seconds={0}, nanoseconds={1})".format(self.seconds, self.nanoseconds)
return f"Timestamp(seconds={self.seconds}, nanoseconds={self.nanoseconds})"

def __eq__(self, other):
"""Check for equality with another Timestamp object"""
Expand Down
51 changes: 16 additions & 35 deletions msgpack/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,6 @@
import struct


if sys.version_info < (3, 5):
# Ugly hack...
RecursionError = RuntimeError

def _is_recursionerror(e):
return (
len(e.args) == 1
and isinstance(e.args[0], str)
and e.args[0].startswith("maximum recursion depth exceeded")
)

else:

def _is_recursionerror(e):
return True


if hasattr(sys, "pypy_version_info"):
# StringIO is slow on PyPy, StringIO is faster. However: PyPy's own
# StringBuilder is fastest.
Expand All @@ -32,7 +15,7 @@ def _is_recursionerror(e):
from __pypy__.builders import StringBuilder
USING_STRINGBUILDER = True

class StringIO(object):
class StringIO:
def __init__(self, s=b""):
if s:
self.builder = StringBuilder(len(s))
Expand Down Expand Up @@ -109,10 +92,8 @@ def unpackb(packed, **kwargs):
ret = unpacker._unpack()
except OutOfData:
raise ValueError("Unpack failed: incomplete input")
except RecursionError as e:
if _is_recursionerror(e):
raise StackError
raise
except RecursionError:
raise StackError
if unpacker._got_extradata():
raise ExtraData(ret, unpacker._get_extradata())
return ret
Expand Down Expand Up @@ -151,7 +132,7 @@ def unpackb(packed, **kwargs):
}


class Unpacker(object):
class Unpacker:
"""Streaming unpacker.

Arguments:
Expand Down Expand Up @@ -426,18 +407,18 @@ def _read_header(self):
n = b & 0b00011111
typ = TYPE_RAW
if n > self._max_str_len:
raise ValueError("%s exceeds max_str_len(%s)" % (n, self._max_str_len))
raise ValueError(f"{n} exceeds max_str_len({self._max_str_len})")
obj = self._read(n)
elif b & 0b11110000 == 0b10010000:
n = b & 0b00001111
typ = TYPE_ARRAY
if n > self._max_array_len:
raise ValueError("%s exceeds max_array_len(%s)" % (n, self._max_array_len))
raise ValueError(f"{n} exceeds max_array_len({self._max_array_len})")
elif b & 0b11110000 == 0b10000000:
n = b & 0b00001111
typ = TYPE_MAP
if n > self._max_map_len:
raise ValueError("%s exceeds max_map_len(%s)" % (n, self._max_map_len))
raise ValueError(f"{n} exceeds max_map_len({self._max_map_len})")
elif b == 0xC0:
obj = None
elif b == 0xC2:
Expand All @@ -453,15 +434,15 @@ def _read_header(self):
n = self._buffer[self._buff_i]
self._buff_i += size
if n > self._max_bin_len:
raise ValueError("%s exceeds max_bin_len(%s)" % (n, self._max_bin_len))
raise ValueError(f"{n} exceeds max_bin_len({self._max_bin_len})")
obj = self._read(n)
elif 0xC7 <= b <= 0xC9:
size, fmt, typ = _MSGPACK_HEADERS[b]
self._reserve(size)
L, n = struct.unpack_from(fmt, self._buffer, self._buff_i)
self._buff_i += size
if L > self._max_ext_len:
raise ValueError("%s exceeds max_ext_len(%s)" % (L, self._max_ext_len))
raise ValueError(f"{L} exceeds max_ext_len({self._max_ext_len})")
obj = self._read(L)
elif 0xCA <= b <= 0xD3:
size, fmt = _MSGPACK_HEADERS[b]
Expand All @@ -474,7 +455,7 @@ def _read_header(self):
elif 0xD4 <= b <= 0xD8:
size, fmt, typ = _MSGPACK_HEADERS[b]
if self._max_ext_len < size:
raise ValueError("%s exceeds max_ext_len(%s)" % (size, self._max_ext_len))
raise ValueError(f"{size} exceeds max_ext_len({self._max_ext_len})")
self._reserve(size + 1)
n, obj = struct.unpack_from(fmt, self._buffer, self._buff_i)
self._buff_i += size + 1
Expand All @@ -487,22 +468,22 @@ def _read_header(self):
n = self._buffer[self._buff_i]
self._buff_i += size
if n > self._max_str_len:
raise ValueError("%s exceeds max_str_len(%s)" % (n, self._max_str_len))
raise ValueError(f"{n} exceeds max_str_len({self._max_str_len})")
obj = self._read(n)
elif 0xDC <= b <= 0xDD:
size, fmt, typ = _MSGPACK_HEADERS[b]
self._reserve(size)
(n,) = struct.unpack_from(fmt, self._buffer, self._buff_i)
self._buff_i += size
if n > self._max_array_len:
raise ValueError("%s exceeds max_array_len(%s)" % (n, self._max_array_len))
raise ValueError(f"{n} exceeds max_array_len({self._max_array_len})")
elif 0xDE <= b <= 0xDF:
size, fmt, typ = _MSGPACK_HEADERS[b]
self._reserve(size)
(n,) = struct.unpack_from(fmt, self._buffer, self._buff_i)
self._buff_i += size
if n > self._max_map_len:
raise ValueError("%s exceeds max_map_len(%s)" % (n, self._max_map_len))
raise ValueError(f"{n} exceeds max_map_len({self._max_map_len})")
else:
raise FormatError("Unknown header: 0x%x" % b)
return typ, n, obj
Expand Down Expand Up @@ -623,7 +604,7 @@ def tell(self):
return self._stream_offset


class Packer(object):
class Packer:
"""
MessagePack Packer

Expand Down Expand Up @@ -833,9 +814,9 @@ def _pack(
continue

if self._datetime and check(obj, _DateTime):
raise ValueError("Cannot serialize %r where tzinfo=None" % (obj,))
raise ValueError(f"Cannot serialize {obj!r} where tzinfo=None")

raise TypeError("Cannot serialize %r" % (obj,))
raise TypeError(f"Cannot serialize {obj!r}")

def pack(self, obj):
try:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Cython~=0.29.30

# Tools required only for development. No need to add it to pyproject.toml file.
black==23.3.0
pyupgrade==3.4.0
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding: utf-8
import io
import os
import sys
Expand All @@ -25,7 +24,7 @@ class NoCython(Exception):


def cythonize(src):
sys.stderr.write("cythonize: %r\n" % (src,))
sys.stderr.write(f"cythonize: {src!r}\n")
cython_compiler.compile([src], cplus=True)


Expand Down
1 change: 0 additions & 1 deletion test/test_buffer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding: utf-8

import sys
import pytest
Expand Down
7 changes: 3 additions & 4 deletions test/test_case.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/usr/bin/env python
# coding: utf-8
from msgpack import packb, unpackb


def check(length, obj, use_bin_type=True):
v = packb(obj, use_bin_type=use_bin_type)
assert len(v) == length, "%r length should be %r but get %r" % (obj, length, len(v))
assert len(v) == length, f"{obj!r} length should be {length!r} but get {len(v)!r}"
assert unpackb(v, use_list=0, raw=not use_bin_type) == obj


Expand Down Expand Up @@ -120,11 +119,11 @@ def test_match():
),
({}, b"\x80"),
(
dict([(x, x) for x in range(15)]),
{x: x for x in range(15)},
b"\x8f\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e",
),
(
dict([(x, x) for x in range(16)]),
{x: x for x in range(16)},
b"\xde\x00\x10\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e\x0f\x0f",
),
]
Expand Down
1 change: 0 additions & 1 deletion test/test_except.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding: utf-8

from pytest import raises
from msgpack import packb, unpackb, Unpacker, FormatError, StackError, OutOfData
Expand Down
3 changes: 1 addition & 2 deletions test/test_extension.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import array
import msgpack
from msgpack import ExtType
Expand Down Expand Up @@ -47,7 +46,7 @@ def default(obj):
except AttributeError:
data = obj.tostring()
return ExtType(typecode, data)
raise TypeError("Unknown type object %r" % (obj,))
raise TypeError(f"Unknown type object {obj!r}")

def ext_hook(code, data):
print("ext_hook called", code, data)
Expand Down
1 change: 0 additions & 1 deletion test/test_format.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding: utf-8

from msgpack import unpackb

Expand Down
2 changes: 0 additions & 2 deletions test/test_limits.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/env python
# coding: utf-8
from __future__ import absolute_import, division, print_function, unicode_literals
import pytest

from msgpack import (
Expand Down
1 change: 0 additions & 1 deletion test/test_memoryview.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding: utf-8

import pytest
from array import array
Expand Down
2 changes: 0 additions & 2 deletions test/test_newspec.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# coding: utf-8

from msgpack import packb, unpackb, ExtType


Expand Down
1 change: 0 additions & 1 deletion test/test_obj.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding: utf-8

from pytest import raises
from msgpack import packb, unpackb
Expand Down
8 changes: 3 additions & 5 deletions test/test_pack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/env python
# coding: utf-8
from __future__ import absolute_import, division, print_function, unicode_literals

from collections import OrderedDict
from io import BytesIO
Expand Down Expand Up @@ -106,8 +104,8 @@ def testDecodeBinary():


def testPackFloat():
assert packb(1.0, use_single_float=True) == b"\xca" + struct.pack(str(">f"), 1.0)
assert packb(1.0, use_single_float=False) == b"\xcb" + struct.pack(str(">d"), 1.0)
assert packb(1.0, use_single_float=True) == b"\xca" + struct.pack(">f", 1.0)
assert packb(1.0, use_single_float=False) == b"\xcb" + struct.pack(">d", 1.0)


def testArraySize(sizes=[0, 5, 50, 1000]):
Expand Down Expand Up @@ -152,7 +150,7 @@ def testMapSize(sizes=[0, 5, 50, 1000]):
bio.seek(0)
unpacker = Unpacker(bio, strict_map_key=False)
for size in sizes:
assert unpacker.unpack() == dict((i, i * 2) for i in range(size))
assert unpacker.unpack() == {i: i * 2 for i in range(size)}


def test_odict():
Expand Down
1 change: 0 additions & 1 deletion test/test_seq.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding: utf-8

import io
import msgpack
Expand Down
1 change: 0 additions & 1 deletion test/test_sequnpack.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding: utf-8
import io
from msgpack import Unpacker, BufferFull
from msgpack import pack, packb
Expand Down
8 changes: 3 additions & 5 deletions test/test_stricttype.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# coding: utf-8

from collections import namedtuple
from msgpack import packb, unpackb, ExtType

Expand All @@ -10,7 +8,7 @@ def test_namedtuple():
def default(o):
if isinstance(o, T):
return dict(o._asdict())
raise TypeError("Unsupported type %s" % (type(o),))
raise TypeError(f"Unsupported type {type(o)}")

packed = packb(T(1, 42), strict_types=True, use_bin_type=True, default=default)
unpacked = unpackb(packed, raw=False)
Expand All @@ -23,7 +21,7 @@ def test_tuple():
def default(o):
if isinstance(o, tuple):
return {"__type__": "tuple", "value": list(o)}
raise TypeError("Unsupported type %s" % (type(o),))
raise TypeError(f"Unsupported type {type(o)}")

def convert(o):
if o.get("__type__") == "tuple":
Expand Down Expand Up @@ -52,7 +50,7 @@ def convert(code, payload):
if code == MSGPACK_EXT_TYPE_TUPLE:
# Unpack and convert to tuple
return tuple(unpackb(payload, raw=False, ext_hook=convert))
raise ValueError("Unknown Ext code {}".format(code))
raise ValueError(f"Unknown Ext code {code}")

data = packb(t, strict_types=True, use_bin_type=True, default=default)
expected = unpackb(data, raw=False, ext_hook=convert)
Expand Down
1 change: 0 additions & 1 deletion test/test_subtype.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding: utf-8

from msgpack import packb, unpackb
from collections import namedtuple
Expand Down
2 changes: 1 addition & 1 deletion test/test_unpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def hook(x):
def test_unpacker_ext_hook():
class MyUnpacker(Unpacker):
def __init__(self):
super(MyUnpacker, self).__init__(ext_hook=self._hook, raw=False)
super().__init__(ext_hook=self._hook, raw=False)

def _hook(self, code, data):
if code == 1:
Expand Down