Skip to content

Commit 3dd9dba

Browse files
authored
Merge pull request numpy#24270 from seberg/void-scalar-legacy
BUG: Move legacy check for void printing
2 parents e14d9c8 + d624b62 commit 3dd9dba

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

numpy/core/arrayprint.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,10 @@ def _void_scalar_to_string(x, is_repr=True):
14151415
formatters defined above.
14161416
"""
14171417
options = _format_options.copy()
1418+
1419+
if options["legacy"] <= 125:
1420+
return StructuredVoidFormat.from_data(array(x), **_format_options)(x)
1421+
14181422
if options.get('formatter') is None:
14191423
options['formatter'] = {}
14201424
options['formatter'].setdefault('float_kind', str)

numpy/core/src/multiarray/scalartypes.c.src

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,8 @@ voidtype_repr(PyObject *self)
622622
{
623623
PyVoidScalarObject *s = (PyVoidScalarObject*) self;
624624
if (PyDataType_HASFIELDS(s->descr)) {
625-
/* use string on old versions */
626-
return _void_scalar_to_string(self, npy_legacy_print_mode > 125);
625+
/* Python helper checks for the legacy mode printing */
626+
return _void_scalar_to_string(self, 1);
627627
}
628628
if (npy_legacy_print_mode > 125) {
629629
return _void_to_hex(s->obval, s->descr->elsize, "np.void(b'", "\\x", "')");

numpy/core/tests/test_arrayprint.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,9 @@ def test_scalar_repr_numbers(dtype, value):
10841084
(np.void((True, 2), dtype="?,<i8"),
10851085
"(True, 2)",
10861086
"np.void((True, 2), dtype=[('f0', '?'), ('f1', '<i8')])"),
1087+
(np.void((1, 2), dtype="<f8,>f4"),
1088+
"(1., 2.)",
1089+
"np.void((1.0, 2.0), dtype=[('f0', '<f8'), ('f1', '>f4')])"),
10871090
(np.void(b'a'), r"void(b'\x61')", r"np.void(b'\x61')"),
10881091
])
10891092
def test_scalar_repr_special(scalar, legacy_repr, representation):
@@ -1092,3 +1095,10 @@ def test_scalar_repr_special(scalar, legacy_repr, representation):
10921095

10931096
with np.printoptions(legacy="1.25"):
10941097
assert repr(scalar) == legacy_repr
1098+
1099+
def test_scalar_void_float_str():
1100+
# Note that based on this currently we do not print the same as a tuple
1101+
# would, since the tuple would include the repr() inside for floats, but
1102+
# we do not do that.
1103+
scalar = np.void((1.0, 2.0), dtype=[('f0', '<f8'), ('f1', '>f4')])
1104+
assert str(scalar) == "(1.0, 2.0)"

0 commit comments

Comments
 (0)