From 997faffdf26ef5c77bdff0379e7f501119c9a99c Mon Sep 17 00:00:00 2001 From: person93 Date: Wed, 29 Jul 2026 21:49:43 -0400 Subject: [PATCH 1/4] add tss api to ffi crate --- pyo3-ffi/src/lib.rs | 3 ++- pyo3-ffi/src/pythead.rs | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 pyo3-ffi/src/pythead.rs diff --git a/pyo3-ffi/src/lib.rs b/pyo3-ffi/src/lib.rs index 7fdf12a0094..77a43cc2cf5 100644 --- a/pyo3-ffi/src/lib.rs +++ b/pyo3-ffi/src/lib.rs @@ -483,6 +483,7 @@ pub use self::pymem::*; pub use self::pyport::*; pub use self::pystate::*; pub use self::pystrtod::*; +pub use self::pythead::*; pub use self::pythonrun::*; pub use self::pytypedefs::*; pub use self::rangeobject::*; @@ -574,7 +575,7 @@ mod pythonrun; // skipped pystrhex.h // skipped pystrcmp.h mod pystrtod; -// skipped pythread.h +mod pythead; // skipped pytime.h mod pytypedefs; mod rangeobject; diff --git a/pyo3-ffi/src/pythead.rs b/pyo3-ffi/src/pythead.rs new file mode 100644 index 00000000000..b800fc97136 --- /dev/null +++ b/pyo3-ffi/src/pythead.rs @@ -0,0 +1,15 @@ +use core::ffi::{c_int, c_void}; + +// TODO use non-opaque type if not(PY_LIMITED_API) +opaque_struct!(pub Py_tss_t); + +extern_libpython! { + pub fn PyThread_tss_alloc() -> *mut Py_tss_t; + pub fn PyThread_tss_free(key: *mut Py_tss_t); + + pub fn PyThread_tss_is_created(key: *mut Py_tss_t) -> c_int; + pub fn PyThread_tss_create(key: *mut Py_tss_t) -> c_int; + pub fn PyThread_tss_delete(key: *mut Py_tss_t); + pub fn PyThread_tss_set(key: *mut Py_tss_t, value: *mut c_void); + pub fn PyThread_tss_get(key: *mut Py_tss_t) -> *mut c_void; +} From c2546e47b15f80a599f70c7e52f50bc53252124b Mon Sep 17 00:00:00 2001 From: person93 Date: Wed, 29 Jul 2026 22:17:37 -0400 Subject: [PATCH 2/4] add newsfragment --- newsfragments/6262.added.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/6262.added.md diff --git a/newsfragments/6262.added.md b/newsfragments/6262.added.md new file mode 100644 index 00000000000..8c1452089dc --- /dev/null +++ b/newsfragments/6262.added.md @@ -0,0 +1 @@ +Add tss api to ffi crate From e33cc825b98cd8841cda81cfe47e762d232d50c6 Mon Sep 17 00:00:00 2001 From: person93 Date: Wed, 29 Jul 2026 23:06:20 -0400 Subject: [PATCH 3/4] fix return type of PyThread_tss_set --- pyo3-ffi/src/pythead.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyo3-ffi/src/pythead.rs b/pyo3-ffi/src/pythead.rs index b800fc97136..c03f241bc40 100644 --- a/pyo3-ffi/src/pythead.rs +++ b/pyo3-ffi/src/pythead.rs @@ -10,6 +10,6 @@ extern_libpython! { pub fn PyThread_tss_is_created(key: *mut Py_tss_t) -> c_int; pub fn PyThread_tss_create(key: *mut Py_tss_t) -> c_int; pub fn PyThread_tss_delete(key: *mut Py_tss_t); - pub fn PyThread_tss_set(key: *mut Py_tss_t, value: *mut c_void); + pub fn PyThread_tss_set(key: *mut Py_tss_t, value: *mut c_void) -> c_int; pub fn PyThread_tss_get(key: *mut Py_tss_t) -> *mut c_void; } From 8d28a1212a43e0d25689b1ef7ee94ca0897d2a0d Mon Sep 17 00:00:00 2001 From: person93 Date: Thu, 13 Aug 2026 00:36:53 -0400 Subject: [PATCH 4/4] use transparent type for Py_tss_t if not Py_LIMITED_API --- pyo3-ffi/src/pythead.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pyo3-ffi/src/pythead.rs b/pyo3-ffi/src/pythead.rs index c03f241bc40..75dfa2eb517 100644 --- a/pyo3-ffi/src/pythead.rs +++ b/pyo3-ffi/src/pythead.rs @@ -1,8 +1,27 @@ use core::ffi::{c_int, c_void}; -// TODO use non-opaque type if not(PY_LIMITED_API) +#[cfg(Py_LIMITED_API)] opaque_struct!(pub Py_tss_t); +#[cfg(not(Py_LIMITED_API))] +#[repr(C)] +pub struct Py_tss_t { + _is_initialized: c_int, + _key: NATIVE_TSS_KEY_T, +} + +#[cfg(not(Py_LIMITED_API))] +pub const Py_tss_NEEDS_INIT: Py_tss_t = Py_tss_t { + _is_initialized: 0, + _key: 0, +}; + +#[cfg(all(windows, not(Py_LIMITED_API)))] +type NATIVE_TSS_KEY_T = core::ffi::c_ulong; + +#[cfg(all(not(windows), not(Py_LIMITED_API)))] +type NATIVE_TSS_KEY_T = libc::pthread_key_t; + extern_libpython! { pub fn PyThread_tss_alloc() -> *mut Py_tss_t; pub fn PyThread_tss_free(key: *mut Py_tss_t);