diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/__init__.pyi | 2 | ||||
-rw-r--r-- | numpy/array_api/__init__.py | 4 | ||||
-rw-r--r-- | numpy/array_api/_creation_functions.py | 2 | ||||
-rw-r--r-- | numpy/core/_add_newdocs.py | 4 | ||||
-rw-r--r-- | numpy/core/multiarray.py | 17 | ||||
-rw-r--r-- | numpy/core/numeric.py | 4 | ||||
-rw-r--r-- | numpy/core/src/common/npy_dlpack.h | 2 | ||||
-rw-r--r-- | numpy/core/src/multiarray/dlpack.c | 2 | ||||
-rw-r--r-- | numpy/core/src/multiarray/dlpack.h | 18 | ||||
-rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.c | 2 | ||||
-rw-r--r-- | numpy/core/tests/test_dlpack.py | 24 |
11 files changed, 32 insertions, 49 deletions
diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 1562ce89e..b2e9eec77 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -4335,5 +4335,5 @@ class chararray(ndarray[_ShapeType, _CharDType]): class _SupportsDLPack(Protocol[_T_contra]): def __dlpack__(self, *, stream: None | _T_contra = ...) -> _PyCapsule: ... -def from_dlpack(__obj: _SupportsDLPack[None]) -> NDArray[Any]: ... +def _from_dlpack(__obj: _SupportsDLPack[None]) -> NDArray[Any]: ... diff --git a/numpy/array_api/__init__.py b/numpy/array_api/__init__.py index d8b29057e..89f5e9cba 100644 --- a/numpy/array_api/__init__.py +++ b/numpy/array_api/__init__.py @@ -136,7 +136,7 @@ from ._creation_functions import ( empty, empty_like, eye, - from_dlpack, + _from_dlpack, full, full_like, linspace, @@ -155,7 +155,7 @@ __all__ += [ "empty", "empty_like", "eye", - "from_dlpack", + "_from_dlpack", "full", "full_like", "linspace", diff --git a/numpy/array_api/_creation_functions.py b/numpy/array_api/_creation_functions.py index e36807468..c3644ac2c 100644 --- a/numpy/array_api/_creation_functions.py +++ b/numpy/array_api/_creation_functions.py @@ -151,7 +151,7 @@ def eye( return Array._new(np.eye(n_rows, M=n_cols, k=k, dtype=dtype)) -def from_dlpack(x: object, /) -> Array: +def _from_dlpack(x: object, /) -> Array: # Note: dlpack support is not yet implemented on Array raise NotImplementedError("DLPack support is not yet implemented") diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py index f1a42dffe..cae5bc281 100644 --- a/numpy/core/_add_newdocs.py +++ b/numpy/core/_add_newdocs.py @@ -1573,9 +1573,9 @@ add_newdoc('numpy.core.multiarray', 'frombuffer', array_function_like_doc, )) -add_newdoc('numpy.core.multiarray', 'from_dlpack', +add_newdoc('numpy.core.multiarray', '_from_dlpack', """ - from_dlpack(x, /) + _from_dlpack(x, /) Create a NumPy array from an object implementing the ``__dlpack__`` protocol. diff --git a/numpy/core/multiarray.py b/numpy/core/multiarray.py index 9f431f01b..f96274263 100644 --- a/numpy/core/multiarray.py +++ b/numpy/core/multiarray.py @@ -14,8 +14,9 @@ from ._multiarray_umath import * # noqa: F403 # do not change them. issue gh-15518 # _get_ndarray_c_version is semi-public, on purpose not added to __all__ from ._multiarray_umath import ( - _fastCopyAndTranspose, _flagdict, _insert, _reconstruct, _vec_string, - _ARRAY_API, _monotonicity, _get_ndarray_c_version, _set_madvise_hugepage, + _fastCopyAndTranspose, _flagdict, _from_dlpack, _insert, _reconstruct, + _vec_string, _ARRAY_API, _monotonicity, _get_ndarray_c_version, + _set_madvise_hugepage, ) __all__ = [ @@ -23,15 +24,15 @@ __all__ = [ 'ITEM_HASOBJECT', 'ITEM_IS_POINTER', 'LIST_PICKLE', 'MAXDIMS', 'MAY_SHARE_BOUNDS', 'MAY_SHARE_EXACT', 'NEEDS_INIT', 'NEEDS_PYAPI', 'RAISE', 'USE_GETITEM', 'USE_SETITEM', 'WRAP', '_fastCopyAndTranspose', - '_flagdict', '_insert', '_reconstruct', '_vec_string', '_monotonicity', - 'add_docstring', 'arange', 'array', 'asarray', 'asanyarray', - 'ascontiguousarray', 'asfortranarray', 'bincount', 'broadcast', - 'busday_count', 'busday_offset', 'busdaycalendar', 'can_cast', + '_flagdict', '_from_dlpack', '_insert', '_reconstruct', '_vec_string', + '_monotonicity', 'add_docstring', 'arange', 'array', 'asarray', + 'asanyarray', 'ascontiguousarray', 'asfortranarray', 'bincount', + 'broadcast', 'busday_count', 'busday_offset', 'busdaycalendar', 'can_cast', 'compare_chararrays', 'concatenate', 'copyto', 'correlate', 'correlate2', 'count_nonzero', 'c_einsum', 'datetime_as_string', 'datetime_data', 'dot', 'dragon4_positional', 'dragon4_scientific', 'dtype', 'empty', 'empty_like', 'error', 'flagsobj', 'flatiter', 'format_longfloat', - 'frombuffer', 'from_dlpack', 'fromfile', 'fromiter', 'fromstring', + 'frombuffer', 'fromfile', 'fromiter', 'fromstring', 'get_handler_name', 'inner', 'interp', 'interp_complex', 'is_busday', 'lexsort', 'matmul', 'may_share_memory', 'min_scalar_type', 'ndarray', 'nditer', 'nested_iters', 'normalize_axis_index', 'packbits', @@ -46,6 +47,7 @@ _reconstruct.__module__ = 'numpy.core.multiarray' scalar.__module__ = 'numpy.core.multiarray' +_from_dlpack.__module__ = 'numpy' arange.__module__ = 'numpy' array.__module__ = 'numpy' asarray.__module__ = 'numpy' @@ -55,7 +57,6 @@ asfortranarray.__module__ = 'numpy' datetime_data.__module__ = 'numpy' empty.__module__ = 'numpy' frombuffer.__module__ = 'numpy' -from_dlpack.__module__ = 'numpy' fromfile.__module__ = 'numpy' fromiter.__module__ = 'numpy' frompyfunc.__module__ = 'numpy' diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 987470f92..344d40d93 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -13,7 +13,7 @@ from .multiarray import ( WRAP, arange, array, asarray, asanyarray, ascontiguousarray, asfortranarray, broadcast, can_cast, compare_chararrays, concatenate, copyto, dot, dtype, empty, - empty_like, flatiter, frombuffer, from_dlpack, fromfile, fromiter, + empty_like, flatiter, frombuffer, _from_dlpack, fromfile, fromiter, fromstring, inner, lexsort, matmul, may_share_memory, min_scalar_type, ndarray, nditer, nested_iters, promote_types, putmask, result_type, set_numeric_ops, shares_memory, vdot, where, @@ -41,7 +41,7 @@ __all__ = [ 'newaxis', 'ndarray', 'flatiter', 'nditer', 'nested_iters', 'ufunc', 'arange', 'array', 'asarray', 'asanyarray', 'ascontiguousarray', 'asfortranarray', 'zeros', 'count_nonzero', 'empty', 'broadcast', 'dtype', - 'fromstring', 'fromfile', 'frombuffer', 'from_dlpack', 'where', + 'fromstring', 'fromfile', 'frombuffer', '_from_dlpack', 'where', 'argwhere', 'copyto', 'concatenate', 'fastCopyAndTranspose', 'lexsort', 'set_numeric_ops', 'can_cast', 'promote_types', 'min_scalar_type', 'result_type', 'isfortran', 'empty_like', 'zeros_like', 'ones_like', diff --git a/numpy/core/src/common/npy_dlpack.h b/numpy/core/src/common/npy_dlpack.h index cb926a262..14ca352c0 100644 --- a/numpy/core/src/common/npy_dlpack.h +++ b/numpy/core/src/common/npy_dlpack.h @@ -23,6 +23,6 @@ array_dlpack_device(PyArrayObject *self, PyObject *NPY_UNUSED(args)); NPY_NO_EXPORT PyObject * -from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj); +_from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj); #endif diff --git a/numpy/core/src/multiarray/dlpack.c b/numpy/core/src/multiarray/dlpack.c index 9de304379..21930b0ef 100644 --- a/numpy/core/src/multiarray/dlpack.c +++ b/numpy/core/src/multiarray/dlpack.c @@ -260,7 +260,7 @@ array_dlpack_device(PyArrayObject *self, PyObject *NPY_UNUSED(args)) } NPY_NO_EXPORT PyObject * -from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { +_from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { PyObject *capsule = PyObject_CallMethod((PyObject *)obj->ob_type, "__dlpack__", "O", obj); if (capsule == NULL) { diff --git a/numpy/core/src/multiarray/dlpack.h b/numpy/core/src/multiarray/dlpack.h deleted file mode 100644 index 7d4a289d9..000000000 --- a/numpy/core/src/multiarray/dlpack.h +++ /dev/null @@ -1,18 +0,0 @@ -#define NPY_NO_DEPRECATED_API NPY_API_VERSION -#define _MULTIARRAYMODULE - -#define PY_SSIZE_T_CLEAN -#include <Python.h> -#include "numpy/arrayobject.h" - -static PyObject * -array_dlpack(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, - PyObject *kwnames); - - -static PyObject * -array_dlpack_device(PyArrayObject *self, PyObject *NPY_UNUSED(args)); - - -NPY_NO_EXPORT PyObject * -from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj); diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index ac4c1ae0b..3cb4e83f1 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -4446,7 +4446,7 @@ static struct PyMethodDef array_module_methods[] = { {"_reload_guard", (PyCFunction)_reload_guard, METH_NOARGS, "Give a warning on reload and big warning in sub-interpreters."}, - {"from_dlpack", (PyCFunction)from_dlpack, + {"_from_dlpack", (PyCFunction)_from_dlpack, METH_O, NULL}, {NULL, NULL, 0, NULL} /* sentinel */ }; diff --git a/numpy/core/tests/test_dlpack.py b/numpy/core/tests/test_dlpack.py index 2561991e8..06fc042ec 100644 --- a/numpy/core/tests/test_dlpack.py +++ b/numpy/core/tests/test_dlpack.py @@ -27,12 +27,12 @@ class TestDLPack: z = y['int'] with pytest.raises(RuntimeError): - np.from_dlpack(z) + np._from_dlpack(z) @pytest.mark.skipif(IS_PYPY, reason="PyPy can't get refcounts.") def test_from_dlpack_refcount(self): x = np.arange(5) - y = np.from_dlpack(x) + y = np._from_dlpack(x) assert sys.getrefcount(x) == 3 del y assert sys.getrefcount(x) == 2 @@ -45,7 +45,7 @@ class TestDLPack: ]) def test_dtype_passthrough(self, dtype): x = np.arange(5, dtype=dtype) - y = np.from_dlpack(x) + y = np._from_dlpack(x) assert y.dtype == x.dtype assert_array_equal(x, y) @@ -54,44 +54,44 @@ class TestDLPack: x = np.asarray(np.datetime64('2021-05-27')) with pytest.raises(TypeError): - np.from_dlpack(x) + np._from_dlpack(x) def test_invalid_byte_swapping(self): dt = np.dtype('=i8').newbyteorder() x = np.arange(5, dtype=dt) with pytest.raises(TypeError): - np.from_dlpack(x) + np._from_dlpack(x) def test_non_contiguous(self): x = np.arange(25).reshape((5, 5)) y1 = x[0] - assert_array_equal(y1, np.from_dlpack(y1)) + assert_array_equal(y1, np._from_dlpack(y1)) y2 = x[:, 0] - assert_array_equal(y2, np.from_dlpack(y2)) + assert_array_equal(y2, np._from_dlpack(y2)) y3 = x[1, :] - assert_array_equal(y3, np.from_dlpack(y3)) + assert_array_equal(y3, np._from_dlpack(y3)) y4 = x[1] - assert_array_equal(y4, np.from_dlpack(y4)) + assert_array_equal(y4, np._from_dlpack(y4)) y5 = np.diagonal(x) - assert_array_equal(y5, np.from_dlpack(y5)) + assert_array_equal(y5, np._from_dlpack(y5)) @pytest.mark.parametrize("ndim", range(33)) def test_higher_dims(self, ndim): shape = (1,) * ndim x = np.zeros(shape, dtype=np.float64) - assert shape == np.from_dlpack(x).shape + assert shape == np._from_dlpack(x).shape def test_dlpack_device(self): x = np.arange(5) assert x.__dlpack_device__() == (1, 0) - assert np.from_dlpack(x).__dlpack_device__() == (1, 0) + assert np._from_dlpack(x).__dlpack_device__() == (1, 0) def dlpack_deleter_exception(self): x = np.arange(5) |