Skip to content

Commit 2b364d3

Browse files
committed
support 3.13t free-threaded python
1 parent c386c8e commit 2b364d3

File tree

4 files changed

+14
-13
lines changed

4 files changed

+14
-13
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
"3.11",
4040
"3.12",
4141
"3.13",
42+
"3.13t",
4243
"pypy-3.9",
4344
"pypy-3.10",
4445
]
@@ -108,7 +109,7 @@ jobs:
108109
steps:
109110
- uses: actions/checkout@v4
110111
- name: Set up Python ${{ matrix.python-version }}
111-
uses: actions/setup-python@v5
112+
uses: Quansight-Labs/setup-python@v5
112113
with:
113114
python-version: ${{ matrix.python-version }}
114115
architecture: ${{ matrix.platform.python-architecture }}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- removed the `gil-refs` feature
77
- reintroduced function names without `_bound` suffix + deprecating the old names
88
- switched to `IntoPyObject` as trait bound
9+
- Support Python 3.13t "free-threaded" Python. ([#471](https://github.com/PyO3/rust-numpy/pull/471)
910

1011
- v0.22.1
1112
- Fix building on 32-bit Windows. ([#463](https://github.com/PyO3/rust-numpy/pull/463))

src/datetime.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@
5454
//! [scalars-datetime64]: https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.datetime64
5555
//! [scalars-timedelta64]: https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.timedelta64
5656
57-
use std::cell::RefCell;
5857
use std::collections::hash_map::Entry;
5958
use std::fmt;
6059
use std::hash::Hash;
6160
use std::marker::PhantomData;
61+
use std::sync::Mutex;
6262

63-
use pyo3::{sync::GILProtected, Bound, Py, Python};
63+
use pyo3::{Bound, Py, Python};
6464
use rustc_hash::FxHashMap;
6565

6666
use crate::dtype::{clone_methods_impl, Element, PyArrayDescr, PyArrayDescrMethods};
@@ -209,22 +209,22 @@ impl<U: Unit> fmt::Debug for Timedelta<U> {
209209

210210
struct TypeDescriptors {
211211
npy_type: NPY_TYPES,
212-
#[allow(clippy::type_complexity)]
213-
dtypes: GILProtected<RefCell<Option<FxHashMap<NPY_DATETIMEUNIT, Py<PyArrayDescr>>>>>,
212+
dtypes: Mutex<Option<FxHashMap<NPY_DATETIMEUNIT, Py<PyArrayDescr>>>>,
214213
}
215214

216215
impl TypeDescriptors {
217216
/// `npy_type` must be either `NPY_DATETIME` or `NPY_TIMEDELTA`.
218217
const unsafe fn new(npy_type: NPY_TYPES) -> Self {
219218
Self {
220219
npy_type,
221-
dtypes: GILProtected::new(RefCell::new(None)),
220+
dtypes: Mutex::new(None),
222221
}
223222
}
224223

225224
#[allow(clippy::wrong_self_convention)]
226225
fn from_unit<'py>(&self, py: Python<'py>, unit: NPY_DATETIMEUNIT) -> Bound<'py, PyArrayDescr> {
227-
let mut dtypes = self.dtypes.get(py).borrow_mut();
226+
// FIXME probably a deadlock risk here due to the GIL? Might need MutexExt trait in PyO3
227+
let mut dtypes = self.dtypes.lock().expect("dtype cache poisoned");
228228

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

src/strings.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
//! [ascii]: https://numpy.org/doc/stable/reference/c-api/dtype.html#c.NPY_STRING
44
//! [ucs4]: https://numpy.org/doc/stable/reference/c-api/dtype.html#c.NPY_UNICODE
55
6-
use std::cell::RefCell;
76
use std::collections::hash_map::Entry;
87
use std::fmt;
98
use std::mem::size_of;
109
use std::os::raw::c_char;
1110
use std::str;
11+
use std::sync::Mutex;
1212

1313
use pyo3::{
1414
ffi::{Py_UCS1, Py_UCS4},
15-
sync::GILProtected,
1615
Bound, Py, Python,
1716
};
1817
use rustc_hash::FxHashMap;
@@ -160,14 +159,13 @@ unsafe impl<const N: usize> Element for PyFixedUnicode<N> {
160159
}
161160

162161
struct TypeDescriptors {
163-
#[allow(clippy::type_complexity)]
164-
dtypes: GILProtected<RefCell<Option<FxHashMap<usize, Py<PyArrayDescr>>>>>,
162+
dtypes: Mutex<Option<FxHashMap<usize, Py<PyArrayDescr>>>>,
165163
}
166164

167165
impl TypeDescriptors {
168166
const fn new() -> Self {
169167
Self {
170-
dtypes: GILProtected::new(RefCell::new(None)),
168+
dtypes: Mutex::new(None),
171169
}
172170
}
173171

@@ -180,7 +178,8 @@ impl TypeDescriptors {
180178
byteorder: c_char,
181179
size: usize,
182180
) -> Bound<'py, PyArrayDescr> {
183-
let mut dtypes = self.dtypes.get(py).borrow_mut();
181+
// FIXME probably a deadlock risk here due to the GIL? Might need MutexExt trait in PyO3
182+
let mut dtypes = self.dtypes.lock().expect("dtype cache poisoned");
184183

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

0 commit comments

Comments
 (0)