diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi index bf9a6d67228..f460ebcfff7 100644 --- a/python/pyarrow/array.pxi +++ b/python/pyarrow/array.pxi @@ -2390,12 +2390,15 @@ cdef class Array(_PandasConvertible): ) # Note: from March 2025 onwards, it's okay to raise BufferError here. # Still we keep the V0 version as the V1 was only added in August 2026. + legacy_tensor = GetResultValue(ExportArrayToDLPack(self.sp_array)) + # Wrap the exported tensor in its capsule before warning so that a + # warning escalated to an error cannot leak the tensor. + capsule = PyCapsule_New(legacy_tensor, 'dltensor', dlpack_pycapsule_deleter) warnings.warn( "Exporting an unversioned DLPack capsule is deprecated, " "pass max_version=(1, 0) or higher.", DeprecationWarning, stacklevel=2) - legacy_tensor = GetResultValue(ExportArrayToDLPack(self.sp_array)) - return PyCapsule_New(legacy_tensor, 'dltensor', dlpack_pycapsule_deleter) + return capsule # Currently no major version other than legacy 0 and current 1.3 dlm_tensor = GetResultValue( diff --git a/python/pyarrow/tensor.pxi b/python/pyarrow/tensor.pxi index 7f006f29417..c66fc6321de 100644 --- a/python/pyarrow/tensor.pxi +++ b/python/pyarrow/tensor.pxi @@ -401,12 +401,15 @@ strides: {self.strides}""" ) # Note: from March 2025 onwards, it's okay to raise BufferError here. # Still we keep the V0 version as the V1 was only added in August 2026. + legacy_tensor = GetResultValue(ExportTensorToDLPack(self.sp_tensor)) + # Wrap the exported tensor in its capsule before warning so that a + # warning escalated to an error cannot leak the tensor. + capsule = PyCapsule_New(legacy_tensor, 'dltensor', dlpack_pycapsule_deleter) warnings.warn( "Exporting an unversioned DLPack capsule is deprecated, " "pass max_version=(1, 0) or higher.", DeprecationWarning, stacklevel=2) - legacy_tensor = GetResultValue(ExportTensorToDLPack(self.sp_tensor)) - return PyCapsule_New(legacy_tensor, 'dltensor', dlpack_pycapsule_deleter) + return capsule # Currently no major version other than legacy 0 and current 1.3 dlm_tensor = GetResultValue( diff --git a/python/pyarrow/tests/test_array.py b/python/pyarrow/tests/test_array.py index 74ff837e16e..d9a14abe1a3 100644 --- a/python/pyarrow/tests/test_array.py +++ b/python/pyarrow/tests/test_array.py @@ -2737,6 +2737,10 @@ def test_array_from_list_of_timestamps(unit): @pytest.mark.numpy +@pytest.mark.filterwarnings( + "ignore:The 'generic' unit for NumPy timedelta is deprecated:" + "DeprecationWarning" +) def test_array_from_timestamp_with_generic_unit(): if Version(np.__version__) >= Version("2.5.0"): pytest.skip("generic units of timedelta64 deprecated") @@ -2779,6 +2783,10 @@ def test_array_from_numpy_timedelta(dtype, type): @pytest.mark.numpy +@pytest.mark.filterwarnings( + "ignore:The 'generic' unit for NumPy timedelta is deprecated:" + "DeprecationWarning" +) def test_array_from_numpy_timedelta_incorrect_unit(): # generic (no unit) if Version(np.__version__) < Version("2.5.0"): diff --git a/python/pyarrow/tests/test_dlpack.py b/python/pyarrow/tests/test_dlpack.py index 0c3c082fd6e..88eab11e060 100644 --- a/python/pyarrow/tests/test_dlpack.py +++ b/python/pyarrow/tests/test_dlpack.py @@ -307,7 +307,7 @@ def test_dlpack_versioned_capsule(obj, max_version, copy): @check_bytes_allocated @pytest.mark.parametrize('obj', dlpack_objects()) def test_dlpack_versioned_roundtrip(obj): - expected = np.from_dlpack(DLPackForwarder(obj, max_version=None)) + expected = np.from_dlpack(DLPackForwarder(obj, max_version=(1, 0))) for copy in [None, False, True]: result = np.from_dlpack( DLPackForwarder(obj, max_version=(1, 0), copy=copy)) @@ -335,24 +335,24 @@ def test_dlpack_not_supported(): arr = pa.array([1, None, 3]) with pytest.raises(TypeError, match="Can only use DLPack " "on arrays with no nulls."): - np.from_dlpack(arr) + np.from_dlpack(DLPackForwarder(arr, max_version=(1, 0))) arr = pa.array( [[0, 1], [3, 4]], type=pa.list_(pa.int32()) ) with pytest.raises(TypeError, match="DataType is not compatible with DLPack spec"): - np.from_dlpack(arr) + np.from_dlpack(DLPackForwarder(arr, max_version=(1, 0))) arr = pa.array([]) with pytest.raises(TypeError, match="DataType is not compatible with DLPack spec"): - np.from_dlpack(arr) + np.from_dlpack(DLPackForwarder(arr, max_version=(1, 0))) # DLPack doesn't support bit-packed boolean values arr = pa.array([True, False, True]) with pytest.raises(TypeError, match="Bit-packed boolean data type " "not supported by DLPack."): - np.from_dlpack(arr) + np.from_dlpack(DLPackForwarder(arr, max_version=(1, 0))) def test_dlpack_cuda_not_supported():