Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial free threaded bindings #4421

Merged
merged 31 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2685691
add support in pyo3-build-config for free-threaded python
ngoldbaum Aug 6, 2024
e8613d5
update object.h bindings for free-threaded build
ngoldbaum Aug 6, 2024
eeac20c
Add PyMutex bindings
ngoldbaum Aug 6, 2024
8eebe43
fix po3-ffi-check with free-threaded build
ngoldbaum Aug 6, 2024
9e9d77e
error when building with limited api and Py_GIL_DISABLED
ngoldbaum Aug 6, 2024
79dd1a7
Add CI job for free-threaded build
ngoldbaum Jul 31, 2024
5aaa03a
fix issues building on older pythons
ngoldbaum Aug 6, 2024
f4219b3
ci config fixup
ngoldbaum Aug 6, 2024
4ff9a62
fix clippy on gil-enabled 3.13 build
ngoldbaum Aug 6, 2024
57e7e3d
Apply suggestions from code review
ngoldbaum Aug 13, 2024
a042e56
make PyMutex and PyObject refcounting fields atomics
ngoldbaum Aug 13, 2024
7c493eb
add new field on PyConfig in 3.13 debug ABI
ngoldbaum Aug 13, 2024
da3e50c
warn and disable abi3 on gil-disabled build
ngoldbaum Aug 13, 2024
77b4d96
fix conditional compilation for PyMutex usage
ngoldbaum Aug 13, 2024
96dd13e
temporarily skip test that deadlocks
ngoldbaum Aug 13, 2024
dbcb9d2
remove Py_GIL_DISABLED from py_sys_config cfg options
ngoldbaum Aug 13, 2024
d9dac3c
only expose PyMutex in 3.13
ngoldbaum Aug 13, 2024
9b70b07
make PyObject_HEAD_INIT a function
ngoldbaum Aug 14, 2024
3e03dd1
intialize ob_ref_local to _Py_IMMORTAL_REFCNT_LOCAL in HEAD_INIT
ngoldbaum Aug 14, 2024
f695203
Fix clippy lint about static with interior mutability
ngoldbaum Aug 14, 2024
848c793
add TODO comments about INCREF and DECREF in free-threaded build
ngoldbaum Aug 14, 2024
6e66cb0
make the _bits field of PyMutex pub(crate)
ngoldbaum Aug 14, 2024
126c97b
refactor so HEAD_INIT remains a constant
ngoldbaum Aug 14, 2024
162a65e
ignore clippy lint about interior mutability
ngoldbaum Aug 14, 2024
6103d26
revert unnecessary changes to pyo3-build-config
ngoldbaum Aug 14, 2024
8a9ef2d
add changelog entries
ngoldbaum Aug 14, 2024
e1ddd01
use derive(Debug) for PyMutex
ngoldbaum Aug 15, 2024
1a088ea
Add PhantomPinned field to PyMutex bindings
ngoldbaum Aug 15, 2024
38bc492
Update pyo3-build-config/src/impl_.rs
ngoldbaum Aug 15, 2024
a85fa4c
Update pyo3-ffi/src/object.rs
ngoldbaum Aug 15, 2024
60c26c4
fix build config again
ngoldbaum Aug 15, 2024
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
2 changes: 1 addition & 1 deletion examples/sequential/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pyo3_ffi::*;
use std::os::raw::{c_int, c_void};

