Skip to content

Commit de897af

Browse files
committed
MNT: Remove set_string_function
1 parent a2d1972 commit de897af

File tree

8 files changed

+9
-185
lines changed

8 files changed

+9
-185
lines changed

doc/source/reference/c-api/array.rst

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4077,21 +4077,6 @@ extension with the lowest :c:data:`NPY_FEATURE_VERSION` as possible.
40774077
:c:data:`NPY_FEATURE_VERSION` changes whenever the API changes (e.g. a
40784078
function is added). A changed value does not always require a recompile.
40794079
4080-
Internal Flexibility
4081-
~~~~~~~~~~~~~~~~~~~~
4082-
4083-
.. c:function:: void PyArray_SetStringFunction(PyObject* op, int repr)
4084-
4085-
This function allows you to alter the tp_str and tp_repr methods
4086-
of the array object to any Python function. Thus you can alter
4087-
what happens for all arrays when str(arr) or repr(arr) is called
4088-
from Python. The function to be called is passed in as *op*. If
4089-
*repr* is non-zero, then this function will be called in response
4090-
to repr(arr), otherwise the function will be called in response to
4091-
str(arr). No check on whether or not *op* is callable is
4092-
performed. The callable passed in to *op* should expect an array
4093-
argument and should return a string to be printed.
4094-
40954080
40964081
Memory management
40974082
~~~~~~~~~~~~~~~~~

doc/source/reference/c-api/types-and-structures.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,11 @@ The :c:data:`PyArray_Type` can also be sub-typed.
215215

216216
.. tip::
217217

218-
The ``tp_as_number`` methods use a generic approach to call whatever
219-
function has been registered for handling the operation. When the
220-
``_multiarray_umath module`` is imported, it sets the numeric operations
221-
for all arrays to the corresponding ufuncs. This choice can be changed with
222-
:c:func:`PyUFunc_ReplaceLoopBySignature` The ``tp_str`` and ``tp_repr``
223-
methods can also be altered using :c:func:`PyArray_SetStringFunction`.
218+
The :c:member:`tp_as_number <PyTypeObject.tp_as_number>` methods use
219+
a generic approach to call whatever function has been registered for
220+
handling the operation. When the ``_multiarray_umath`` module is imported,
221+
it sets the numeric operations for all arrays to the corresponding ufuncs.
222+
This choice can be changed with :c:func:`PyUFunc_ReplaceLoopBySignature`.
224223

225224
PyGenericArrType_Type
226225
---------------------

numpy/_core/_add_newdocs.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,15 +1830,6 @@
18301830
18311831
""")
18321832

1833-
1834-
add_newdoc('numpy._core.multiarray', 'set_string_function',
1835-
"""
1836-
set_string_function(f, repr=1)
1837-
1838-
Internal method to set a function to be used when pretty printing arrays.
1839-
1840-
""")
1841-
18421833
add_newdoc('numpy._core.multiarray', 'promote_types',
18431834
"""
18441835
promote_types(type1, type2)

