Skip to content

Commit

Permalink
parity with orjson (#47)
Browse files Browse the repository at this point in the history
* update dependencies

* more parity
  • Loading branch information
aviramha authored Oct 13, 2021
1 parent d133660 commit d3e8d37
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 23 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog
## Next Version
## 1.0.1 - 13/10/2021
### Fixed
- Decrement refcount for numpy `PyArrayInterface`. by [@ilj](https://github.com/ijl/orjson/commit/4c312a82f5215ff71eed5bd09d28fa004999299b).
- Fix serialization of dataclass inheriting from `abc.ABC` and using `__slots__`. by [@ilj](https://github.com/ijl/orjson/commit/4c312a82f5215ff71eed5bd09d28fa004999299b)
### Changed
- Updated dependencies.
- `find_str_kind` test for 4-byte before latin1. by [@ilj](https://github.com/ijl/orjson/commit/05860e1a2ea3e8f90823d6a59e5fc9929a8692b5)
## 1.0.0 - 31/8/2021
### Changed
- Aligned to orjson's flags and features of SIMD. Didn't include the stable compilation part as seems unnecessary.
Expand Down
30 changes: 15 additions & 15 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ormsgpack"
version = "1.0.0"
version = "1.0.1"
authors = ["Aviram Hassan <[email protected]>"]
description = "Fast, correct Python msgpack library supporting dataclasses, datetimes, and numpy"
edition = "2018"
Expand Down Expand Up @@ -44,11 +44,11 @@ encoding_rs = { version = "0.8", default_features = false }
inlinable_string = { version = "0.1" }
itoa = { version = "0.4", default_features = false }
once_cell = { version = "1", default_features = false }
pyo3 = { version = "^0.14.1", default_features = false, features = ["extension-module"]}
pyo3 = { version = "^0.14.5", default_features = false, features = ["extension-module"]}
ryu = { version = "1", default_features = false }
serde = { version = "1", default_features = false }
simdutf8 = { version = "0.1", default_features = false, features = ["std"] }
smallvec = { version = "^1.6", default_features = false, features = ["union", "write"] }
smallvec = { version = "^1.7", default_features = false, features = ["union", "write"] }
rmp = { version = "^0.8.10"}
rmp-serde = {version = "^0.15.4"}

Expand Down
3 changes: 2 additions & 1 deletion src/serialize/numpy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ impl<'a> NumpyArray {
impl Drop for NumpyArray {
fn drop(&mut self) {
if self.depth == 0 {
ffi!(Py_XDECREF(self.capsule as *mut pyo3::ffi::PyObject))
ffi!(Py_DECREF(self.array as *mut pyo3::ffi::PyObject));
ffi!(Py_DECREF(self.capsule as *mut pyo3::ffi::PyObject));
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/serialize/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,10 @@ impl<'p> Serialize for PyObjectSerializer {
err!(RECURSION_LIMIT_REACHED)
}
let dict = ffi!(PyObject_GetAttr(self.ptr, DICT_STR));
if unlikely!(dict.is_null()) {
let ob_type = ob_type!(self.ptr);
if unlikely!(
dict.is_null() || ffi!(PyDict_Contains((*ob_type).tp_dict, SLOTS_STR)) == 1
) {
unsafe { pyo3::ffi::PyErr_Clear() };
DataclassFallbackSerializer::new(
self.ptr,
Expand Down
2 changes: 2 additions & 0 deletions src/typeref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub static mut EMPTY_UNICODE: *mut PyObject = 0 as *mut PyObject;
pub static mut DST_STR: *mut PyObject = 0 as *mut PyObject;
pub static mut DICT_STR: *mut PyObject = 0 as *mut PyObject;
pub static mut DATACLASS_FIELDS_STR: *mut PyObject = 0 as *mut PyObject;
pub static mut SLOTS_STR: *mut PyObject = 0 as *mut PyObject;
pub static mut PYDANTIC_FIELDS_STR: *mut PyObject = 0 as *mut PyObject;
pub static mut FIELD_TYPE_STR: *mut PyObject = 0 as *mut PyObject;
pub static mut ARRAY_STRUCT_STR: *mut PyObject = 0 as *mut PyObject;
Expand Down Expand Up @@ -120,6 +121,7 @@ pub fn init_typerefs() {
DICT_STR = PyUnicode_InternFromString("__dict__\0".as_ptr() as *const c_char);
DATACLASS_FIELDS_STR =
PyUnicode_InternFromString("__dataclass_fields__\0".as_ptr() as *const c_char);
SLOTS_STR = PyUnicode_InternFromString("__slots__\0".as_ptr() as *const c_char);
PYDANTIC_FIELDS_STR = PyUnicode_InternFromString("__fields__\0".as_ptr() as *const c_char);
FIELD_TYPE_STR = PyUnicode_InternFromString("_field_type\0".as_ptr() as *const c_char);
ARRAY_STRUCT_STR =
Expand Down
4 changes: 2 additions & 2 deletions src/unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ enum PyUnicodeKind {
fn find_str_kind(buf: &str, num_chars: usize) -> PyUnicodeKind {
if buf.len() == num_chars {
PyUnicodeKind::Ascii
} else if is_four_byte(buf) {
PyUnicodeKind::FourByte
} else if encoding_rs::mem::is_str_latin1(buf) {
// fails fast, no obvious effect on CJK
PyUnicodeKind::OneByte
} else if is_four_byte(buf) {
PyUnicodeKind::FourByte
} else {
PyUnicodeKind::TwoByte
}
Expand Down
23 changes: 22 additions & 1 deletion tests/test_dataclass.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import abc
import uuid
from dataclasses import InitVar, asdict, dataclass, field
from enum import Enum
Expand Down Expand Up @@ -96,6 +96,22 @@ def __post_init__(self, a: str, b: str):
self._other = 1
self.ab = f"{a} {b}"

class AbstractBase(abc.ABC):
@abc.abstractmethod
def key(self):
raise NotImplementedError


@dataclass(frozen=True)
class ConcreteAbc(AbstractBase):

__slots__ = ("attr",)

attr: float

def key(self):
return "dkjf"


def test_dataclass():
"""
Expand Down Expand Up @@ -260,3 +276,8 @@ def default(obj):
assert ormsgpack.packb(
obj, option=ormsgpack.OPT_PASSTHROUGH_DATACLASS, default=default
) == msgpack.packb({"name": "a", "number": 1})


def test_dataclass_abc():
obj = ConcreteAbc(1.0)
assert ormsgpack.packb(obj) == msgpack.packb({"attr": 1.0})

0 comments on commit d3e8d37

Please sign in to comment.