Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions benchmarks/benchmarks/bench_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,15 @@ def time_add_other_and_int32arr(self, typename):
other + int32
other + int32
other + int32


class ScalarStr(Benchmark):
# Test scalar to str conversion
params = [TYPES1]
param_names = ["type"]

def setup(self, typename):
self.a = np.array([100] * 100, dtype=typename)

def time_str_repr(self, typename):
res = [str(x) for x in self.a]
38 changes: 37 additions & 1 deletion numpy/core/src/multiarray/scalartypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,43 @@ static PyObject *
genint_type_str(PyObject *self)
{
PyObject *item, *item_str;
item = gentype_generic_method(self, NULL, NULL, "item");
PyArray_Descr *descr = PyArray_DescrFromTypeObject((PyObject *)Py_TYPE(self));
void *val = scalar_value(self, descr);
switch (descr->type_num) {
case NPY_BYTE:
item = PyLong_FromLong(*(int8_t *)val);
break;
case NPY_UBYTE:
item = PyLong_FromUnsignedLong(*(uint8_t *)val);
break;
case NPY_SHORT:
item = PyLong_FromLong(*(int16_t *)val);
break;
case NPY_USHORT:
item = PyLong_FromUnsignedLong(*(uint16_t *)val);
break;
case NPY_INT:
item = PyLong_FromLong(*(int32_t *)val);
break;
case NPY_UINT:
item = PyLong_FromUnsignedLong(*(uint32_t *)val);
break;
case NPY_LONG:
item = PyLong_FromLong(*(int64_t *)val);
break;
case NPY_ULONG:
item = PyLong_FromUnsignedLong(*(uint64_t *)val);
break;
case NPY_LONGLONG:
item = PyLong_FromLongLong(*(long long *)val);
break;
case NPY_ULONGLONG:
item = PyLong_FromUnsignedLongLong(*(unsigned long long *)val);
break;
default:
item = gentype_generic_method(self, NULL, NULL, "item");
break;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A fallback for some as-yet-unknown user defined int dtype

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to generate a function for each type anyway. I don't think that str(self.item()) is a good default to begin with.

That said, I don't mind just doing this, I will just delete it again on main in the next 2 months hopefully. (The work should already be in the repr PR, and its time to push that after branching.)

}
if (item == NULL) {
return NULL;
}
Expand Down
5 changes: 3 additions & 2 deletions numpy/core/tests/test_arrayprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,7 @@ def _format_function(x):
assert_(np.array2string(s, formatter={'numpystr':lambda s: s*2}) ==
'[abcabc defdef]')


def test_structure_format(self):
def test_structure_format_mixed(self):
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I split this test into 3, since the other test functions were not related to this one. I hit this by accident when I was trying out a different version of the code and printing was failing.

dt = np.dtype([('name', np.str_, 16), ('grades', np.float64, (2,))])
x = np.array([('Sarah', (8.0, 7.0)), ('John', (6.0, 7.0))], dtype=dt)
assert_equal(np.array2string(x),
Expand Down Expand Up @@ -301,6 +300,7 @@ def test_structure_format(self):
( 'NaT',) ( 'NaT',) ( 'NaT',)]""")
)

def test_structure_format_int(self):
# See #8160
struct_int = np.array([([1, -1],), ([123, 1],)], dtype=[('B', 'i4', 2)])
assert_equal(np.array2string(struct_int),
Expand All @@ -310,6 +310,7 @@ def test_structure_format(self):
assert_equal(np.array2string(struct_2dint),
"[([[ 0, 1], [ 2, 3]],) ([[12, 0], [ 0, 0]],)]")

def test_structure_format_float(self):
# See #8172
array_scalar = np.array(
(1., 2.1234567890123456789, 3.), dtype=('f8,f8,f8'))
Expand Down