Skip to content

Commit ad5d58c

Browse files
authored
Merge pull request #15867 from eric-wieser/deprecate-tostring
DEP: Deprecate ndarray.tostring()
2 parents 9ccb42d + 6df5d8f commit ad5d58c

File tree

7 files changed

+73
-21
lines changed

7 files changed

+73
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
``numpy.ndarray.tostring()`` is deprecated in favor of ``tobytes()``
2+
--------------------------------------------------------------------
3+
`~numpy.ndarray.tobytes` has existed since the 1.9 release, but until this
4+
release `~numpy.ndarray.tostring` emitted no warning. The change to emit a
5+
warning brings NumPy in line with the builtin `array.array` methods of the
6+
same name.

numpy/core/_add_newdocs.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -3932,8 +3932,8 @@
39323932
"""))
39333933

39343934

3935-
tobytesdoc = """
3936-
a.{name}(order='C')
3935+
add_newdoc('numpy.core.multiarray', 'ndarray', ('tobytes', """
3936+
a.tobytes(order='C')
39373937
39383938
Construct Python bytes containing the raw data bytes in the array.
39393939
@@ -3943,11 +3943,11 @@
39433943
unless the F_CONTIGUOUS flag in the array is set, in which case it
39443944
means 'Fortran' order.
39453945
3946-
{deprecated}
3946+
.. versionadded:: 1.9.0
39473947
39483948
Parameters
39493949
----------
3950-
order : {{'C', 'F', None}}, optional
3950+
order : {'C', 'F', None}, optional
39513951
Order of the data for multidimensional arrays:
39523952
C, Fortran, or the same as for the original array.
39533953
@@ -3966,18 +3966,19 @@
39663966
>>> x.tobytes('F')
39673967
b'\\x00\\x00\\x02\\x00\\x01\\x00\\x03\\x00'
39683968
3969-
"""
3969+
"""))
3970+
3971+
3972+
add_newdoc('numpy.core.multiarray', 'ndarray', ('tostring', r"""
3973+
a.tostring(order='C')
3974+
3975+
A compatibility alias for `tobytes`, with exactly the same behavior.
3976+
3977+
Despite its name, it returns `bytes` not `str`\ s.
3978+
3979+
.. deprecated:: 1.19.0
3980+
"""))
39703981

3971-
add_newdoc('numpy.core.multiarray', 'ndarray',
3972-
('tostring', tobytesdoc.format(name='tostring',
3973-
deprecated=
3974-
'This function is a compatibility '
3975-
'alias for tobytes. Despite its '
3976-
'name it returns bytes not '
3977-
'strings.')))
3978-
add_newdoc('numpy.core.multiarray', 'ndarray',
3979-
('tobytes', tobytesdoc.format(name='tobytes',
3980-
deprecated='.. versionadded:: 1.9.0')))
39813982

39823983
add_newdoc('numpy.core.multiarray', 'ndarray', ('trace',
39833984
"""

numpy/core/src/multiarray/methods.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,23 @@ array_tobytes(PyArrayObject *self, PyObject *args, PyObject *kwds)
566566
return PyArray_ToString(self, order);
567567
}
568568

569+
static PyObject *
570+
array_tostring(PyArrayObject *self, PyObject *args, PyObject *kwds)
571+
{
572+
NPY_ORDER order = NPY_CORDER;
573+
static char *kwlist[] = {"order", NULL};
574+
575+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&:tostring", kwlist,
576+
PyArray_OrderConverter, &order)) {
577+
return NULL;
578+
}
579+
/* 2020-03-30, NumPy 1.19 */
580+
if (DEPRECATE("tostring() is deprecated. Use tobytes() instead.") < 0) {
581+
return NULL;
582+
}
583+
return PyArray_ToString(self, order);
584+
}
585+
569586

570587
/* This should grow an order= keyword to be consistent
571588
*/
@@ -2844,7 +2861,7 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = {
28442861
(PyCFunction)array_tolist,
28452862
METH_VARARGS, NULL},
28462863
{"tostring",
2847-
(PyCFunction)array_tobytes,
2864+
(PyCFunction)array_tostring,
28482865
METH_VARARGS | METH_KEYWORDS, NULL},
28492866
{"trace",
28502867
(PyCFunction)array_trace,

numpy/core/tests/test_deprecations.py

+17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import warnings
99
import pytest
1010
import tempfile
11+
import re
1112

1213
import numpy as np
1314
from numpy.testing import (
@@ -548,6 +549,22 @@ def test_deprecate_ragged_arrays():
548549
np.array(arg)
549550

550551

552+
class TestToString(_DeprecationTestCase):
553+
# 2020-03-06 1.19.0
554+
message = re.escape("tostring() is deprecated. Use tobytes() instead.")
555+
556+
def test_tostring(self):
557+
arr = np.array(list(b"test\xFF"), dtype=np.uint8)
558+
self.assert_deprecated(arr.tostring)
559+
560+
def test_tostring_matches_tobytes(self):
561+
arr = np.array(list(b"test\xFF"), dtype=np.uint8)
562+
b = arr.tobytes()
563+
with assert_warns(DeprecationWarning):
564+
s = arr.tostring()
565+
assert s == b
566+
567+
551568
class TestDTypeCoercion(_DeprecationTestCase):
552569
# 2020-02-06 1.19.0
553570
message = "Converting .* to a dtype .*is deprecated"

numpy/lib/user_array.py

+4
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ def tostring(self):
227227
""
228228
return self.array.tostring()
229229

230+
def tobytes(self):
231+
""
232+
return self.array.tobytes()
233+
230234
def byteswap(self):
231235
""
232236
return self._rc(self.array.byteswap())

numpy/ma/core.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -5941,10 +5941,17 @@ def tolist(self, fill_value=None):
59415941
return result.tolist()
59425942

59435943
def tostring(self, fill_value=None, order='C'):
5944+
r"""
5945+
A compatibility alias for `tobytes`, with exactly the same behavior.
5946+
5947+
Despite its name, it returns `bytes` not `str`\ s.
5948+
5949+
.. deprecated:: 1.19.0
59445950
"""
5945-
This function is a compatibility alias for tobytes. Despite its name it
5946-
returns bytes not strings.
5947-
"""
5951+
# 2020-03-30, Numpy 1.19.0
5952+
warnings.warn(
5953+
"tostring() is deprecated. Use tobytes() instead.",
5954+
DeprecationWarning, stacklevel=2)
59485955

59495956
return self.tobytes(fill_value, order=order)
59505957

numpy/ma/tests/test_regression.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,6 @@ def test_empty_list_on_structured(self):
8686
ma = np.ma.MaskedArray([(1, 1.), (2, 2.), (3, 3.)], dtype='i4,f4')
8787
assert_array_equal(ma[[]], ma[:0])
8888

89-
def test_masked_array_tostring_fortran(self):
89+
def test_masked_array_tobytes_fortran(self):
9090
ma = np.ma.arange(4).reshape((2,2))
91-
assert_array_equal(ma.tostring(order='F'), ma.T.tostring())
91+
assert_array_equal(ma.tobytes(order='F'), ma.T.tobytes())

0 commit comments

Comments
 (0)