Skip to content

Commit b3a52c3

Browse files
add support for PyFrozenDict
1 parent 9e6697d commit b3a52c3

6 files changed

Lines changed: 564 additions & 1 deletion

File tree

newsfragments/6174.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add support for `PyFrozenDict` on 3.15+

pyo3-ffi/src/cpython/dictobject.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use crate::object::*;
33
#[cfg(not(any(PyPy, GraalPy)))]
44
use crate::pyport::Py_ssize_t;
55

6+
use crate::dictobject::{PyDict_Check, PyDict_Type};
7+
68
#[cfg(all(not(PyPy), Py_3_13))]
79
use core::ffi::c_char;
810
#[cfg(all(not(PyPy), Py_3_12))]
@@ -46,7 +48,7 @@ pub struct PyDictObject {
4648

4749
extern_libpython! {
4850
#[cfg(Py_3_15)]
49-
pub fn PyFrozenDict_New(iterable: *mut PyObject) -> *mut PyObject;
51+
pub static mut PyFrozenDict_Type: PyTypeObject;
5052
}
5153

5254
// skipped private _PyDict_GetItem_KnownHash
@@ -110,3 +112,33 @@ extern_libpython! {
110112
#[cfg(not(GraalPy))]
111113
pub fn PyDict_Unwatch(watcher_id: c_int, dict: *mut PyObject) -> c_int;
112114
}
115+
116+
extern_libpython! {
117+
#[cfg(Py_3_15)]
118+
pub fn PyFrozenDict_New(iterable: *mut PyObject) -> *mut PyObject;
119+
}
120+
121+
#[inline]
122+
#[cfg(Py_3_15)]
123+
pub unsafe fn PyFrozenDict_CheckExact(op: *mut PyObject) -> c_int {
124+
(Py_TYPE(op) == &raw mut PyFrozenDict_Type) as c_int
125+
}
126+
127+
#[inline]
128+
#[cfg(Py_3_15)]
129+
pub unsafe fn PyFrozenDict_Check(op: *mut PyObject) -> c_int {
130+
(Py_TYPE(op) == &raw mut PyFrozenDict_Type
131+
|| PyType_IsSubtype(Py_TYPE(op), &raw mut PyFrozenDict_Type) != 0) as c_int
132+
}
133+
134+
#[inline]
135+
#[cfg(Py_3_15)]
136+
pub unsafe fn PyAnyDict_Check(op: *mut PyObject) -> c_int {
137+
(PyDict_Check(op) != 0 || PyFrozenDict_Check(op) != 0) as c_int
138+
}
139+
140+
#[inline]
141+
#[cfg(Py_3_15)]
142+
pub unsafe fn PyAnyDict_CheckExact(op: *mut PyObject) -> c_int {
143+
(Py_TYPE(op) == &raw mut PyDict_Type || Py_TYPE(op) == &raw mut PyFrozenDict_Type) as c_int
144+
}

src/prelude.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub use crate::types::capsule::PyCapsuleMethods;
3333
pub use crate::types::complex::PyComplexMethods;
3434
pub use crate::types::dict::PyDictMethods;
3535
pub use crate::types::float::PyFloatMethods;
36+
#[cfg(Py_3_15)]
37+
pub use crate::types::frozendict::PyFrozenDictMethods;
3638
pub use crate::types::frozenset::PyFrozenSetMethods;
3739
pub use crate::types::list::PyListMethods;
3840
pub use crate::types::mapping::PyMappingMethods;

src/sealed.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::impl_::pyfunction::PyFunctionDef;
22
#[cfg(all(not(Py_LIMITED_API), not(PyPy), not(GraalPy)))]
33
use crate::types::PyFrame;
4+
#[cfg(Py_3_15)]
5+
use crate::types::PyFrozenDict;
46
use crate::types::{
57
PyBool, PyByteArray, PyBytes, PyCapsule, PyComplex, PyDict, PyFloat, PyFrozenSet, PyList,
68
PyMapping, PyMappingProxy, PyModule, PyRange, PySequence, PySet, PySlice, PyString,
@@ -33,6 +35,8 @@ impl Sealed for Bound<'_, PyCapsule> {}
3335
impl Sealed for Bound<'_, PyComplex> {}
3436
impl Sealed for Bound<'_, PyDict> {}
3537
impl Sealed for Bound<'_, PyFloat> {}
38+
#[cfg(Py_3_15)]
39+
impl Sealed for Bound<'_, PyFrozenDict> {}
3640
impl Sealed for Bound<'_, PyFrozenSet> {}
3741
impl Sealed for Bound<'_, PyList> {}
3842
impl Sealed for Bound<'_, PyMapping> {}

0 commit comments

Comments
 (0)