Skip to content

ENH: add no-op fallback in np.byteswap for types with no byte order - #32150

Merged
ngoldbaum merged 8 commits into
numpy:mainfrom
ngoldbaum:fix-byteswap-segfault
Aug 12, 2026
Merged

ENH: add no-op fallback in np.byteswap for types with no byte order#32150
ngoldbaum merged 8 commits into
numpy:mainfrom
ngoldbaum:fix-byteswap-segfault

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Jul 30, 2026

Copy link
Copy Markdown
Member

PR summary

This adds a no-op fallback for dtypes that declare no particular byte order ("|"). IMO this is a sensible default for these dtypes.

To make e.g. QuadDType work properly here we'll need to add an API hook that allows a user dtype to define a byteswapping operation. Punting on that for now.

AI Disclosure

I used an AI model to assist with debugging this change.

@ngoldbaum ngoldbaum added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Jul 30, 2026
@ngoldbaum
ngoldbaum marked this pull request as draft July 31, 2026 15:57
@ngoldbaum

Copy link
Copy Markdown
Member Author

I marked this as draft because I want to do this after #32151 merges now. This PR will contain the "new" allowed behaviors, once we decide on them. I think only error make sense for a backport.

@ikrommyd ikrommyd 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.

Looks good to me overall. Got a release note change request + a suggestion to fix a pre-existing bug.

Comment on lines +1 to +4
* ``ndarray.byteswap`` and ``numpy.byteswap`` now support dtypes that have no
defined byte order (e.g. when ``dtype.byteorder == "|"``). This includes NumPy's
string dtypes. Such values are left unchanged, including when they occur in
structured dtypes.

@ikrommyd ikrommyd Aug 8, 2026

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.

This needs a different format with a title right? + numpy.byteswap does not exist.

Suggested change
* ``ndarray.byteswap`` and ``numpy.byteswap`` now support dtypes that have no
defined byte order (e.g. when ``dtype.byteorder == "|"``). This includes NumPy's
string dtypes. Such values are left unchanged, including when they occur in
structured dtypes.
Byteswapping dtypes without a defined byte order is now supported as as no-op
-----------------------------------------------------------------------------
``ndarray.byteswap`` now supports dtypes that have no defined byte order
(e.g. when ``dtype.byteorder == "|"``). This includes NumPy's string dtypes.
Such values are left unchanged, including when they occur in structured dtypes.

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.

This needs a different format with a title right?

You don't need to use a title, no. We also have all_bullets = false in our towncrier config:

all_bullets = false

So if I explicitly add a bullet like this, it'll render as one of a number of bulleted bugfixes. I don't think this is worth a whole heading in the release notes, personally.

Thanks for catching my using the wrong name!

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.

Ah I was not aware of the no-heading formatting. numpy.byteswap is the only valid comment then here.

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.

we should probably add some text about it to the README we have in the release notes folder.

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 also loosened the text about string dtypes here. Of course the UCS-4 unicode dtype is byteswappable.

Comment thread numpy/_core/src/multiarray/scalartypes.c.src
Comment thread numpy/_core/src/multiarray/scalartypes.c.src Outdated

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Had a decent go-over and this looks all fine (I would not have picked up on the VOID/etc asserts...).

@ikrommyd ikrommyd 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.

I think only the numpy.byteswap thing on the release note comment is left here. Otherwise good to go from my side. CI needs to run though.

@ikrommyd ikrommyd 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.

If CI is happy, I'm happy :)

@ngoldbaum ngoldbaum removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Aug 12, 2026
@ngoldbaum
ngoldbaum merged commit ea907a8 into numpy:main Aug 12, 2026
91 checks passed
@ngoldbaum

Copy link
Copy Markdown
Member Author

Thanks for the review all!

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.

3 participants