Skip to content
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ msgpack/*.cpp
/tags
/docs/_build
.cache
/target
180 changes: 180 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "msgpack-python-rust"
version = "0.1.0"
edition = "2021"

[lib]
name = "_cmsgpack"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.23", features = ["extension-module"] }
2 changes: 1 addition & 1 deletion DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
### Build

```
$ make cython
$ make all
```


Expand Down
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
include setup.py
include COPYING
include README.md
include Cargo.toml
recursive-include src *.rs
recursive-include msgpack *.h *.c *.pyx
recursive-include test *.py
9 changes: 3 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PYTHON_SOURCES = msgpack test setup.py

.PHONY: all
all: cython
all:
python setup.py build_ext -i -f

.PHONY: format
Expand All @@ -20,12 +20,8 @@ doc:
pyupgrade:
@find $(PYTHON_SOURCES) -name '*.py' -type f -exec pyupgrade --py37-plus '{}' \;

.PHONY: cython
cython:
cython msgpack/_cmsgpack.pyx

.PHONY: test
test: cython
test:
pip install -e .
pytest -v test
MSGPACK_PUREPYTHON=1 pytest -v test
Expand All @@ -40,6 +36,7 @@ clean:
rm -f msgpack/_cmsgpack.cpp
rm -f msgpack/_cmsgpack.*.so
rm -f msgpack/_cmsgpack.*.pyd
rm -rf target
rm -rf msgpack/__pycache__
rm -rf test/__pycache__

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $ pip install msgpack

### Pure Python implementation

The extension module in msgpack (`msgpack._cmsgpack`) does not support PyPy.
The Rust extension module in msgpack (`msgpack._cmsgpack`) does not support PyPy.

But msgpack provides a pure Python implementation (`msgpack.fallback`) for PyPy.

Expand Down
65 changes: 36 additions & 29 deletions msgpack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,53 @@

if os.environ.get("MSGPACK_PUREPYTHON"):
from .fallback import Packer, Unpacker, unpackb
_using_cmsgpack = False
else:
try:
from ._cmsgpack import Packer, Unpacker, unpackb
from ._cmsgpack import Packer, Unpacker, pack, packb, unpack, unpackb
_using_cmsgpack = True
except ImportError:
from .fallback import Packer, Unpacker, unpackb
_using_cmsgpack = False


def pack(o, stream, **kwargs):
"""
Pack object `o` and write it to `stream`
if not _using_cmsgpack:

See :class:`Packer` for options.
"""
packer = Packer(**kwargs)
stream.write(packer.pack(o))
def pack(o, stream, **kwargs):
"""
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 object `o` and return packed bytes
def packb(o, **kwargs):
"""
Pack object `o` and return packed bytes

See :class:`Packer` for options.
"""
return Packer(**kwargs).pack(o)
See :class:`Packer` for options.
"""
return Packer(**kwargs).pack(o)

def unpack(stream, **kwargs):
"""
Unpack an object from `stream`.

def unpack(stream, **kwargs):
"""
Unpack an object from `stream`.
Raises `ExtraData` when `stream` contains extra bytes.
See :class:`Unpacker` for options.
"""
data = stream.read()
return unpackb(data, **kwargs)

Raises `ExtraData` when `stream` contains extra bytes.
See :class:`Unpacker` for options.
"""
data = stream.read()
return unpackb(data, **kwargs)
# alias for compatibility to simplejson/marshal/pickle.
load = unpack
loads = unpackb


# alias for compatibility to simplejson/marshal/pickle.
load = unpack
loads = unpackb

dump = pack
dumps = packb
dump = pack
dumps = packb
else:
load = unpack
loads = unpackb
dump = pack
dumps = packb
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools >= 78.1.1"]
requires = ["setuptools >= 78.1.1", "setuptools-rust>=1.8"]
build-backend = "setuptools.build_meta"

[project]
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Cython==3.2.1
setuptools==78.1.1
setuptools-rust>=1.8
build
26 changes: 9 additions & 17 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,23 @@
import os
import sys

from setuptools import Extension, setup
from setuptools import setup
from setuptools_rust import Binding, RustExtension

PYPY = hasattr(sys, "pypy_version_info")

libraries = []
macros = []
ext_modules = []

if sys.platform == "win32":
libraries.append("ws2_32")
macros = [("__LITTLE_ENDIAN__", "1")]

rust_extensions = []
if not PYPY and not os.environ.get("MSGPACK_PUREPYTHON"):
ext_modules.append(
Extension(
rust_extensions.append(
RustExtension(
"msgpack._cmsgpack",
sources=["msgpack/_cmsgpack.c"],
libraries=libraries,
include_dirs=["."],
define_macros=macros,
path="Cargo.toml",
binding=Binding.PyO3,
)
)
del libraries, macros

setup(
ext_modules=ext_modules,
rust_extensions=rust_extensions,
packages=["msgpack"],
zip_safe=False,
)
Loading