Skip to content
Draft
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
1 change: 1 addition & 0 deletions newsfragments/6174.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for `PyFrozenDict` on 3.15+
1 change: 1 addition & 0 deletions pyo3-ffi-check/definitions/wrapper.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Python.h"
#include "datetime.h"
#include "dictobject.h"
#include "frameobject.h"
#include "structmember.h"
#include "marshal.h"
8 changes: 6 additions & 2 deletions pyo3-ffi-check/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ pub fn for_all_structs(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
.unwrap();

if pyo3_build_config::get().target_abi().version() < PY_3_15
&& struct_name == "PyBytesWriter"
&& (struct_name == "PyBytesWriter" || struct_name == "PyFrozenDictObject")
{
// PyBytesWriter was added in Python 3.15
// PyBytesWriter and PyFrozenDictObject were added in Python 3.15
continue;
}

Expand Down Expand Up @@ -247,6 +247,8 @@ const MACRO_EXCLUSIONS: &[(&str, &str)] = &[
// FIXME: for many of these `not(PyPy)` cases,
// it seems that PyPy might actually offer symbols which PyO3
// should be using rather than implementing inline functions
("PyAnyDict_Check", "not(PyPy)"),
("PyAnyDict_CheckExact", "not(PyPy)"),
("PyAnySet_Check", "not(PyPy)"),
("PyAnySet_CheckExact", "not(PyPy)"),
("PyAsyncGen_CheckExact", ""),
Expand Down Expand Up @@ -324,6 +326,8 @@ const MACRO_EXCLUSIONS: &[(&str, &str)] = &[
("PyFloat_CheckExact", "not(PyPy)"),
("PyFrame_Check", ""),
("PyFrameLocalsProxy_Check", ""),
("PyFrozenDict_Check", "not(PyPy)"),
("PyFrozenDict_CheckExact", "not(PyPy)"),
("PyFrozenSet_Check", "not(PyPy)"),
("PyFrozenSet_CheckExact", "not(PyPy)"),
("PyFunction_Check", "not(PyPy)"),
Expand Down
9 changes: 5 additions & 4 deletions pyo3-ffi/src/cpython/dictobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ pub struct PyDictObject {
_tmpkeys: *mut PyObject,
}

extern_libpython! {
#[cfg(Py_3_15)]
pub fn PyFrozenDict_New(iterable: *mut PyObject) -> *mut PyObject;
}
#[cfg(Py_3_15)]
#[cfg(not(GraalPy))]
opaque_struct!(pub PyFrozenDictObject);

// PyFrozenDict_Type and PyFrozenDict_New are defined in the public dictobject.rs module

// skipped private _PyDict_GetItem_KnownHash
// skipped private _PyDict_GetItemStringWithError
Expand Down
37 changes: 37 additions & 0 deletions pyo3-ffi/src/dictobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,41 @@ pub unsafe fn PyDict_CheckExact(op: *mut PyObject) -> c_int {
Py_IS_TYPE(op, &raw mut PyDict_Type)
}

#[cfg(Py_3_15)]
#[cfg(not(RustPython))]
extern_libpython! {
pub static mut PyFrozenDict_Type: PyTypeObject;
}

#[inline]
#[cfg(Py_3_15)]
#[cfg(not(RustPython))]
pub unsafe fn PyFrozenDict_CheckExact(op: *mut PyObject) -> c_int {
(Py_TYPE(op) == &raw mut PyFrozenDict_Type) as c_int
}

#[inline]
#[cfg(Py_3_15)]
#[cfg(not(RustPython))]
pub unsafe fn PyFrozenDict_Check(op: *mut PyObject) -> c_int {
(Py_TYPE(op) == &raw mut PyFrozenDict_Type
|| PyType_IsSubtype(Py_TYPE(op), &raw mut PyFrozenDict_Type) != 0) as c_int
}

#[inline]
#[cfg(Py_3_15)]
#[cfg(not(RustPython))]
pub unsafe fn PyAnyDict_Check(op: *mut PyObject) -> c_int {
(PyDict_Check(op) != 0 || PyFrozenDict_Check(op) != 0) as c_int
}

#[inline]
#[cfg(Py_3_15)]
#[cfg(not(RustPython))]
pub unsafe fn PyAnyDict_CheckExact(op: *mut PyObject) -> c_int {
(Py_TYPE(op) == &raw mut PyDict_Type || Py_TYPE(op) == &raw mut PyFrozenDict_Type) as c_int
}

extern_libpython! {
#[cfg(RustPython)]
pub fn PyDict_Check(op: *mut PyObject) -> c_int;
Expand All @@ -28,6 +63,8 @@ extern_libpython! {

#[cfg_attr(PyPy, link_name = "PyPyDict_New")]
pub fn PyDict_New() -> *mut PyObject;
#[cfg(Py_3_15)]
pub fn PyFrozenDict_New(iterable: *mut PyObject) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyDict_GetItem")]
pub fn PyDict_GetItem(mp: *mut PyObject, key: *mut PyObject) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyDict_GetItemWithError")]
Expand Down
2 changes: 2 additions & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub use crate::types::capsule::PyCapsuleMethods;
pub use crate::types::complex::PyComplexMethods;
pub use crate::types::dict::PyDictMethods;
pub use crate::types::float::PyFloatMethods;
#[cfg(Py_3_15)]
pub use crate::types::frozendict::PyFrozenDictMethods;
pub use crate::types::frozenset::PyFrozenSetMethods;
pub use crate::types::list::PyListMethods;
pub use crate::types::mapping::PyMappingMethods;
Expand Down
5 changes: 3 additions & 2 deletions src/sealed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::impl_::pyfunction::PyFunctionDef;
#[cfg(all(not(Py_LIMITED_API), not(PyPy), not(GraalPy)))]
use crate::types::PyFrame;
use crate::types::{
PyBool, PyByteArray, PyBytes, PyCapsule, PyComplex, PyDict, PyFloat, PyFrozenSet, PyList,
PyMapping, PyMappingProxy, PyModule, PyRange, PySequence, PySet, PySlice, PyString,
PyBool, PyByteArray, PyBytes, PyCapsule, PyComplex, PyDict, PyFloat, PyFrozenDict, PyFrozenSet,
PyList, PyMapping, PyMappingProxy, PyModule, PyRange, PySequence, PySet, PySlice, PyString,
PyTraceback, PyTuple, PyType, PyWeakref, PyWeakrefProxy, PyWeakrefReference,
};
use crate::{ffi, Bound, PyAny, PyResult};
Expand Down Expand Up @@ -33,6 +33,7 @@ impl Sealed for Bound<'_, PyCapsule> {}
impl Sealed for Bound<'_, PyComplex> {}
impl Sealed for Bound<'_, PyDict> {}
impl Sealed for Bound<'_, PyFloat> {}
impl Sealed for Bound<'_, PyFrozenDict> {}
impl Sealed for Bound<'_, PyFrozenSet> {}
impl Sealed for Bound<'_, PyList> {}
impl Sealed for Bound<'_, PyMapping> {}
Expand Down
Loading
Loading