Skip to content

Commit

Permalink
support 3.13t free-threaded python
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Nov 22, 2024
1 parent c386c8e commit 2b364d3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
"3.11",
"3.12",
"3.13",
"3.13t",
"pypy-3.9",
"pypy-3.10",
]
Expand Down Expand Up @@ -108,7 +109,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
uses: Quansight-Labs/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
architecture: ${{ matrix.platform.python-architecture }}
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- removed the `gil-refs` feature
- reintroduced function names without `_bound` suffix + deprecating the old names
- switched to `IntoPyObject` as trait bound
- Support Python 3.13t "free-threaded" Python. ([#471](https://github.com/PyO3/rust-numpy/pull/471)

- v0.22.1
- Fix building on 32-bit Windows. ([#463](https://github.com/PyO3/rust-numpy/pull/463))
Expand Down
12 changes: 6 additions & 6 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
//! [scalars-datetime64]: https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.datetime64
//! [scalars-timedelta64]: https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.timedelta64
use std::cell::RefCell;
use std::collections::hash_map::Entry;
use std::fmt;
use std::hash::Hash;
use std::marker::PhantomData;
use std::sync::Mutex;

use pyo3::{sync::GILProtected, Bound, Py, Python};
use pyo3::{Bound, Py, Python};
use rustc_hash::FxHashMap;

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

struct TypeDescriptors {
npy_type: NPY_TYPES,
#[allow(clippy::type_complexity)]
dtypes: GILProtected<RefCell<Option<FxHashMap<NPY_DATETIMEUNIT, Py<PyArrayDescr>>>>>,
dtypes: Mutex<Option<FxHashMap<NPY_DATETIMEUNIT, Py<PyArrayDescr>>>>,
}

impl TypeDescriptors {
/// `npy_type` must be either `NPY_DATETIME` or `NPY_TIMEDELTA`.
const unsafe fn new(npy_type: NPY_TYPES) -> Self {
Self {
npy_type,
dtypes: GILProtected::new(RefCell::new(None)),
dtypes: Mutex::new(None),
}
}

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

let dtype = match dtypes.get_or_insert_with(Default::default).entry(unit) {
Entry::Occupied(entry) => entry.into_mut(),
Expand Down
11 changes: 5 additions & 6 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
//! [ascii]: https://numpy.org/doc/stable/reference/c-api/dtype.html#c.NPY_STRING
//! [ucs4]: https://numpy.org/doc/stable/reference/c-api/dtype.html#c.NPY_UNICODE
use std::cell::RefCell;
use std::collections::hash_map::Entry;
use std::fmt;
use std::mem::size_of;
use std::os::raw::c_char;
use std::str;
use std::sync::Mutex;

use pyo3::{
ffi::{Py_UCS1, Py_UCS4},
sync::GILProtected,
Bound, Py, Python,
};
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -160,14 +159,13 @@ unsafe impl<const N: usize> Element for PyFixedUnicode<N> {
}

struct TypeDescriptors {
#[allow(clippy::type_complexity)]
dtypes: GILProtected<RefCell<Option<FxHashMap<usize, Py<PyArrayDescr>>>>>,
dtypes: Mutex<Option<FxHashMap<usize, Py<PyArrayDescr>>>>,
}

impl TypeDescriptors {
const fn new() -> Self {
Self {
dtypes: GILProtected::new(RefCell::new(None)),
dtypes: Mutex::new(None),
}
}

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

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

0 comments on commit 2b364d3

Please sign in to comment.