Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEP: Deprecate 4 ndarray.ctypes methods #19031

Merged
merged 7 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/release/upcoming_changes/19031.deprecation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Four `ndarray.ctypes` methods have been deprecated
--------------------------------------------------
Four methods of the `ndarray.ctypes` object have been deprecated,
as they are (undocumentated) implementation artifacts of their respective
properties.

The methods in question are:

* ``_ctypes.get_data`` (use ``_ctypes.data`` instead)
* ``_ctypes.get_shape`` (use ``_ctypes.shape`` instead)
* ``_ctypes.get_strides`` (use ``_ctypes.strides`` instead)
* ``_ctypes.get_as_parameter`` (use ``_ctypes._as_parameter_`` instead)
11 changes: 6 additions & 5 deletions doc/source/user/misc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,12 @@ Only a survey of the choices. Little detail on how each works.
- good numpy support: arrays have all these in their ctypes
attribute: ::

a.ctypes.data a.ctypes.get_strides
a.ctypes.data_as a.ctypes.shape
a.ctypes.get_as_parameter a.ctypes.shape_as
a.ctypes.get_data a.ctypes.strides
a.ctypes.get_shape a.ctypes.strides_as
a.ctypes.data
a.ctypes.data_as
a.ctypes.shape
a.ctypes.shape_as
a.ctypes.strides
a.ctypes.strides_as

- Minuses:

Expand Down
45 changes: 40 additions & 5 deletions numpy/core/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import re
import sys
import platform
import warnings

from .multiarray import dtype, array, ndarray
try:
Expand Down Expand Up @@ -350,11 +351,45 @@ def _as_parameter_(self):
"""
return self.data_as(ctypes.c_void_p)

# kept for compatibility
get_data = data.fget
get_shape = shape.fget
get_strides = strides.fget
get_as_parameter = _as_parameter_.fget
# Numpy 1.21.0, 2021-05-18

def get_data(self):
"""Deprecated getter for the `_ctypes.data` property.

.. deprecated:: 1.21
"""
warnings.warn('"get_data" is deprecated. Use "data" instead',
DeprecationWarning, stacklevel=2)
return self.data

def get_shape(self):
"""Deprecated getter for the `_ctypes.shape` property.

.. deprecated:: 1.21
"""
warnings.warn('"get_shape" is deprecated. Use "shape" instead',
DeprecationWarning, stacklevel=2)
return self.shape

def get_strides(self):
"""Deprecated getter for the `_ctypes.strides` property.

.. deprecated:: 1.21
"""
warnings.warn('"get_strides" is deprecated. Use "strides" instead',
DeprecationWarning, stacklevel=2)
return self.strides

def get_as_parameter(self):
"""Deprecated getter for the `_ctypes._as_parameter_` property.

.. deprecated:: 1.21
"""
warnings.warn(
'"get_as_parameter" is deprecated. Use "_as_parameter_" instead',
DeprecationWarning, stacklevel=2,
)
return self._as_parameter_


def _newnames(datatype, order):
Expand Down
21 changes: 20 additions & 1 deletion numpy/core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ def test_deprecated(self):
class TestNoseDecoratorsDeprecated(_DeprecationTestCase):
class DidntSkipException(Exception):
pass

def test_slow(self):
def _test_slow():
@np.testing.dec.slow
Expand Down Expand Up @@ -1172,3 +1172,22 @@ def test_deprecated(self):
self.assert_deprecated(lambda: np.equal(1, 1, dtype=object))
self.assert_deprecated(
lambda: np.equal(1, 1, sig=(None, None, object)))


class TestCtypesGetter(_DeprecationTestCase):
# Deprecated 2021-05-18, Numpy 1.21.0
warning_cls = DeprecationWarning
ctypes = np.array([1]).ctypes

@pytest.mark.parametrize(
"name", ["get_data", "get_shape", "get_strides", "get_as_parameter"]
)
def test_deprecated(self, name: str) -> None:
func = getattr(self.ctypes, name)
self.assert_deprecated(lambda: func())

@pytest.mark.parametrize(
"name", ["data", "shape", "strides", "_as_parameter_"]
)
def test_not_deprecated(self, name: str) -> None:
self.assert_not_deprecated(lambda: getattr(self.ctypes, name))