Skip to content

Commit

Permalink
Update setuptools and black (#498)
Browse files Browse the repository at this point in the history
* Use setuptools
* Use black==22.1.0
  • Loading branch information
methane authored Mar 3, 2022
1 parent 89ea577 commit cb50b20
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 84 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/black.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ jobs:
architecture: 'x64'

- name: Checkout
uses: actions/checkout@v1
uses: actions/checkout@v2

- name: Black Code Formatter
run: |
pip install black
black --diff --check msgpack/ test/ setup.py
pip install black==22.1.0
black -S --diff --check msgpack/ test/ setup.py
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ all: cython

.PHONY: black
black:
black msgpack/ test/ setup.py
black -S msgpack/ test/ setup.py

.PHONY: cython
cython:
Expand Down
5 changes: 4 additions & 1 deletion msgpack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# coding: utf-8
from ._version import version
from .exceptions import *
from .ext import ExtType, Timestamp

import os
import sys


version = (1, 0, 4, 'dev')
__version__ = "1.0.4dev"


if os.environ.get("MSGPACK_PUREPYTHON") or sys.version_info[0] == 2:
from .fallback import Packer, unpackb, Unpacker
else:
Expand Down
1 change: 0 additions & 1 deletion msgpack/_version.py

This file was deleted.

8 changes: 4 additions & 4 deletions msgpack/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(self, seconds, nanoseconds=0):
raise TypeError("seconds must be an interger")
if not isinstance(nanoseconds, int_types):
raise TypeError("nanoseconds must be an integer")
if not (0 <= nanoseconds < 10 ** 9):
if not (0 <= nanoseconds < 10**9):
raise ValueError(
"nanoseconds must be a non-negative integer less than 999999999."
)
Expand Down Expand Up @@ -143,7 +143,7 @@ def from_unix(unix_sec):
:type unix_float: int or float.
"""
seconds = int(unix_sec // 1)
nanoseconds = int((unix_sec % 1) * 10 ** 9)
nanoseconds = int((unix_sec % 1) * 10**9)
return Timestamp(seconds, nanoseconds)

def to_unix(self):
Expand All @@ -161,15 +161,15 @@ def from_unix_nano(unix_ns):
:param int unix_ns: Posix timestamp in nanoseconds.
:rtype: Timestamp
"""
return Timestamp(*divmod(unix_ns, 10 ** 9))
return Timestamp(*divmod(unix_ns, 10**9))

def to_unix_nano(self):
"""Get the timestamp as a unixtime in nanoseconds.
:returns: posix timestamp in nanoseconds
:rtype: int
"""
return self.seconds * 10 ** 9 + self.nanoseconds
return self.seconds * 10**9 + self.nanoseconds

def to_datetime(self):
"""Get the timestamp as a UTC datetime.
Expand Down
12 changes: 6 additions & 6 deletions msgpack/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def __init__(
self._buf_checkpoint = 0

if not max_buffer_size:
max_buffer_size = 2 ** 31 - 1
max_buffer_size = 2**31 - 1
if max_str_len == -1:
max_str_len = max_buffer_size
if max_bin_len == -1:
Expand Down Expand Up @@ -800,20 +800,20 @@ def _pack(
raise OverflowError("Integer value out of range")
if check(obj, (bytes, bytearray)):
n = len(obj)
if n >= 2 ** 32:
if n >= 2**32:
raise ValueError("%s is too large" % type(obj).__name__)
self._pack_bin_header(n)
return self._buffer.write(obj)
if check(obj, unicode):
obj = obj.encode("utf-8", self._unicode_errors)
n = len(obj)
if n >= 2 ** 32:
if n >= 2**32:
raise ValueError("String is too large")
self._pack_raw_header(n)
return self._buffer.write(obj)
if check(obj, memoryview):
n = len(obj) * obj.itemsize
if n >= 2 ** 32:
if n >= 2**32:
raise ValueError("Memoryview is too large")
self._pack_bin_header(n)
return self._buffer.write(obj)
Expand Down Expand Up @@ -895,7 +895,7 @@ def pack_map_pairs(self, pairs):
return ret

def pack_array_header(self, n):
if n >= 2 ** 32:
if n >= 2**32:
raise ValueError
self._pack_array_header(n)
if self._autoreset:
Expand All @@ -904,7 +904,7 @@ def pack_array_header(self, n):
return ret

def pack_map_header(self, n):
if n >= 2 ** 32:
if n >= 2**32:
raise ValueError
self._pack_map_header(n)
if self._autoreset:
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Also declared in pyproject.toml, if updating here please also update there
Cython~=0.29.13

# dev only tools. no need to add pyproject
black==22.1.0
32 changes: 32 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[metadata]
name = msgpack
#version = attr: msgpack.__version__
version = attr: msgpack.version
license = Apache 2.0
author = Inada Naoki
author_email = songofacandy@gmail.com
description = MessagePack serializer
long_description = file: README.md
long_description_content_type = text/markdown
url = https://msgpack.org/

project_urls =
Documentation = https://msgpack-python.readthedocs.io/
Source = https://github.com/msgpack/msgpack-python
Tracker = https://github.com/msgpack/msgpack-python/issues

classifiers =
Programming Language :: Python :: 3
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: Implementation :: CPython
Programming Language :: Python :: Implementation :: PyPy
Intended Audience :: Developers
License :: OSI Approved :: Apache Software License

[flake8]
max_line_length = 100

42 changes: 2 additions & 40 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import os
import sys
from glob import glob
from distutils.command.sdist import sdist
from setuptools import setup, Extension

from distutils.command.build_ext import build_ext
from setuptools.command.build_ext import build_ext
from setuptools.command.sdist import sdist


PYPY = hasattr(sys, "pypy_version_info")
Expand Down Expand Up @@ -65,12 +64,6 @@ def build_extension(self, ext):
print(e)


exec(open("msgpack/_version.py").read())

version_str = ".".join(str(x) for x in version[:3])
if len(version) > 3 and version[3] != "final":
version_str += version[3]

# Cython is required for sdist
class Sdist(sdist):
def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -99,39 +92,8 @@ def __init__(self, *args, **kwargs):
del libraries, macros


desc = "MessagePack (de)serializer."
with io.open("README.md", encoding="utf-8") as f:
long_desc = f.read()
del f

setup(
name="msgpack",
author="Inada Naoki",
author_email="songofacandy@gmail.com",
version=version_str,
cmdclass={"build_ext": BuildExt, "sdist": Sdist},
ext_modules=ext_modules,
packages=["msgpack"],
description=desc,
long_description=long_desc,
long_description_content_type="text/markdown",
url="https://msgpack.org/",
project_urls={
"Documentation": "https://msgpack-python.readthedocs.io/",
"Source": "https://github.com/msgpack/msgpack-python",
"Tracker": "https://github.com/msgpack/msgpack-python/issues",
},
license="Apache 2.0",
classifiers=[
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
],
)
12 changes: 6 additions & 6 deletions test/test_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@


def test_integer():
x = -(2 ** 63)
x = -(2**63)
assert unpackb(packb(x)) == x
with pytest.raises(PackOverflowError):
packb(x - 1)

x = 2 ** 64 - 1
x = 2**64 - 1
assert unpackb(packb(x)) == x
with pytest.raises(PackOverflowError):
packb(x + 1)


def test_array_header():
packer = Packer()
packer.pack_array_header(2 ** 32 - 1)
packer.pack_array_header(2**32 - 1)
with pytest.raises(PackValueError):
packer.pack_array_header(2 ** 32)
packer.pack_array_header(2**32)


def test_map_header():
packer = Packer()
packer.pack_map_header(2 ** 32 - 1)
packer.pack_map_header(2**32 - 1)
with pytest.raises(PackValueError):
packer.pack_array_header(2 ** 32)
packer.pack_array_header(2**32)


def test_max_str_len():
Expand Down
28 changes: 14 additions & 14 deletions test/test_memoryview.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,46 +53,46 @@ def test_fixstr_from_float():


def test_str16_from_byte():
_runtest("B", 2 ** 8, b"\xda", b"\x01\x00", False)
_runtest("B", 2 ** 16 - 1, b"\xda", b"\xff\xff", False)
_runtest("B", 2**8, b"\xda", b"\x01\x00", False)
_runtest("B", 2**16 - 1, b"\xda", b"\xff\xff", False)


def test_str16_from_float():
_runtest("f", 2 ** 8, b"\xda", b"\x01\x00", False)
_runtest("f", 2 ** 16 - 4, b"\xda", b"\xff\xfc", False)
_runtest("f", 2**8, b"\xda", b"\x01\x00", False)
_runtest("f", 2**16 - 4, b"\xda", b"\xff\xfc", False)


def test_str32_from_byte():
_runtest("B", 2 ** 16, b"\xdb", b"\x00\x01\x00\x00", False)
_runtest("B", 2**16, b"\xdb", b"\x00\x01\x00\x00", False)


def test_str32_from_float():
_runtest("f", 2 ** 16, b"\xdb", b"\x00\x01\x00\x00", False)
_runtest("f", 2**16, b"\xdb", b"\x00\x01\x00\x00", False)


def test_bin8_from_byte():
_runtest("B", 1, b"\xc4", b"\x01", True)
_runtest("B", 2 ** 8 - 1, b"\xc4", b"\xff", True)
_runtest("B", 2**8 - 1, b"\xc4", b"\xff", True)


def test_bin8_from_float():
_runtest("f", 4, b"\xc4", b"\x04", True)
_runtest("f", 2 ** 8 - 4, b"\xc4", b"\xfc", True)
_runtest("f", 2**8 - 4, b"\xc4", b"\xfc", True)


def test_bin16_from_byte():
_runtest("B", 2 ** 8, b"\xc5", b"\x01\x00", True)
_runtest("B", 2 ** 16 - 1, b"\xc5", b"\xff\xff", True)
_runtest("B", 2**8, b"\xc5", b"\x01\x00", True)
_runtest("B", 2**16 - 1, b"\xc5", b"\xff\xff", True)


def test_bin16_from_float():
_runtest("f", 2 ** 8, b"\xc5", b"\x01\x00", True)
_runtest("f", 2 ** 16 - 4, b"\xc5", b"\xff\xfc", True)
_runtest("f", 2**8, b"\xc5", b"\x01\x00", True)
_runtest("f", 2**16 - 4, b"\xc5", b"\xff\xfc", True)


def test_bin32_from_byte():
_runtest("B", 2 ** 16, b"\xc6", b"\x00\x01\x00\x00", True)
_runtest("B", 2**16, b"\xc6", b"\x00\x01\x00\x00", True)


def test_bin32_from_float():
_runtest("f", 2 ** 16, b"\xc6", b"\x00\x01\x00\x00", True)
_runtest("f", 2**16, b"\xc6", b"\x00\x01\x00\x00", True)
4 changes: 2 additions & 2 deletions test/test_sequnpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ def test_issue124():

def test_unpack_tell():
stream = io.BytesIO()
messages = [2 ** i - 1 for i in range(65)]
messages += [-(2 ** i) for i in range(1, 64)]
messages = [2**i - 1 for i in range(65)]
messages += [-(2**i) for i in range(1, 64)]
messages += [
b"hello",
b"hello" * 1000,
Expand Down
12 changes: 6 additions & 6 deletions test/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@

def test_timestamp():
# timestamp32
ts = Timestamp(2 ** 32 - 1)
ts = Timestamp(2**32 - 1)
assert ts.to_bytes() == b"\xff\xff\xff\xff"
packed = msgpack.packb(ts)
assert packed == b"\xd6\xff" + ts.to_bytes()
unpacked = msgpack.unpackb(packed)
assert ts == unpacked
assert ts.seconds == 2 ** 32 - 1 and ts.nanoseconds == 0
assert ts.seconds == 2**32 - 1 and ts.nanoseconds == 0

# timestamp64
ts = Timestamp(2 ** 34 - 1, 999999999)
ts = Timestamp(2**34 - 1, 999999999)
assert ts.to_bytes() == b"\xee\x6b\x27\xff\xff\xff\xff\xff"
packed = msgpack.packb(ts)
assert packed == b"\xd7\xff" + ts.to_bytes()
unpacked = msgpack.unpackb(packed)
assert ts == unpacked
assert ts.seconds == 2 ** 34 - 1 and ts.nanoseconds == 999999999
assert ts.seconds == 2**34 - 1 and ts.nanoseconds == 999999999

# timestamp96
ts = Timestamp(2 ** 63 - 1, 999999999)
ts = Timestamp(2**63 - 1, 999999999)
assert ts.to_bytes() == b"\x3b\x9a\xc9\xff\x7f\xff\xff\xff\xff\xff\xff\xff"
packed = msgpack.packb(ts)
assert packed == b"\xc7\x0c\xff" + ts.to_bytes()
unpacked = msgpack.unpackb(packed)
assert ts == unpacked
assert ts.seconds == 2 ** 63 - 1 and ts.nanoseconds == 999999999
assert ts.seconds == 2**63 - 1 and ts.nanoseconds == 999999999

# negative fractional
ts = Timestamp.from_unix(-2.3) # s: -3, ns: 700000000
Expand Down

0 comments on commit cb50b20

Please sign in to comment.