Skip to content

MAINT: parse dtype endian as unicode directly (gh-15317) - #32353

Merged
ngoldbaum merged 3 commits into
numpy:mainfrom
nakasan617:maint-descriptor-endian-unicode
Aug 20, 2026
Merged

ngoldbaum merged 3 commits into
numpy:mainfrom
nakasan617:maint-descriptor-endian-unicode

Conversation

@nakasan617

@nakasan617 nakasan617 commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Part of gh-15317, following #32232 (nditer flags) and #32305 (ndarray.flags keys).
(I realized that the issue is already closed, but I think we can still work on this one as the last one of the issue)

We are trying to replace PyUnicode_AsASCIIString which throws UnicodeEncodeError, but should rather be ValueError (or something that better describes the error in each context). This function also have a side effect of needing to allocate temporary memory for python string objects. In this pr, we're working on arraydescr_setstate, which looks into the byte order (e.g. little endian) when we are depickling (i.e. from byte to obj) and parses them . We change whenever the non ASCII is passed to give a ValueError instead of UnicodeEncodeError.

We added the tests to verify that it still reads the byte string correctly, throws the right error (i.e. ValueError) with correct error messages.

The release note will be bundled together for the same issue (with multiple PRs) as discussed in the previous PR.

AI Disclosure

Code, tests are AI-generated (Claude model Opus). PR summaries and comments are mine. I've looked into the code extensively, so I should know what's going on fully.

@nakasan617
nakasan617 force-pushed the maint-descriptor-endian-unicode branch from 4d5c852 to 5b30924 Compare August 19, 2026 21:43
@nakasan617
nakasan617 force-pushed the maint-descriptor-endian-unicode branch from 5b30924 to fbdd0b9 Compare August 20, 2026 01:16
@nakasan617
nakasan617 marked this pull request as ready for review August 20, 2026 03:20

@ngoldbaum ngoldbaum left a comment

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.

A few comments, thanks for pushing on this!

Comment thread numpy/_core/tests/test_dtype.py
Comment thread numpy/_core/src/multiarray/descriptor.c
Comment thread numpy/_core/tests/test_dtype.py Outdated
Comment thread numpy/_core/tests/test_dtype.py Outdated
@nakasan617
nakasan617 marked this pull request as draft August 20, 2026 19:48
nakasan617 and others added 2 commits August 20, 2026 14:54
Co-authored-by: Nathan Goldbaum <[email protected]>
Address review: PyUnicode_AsUTF8AndSize still raises UnicodeEncodeError
for lone surrogates, so gate on PyUnicode_IS_ASCII and fall through to
the existing "endian is not 1-char string" error instead.

Also build the dtype inside test_setstate_endian so __setstate__ does
not mutate an object shared between parametrized runs.
@ngoldbaum
ngoldbaum marked this pull request as ready for review August 20, 2026 20:34
@ngoldbaum

Copy link
Copy Markdown
Member

Thanks @nakasan617!

@ngoldbaum
ngoldbaum merged commit a7d83b9 into numpy:main Aug 20, 2026
91 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants