Skip to content

Commit 33e0e86

Browse files
authored
Cleanup code and pyproject (#608)
* use isort * fallback: use BytesIO instead of StringIO. We had dropped Python 2 already.
1 parent e0f0e14 commit 33e0e86

21 files changed

+73
-78
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ all: cython
66

77
.PHONY: format
88
format:
9-
pipx run ruff format $(PYTHON_SOURCES)
9+
ruff format $(PYTHON_SOURCES)
1010

1111
.PHONY: lint
1212
lint:
13-
pipx run ruff check $(PYTHON_SOURCES)
13+
ruff check $(PYTHON_SOURCES)
1414

1515
.PHONY: doc
1616
doc:

msgpack/__init__.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
from .exceptions import *
2-
from .ext import ExtType, Timestamp
3-
1+
# ruff: noqa: F401
42
import os
53

4+
from .exceptions import * # noqa: F403
5+
from .ext import ExtType, Timestamp
66

77
version = (1, 0, 8)
88
__version__ = "1.0.8"
99

1010

1111
if os.environ.get("MSGPACK_PUREPYTHON"):
12-
from .fallback import Packer, unpackb, Unpacker
12+
from .fallback import Packer, Unpacker, unpackb
1313
else:
1414
try:
15-
from ._cmsgpack import Packer, unpackb, Unpacker
15+
from ._cmsgpack import Packer, Unpacker, unpackb
1616
except ImportError:
17-
from .fallback import Packer, unpackb, Unpacker
17+
from .fallback import Packer, Unpacker, unpackb
1818

1919

2020
def pack(o, stream, **kwargs):

msgpack/ext.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from collections import namedtuple
21
import datetime
32
import struct
3+
from collections import namedtuple
44

55

66
class ExtType(namedtuple("ExtType", "code data")):

msgpack/fallback.py

+23-29
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
"""Fallback pure Python implementation of msgpack"""
22

3-
from datetime import datetime as _DateTime
4-
import sys
53
import struct
6-
4+
import sys
5+
from datetime import datetime as _DateTime
76

87
if hasattr(sys, "pypy_version_info"):
9-
# StringIO is slow on PyPy, StringIO is faster. However: PyPy's own
10-
# StringBuilder is fastest.
118
from __pypy__ import newlist_hint
9+
from __pypy__.builders import BytesBuilder
1210

13-
try:
14-
from __pypy__.builders import BytesBuilder as StringBuilder
15-
except ImportError:
16-
from __pypy__.builders import StringBuilder
17-
USING_STRINGBUILDER = True
11+
_USING_STRINGBUILDER = True
1812

19-
class StringIO:
13+
class BytesIO:
2014
def __init__(self, s=b""):
2115
if s:
22-
self.builder = StringBuilder(len(s))
16+
self.builder = BytesBuilder(len(s))
2317
self.builder.append(s)
2418
else:
25-
self.builder = StringBuilder()
19+
self.builder = BytesBuilder()
2620

2721
def write(self, s):
2822
if isinstance(s, memoryview):
@@ -35,17 +29,17 @@ def getvalue(self):
3529
return self.builder.build()
3630

3731
else:
38-
USING_STRINGBUILDER = False
39-
from io import BytesIO as StringIO
32+
from io import BytesIO
4033

41-
newlist_hint = lambda size: []
34+
_USING_STRINGBUILDER = False
4235

36+
def newlist_hint(size):
37+
return []
4338

44-
from .exceptions import BufferFull, OutOfData, ExtraData, FormatError, StackError
4539

40+
from .exceptions import BufferFull, ExtraData, FormatError, OutOfData, StackError
4641
from .ext import ExtType, Timestamp
4742

48-
4943
EX_SKIP = 0
5044
EX_CONSTRUCT = 1
5145
EX_READ_ARRAY_HEADER = 2
@@ -335,6 +329,7 @@ def feed(self, next_bytes):
335329

336330
# Use extend here: INPLACE_ADD += doesn't reliably typecast memoryview in jython
337331
self._buffer.extend(view)
332+
view.release()
338333

339334
def _consume(self):
340335
"""Gets rid of the used parts of the buffer."""
@@ -671,12 +666,11 @@ def __init__(
671666
self._use_float = use_single_float
672667
self._autoreset = autoreset
673668
self._use_bin_type = use_bin_type
674-
self._buffer = StringIO()
669+
self._buffer = BytesIO()
675670
self._datetime = bool(datetime)
676671
self._unicode_errors = unicode_errors or "strict"
677-
if default is not None:
678-
if not callable(default):
679-
raise TypeError("default must be callable")
672+
if default is not None and not callable(default):
673+
raise TypeError("default must be callable")
680674
self._default = default
681675

682676
def _pack(
@@ -807,18 +801,18 @@ def pack(self, obj):
807801
try:
808802
self._pack(obj)
809803
except:
810-
self._buffer = StringIO() # force reset
804+
self._buffer = BytesIO() # force reset
811805
raise
812806
if self._autoreset:
813807
ret = self._buffer.getvalue()
814-
self._buffer = StringIO()
808+
self._buffer = BytesIO()
815809
return ret
816810

817811
def pack_map_pairs(self, pairs):
818812
self._pack_map_pairs(len(pairs), pairs)
819813
if self._autoreset:
820814
ret = self._buffer.getvalue()
821-
self._buffer = StringIO()
815+
self._buffer = BytesIO()
822816
return ret
823817

824818
def pack_array_header(self, n):
@@ -827,7 +821,7 @@ def pack_array_header(self, n):
827821
self._pack_array_header(n)
828822
if self._autoreset:
829823
ret = self._buffer.getvalue()
830-
self._buffer = StringIO()
824+
self._buffer = BytesIO()
831825
return ret
832826

833827
def pack_map_header(self, n):
@@ -836,7 +830,7 @@ def pack_map_header(self, n):
836830
self._pack_map_header(n)
837831
if self._autoreset:
838832
ret = self._buffer.getvalue()
839-
self._buffer = StringIO()
833+
self._buffer = BytesIO()
840834
return ret
841835

842836
def pack_ext_type(self, typecode, data):
@@ -925,11 +919,11 @@ def reset(self):
925919
926920
This method is useful only when autoreset=False.
927921
"""
928-
self._buffer = StringIO()
922+
self._buffer = BytesIO()
929923

930924
def getbuffer(self):
931925
"""Return view of internal buffer."""
932-
if USING_STRINGBUILDER:
926+
if _USING_STRINGBUILDER:
933927
return memoryview(self.bytes())
934928
else:
935929
return self._buffer.getbuffer()

pyproject.toml

+6-11
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,12 @@ include-package-data = false
4545
[tool.setuptools.dynamic]
4646
version = {attr = "msgpack.__version__"}
4747

48-
[tool.black]
49-
line-length = 100
50-
target-version = ["py37"]
51-
skip_string_normalization = true
52-
5348
[tool.ruff]
5449
line-length = 100
5550
target-version = "py38"
56-
lint.ignore = []
57-
58-
[tool.ruff.lint.per-file-ignores]
59-
"msgpack/__init__.py" = ["F401", "F403"]
60-
"msgpack/fallback.py" = ["E731"]
61-
"test/test_seq.py" = ["E501"]
51+
lint.select = [
52+
"E", # pycodestyle
53+
"F", # Pyflakes
54+
"I", # isort
55+
#"UP", pyupgrade
56+
]

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/usr/bin/env python
22
import os
33
import sys
4-
from setuptools import setup, Extension
4+
5+
from setuptools import Extension, setup
56
from setuptools.command.build_ext import build_ext
67
from setuptools.command.sdist import sdist
78

8-
99
PYPY = hasattr(sys, "pypy_version_info")
1010

1111

test/test_buffer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pytest import raises
22

3-
from msgpack import packb, unpackb, Packer
3+
from msgpack import Packer, packb, unpackb
44

55

66
def test_unpack_buffer():

test/test_except.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#!/usr/bin/env python
22

3+
import datetime
4+
35
from pytest import raises
4-
from msgpack import packb, unpackb, Unpacker, FormatError, StackError, OutOfData
56

6-
import datetime
7+
from msgpack import FormatError, OutOfData, StackError, Unpacker, packb, unpackb
78

89

910
class DummyException(Exception):

test/test_extension.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import array
2+
23
import msgpack
34
from msgpack import ExtType
45

test/test_limits.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
import pytest
33

44
from msgpack import (
5-
packb,
6-
unpackb,
7-
Packer,
8-
Unpacker,
95
ExtType,
6+
Packer,
107
PackOverflowError,
118
PackValueError,
9+
Unpacker,
1210
UnpackValueError,
11+
packb,
12+
unpackb,
1313
)
1414

1515

test/test_memoryview.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22

33
from array import array
4+
45
from msgpack import packb, unpackb
56

67

test/test_newspec.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from msgpack import packb, unpackb, ExtType
1+
from msgpack import ExtType, packb, unpackb
22

33

44
def test_str8():

test/test_obj.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22

33
from pytest import raises
4+
45
from msgpack import packb, unpackb
56

67

test/test_pack.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env python
22

3+
import struct
34
from collections import OrderedDict
45
from io import BytesIO
5-
import struct
66

77
import pytest
88

9-
from msgpack import packb, unpackb, Unpacker, Packer
9+
from msgpack import Packer, Unpacker, packb, unpackb
1010

1111

1212
def check(data, use_list=False):

test/test_read_size.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Test Unpacker's read_array_header and read_map_header methods"""
22

3-
from msgpack import packb, Unpacker, OutOfData
3+
from msgpack import OutOfData, Unpacker, packb
44

55
UnexpectedTypeException = ValueError
66

test/test_seq.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#!/usr/bin/env python
2-
1+
# ruff: noqa: E501
2+
# ignore line length limit for long comments
33
import io
4-
import msgpack
54

5+
import msgpack
66

77
binarydata = bytes(bytearray(range(256)))
88

test/test_sequnpack.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#!/usr/bin/env python
22
import io
3-
from msgpack import Unpacker, BufferFull
4-
from msgpack import pack, packb
5-
from msgpack.exceptions import OutOfData
3+
64
from pytest import raises
75

6+
from msgpack import BufferFull, Unpacker, pack, packb
7+
from msgpack.exceptions import OutOfData
8+
89

910
def test_partialdata():
1011
unpacker = Unpacker()

test/test_stricttype.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections import namedtuple
2-
from msgpack import packb, unpackb, ExtType
2+
3+
from msgpack import ExtType, packb, unpackb
34

45

56
def test_namedtuple():

test/test_subtype.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/usr/bin/env python
22

3-
from msgpack import packb
43
from collections import namedtuple
54

5+
from msgpack import packb
6+
67

78
class MyList(list):
89
pass

test/test_timestamp.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import pytest
21
import datetime
2+
3+
import pytest
4+
35
import msgpack
46
from msgpack.ext import Timestamp
57

test/test_unpack.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
from io import BytesIO
21
import sys
3-
from msgpack import Unpacker, packb, OutOfData, ExtType
4-
from pytest import raises, mark
2+
from io import BytesIO
3+
4+
from pytest import mark, raises
55

6-
try:
7-
from itertools import izip as zip
8-
except ImportError:
9-
pass
6+
from msgpack import ExtType, OutOfData, Unpacker, packb
107

118

129
def test_unpack_array_header_from_file():

0 commit comments

Comments
 (0)