summaryrefslogtreecommitdiff
path: root/numpy/core
diff options
context:
space:
mode:
authorMatti Picus <matti.picus@gmail.com>2020-04-01 12:23:05 +0300
committerGitHub <noreply@github.com>2020-04-01 12:23:05 +0300
commitad5d58c29ab4793cd66afa07eed0f77c7ad59dc4 (patch)
treee584552ae04d7b9d64d45f33adfce643b91bac86 /numpy/core
parent9ccb42d02f7b3fb2b47854bbd4ab418676e6be42 (diff)
parent6df5d8f8f55156aeb798dfd8be2425e5adbefbf1 (diff)
downloadnumpy-ad5d58c29ab4793cd66afa07eed0f77c7ad59dc4.tar.gz
Merge pull request #15867 from eric-wieser/deprecate-tostring
DEP: Deprecate ndarray.tostring()
Diffstat (limited to 'numpy/core')
-rw-r--r--numpy/core/_add_newdocs.py31
-rw-r--r--numpy/core/src/multiarray/methods.c19
-rw-r--r--numpy/core/tests/test_deprecations.py17
3 files changed, 51 insertions, 16 deletions
diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py
index b963f32b8..18ab10078 100644
--- a/numpy/core/_add_newdocs.py
+++ b/numpy/core/_add_newdocs.py
@@ -3932,8 +3932,8 @@ add_newdoc('numpy.core.multiarray', 'ndarray', ('tolist',
"""))
-tobytesdoc = """
- a.{name}(order='C')
+add_newdoc('numpy.core.multiarray', 'ndarray', ('tobytes', """
+ a.tobytes(order='C')
Construct Python bytes containing the raw data bytes in the array.
@@ -3943,11 +3943,11 @@ tobytesdoc = """
unless the F_CONTIGUOUS flag in the array is set, in which case it
means 'Fortran' order.
- {deprecated}
+ .. versionadded:: 1.9.0
Parameters
----------
- order : {{'C', 'F', None}}, optional
+ order : {'C', 'F', None}, optional
Order of the data for multidimensional arrays:
C, Fortran, or the same as for the original array.
@@ -3966,18 +3966,19 @@ tobytesdoc = """
>>> x.tobytes('F')
b'\\x00\\x00\\x02\\x00\\x01\\x00\\x03\\x00'
- """
+ """))
+
+
+add_newdoc('numpy.core.multiarray', 'ndarray', ('tostring', r"""
+ a.tostring(order='C')
+
+ A compatibility alias for `tobytes`, with exactly the same behavior.
+
+ Despite its name, it returns `bytes` not `str`\ s.
+
+ .. deprecated:: 1.19.0
+ """))
-add_newdoc('numpy.core.multiarray', 'ndarray',
- ('tostring', tobytesdoc.format(name='tostring',
- deprecated=
- 'This function is a compatibility '
- 'alias for tobytes. Despite its '
- 'name it returns bytes not '
- 'strings.')))
-add_newdoc('numpy.core.multiarray', 'ndarray',
- ('tobytes', tobytesdoc.format(name='tobytes',
- deprecated='.. versionadded:: 1.9.0')))
add_newdoc('numpy.core.multiarray', 'ndarray', ('trace',
"""
diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c
index ebdf8a4cd..7bfbeca15 100644
--- a/numpy/core/src/multiarray/methods.c
+++ b/numpy/core/src/multiarray/methods.c
@@ -566,6 +566,23 @@ array_tobytes(PyArrayObject *self, PyObject *args, PyObject *kwds)
return PyArray_ToString(self, order);
}
+static PyObject *
+array_tostring(PyArrayObject *self, PyObject *args, PyObject *kwds)
+{
+ NPY_ORDER order = NPY_CORDER;
+ static char *kwlist[] = {"order", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&:tostring", kwlist,
+ PyArray_OrderConverter, &order)) {
+ return NULL;
+ }
+ /* 2020-03-30, NumPy 1.19 */
+ if (DEPRECATE("tostring() is deprecated. Use tobytes() instead.") < 0) {
+ return NULL;
+ }
+ return PyArray_ToString(self, order);
+}
+
/* This should grow an order= keyword to be consistent
*/
@@ -2844,7 +2861,7 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = {
(PyCFunction)array_tolist,
METH_VARARGS, NULL},
{"tostring",
- (PyCFunction)array_tobytes,
+ (PyCFunction)array_tostring,
METH_VARARGS | METH_KEYWORDS, NULL},
{"trace",
(PyCFunction)array_trace,
diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py
index d2cf315a9..5d35bde6c 100644
--- a/numpy/core/tests/test_deprecations.py
+++ b/numpy/core/tests/test_deprecations.py
@@ -8,6 +8,7 @@ import operator
import warnings
import pytest
import tempfile
+import re
import numpy as np
from numpy.testing import (
@@ -548,6 +549,22 @@ def test_deprecate_ragged_arrays():
np.array(arg)
+class TestToString(_DeprecationTestCase):
+ # 2020-03-06 1.19.0
+ message = re.escape("tostring() is deprecated. Use tobytes() instead.")
+
+ def test_tostring(self):
+ arr = np.array(list(b"test\xFF"), dtype=np.uint8)
+ self.assert_deprecated(arr.tostring)
+
+ def test_tostring_matches_tobytes(self):
+ arr = np.array(list(b"test\xFF"), dtype=np.uint8)
+ b = arr.tobytes()
+ with assert_warns(DeprecationWarning):
+ s = arr.tostring()
+ assert s == b
+
+
class TestDTypeCoercion(_DeprecationTestCase):
# 2020-02-06 1.19.0
message = "Converting .* to a dtype .*is deprecated"