From 4883fb8dd854537c998249e73fee6f992148438b Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Wed, 23 Apr 2025 12:34:47 +0200 Subject: [PATCH] Support Python 3.14 --- numpy/_core/src/multiarray/temp_elide.c | 48 +++++++++++++++++++++++- numpy/_core/tests/test_dlpack.py | 8 ++-- numpy/_core/tests/test_dtype.py | 3 +- numpy/_core/tests/test_indexing.py | 6 ++- numpy/_core/tests/test_item_selection.py | 8 +++- numpy/_core/tests/test_multiarray.py | 4 +- numpy/_core/tests/test_nditer.py | 7 +++- numpy/_core/tests/test_regression.py | 43 ++++++++++----------- numpy/_core/tests/test_umath.py | 4 +- 9 files changed, 93 insertions(+), 38 deletions(-) diff --git a/numpy/_core/src/multiarray/temp_elide.c b/numpy/_core/src/multiarray/temp_elide.c index 662a2fa..667ba2b 100644 --- a/numpy/_core/src/multiarray/temp_elide.c +++ b/numpy/_core/src/multiarray/temp_elide.c @@ -62,6 +62,13 @@ #include +#if PY_VERSION_HEX >= 0x030E00A7 && !defined(PYPY_VERSION) +#define Py_BUILD_CORE +#include "internal/pycore_frame.h" +#include "internal/pycore_interpframe.h" +#undef Py_BUILD_CORE +#endif + /* 1 prints elided operations, 2 prints stacktraces */ #define NPY_ELIDE_DEBUG 0 #define NPY_MAX_STACKSIZE 10 @@ -109,6 +116,41 @@ find_addr(void * addresses[], npy_intp naddr, void * addr) return 0; } +static int +check_unique_temporary(PyObject *lhs) +{ +#if PY_VERSION_HEX >= 0x030E00A7 && !defined(PYPY_VERSION) + // In Python 3.14.0a7 and later, a reference count of one doesn't guarantee + // that an object is a unique temporary variable. We scan the top of the + // interpreter stack to check if the object exists as an "owned" reference + // in the temporary stack. + PyFrameObject *frame = PyEval_GetFrame(); + if (frame == NULL) { + return 0; + } + _PyInterpreterFrame *f = frame->f_frame; + _PyStackRef *base = _PyFrame_Stackbase(f); + _PyStackRef *stackpointer = f->stackpointer; + int found_once = 0; + while (stackpointer > base) { + stackpointer--; + PyObject *obj = PyStackRef_AsPyObjectBorrow(*stackpointer); + if (obj == lhs) { + if (!found_once && PyStackRef_IsHeapSafe(*stackpointer)) { + found_once = 1; + } + else { + return 0; + } + } + return found_once; + } + return 0; +#else + return 1; +#endif +} + static int check_callers(int * cannot) { @@ -295,7 +337,8 @@ can_elide_temp(PyObject *olhs, PyObject *orhs, int *cannot) !PyArray_CHKFLAGS(alhs, NPY_ARRAY_OWNDATA) || !PyArray_ISWRITEABLE(alhs) || PyArray_CHKFLAGS(alhs, NPY_ARRAY_WRITEBACKIFCOPY) || - PyArray_NBYTES(alhs) < NPY_MIN_ELIDE_BYTES) { + PyArray_NBYTES(alhs) < NPY_MIN_ELIDE_BYTES || + !check_unique_temporary(olhs)) { return 0; } if (PyArray_CheckExact(orhs) || @@ -372,7 +415,8 @@ can_elide_temp_unary(PyArrayObject * m1) !PyArray_ISNUMBER(m1) || !PyArray_CHKFLAGS(m1, NPY_ARRAY_OWNDATA) || !PyArray_ISWRITEABLE(m1) || - PyArray_NBYTES(m1) < NPY_MIN_ELIDE_BYTES) { + PyArray_NBYTES(m1) < NPY_MIN_ELIDE_BYTES || + !check_unique_temporary((PyObject *)m1)) { return 0; } if (check_callers(&cannot)) { diff --git a/numpy/_core/tests/test_dlpack.py b/numpy/_core/tests/test_dlpack.py index 41dd724..d273bd7 100644 --- a/numpy/_core/tests/test_dlpack.py +++ b/numpy/_core/tests/test_dlpack.py @@ -22,9 +22,9 @@ class TestDLPack: def test_dunder_dlpack_refcount(self, max_version): x = np.arange(5) y = x.__dlpack__(max_version=max_version) - assert sys.getrefcount(x) == 3 + startcount = sys.getrefcount(x) del y - assert sys.getrefcount(x) == 2 + assert startcount - sys.getrefcount(x) == 1 def test_dunder_dlpack_stream(self): x = np.arange(5) @@ -58,9 +58,9 @@ def test_strides_not_multiple_of_itemsize(self): def test_from_dlpack_refcount(self, arr): arr = arr.copy() y = np.from_dlpack(arr) - assert sys.getrefcount(arr) == 3 + startcount = sys.getrefcount(arr) del y - assert sys.getrefcount(arr) == 2 + assert startcount - sys.getrefcount(arr) == 1 @pytest.mark.parametrize("dtype", [ np.bool, diff --git a/numpy/_core/tests/test_dtype.py b/numpy/_core/tests/test_dtype.py index deeca51..759eefe 100644 --- a/numpy/_core/tests/test_dtype.py +++ b/numpy/_core/tests/test_dtype.py @@ -1901,9 +1901,10 @@ class mytype: if HAS_REFCOUNT: # Create an array and test that memory gets cleaned up (gh-25949) o = object() + startcount = sys.getrefcount(o) a = np.array([o], dtype=dt) del a - assert sys.getrefcount(o) == 2 + assert sys.getrefcount(o) == startcount def test_custom_structured_dtype_errors(self): class mytype: diff --git a/numpy/_core/tests/test_indexing.py b/numpy/_core/tests/test_indexing.py index f393c40..bb757cd 100644 --- a/numpy/_core/tests/test_indexing.py +++ b/numpy/_core/tests/test_indexing.py @@ -1174,6 +1174,8 @@ def _compare_index_result(self, arr, index, mimic_get, no_copy): """Compare mimicked result to indexing result. """ arr = arr.copy() + if HAS_REFCOUNT: + startcount = sys.getrefcount(arr) indexed_arr = arr[index] assert_array_equal(indexed_arr, mimic_get) # Check if we got a view, unless its a 0-sized or 0-d array. @@ -1184,9 +1186,9 @@ def _compare_index_result(self, arr, index, mimic_get, no_copy): if HAS_REFCOUNT: if no_copy: # refcount increases by one: - assert_equal(sys.getrefcount(arr), 3) + assert_equal(sys.getrefcount(arr), startcount + 1) else: - assert_equal(sys.getrefcount(arr), 2) + assert_equal(sys.getrefcount(arr), startcount) # Test non-broadcast setitem: b = arr.copy() diff --git a/numpy/_core/tests/test_item_selection.py b/numpy/_core/tests/test_item_selection.py index 5660ef5..839127e 100644 --- a/numpy/_core/tests/test_item_selection.py +++ b/numpy/_core/tests/test_item_selection.py @@ -50,19 +50,23 @@ def test_simple(self): def test_refcounting(self): objects = [object() for i in range(10)] + if HAS_REFCOUNT: + orig_rcs = [sys.getrefcount(o) for o in objects] for mode in ('raise', 'clip', 'wrap'): a = np.array(objects) b = np.array([2, 2, 4, 5, 3, 5]) a.take(b, out=a[:6], mode=mode) del a if HAS_REFCOUNT: - assert_(all(sys.getrefcount(o) == 3 for o in objects)) + assert_(all(sys.getrefcount(o) == rc + 1 + for o, rc in zip(objects, orig_rcs))) # not contiguous, example: a = np.array(objects * 2)[::2] a.take(b, out=a[:6], mode=mode) del a if HAS_REFCOUNT: - assert_(all(sys.getrefcount(o) == 3 for o in objects)) + assert_(all(sys.getrefcount(o) == rc + 1 + for o, rc in zip(objects, orig_rcs))) def test_unicode_mode(self): d = np.arange(10) diff --git a/numpy/_core/tests/test_multiarray.py b/numpy/_core/tests/test_multiarray.py index 8750873..3f26578 100644 --- a/numpy/_core/tests/test_multiarray.py +++ b/numpy/_core/tests/test_multiarray.py @@ -6779,10 +6779,12 @@ def test_dot_3args(self): v = np.random.random_sample((16, 32)) r = np.empty((1024, 32)) + if HAS_REFCOUNT: + orig_refcount = sys.getrefcount(r) for i in range(12): dot(f, v, r) if HAS_REFCOUNT: - assert_equal(sys.getrefcount(r), 2) + assert_equal(sys.getrefcount(r), orig_refcount) r2 = dot(f, v, out=None) assert_array_equal(r2, r) assert_(r is dot(f, v, out=r)) diff --git a/numpy/_core/tests/test_nditer.py b/numpy/_core/tests/test_nditer.py index b0d911f..3e31e82 100644 --- a/numpy/_core/tests/test_nditer.py +++ b/numpy/_core/tests/test_nditer.py @@ -1127,7 +1127,12 @@ def test_iter_object_arrays_conversions(): for x in i: x[...] += 1 if HAS_REFCOUNT: - assert_(sys.getrefcount(ob) == rc-1) + # TODO: why did this change? + newrc = sys.getrefcount(ob) + if sys.version_info < (3, 14): + assert_(newrc == rc - 1) + else: + assert_(newrc == rc) assert_equal(a, np.arange(6)+98172489) def test_iter_common_dtype(): diff --git a/numpy/_core/tests/test_regression.py b/numpy/_core/tests/test_regression.py index 851ce32..eeb6406 100644 --- a/numpy/_core/tests/test_regression.py +++ b/numpy/_core/tests/test_regression.py @@ -1586,29 +1586,26 @@ def test_take_refcount(self): def test_fromfile_tofile_seeks(self): # On Python 3, tofile/fromfile used to get (#1610) the Python # file handle out of sync - f0 = tempfile.NamedTemporaryFile() - f = f0.file - f.write(np.arange(255, dtype='u1').tobytes()) - - f.seek(20) - ret = np.fromfile(f, count=4, dtype='u1') - assert_equal(ret, np.array([20, 21, 22, 23], dtype='u1')) - assert_equal(f.tell(), 24) - - f.seek(40) - np.array([1, 2, 3], dtype='u1').tofile(f) - assert_equal(f.tell(), 43) - - f.seek(40) - data = f.read(3) - assert_equal(data, b"\x01\x02\x03") - - f.seek(80) - f.read(4) - data = np.fromfile(f, dtype='u1', count=4) - assert_equal(data, np.array([84, 85, 86, 87], dtype='u1')) - - f.close() + with tempfile.NamedTemporaryFile() as f: + f.write(np.arange(255, dtype='u1').tobytes()) + + f.seek(20) + ret = np.fromfile(f, count=4, dtype='u1') + assert_equal(ret, np.array([20, 21, 22, 23], dtype='u1')) + assert_equal(f.tell(), 24) + + f.seek(40) + np.array([1, 2, 3], dtype='u1').tofile(f) + assert_equal(f.tell(), 43) + + f.seek(40) + data = f.read(3) + assert_equal(data, b"\x01\x02\x03") + + f.seek(80) + f.read(4) + data = np.fromfile(f, dtype='u1', count=4) + assert_equal(data, np.array([84, 85, 86, 87], dtype='u1')) def test_complex_scalar_warning(self): for tp in [np.csingle, np.cdouble, np.clongdouble]: diff --git a/numpy/_core/tests/test_umath.py b/numpy/_core/tests/test_umath.py index 4d56c78..d432e33 100644 --- a/numpy/_core/tests/test_umath.py +++ b/numpy/_core/tests/test_umath.py @@ -269,9 +269,9 @@ class ArrSubclass(np.ndarray): pass arr = np.arange(10).view(ArrSubclass) - + orig_refcount = sys.getrefcount(arr) arr *= 1 - assert sys.getrefcount(arr) == 2 + assert sys.getrefcount(arr) == orig_refcount class TestComparisons: -- 2.49.0