Skip to content

Commit 65594c5

Browse files
committed
clean up parsing and docs for module macro options
ffi: update `object.rs` for 3.13
1 parent 144b199 commit 65594c5

File tree

8 files changed

+201
-99
lines changed

8 files changed

+201
-99
lines changed

Architecture.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,9 @@ automated tooling because:
3737
- it gives us best control about how to adapt C conventions to Rust, and
3838
- there are many Python interpreter versions we support in a single set of files.
3939

40-
We aim to provide straight-forward Rust wrappers resembling the file structure of
41-
[`cpython/Include`](https://github.com/python/cpython/tree/v3.9.2/Include).
40+
We aim to provide straight-forward Rust wrappers resembling the file structure of [`cpython/Include`](https://github.com/python/cpython/tree/3.13/Include).
4241

43-
However, we still lack some APIs and are continuously updating the module to match
44-
the file contents upstream in CPython.
45-
The tracking issue is [#1289](https://github.com/PyO3/pyo3/issues/1289), and contribution is welcome.
42+
We are continuously updating the module to match the latest CPython version which PyO3 supports (i.e. as of time of writing Python 3.13). The tracking issue is [#1289](https://github.com/PyO3/pyo3/issues/1289), and contribution is welcome.
4643

4744
In the [`pyo3-ffi`] crate, there is lots of conditional compilation such as `#[cfg(Py_LIMITED_API)]`,
4845
`#[cfg(Py_3_7)]`, and `#[cfg(PyPy)]`.

pyo3-ffi/src/abstract_.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
use crate::object::*;
22
use crate::pyport::Py_ssize_t;
33
use std::os::raw::{c_char, c_int};
4-
use std::ptr;
5-
6-
extern "C" {
7-
#[cfg(PyPy)]
8-
#[link_name = "PyPyObject_DelAttrString"]
9-
pub fn PyObject_DelAttrString(o: *mut PyObject, attr_name: *const c_char) -> c_int;
10-
}
114

125
#[inline]
13-
#[cfg(not(PyPy))]
6+
#[cfg(all(not(Py_3_13), not(PyPy)))] // CPython exposed as a function in 3.13, in object.h
147
pub unsafe fn PyObject_DelAttrString(o: *mut PyObject, attr_name: *const c_char) -> c_int {
15-
PyObject_SetAttrString(o, attr_name, ptr::null_mut())
8+
PyObject_SetAttrString(o, attr_name, std::ptr::null_mut())
169
}
1710

1811
#[inline]
12+
#[cfg(all(not(Py_3_13), not(PyPy)))] // CPython exposed as a function in 3.13, in object.h
1913
pub unsafe fn PyObject_DelAttr(o: *mut PyObject, attr_name: *mut PyObject) -> c_int {
20-
PyObject_SetAttr(o, attr_name, ptr::null_mut())
14+
PyObject_SetAttr(o, attr_name, std::ptr::null_mut())
2115
}
2216

2317
extern "C" {

pyo3-ffi/src/boolobject.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ use crate::object::*;
44
use std::os::raw::{c_int, c_long};
55
use std::ptr::addr_of_mut;
66

7-
#[cfg_attr(windows, link(name = "pythonXY"))]
8-
extern "C" {
9-
#[cfg_attr(PyPy, link_name = "PyPyBool_Type")]
10-
pub static mut PyBool_Type: PyTypeObject;
11-
}
12-
137
#[inline]
148
pub unsafe fn PyBool_Check(op: *mut PyObject) -> c_int {
159
(Py_TYPE(op) == addr_of_mut!(PyBool_Type)) as c_int

pyo3-ffi/src/cpython/object.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,10 @@ pub struct PyTypeObject {
240240
pub tp_getattro: Option<object::getattrofunc>,
241241
pub tp_setattro: Option<object::setattrofunc>,
242242
pub tp_as_buffer: *mut PyBufferProcs,
243+
#[cfg(not(Py_GIL_DISABLED))]
243244
pub tp_flags: c_ulong,
245+
#[cfg(Py_GIL_DISABLED)]
246+
pub tp_flags: crate::impl_::AtomicCULong,
244247
pub tp_doc: *const c_char,
245248
pub tp_traverse: Option<object::traverseproc>,
246249
pub tp_clear: Option<object::inquiry>,

pyo3-ffi/src/impl_/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#[cfg(Py_GIL_DISABLED)]
2+
mod atomic_c_ulong {
3+
pub(crate) struct GetAtomicCULong<const SIZE: usize>();
4+
5+
pub(crate) trait AtomicCULongType {
6+
type Type;
7+
}
8+
impl AtomicCULongType for GetAtomicCULong<32> {
9+
type Type = std::sync::atomic::AtomicU32;
10+
}
11+
impl AtomicCULongType for GetAtomicCULong<64> {
12+
type Type = std::sync::atomic::AtomicU64;
13+
}
14+
15+
pub(crate) type TYPE = GetAtomicCULong<{ std::mem::size_of::<std::os::raw::c_ulong>() }>;
16+
}
17+
18+
/// Typedef for an atomic integer to match the platform-dependent c_ulong type.
19+
#[cfg(Py_GIL_DISABLED)]
20+
pub(crate) type AtomicCULong = atomic_c_ulong::TYPE;

pyo3-ffi/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ pub const fn _cstr_from_utf8_with_nul_checked(s: &str) -> &CStr {
292292
use std::ffi::CStr;
293293

294294
pub mod compat;
295+
mod impl_;
295296

296297
pub use self::abstract_::*;
297298
pub use self::bltinmodule::*;

pyo3-ffi/src/longobject.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ use std::ptr::addr_of_mut;
66

77
opaque_struct!(PyLongObject);
88

9-
#[cfg_attr(windows, link(name = "pythonXY"))]
10-
extern "C" {
11-
#[cfg_attr(PyPy, link_name = "PyPyLong_Type")]
12-
pub static mut PyLong_Type: PyTypeObject;
13-
}
14-
159
#[inline]
1610
pub unsafe fn PyLong_Check(op: *mut PyObject) -> c_int {
1711
PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_LONG_SUBCLASS)

0 commit comments

Comments
 (0)