Skip to content

Commit 288e15d

Browse files
add support for PyFrozenDict
1 parent ff7c5e5 commit 288e15d

7 files changed

Lines changed: 796 additions & 1 deletion

File tree

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ required-features = ["macros"]
328328
name = "test_exceptions"
329329
required-features = ["macros"]
330330

331+
[[test]]
332+
name = "test_frozendict"
333+
required-features = ["macros"]
334+
331335
[[test]]
332336
name = "test_field_cfg"
333337
required-features = ["macros"]

pyo3-ffi/src/cpython/dictobject.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,18 @@ pub struct PyDictObject {
4444
_tmpkeys: *mut PyObject,
4545
}
4646

47+
#[cfg(Py_3_15)]
48+
#[cfg(not(GraalPy))]
49+
opaque_struct!(pub PyFrozenDictObject);
50+
4751
extern_libpython! {
4852
#[cfg(Py_3_15)]
49-
pub fn PyFrozenDict_New(iterable: *mut PyObject) -> *mut PyObject;
53+
#[cfg_attr(not(RustPython), link_name = "PyFrozenDict_Type")]
54+
pub static mut PyFrozenDict_Type: crate::object::PyTypeObject;
5055
}
5156

57+
// PyFrozenDict_New declared in dictobject.rs (public FFI layer)
58+
5259
// skipped private _PyDict_GetItem_KnownHash
5360
// skipped private _PyDict_GetItemStringWithError
5461

pyo3-ffi/src/dictobject.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,41 @@ pub unsafe fn PyDict_CheckExact(op: *mut PyObject) -> c_int {
2020
Py_IS_TYPE(op, &raw mut PyDict_Type)
2121
}
2222

23+
#[cfg(Py_3_15)]
24+
#[cfg(not(RustPython))]
25+
extern_libpython! {
26+
pub static mut PyFrozenDict_Type: PyTypeObject;
27+
}
28+
29+
#[inline]
30+
#[cfg(Py_3_15)]
31+
#[cfg(not(RustPython))]
32+
pub unsafe fn PyFrozenDict_CheckExact(op: *mut PyObject) -> c_int {
33+
(Py_TYPE(op) == &raw mut PyFrozenDict_Type) as c_int
34+
}
35+
36+
#[inline]
37+
#[cfg(Py_3_15)]
38+
#[cfg(not(RustPython))]
39+
pub unsafe fn PyFrozenDict_Check(op: *mut PyObject) -> c_int {
40+
(Py_TYPE(op) == &raw mut PyFrozenDict_Type
41+
|| PyType_IsSubtype(Py_TYPE(op), &raw mut PyFrozenDict_Type) != 0) as c_int
42+
}
43+
44+
#[inline]
45+
#[cfg(Py_3_15)]
46+
#[cfg(not(RustPython))]
47+
pub unsafe fn PyAnyDict_Check(op: *mut PyObject) -> c_int {
48+
PyDict_Check(op) != 0 || PyFrozenDict_Check(op) != 0
49+
}
50+
51+
#[inline]
52+
#[cfg(Py_3_15)]
53+
#[cfg(not(RustPython))]
54+
pub unsafe fn PyAnyDict_CheckExact(op: *mut PyObject) -> c_int {
55+
(Py_TYPE(op) == &raw mut PyDict_Type || Py_TYPE(op) == &raw mut PyFrozenDict_Type) as c_int
56+
}
57+
2358
extern_libpython! {
2459
#[cfg(RustPython)]
2560
pub fn PyDict_Check(op: *mut PyObject) -> c_int;
@@ -28,6 +63,8 @@ extern_libpython! {
2863

2964
#[cfg_attr(PyPy, link_name = "PyPyDict_New")]
3065
pub fn PyDict_New() -> *mut PyObject;
66+
#[cfg(Py_3_15)]
67+
pub fn PyFrozenDict_New(iterable: *mut PyObject) -> *mut PyObject;
3168
#[cfg_attr(PyPy, link_name = "PyPyDict_GetItem")]
3269
pub fn PyDict_GetItem(mp: *mut PyObject, key: *mut PyObject) -> *mut PyObject;
3370
#[cfg_attr(PyPy, link_name = "PyPyDict_GetItemWithError")]

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;

0 commit comments

Comments
 (0)