numpy/_core/arrayprint.py

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,78 +1727,3 @@ def array_str(a, max_line_width=None, precision=None, suppress_small=None):
17271727
array2string=_array2string_impl)
17281728
_default_array_repr = functools.partial(_array_repr_implementation,
17291729
array2string=_array2string_impl)
1730-
1731-
1732-
def set_string_function(f, repr=True):
1733-
"""
1734-
Set a Python function to be used when pretty printing arrays.
1735-
1736-
.. deprecated:: 2.0
1737-
Use `np.set_printoptions` instead with a formatter for custom
1738-
printing of NumPy objects.
1739-
1740-
Parameters
1741-
----------
1742-
f : function or None
1743-
Function to be used to pretty print arrays. The function should expect
1744-
a single array argument and return a string of the representation of
1745-
the array. If None, the function is reset to the default NumPy function
1746-
to print arrays.
1747-
repr : bool, optional
1748-
If True (default), the function for pretty printing (``__repr__``)
1749-
is set, if False the function that returns the default string
1750-
representation (``__str__``) is set.
1751-
1752-
See Also
1753-
--------
1754-
set_printoptions, get_printoptions
1755-
1756-
Examples
1757-
--------
1758-
>>> from numpy._core.arrayprint import set_string_function
1759-
>>> def pprint(arr):
1760-
... return 'HA! - What are you going to do now?'
1761-
...
1762-
>>> set_string_function(pprint)
1763-
>>> a = np.arange(10)
1764-
>>> a
1765-
HA! - What are you going to do now?
1766-
>>> _ = a
1767-
>>> # [0 1 2 3 4 5 6 7 8 9]
1768-
1769-
We can reset the function to the default:
1770-
1771-
>>> set_string_function(None)
1772-
>>> a
1773-
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
1774-
1775-
`repr` affects either pretty printing or normal string representation.
1776-
Note that ``__repr__`` is still affected by setting ``__str__``
1777-
because the width of each array element in the returned string becomes
1778-
equal to the length of the result of ``__str__()``.
1779-
1780-
>>> x = np.arange(4)
1781-
>>> set_string_function(lambda x:'random', repr=False)
1782-
>>> x.__str__()
1783-
'random'
1784-
>>> x.__repr__()
1785-
'array([0, 1, 2, 3])'
1786-
1787-
"""
1788-
1789-
# Deprecated in NumPy 2.0, 2023-07-11
1790-
warnings.warn(
1791-
"`set_string_function` is deprecated. Use `np.set_printoptions` "
1792-
"with a formatter for custom printing NumPy objects. "
1793-
"(deprecated in NumPy 2.0)",
1794-
DeprecationWarning,
1795-
stacklevel=2
1796-
)
1797-
1798-
if f is None:
1799-
if repr:
1800-
return multiarray.set_string_function(_default_array_repr, 1)
1801-
else:
1802-
return multiarray.set_string_function(_default_array_str, 0)
1803-
else:
1804-
return multiarray.set_string_function(f, repr)

numpy/_core/src/multiarray/multiarraymodule.c

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3172,31 +3172,6 @@ array__reconstruct(PyObject *NPY_UNUSED(dummy), PyObject *args)
31723172
return NULL;
31733173
}
31743174

