Skip to content

Commit 5488b77

Browse files
committed
BUG: fix str_ and bytes_ scalars not supporting [()] indexing
numpy.str_ and numpy.bytes_ inherit Python's str.__getitem__ and bytes.__getitem__, which intercept [()] (empty tuple) before NumPy's 0-d scalar indexing protocol, raising TypeError instead of returning the scalar value. Fix by adding a custom tp_as_mapping slot with stringtype_subscript that intercepts the empty-tuple case via .item(), and delegates all other keys to the generic 0-d array subscript path (PyArray_FromScalar + array_subscript) — the same pattern used by voidtype_subscript in scalartypes.c.src. Fixes: numpy#31435 Signed-off-by: Mohataseem Khan <[email protected]>
1 parent 9721c24 commit 5488b77

1 file changed

Lines changed: 23 additions & 18 deletions

File tree

‎numpy/_core/src/multiarray/scalartypes.c.src‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4217,28 +4217,27 @@ NPY_NO_EXPORT PyTypeObject PyObjectArrType_Type = {
42174217
};
42184218

42194219
static PyObject *
4220-
gen_arrtype_subscript(PyObject *self, PyObject *key)
4220+
stringtype_subscript(PyObject *self, PyObject *key)
42214221
{
4222-
/*
4223-
* Only [...], [...,<???>], [<???>, ...],
4224-
* is allowed for indexing a scalar
4225-
*
4226-
* These return a new N-d array with a copy of
4227-
* the data where N is the number of None's in <???>.
4228-
*/
4229-
PyObject *res, *ret;
4230-
4231-
res = PyArray_FromScalar(self, NULL);
4232-
4233-
ret = array_subscript((PyArrayObject *)res, key);
4234-
Py_DECREF(res);
4235-
if (ret == NULL) {
4236-
PyErr_SetString(PyExc_IndexError,
4237-
"invalid index to scalar variable.");
4222+
if (PyTuple_Check(key) && PyTuple_GET_SIZE(key) == 0) {
4223+
return PyObject_CallMethod(self, "item", NULL);
4224+
}
4225+
/* For all other keys, convert to 0-d array and use array subscript */
4226+
PyObject *res = PyArray_FromScalar(self, NULL);
4227+
if (res == NULL) {
4228+
return NULL;
42384229
}
4230+
PyObject *ret = array_subscript((PyArrayObject *)res, key);
4231+
Py_DECREF(res);
42394232
return ret;
42404233
}
42414234

4235+
static PyMappingMethods stringtype_as_mapping = {
4236+
NULL,
4237+
(binaryfunc)stringtype_subscript,
4238+
NULL,
4239+
};
4240+
42424241

42434242
/**begin repeat
42444243
* #Name = Bool,
@@ -4267,7 +4266,7 @@ NPY_NO_EXPORT PyTypeObject Py@Name@ArrType_Type = {
42674266

42684267

42694268
static PyMappingMethods gentype_as_mapping = {
4270-
.mp_subscript = (binaryfunc)gen_arrtype_subscript,
4269+
.mp_subscript = (binaryfunc)stringtype_subscript,
42714270
};
42724271

42734272

@@ -4492,9 +4491,15 @@ initialize_numeric_types(void)
44924491

44934492
PyStringArrType_Type.tp_repr = stringtype_repr;
44944493
PyStringArrType_Type.tp_str = stringtype_str;
4494+
PyStringArrType_Type.tp_as_mapping = &stringtype_as_mapping;
4495+
44954496

44964497
PyUnicodeArrType_Type.tp_repr = unicodetype_repr;
44974498
PyUnicodeArrType_Type.tp_str = unicodetype_str;
4499+
PyUnicodeArrType_Type.tp_as_mapping = &stringtype_as_mapping;
4500+
4501+
4502+
44984503

44994504
PyVoidArrType_Type.tp_methods = voidtype_methods;
45004505
PyVoidArrType_Type.tp_getset = voidtype_getsets;

0 commit comments

Comments
 (0)