From 27eaf02e7f95517573bcf14c59a9e9158d664330 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 24 Sep 2024 09:39:55 -0600 Subject: [PATCH] make test runnable on GIL-enabled build --- pytests/src/pyclasses.rs | 12 ++++++------ pytests/tests/test_pyclasses.py | 5 ----- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/pytests/src/pyclasses.rs b/pytests/src/pyclasses.rs index 44b7b9d31fe..4e52dbc8712 100644 --- a/pytests/src/pyclasses.rs +++ b/pytests/src/pyclasses.rs @@ -1,4 +1,3 @@ -use std::sync::atomic::{AtomicUsize, Ordering}; use std::{thread, time}; use pyo3::exceptions::{PyStopIteration, PyValueError}; @@ -49,7 +48,7 @@ impl PyClassIter { #[pyclass] #[derive(Default)] struct PyClassThreadIter { - count: AtomicUsize, + count: usize, } #[pymethods] @@ -59,12 +58,13 @@ impl PyClassThreadIter { Default::default() } - fn __next__(&mut self) -> usize { - let current_count = self.count.fetch_add(1, Ordering::SeqCst); + fn __next__(&mut self, py: Python<'_>) -> usize { + let current_count = self.count; + self.count += 1; if current_count == 0 { - thread::sleep(time::Duration::from_millis(100)); + py.allow_threads(|| thread::sleep(time::Duration::from_millis(100))); } - current_count + self.count } } diff --git a/pytests/tests/test_pyclasses.py b/pytests/tests/test_pyclasses.py index 7e1e98754c7..2b403e13dfe 100644 --- a/pytests/tests/test_pyclasses.py +++ b/pytests/tests/test_pyclasses.py @@ -5,8 +5,6 @@ import pytest from pyo3_pytests import pyclasses -FREETHREADED_BUILD = bool(sysconfig.get_config_var("Py_GIL_DISABLED")) - def test_empty_class_init(benchmark): benchmark(pyclasses.EmptyClass) @@ -57,9 +55,6 @@ def test_iter(): assert excinfo.value.value == "Ended" -@pytest.mark.skipif( - not FREETHREADED_BUILD, reason="The GIL enforces runtime borrow checking" -) def test_parallel_iter(): i = pyclasses.PyClassThreadIter()