Repository navigation
BUG: fix str_ and bytes_ scalars not supporting [()] indexing - #31796
Mohataseem89 wants to merge 5 commits into
Conversation
|
I converted this to draft because the CI results indicate that this is jot yet fully baked. |
|
The CI failures here are unrelated to this change. i traced the build failure locally |
|
No other PRs are failing, you're wrong about that. Your local build is failing because you don't have the git submodules checked out. |
|
@ngoldbaum i havve updated the fix. the previous version had an unsafe cast from |
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]>
20ba59e to
5488b77
Compare
|
But did you run the tests? All the test failures in CI are real. Please make sure the tests pass on your local clone of NumPy before doing further work here. |
You are right confirmed the test failures are real found it: my fallback path was routing all indexing through 0-d array subscript instead of falling back to str/bytes's native |
|
The one remaining debug interpreter crash is still real |
…h in string-to-bool cast
Fixes #31435
Summary
numpy.str_andnumpy.bytes_inherit Python'sstr.__getitem__andbytes.__getitem__, which intercept[()](empty tuple) before NumPy's0-d scalar indexing protocol, raising
TypeErrorinstead of returningthe scalar value. This is inconsistent with all other NumPy scalar types
where
scalar[()]works correctlyRoot cause
PyStringArrType_TypeandPyUnicodeArrType_Typehad notp_as_mappingslot, so Python's built-in string subscript ran instead of NumPy's generic
scalar handler
Fix
Added
stringtype_subscriptinscalartypes.c.srcthat intercepts theempty-tuple case and delegates to
.item(), with fallback toobject_arrtype_subscriptfor other keys. Both string types now havetp_as_mapping = &stringtype_as_mappingset ininitialize_numeric_typesTests
all assertions pass on the patched build