pub static mut MODULE_DEF: PyModuleDef = PyModuleDef {
m_base: PyModuleDef_HEAD_INIT,
m_base: PyModuleDef_HEAD_INIT(),
m_name: c_str!("sequential").as_ptr(),
m_doc: c_str!("A library for generating sequential ids, written in Rust.").as_ptr(),
m_size: mem::size_of::<sequential_state>() as Py_ssize_t,
Expand Down
2 changes: 1 addition & 1 deletion examples/string-sum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ptr;
use pyo3_ffi::*;

static mut MODULE_DEF: PyModuleDef = PyModuleDef {
m_base: PyModuleDef_HEAD_INIT,
m_base: PyModuleDef_HEAD_INIT(),
m_name: c_str!("string_sum").as_ptr(),
m_doc: c_str!("A Python module written in Rust.").as_ptr(),
m_size: 0,
Expand Down
2 changes: 1 addition & 1 deletion pyo3-ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use std::ptr;
use pyo3_ffi::*;

static mut MODULE_DEF: PyModuleDef = PyModuleDef {
m_base: PyModuleDef_HEAD_INIT,
m_base: PyModuleDef_HEAD_INIT(),
m_name: c_str!("string_sum").as_ptr(),
m_doc: c_str!("A Python module written in Rust.").as_ptr(),
m_size: 0,
Expand Down
2 changes: 1 addition & 1 deletion pyo3-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
//! use pyo3_ffi::*;
//!
//! static mut MODULE_DEF: PyModuleDef = PyModuleDef {
//! m_base: PyModuleDef_HEAD_INIT,
//! m_base: PyModuleDef_HEAD_INIT(),
//! m_name: c_str!("string_sum").as_ptr(),
//! m_doc: c_str!("A Python module written in Rust.").as_ptr(),
//! m_size: 0,
Expand Down
14 changes: 8 additions & 6 deletions pyo3-ffi/src/moduleobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ pub struct PyModuleDef_Base {
pub m_copy: *mut PyObject,
}

pub const PyModuleDef_HEAD_INIT: PyModuleDef_Base = PyModuleDef_Base {
ob_base: PyObject_HEAD_INIT,
m_init: None,
m_index: 0,
m_copy: std::ptr::null_mut(),
};
pub const fn PyModuleDef_HEAD_INIT() -> PyModuleDef_Base {
PyModuleDef_Base {
ob_base: PyObject_HEAD_INIT(),
m_init: None,
m_index: 0,
m_copy: std::ptr::null_mut(),
}
}

#[repr(C)]
#[derive(Copy, Clone, Eq, PartialEq)]
Expand Down
54 changes: 28 additions & 26 deletions pyo3-ffi/src/object.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::pyport::{Py_hash_t, Py_ssize_t};
#[cfg(all(Py_3_13, not(Py_LIMITED_API)))]
#[cfg(Py_GIL_DISABLED)]
use crate::PyMutex;
use std::mem;
use std::os::raw::{c_char, c_int, c_uint, c_ulong, c_void};
Expand Down Expand Up @@ -31,31 +31,33 @@ pub const _Py_IMMORTAL_REFCNT_LOCAL: u32 = u32::MAX;
#[cfg(Py_GIL_DISABLED)]
pub const _Py_REF_SHARED_SHIFT: isize = 2;

pub const PyObject_HEAD_INIT: PyObject = PyObject {
#[cfg(py_sys_config = "Py_TRACE_REFS")]
_ob_next: std::ptr::null_mut(),
#[cfg(py_sys_config = "Py_TRACE_REFS")]
_ob_prev: std::ptr::null_mut(),
#[cfg(Py_GIL_DISABLED)]
ob_tid: 0,
#[cfg(Py_GIL_DISABLED)]
_padding: 0,
#[cfg(Py_GIL_DISABLED)]
ob_mutex: unsafe { mem::zeroed::<PyMutex>() },
#[cfg(Py_GIL_DISABLED)]
ob_gc_bits: 0,
#[cfg(Py_GIL_DISABLED)]
ob_ref_local: AtomicU32::new(0),
#[cfg(Py_GIL_DISABLED)]
ob_ref_shared: AtomicIsize::new(0),
#[cfg(all(not(Py_GIL_DISABLED), Py_3_12))]
ob_refcnt: PyObjectObRefcnt { ob_refcnt: 1 },
#[cfg(not(Py_3_12))]
ob_refcnt: 1,
#[cfg(PyPy)]
ob_pypy_link: 0,
ob_type: std::ptr::null_mut(),
};
pub const fn PyObject_HEAD_INIT() -> PyObject {
PyObject {
#[cfg(py_sys_config = "Py_TRACE_REFS")]
_ob_next: std::ptr::null_mut(),
#[cfg(py_sys_config = "Py_TRACE_REFS")]
_ob_prev: std::ptr::null_mut(),
#[cfg(Py_GIL_DISABLED)]
ob_tid: 0,
#[cfg(Py_GIL_DISABLED)]
_padding: 0,
#[cfg(Py_GIL_DISABLED)]
ob_mutex: unsafe { mem::zeroed::<PyMutex>() },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be PyMutex::new() if we define it as above.

That avoids the unsafe block. And maybe allows PyObject_HEAD_INIT to remain a constant? Are there other fields with interior mutability?

#[cfg(Py_GIL_DISABLED)]
ob_gc_bits: 0,
#[cfg(Py_GIL_DISABLED)]
ob_ref_local: AtomicU32::new(0),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be immortal (u32::MAX) I think

#[cfg(Py_GIL_DISABLED)]
ob_ref_shared: AtomicIsize::new(0),
#[cfg(all(not(Py_GIL_DISABLED), Py_3_12))]
ob_refcnt: PyObjectObRefcnt { ob_refcnt: 1 },
#[cfg(not(Py_3_12))]
ob_refcnt: 1,
#[cfg(PyPy)]
ob_pypy_link: 0,
ob_type: std::ptr::null_mut(),
}
}

// skipped PyObject_VAR_HEAD
// skipped Py_INVALID_SIZE
Expand Down
2 changes: 1 addition & 1 deletion src/impl_/pymodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl ModuleDef {
initializer: ModuleInitializer,
) -> Self {
const INIT: ffi::PyModuleDef = ffi::PyModuleDef {
m_base: ffi::PyModuleDef_HEAD_INIT,
m_base: ffi::PyModuleDef_HEAD_INIT(),
m_name: std::ptr::null(),
m_doc: std::ptr::null(),
m_size: 0,
Expand Down
Loading