Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup code and pyproject #608

Merged
merged 4 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ all: cython

.PHONY: format
format:
pipx run ruff format $(PYTHON_SOURCES)
ruff format $(PYTHON_SOURCES)

.PHONY: lint
lint:
pipx run ruff check $(PYTHON_SOURCES)
ruff check $(PYTHON_SOURCES)

.PHONY: doc
doc:
Expand Down
12 changes: 6 additions & 6 deletions msgpack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from .exceptions import *
from .ext import ExtType, Timestamp

# ruff: noqa: F401
import os

from .exceptions import * # noqa: F403
from .ext import ExtType, Timestamp

version = (1, 0, 8)
__version__ = "1.0.8"


if os.environ.get("MSGPACK_PUREPYTHON"):
from .fallback import Packer, unpackb, Unpacker
from .fallback import Packer, Unpacker, unpackb
else:
try:
from ._cmsgpack import Packer, unpackb, Unpacker
from ._cmsgpack import Packer, Unpacker, unpackb
except ImportError:
from .fallback import Packer, unpackb, Unpacker
from .fallback import Packer, Unpacker, unpackb


def pack(o, stream, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion msgpack/ext.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import namedtuple
import datetime
import struct
from collections import namedtuple


class ExtType(namedtuple("ExtType", "code data")):
Expand Down
52 changes: 23 additions & 29 deletions msgpack/fallback.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
"""Fallback pure Python implementation of msgpack"""

from datetime import datetime as _DateTime
import sys
import struct

import sys
from datetime import datetime as _DateTime

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

try:
from __pypy__.builders import BytesBuilder as StringBuilder
except ImportError:
from __pypy__.builders import StringBuilder
USING_STRINGBUILDER = True
_USING_STRINGBUILDER = True

class StringIO:
class BytesIO:
def __init__(self, s=b""):
if s:
self.builder = StringBuilder(len(s))
self.builder = BytesBuilder(len(s))
self.builder.append(s)
else:
self.builder = StringBuilder()
self.builder = BytesBuilder()

def write(self, s):
if isinstance(s, memoryview):
Expand All @@ -35,17 +29,17 @@ def getvalue(self):
return self.builder.build()

else:
USING_STRINGBUILDER = False
from io import BytesIO as StringIO
from io import BytesIO

newlist_hint = lambda size: []
_USING_STRINGBUILDER = False

def newlist_hint(size):
return []

from .exceptions import BufferFull, OutOfData, ExtraData, FormatError, StackError

from .exceptions import BufferFull, ExtraData, FormatError, OutOfData, StackError
from .ext import ExtType, Timestamp


EX_SKIP = 0
EX_CONSTRUCT = 1
EX_READ_ARRAY_HEADER = 2
Expand Down Expand Up @@ -335,6 +329,7 @@ def feed(self, next_bytes):

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

def _consume(self):
"""Gets rid of the used parts of the buffer."""
Expand Down Expand Up @@ -671,12 +666,11 @@ def __init__(
self._use_float = use_single_float
self._autoreset = autoreset
self._use_bin_type = use_bin_type
self._buffer = StringIO()
self._buffer = BytesIO()
self._datetime = bool(datetime)
self._unicode_errors = unicode_errors or "strict"
if default is not None:
if not callable(default):
raise TypeError("default must be callable")
if default is not None and not callable(default):
raise TypeError("default must be callable")
self._default = default

def _pack(
Expand Down Expand Up @@ -807,18 +801,18 @@ def pack(self, obj):
try:
self._pack(obj)
except:
self._buffer = StringIO() # force reset
self._buffer = BytesIO() # force reset
raise
if self._autoreset:
ret = self._buffer.getvalue()
self._buffer = StringIO()
self._buffer = BytesIO()
return ret

def pack_map_pairs(self, pairs):
self._pack_map_pairs(len(pairs), pairs)
if self._autoreset:
ret = self._buffer.getvalue()
self._buffer = StringIO()
self._buffer = BytesIO()
return ret

def pack_array_header(self, n):
Expand All @@ -827,7 +821,7 @@ def pack_array_header(self, n):
self._pack_array_header(n)
if self._autoreset:
ret = self._buffer.getvalue()
self._buffer = StringIO()
self._buffer = BytesIO()
return ret

def pack_map_header(self, n):
Expand All @@ -836,7 +830,7 @@ def pack_map_header(self, n):
self._pack_map_header(n)
if self._autoreset:
ret = self._buffer.getvalue()
self._buffer = StringIO()
self._buffer = BytesIO()
return ret

def pack_ext_type(self, typecode, data):
Expand Down Expand Up @@ -925,11 +919,11 @@ def reset(self):

This method is useful only when autoreset=False.
"""
self._buffer = StringIO()
self._buffer = BytesIO()

def getbuffer(self):
"""Return view of internal buffer."""
if USING_STRINGBUILDER:
if _USING_STRINGBUILDER:
return memoryview(self.bytes())
else:
return self._buffer.getbuffer()
17 changes: 6 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,12 @@ include-package-data = false
[tool.setuptools.dynamic]
version = {attr = "msgpack.__version__"}

[tool.black]
line-length = 100
target-version = ["py37"]
skip_string_normalization = true

[tool.ruff]
line-length = 100
target-version = "py38"
lint.ignore = []

[tool.ruff.lint.per-file-ignores]
"msgpack/__init__.py" = ["F401", "F403"]
"msgpack/fallback.py" = ["E731"]
"test/test_seq.py" = ["E501"]
lint.select = [
"E", # pycodestyle
"F", # Pyflakes
"I", # isort
#"UP", pyupgrade
]
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python
import os
import sys
from setuptools import setup, Extension

from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
from setuptools.command.sdist import sdist


PYPY = hasattr(sys, "pypy_version_info")


Expand Down
2 changes: 1 addition & 1 deletion test/test_buffer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pytest import raises

from msgpack import packb, unpackb, Packer
from msgpack import Packer, packb, unpackb


def test_unpack_buffer():
Expand Down
5 changes: 3 additions & 2 deletions test/test_except.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env python

import datetime

from pytest import raises
from msgpack import packb, unpackb, Unpacker, FormatError, StackError, OutOfData

import datetime
from msgpack import FormatError, OutOfData, StackError, Unpacker, packb, unpackb


class DummyException(Exception):
Expand Down
1 change: 1 addition & 0 deletions test/test_extension.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import array

import msgpack
from msgpack import ExtType

Expand Down
8 changes: 4 additions & 4 deletions test/test_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import pytest

from msgpack import (
packb,
unpackb,
Packer,
Unpacker,
ExtType,
Packer,
PackOverflowError,
PackValueError,
Unpacker,
UnpackValueError,
packb,
unpackb,
)


Expand Down
1 change: 1 addition & 0 deletions test/test_memoryview.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python

from array import array

from msgpack import packb, unpackb


Expand Down
2 changes: 1 addition & 1 deletion test/test_newspec.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from msgpack import packb, unpackb, ExtType
from msgpack import ExtType, packb, unpackb


def test_str8():
Expand Down
1 change: 1 addition & 0 deletions test/test_obj.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python

from pytest import raises

from msgpack import packb, unpackb


Expand Down
4 changes: 2 additions & 2 deletions test/test_pack.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env python

import struct
from collections import OrderedDict
from io import BytesIO
import struct

import pytest

from msgpack import packb, unpackb, Unpacker, Packer
from msgpack import Packer, Unpacker, packb, unpackb


def check(data, use_list=False):
Expand Down
2 changes: 1 addition & 1 deletion test/test_read_size.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Test Unpacker's read_array_header and read_map_header methods"""

from msgpack import packb, Unpacker, OutOfData
from msgpack import OutOfData, Unpacker, packb

UnexpectedTypeException = ValueError

Expand Down
6 changes: 3 additions & 3 deletions test/test_seq.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python

# ruff: noqa: E501
# ignore line length limit for long comments
import io
import msgpack

import msgpack

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

Expand Down
7 changes: 4 additions & 3 deletions test/test_sequnpack.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python
import io
from msgpack import Unpacker, BufferFull
from msgpack import pack, packb
from msgpack.exceptions import OutOfData

from pytest import raises

from msgpack import BufferFull, Unpacker, pack, packb
from msgpack.exceptions import OutOfData


def test_partialdata():
unpacker = Unpacker()
Expand Down
3 changes: 2 additions & 1 deletion test/test_stricttype.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import namedtuple
from msgpack import packb, unpackb, ExtType

from msgpack import ExtType, packb, unpackb


def test_namedtuple():
Expand Down
3 changes: 2 additions & 1 deletion test/test_subtype.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env python

from msgpack import packb
from collections import namedtuple

from msgpack import packb


class MyList(list):
pass
Expand Down
4 changes: 3 additions & 1 deletion test/test_timestamp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest
import datetime

import pytest

import msgpack
from msgpack.ext import Timestamp

Expand Down
11 changes: 4 additions & 7 deletions test/test_unpack.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from io import BytesIO
import sys
from msgpack import Unpacker, packb, OutOfData, ExtType
from pytest import raises, mark
from io import BytesIO

from pytest import mark, raises

try:
from itertools import izip as zip
except ImportError:
pass
from msgpack import ExtType, OutOfData, Unpacker, packb


def test_unpack_array_header_from_file():
Expand Down
Loading