Skip to content

Commit

Permalink
Update vendored msgpack-python. Needs patches.
Browse files Browse the repository at this point in the history
  • Loading branch information
honnibal committed Dec 10, 2024
1 parent e621b2b commit 3fe94fd
Show file tree
Hide file tree
Showing 19 changed files with 1,747 additions and 941 deletions.
8 changes: 5 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

PACKAGE_DATA = {"": ["*.pyx", "*.pxd", "*.c", "*.h"]}
PACKAGES = find_packages()
MOD_NAMES = ["srsly.msgpack._unpacker", "srsly.msgpack._packer"]
MOD_NAMES = ["srsly.msgpack._cmsgpack"]
COMPILE_OPTIONS = {
"msvc": ["/Ox", "/EHsc"],
"mingw32": ["-O2", "-Wno-strict-prototypes", "-Wno-unused-function"],
Expand Down Expand Up @@ -94,7 +94,7 @@ def setup_package():
exec(f.read(), about)

with chdir(str(root)):
include_dirs = [get_path("include"), "."]
include_dirs = [get_path("include"), ".", "srsly"]
ext_modules = []
for name in MOD_NAMES:
mod_path = name.replace(".", "/") + ".pyx"
Expand Down Expand Up @@ -122,7 +122,9 @@ def setup_package():
)
)
print("Cythonizing sources")
ext_modules = cythonize(ext_modules, compiler_directives=COMPILER_DIRECTIVES, language_level=2)
ext_modules = cythonize(
ext_modules, compiler_directives=COMPILER_DIRECTIVES, language_level=2
)

setup(
name="srsly",
Expand Down
80 changes: 24 additions & 56 deletions srsly/msgpack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,82 +1,50 @@
# coding: utf-8
# ruff: noqa: F401
import os

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

from ._version import version
from .exceptions import *
from ._packer import Packer as _Packer
from ._unpacker import unpackb as _unpackb
from ._unpacker import unpack as _unpack
from ._unpacker import Unpacker as _Unpacker
from ._ext_type import ExtType
from ._msgpack_numpy import encode_numpy as _encode_numpy
from ._msgpack_numpy import decode_numpy as _decode_numpy
version = (1, 1, 0)
__version__ = "1.1.0"


msgpack_encoders = catalogue.create("srsly", "msgpack_encoders", entry_points=True)
msgpack_decoders = catalogue.create("srsly", "msgpack_decoders", entry_points=True)

msgpack_encoders.register("numpy", func=_encode_numpy)
msgpack_decoders.register("numpy", func=_decode_numpy)


# msgpack_numpy extensions
class Packer(_Packer):
def __init__(self, *args, **kwargs):
default = kwargs.get("default")
for encoder in msgpack_encoders.get_all().values():
default = functools.partial(encoder, chain=default)
kwargs["default"] = default
super(Packer, self).__init__(*args, **kwargs)


class Unpacker(_Unpacker):
def __init__(self, *args, **kwargs):
object_hook = kwargs.get("object_hook")
for decoder in msgpack_decoders.get_all().values():
object_hook = functools.partial(decoder, chain=object_hook)
kwargs["object_hook"] = object_hook
super(Unpacker, self).__init__(*args, **kwargs)
if os.environ.get("MSGPACK_PUREPYTHON"):
from .fallback import Packer, Unpacker, unpackb
else:
try:
from ._cmsgpack import Packer, Unpacker, unpackb
except ImportError:
from .fallback import Packer, Unpacker, unpackb


def pack(o, stream, **kwargs):
"""
Pack an object and write it to a stream.
Pack object `o` and write it to `stream`
See :class:`Packer` for options.
"""
packer = Packer(**kwargs)
stream.write(packer.pack(o))


def packb(o, **kwargs):
"""
Pack an object and return the packed bytes.
Pack object `o` and return packed bytes
See :class:`Packer` for options.
"""
return Packer(**kwargs).pack(o)


def unpack(stream, **kwargs):
"""
Unpack a packed object from a stream.
"""
if "object_pairs_hook" not in kwargs:
object_hook = kwargs.get("object_hook")
for decoder in msgpack_decoders.get_all().values():
object_hook = functools.partial(decoder, chain=object_hook)
kwargs["object_hook"] = object_hook
return _unpack(stream, **kwargs)
Unpack an object from `stream`.

def unpackb(packed, **kwargs):
"""
Unpack a packed object.
Raises `ExtraData` when `stream` contains extra bytes.
See :class:`Unpacker` for options.
"""
if "object_pairs_hook" not in kwargs:
object_hook = kwargs.get("object_hook")
for decoder in msgpack_decoders.get_all().values():
object_hook = functools.partial(decoder, chain=object_hook)
kwargs["object_hook"] = object_hook
return _unpackb(packed, **kwargs)
data = stream.read()
return unpackb(data, **kwargs)


# alias for compatibility to simplejson/marshal/pickle.
Expand Down
11 changes: 11 additions & 0 deletions srsly/msgpack/_cmsgpack.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# coding: utf-8
#cython: embedsignature=True, c_string_encoding=ascii, language_level=3
from cpython.datetime cimport import_datetime, datetime_new
import_datetime()

import datetime
cdef object utc = datetime.timezone.utc
cdef object epoch = datetime_new(1970, 1, 1, 0, 0, 0, 0, tz=utc)

include "_packer.pyx"
include "_unpacker.pyx"
14 changes: 0 additions & 14 deletions srsly/msgpack/_ext_type.py

This file was deleted.

94 changes: 0 additions & 94 deletions srsly/msgpack/_msgpack_numpy.py

This file was deleted.

Loading

0 comments on commit 3fe94fd

Please sign in to comment.