python-orjson/408.patch
2023-07-25 10:33:58 +02:00

56 lines
1.6 KiB
Diff

From c0d62d041371970297c31ac86465cf8983386191 Mon Sep 17 00:00:00 2001
From: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Date: Sun, 23 Jul 2023 17:15:18 +0200
Subject: [PATCH] Use PyType_GetDict to safely access tp_dict [3.12]
---
src/util.rs | 15 ++++++++++++++-
test/test_default.py | 12 ++++++++++++
2 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/src/util.rs b/src/util.rs
index 3b451bd5..ba011c1a 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -125,7 +125,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 {
diff --git a/test/test_default.py b/test/test_default.py
index d1e95587..547fce8c 100644
--- a/test/test_default.py
+++ b/test/test_default.py
@@ -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]"