3175-
static PyObject *
3176-
array_set_string_function(PyObject *NPY_UNUSED(self), PyObject *args,
3177-
PyObject *kwds)
3178-
{
3179-
PyObject *op = NULL;
3180-
int repr = 1;
3181-
static char *kwlist[] = {"f", "repr", NULL};
3182-
3183-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:set_string_function", kwlist, &op, &repr)) {
3184-
return NULL;
3185-
}
3186-
/* reset the array_repr function to built-in */
3187-
if (op == Py_None) {
3188-
op = NULL;
3189-
}
3190-
if (op != NULL && !PyCallable_Check(op)) {
3191-
PyErr_SetString(PyExc_TypeError,
3192-
"Argument must be callable.");
3193-
return NULL;
3194-
}
3195-
PyArray_SetStringFunction(op, repr);
3196-
Py_RETURN_NONE;
3197-
}
3198-
3199-
32003175
static PyObject *
32013176
array_set_datetimeparse_function(PyObject *NPY_UNUSED(self),
32023177
PyObject *NPY_UNUSED(args), PyObject *NPY_UNUSED(kwds))
@@ -4413,9 +4388,6 @@ static struct PyMethodDef array_module_methods[] = {
44134388
{"_reconstruct",
44144389
(PyCFunction)array__reconstruct,
44154390
METH_VARARGS, NULL},
4416-
{"set_string_function",
4417-
(PyCFunction)array_set_string_function,
4418-
METH_VARARGS|METH_KEYWORDS, NULL},
44194391
{"set_datetimeparse_function",
44204392
(PyCFunction)array_set_datetimeparse_function,
44214393
METH_VARARGS|METH_KEYWORDS, NULL},

numpy/_core/src/multiarray/strfuncs.c

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
#include "npy_import.h"
1010
#include "strfuncs.h"
1111

12-
static PyObject *PyArray_StrFunction = NULL;
13-
static PyObject *PyArray_ReprFunction = NULL;
14-
15-
1612
static void
1713
npy_PyErr_SetStringChained(PyObject *type, const char *message)
1814
{
@@ -30,34 +26,14 @@ npy_PyErr_SetStringChained(PyObject *type, const char *message)
3026
NPY_NO_EXPORT void
3127
PyArray_SetStringFunction(PyObject *op, int repr)
3228
{
33-
if (repr) {
34-
/* Dispose of previous callback */
35-
Py_XDECREF(PyArray_ReprFunction);
36-
/* Add a reference to new callback */
37-
Py_XINCREF(op);
38-
/* Remember new callback */
39-
PyArray_ReprFunction = op;
40-
}
41-
else {
42-
/* Dispose of previous callback */
43-
Py_XDECREF(PyArray_StrFunction);
44-
/* Add a reference to new callback */
45-
Py_XINCREF(op);
46-
/* Remember new callback */
47-
PyArray_StrFunction = op;
48-
}
29+
PyErr_SetString(PyExc_ValueError, "PyArray_SetStringFunction was removed");
4930
}
5031

5132

5233
NPY_NO_EXPORT PyObject *
5334
array_repr(PyArrayObject *self)
5435
{
5536
static PyObject *repr = NULL;
56-
57-
if (PyArray_ReprFunction != NULL) {
58-
return PyObject_CallFunctionObjArgs(PyArray_ReprFunction, self, NULL);
59-
}
60-
6137
/*
6238
* We need to do a delayed import here as initialization on module load
6339
* leads to circular import problems.
@@ -76,11 +52,6 @@ NPY_NO_EXPORT PyObject *
7652
array_str(PyArrayObject *self)
7753
{
7854
static PyObject *str = NULL;
79-
80-
if (PyArray_StrFunction != NULL) {
81-
return PyObject_CallFunctionObjArgs(PyArray_StrFunction, self, NULL);
82-
}
83-
8455
/*
8556
* We need to do a delayed import here as initialization on module load leads
8657
* to circular import problems.

numpy/_core/tests/test_multiarray.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,16 +586,16 @@ def assign(v):
586586
)
587587
def test_unicode_assignment(self):
588588
# gh-5049
589-
from numpy._core.arrayprint import set_string_function
589+
from numpy._core.arrayprint import set_printoptions
590590

591591
@contextmanager
592592
def inject_str(s):
593593
""" replace ndarray.__str__ temporarily """
594-
set_string_function(lambda x: s, repr=False)
594+
set_printoptions(formatter={"all": lambda x: s})
595595
try:
596596
yield
597597
finally:
598-
set_string_function(None, repr=False)
598+
set_printoptions()
599599

600600
a1d = np.array(['test'])
601601
a0d = np.array('done')

numpy/_core/tests/test_numeric.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import numpy as np
1010
from numpy._core import umath, sctypes
1111
from numpy._core.numerictypes import obj2sctype
12-
from numpy._core.arrayprint import set_string_function
1312
from numpy.exceptions import AxisError
1413
from numpy.random import rand, randint, randn
1514
from numpy.testing import (
@@ -3582,24 +3581,6 @@ def test_list(self):
35823581
assert_equal(np.argwhere([4, 0, 2, 1, 3]), [[0], [2], [3], [4]])
35833582

35843583

3585-
@pytest.mark.filterwarnings(
3586-
"ignore:.*set_string_function.*:DeprecationWarning"
3587-
)
3588-
class TestStringFunction:
3589-
3590-
def test_set_string_function(self):
3591-
a = np.array([1])
3592-
set_string_function(lambda x: "FOO", repr=True)
3593-
assert_equal(repr(a), "FOO")
3594-
set_string_function(None, repr=True)
3595-
assert_equal(repr(a), "array([1])")
3596-
3597-
set_string_function(lambda x: "FOO", repr=False)
3598-
assert_equal(str(a), "FOO")
3599-
set_string_function(None, repr=False)
3600-
assert_equal(str(a), "[1]")
3601-
3602-
36033584
class TestRoll:
36043585
def test_roll1d(self):
36053586
x = np.arange(10)

0 commit comments

Comments
 (0)