Skip to content

Commit 4c1ad1f

Browse files
committed
Merge branch 'main' into faer_compat
2 parents cd05009 + fd82144 commit 4c1ad1f

File tree

9 files changed

+33
-52
lines changed

9 files changed

+33
-52
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ jobs:
274274
import toml
275275
cargo_toml = toml.load("Cargo.toml")
276276
cargo_toml["dependencies"]["ndarray"] = "0.15.6"
277+
cargo_toml["dependencies"]["once_cell"] = "1.20.3"
277278
with open("Cargo.toml", "w") as f:
278279
toml.dump(cargo_toml, f)
279280
working-directory: examples/simple
@@ -290,6 +291,8 @@ jobs:
290291
pkg_id = pkg["name"] + ":" + pkg["version"]
291292
if pkg["name"] == "ndarray" and pkg["version"] != "0.15.6":
292293
subprocess.run(["cargo", "update", "--package", pkg_id, "--precise", "0.15.6"], check=True)
294+
elif pkg["name"] == "once_cell" and pkg["version"] != "1.20.3":
295+
subprocess.run(["cargo", "update", "--package", pkg_id, "--precise", "1.20.3"], check=True)
293296
working-directory: examples/simple
294297
shell: python
295298
- name: Test example

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ num-complex = ">= 0.2, < 0.5"
2323
num-integer = "0.1"
2424
num-traits = "0.2"
2525
ndarray = ">= 0.15, < 0.17"
26-
pyo3 = { version = "0.23.4", default-features = false, features = ["macros"] }
26+
pyo3 = { version = "0.24", default-features = false, features = ["macros"] }
2727
rustc-hash = "2.0"
2828

2929
[features]
3030
faer = ["dep:faer"]
3131

3232
[dev-dependencies]
33-
pyo3 = { version = "0.23.3", default-features = false, features = ["auto-initialize"] }
33+
pyo3 = { version = "0.24", default-features = false, features = ["auto-initialize"] }
3434
nalgebra = { version = ">=0.30, <0.34", default-features = false, features = ["std"] }
3535

3636
[build-dependencies]
37-
pyo3-build-config = { version = "0.23.1", features = ["resolve-config"] }
37+
pyo3-build-config = { version = "0.24", features = ["resolve-config"] }
3838

3939
[package.metadata.docs.rs]
4040
all-features = true

examples/linalg/Cargo.lock

Lines changed: 14 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/linalg/Cargo.toml

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 8 deletions
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

Lines changed: 0 additions & 17 deletions
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

Lines changed: 5 additions & 8 deletions
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)