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

Use PyType_GetDict to safely access tp_dict [3.12] #408

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 4 additions & 4 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ encoding_rs = { version = "0.8", default_features = false }
itoa = { version = "1", default_features = false }
itoap = { version = "1", features = ["std", "simd"] }
once_cell = { version = "1", default_features = false, features = ["race"] }
pyo3-ffi = { version = "^0.19.1", default_features = false, features = ["extension-module"]}
pyo3-ffi = { version = "^0.19.2", default_features = false, features = ["extension-module"]}
ryu = { version = "1", default_features = false }
serde = { version = "1", default_features = false }
serde_json = { version = "1", default_features = false, features = ["std", "float_roundtrip"] }
Expand All @@ -63,7 +63,7 @@ smallvec = { version = "^1.10", default_features = false, features = ["union", "

[build-dependencies]
cc = { version = "1" }
pyo3-build-config = { version = "^0.19.1" }
pyo3-build-config = { version = "^0.19.2" }
version_check = { version = "0.9" }

[profile.release]
Expand Down
3 changes: 3 additions & 0 deletions src/ffi/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ pub unsafe extern "C" fn orjson_fragmenttype_new() -> *mut PyTypeObject {
let ob = Box::new(PyTypeObject {
ob_base: PyVarObject {
ob_base: PyObject {
#[cfg(Py_3_12)]
ob_refcnt: pyo3_ffi::PyObjectObRefcnt { ob_refcnt: 0 },
#[cfg(not(Py_3_12))]
ob_refcnt: 0,
ob_type: std::ptr::addr_of_mut!(PyType_Type),
},
Expand Down
25 changes: 24 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ macro_rules! str_from_slice {
};
}

#[cfg(Py_3_12)]
macro_rules! py_decref_without_destroy {
($op:expr) => {
unsafe {
(*$op).ob_refcnt.ob_refcnt -= 1;
}
};
}

#[cfg(not(Py_3_12))]
macro_rules! py_decref_without_destroy {
($op:expr) => {
unsafe {
Expand Down Expand Up @@ -125,7 +135,20 @@ macro_rules! str_hash {
};
}

#[cfg(Py_3_10)]
#[cfg(Py_3_12)]
macro_rules! pydict_contains {
($obj1:expr, $obj2:expr) => {
unsafe {
pyo3_ffi::_PyDict_Contains_KnownHash(
pyo3_ffi::PyType_GetDict($obj1),
$obj2,
(*$obj2.cast::<pyo3_ffi::PyASCIIObject>()).hash,
) == 1
}
};
}

#[cfg(all(Py_3_10, not(Py_3_12)))]
macro_rules! pydict_contains {
($obj1:expr, $obj2:expr) => {
unsafe {
Expand Down
12 changes: 12 additions & 0 deletions test/test_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,15 @@ def default(obj):
orjson.dumps(ref, default=default)

assert sys.getrefcount(ref) == 2 # one for ref, one for default

def test_default_set(self):
"""
dumps() default function with set
"""

def default(obj):
if isinstance(obj, set):
return list(obj)
raise TypeError

assert orjson.dumps({1, 2}, default=default) == b"[1,2]"