Skip to content

Commit 1fc79b1

Browse files
authoredMar 10, 2025
update pyo3 to 0.24 (#483)
* update `pyo3` to 0.24 * use `MutexExt` instead of `ThreadStateGuard`
1 parent 8dd4f8e commit 1fc79b1

File tree

8 files changed

+30
-52
lines changed

8 files changed

+30
-52
lines changed
 

‎Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ num-complex = ">= 0.2, < 0.5"
2222
num-integer = "0.1"
2323
num-traits = "0.2"
2424
ndarray = ">= 0.15, < 0.17"
25-
pyo3 = { version = "0.23.4", default-features = false, features = ["macros"] }
25+
pyo3 = { version = "0.24", default-features = false, features = ["macros"] }
2626
rustc-hash = "2.0"
2727

2828
[dev-dependencies]
29-
pyo3 = { version = "0.23.3", default-features = false, features = ["auto-initialize"] }
29+
pyo3 = { version = "0.24", default-features = false, features = ["auto-initialize"] }
3030
nalgebra = { version = ">=0.30, <0.34", default-features = false, features = ["std"] }
3131

3232
[build-dependencies]
33-
pyo3-build-config = { version = "0.23.1", features = ["resolve-config"] }
33+
pyo3-build-config = { version = "0.24", features = ["resolve-config"] }
3434

3535
[package.metadata.docs.rs]
3636
all-features = true

‎examples/linalg/Cargo.lock

+14-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎examples/linalg/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ name = "rust_linalg"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
pyo3 = { version = "0.23.3", features = ["extension-module"] }
12+
pyo3 = { version = "0.24.0", features = ["extension-module"] }
1313
numpy = { path = "../.." }
1414
ndarray-linalg = { version = "0.14.1", features = ["openblas-system"] }
1515

‎examples/parallel/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ name = "rust_parallel"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
pyo3 = { version = "0.23.0", features = ["extension-module", "multiple-pymethods"] }
12+
pyo3 = { version = "0.24.0", features = ["extension-module", "multiple-pymethods"] }
1313
numpy = { path = "../.." }
1414
ndarray = { version = "0.16", features = ["rayon", "blas"] }
1515
blas-src = { version = "0.8", features = ["openblas"] }

‎examples/simple/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ name = "rust_ext"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
pyo3 = { version = "0.23.0", features = ["extension-module", "abi3-py37"] }
12+
pyo3 = { version = "0.24.0", features = ["extension-module", "abi3-py37"] }
1313
numpy = { path = "../.." }
1414

1515
[workspace]

‎src/datetime.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ use std::hash::Hash;
6060
use std::marker::PhantomData;
6161
use std::sync::Mutex;
6262

63+
use pyo3::sync::MutexExt;
6364
use pyo3::{Bound, Py, Python};
6465
use rustc_hash::FxHashMap;
6566

6667
use crate::dtype::{clone_methods_impl, Element, PyArrayDescr, PyArrayDescrMethods};
6768
use crate::npyffi::{
6869
PyArray_DatetimeDTypeMetaData, PyDataType_C_METADATA, NPY_DATETIMEUNIT, NPY_TYPES,
6970
};
70-
use crate::ThreadStateGuard;
7171

7272
/// Represents the [datetime units][datetime-units] supported by NumPy
7373
///
@@ -224,13 +224,10 @@ impl TypeDescriptors {
224224

225225
#[allow(clippy::wrong_self_convention)]
226226
fn from_unit<'py>(&self, py: Python<'py>, unit: NPY_DATETIMEUNIT) -> Bound<'py, PyArrayDescr> {
227-
// Detach from the runtime to avoid deadlocking on acquiring the mutex.
228-
let ts_guard = ThreadStateGuard::new();
229-
230-
let mut dtypes = self.dtypes.lock().expect("dtype cache poisoned");
231-
232-
// Now we hold the mutex so it's safe to re-attach to the runtime.
233-
drop(ts_guard);
227+
let mut dtypes = self
228+
.dtypes
229+
.lock_py_attached(py)
230+
.expect("dtype cache poisoned");
234231

235232
let dtype = match dtypes.get_or_insert_with(Default::default).entry(unit) {
236233
Entry::Occupied(entry) => entry.into_mut(),

‎src/lib.rs

-17
Original file line numberDiff line numberDiff line change
@@ -149,23 +149,6 @@ mod doctest {
149149
#[inline(always)]
150150
fn cold() {}
151151

152-
/// An RAII guard for avoiding deadlocks with the GIL or other global
153-
/// synchronization events in the Python runtime
154-
// FIXME create a proper MutexExt trait that handles poisoning and upstream to PyO3
155-
struct ThreadStateGuard(*mut pyo3::ffi::PyThreadState);
156-
157-
impl ThreadStateGuard {
158-
fn new() -> ThreadStateGuard {
159-
ThreadStateGuard(unsafe { pyo3::ffi::PyEval_SaveThread() })
160-
}
161-
}
162-
163-
impl Drop for ThreadStateGuard {
164-
fn drop(&mut self) {
165-
unsafe { pyo3::ffi::PyEval_RestoreThread(self.0) };
166-
}
167-
}
168-
169152
/// Create a [`PyArray`] with one, two or three dimensions.
170153
///
171154
/// This macro is backed by [`ndarray::array`].

‎src/strings.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::os::raw::c_char;
1010
use std::str;
1111
use std::sync::Mutex;
1212

13+
use pyo3::sync::MutexExt;
1314
use pyo3::{
1415
ffi::{Py_UCS1, Py_UCS4},
1516
Bound, Py, Python,
@@ -19,7 +20,6 @@ use rustc_hash::FxHashMap;
1920
use crate::dtype::{clone_methods_impl, Element, PyArrayDescr, PyArrayDescrMethods};
2021
use crate::npyffi::PyDataType_SET_ELSIZE;
2122
use crate::npyffi::NPY_TYPES;
22-
use crate::ThreadStateGuard;
2323

2424
/// A newtype wrapper around [`[u8; N]`][Py_UCS1] to handle [`byte` scalars][numpy-bytes] while satisfying coherence.
2525
///
@@ -179,13 +179,10 @@ impl TypeDescriptors {
179179
byteorder: c_char,
180180
size: usize,
181181
) -> Bound<'py, PyArrayDescr> {
182-
// Detach from the runtime to avoid deadlocking on acquiring the mutex.
183-
let ts_guard = ThreadStateGuard::new();
184-
185-
let mut dtypes = self.dtypes.lock().expect("dtype cache poisoned");
186-
187-
// Now we hold the mutex so it's safe to re-attach to the runtime.
188-
drop(ts_guard);
182+
let mut dtypes = self
183+
.dtypes
184+
.lock_py_attached(py)
185+
.expect("dtype cache poisoned");
189186

190187
let dtype = match dtypes.get_or_insert_with(Default::default).entry(size) {
191188
Entry::Occupied(entry) => entry.into_mut(),

0 commit comments

Comments
 (0)
Failed to load comments.