Skip to content

BUG: incorrect temp elision for new-style (NEP 43) user-defined dtypes (#31193)#31329

Merged
charris merged 1 commit into
numpy:maintenance/2.4.xfrom
charris:backport-31193
Apr 25, 2026
Merged

BUG: incorrect temp elision for new-style (NEP 43) user-defined dtypes (#31193)#31329
charris merged 1 commit into
numpy:maintenance/2.4.xfrom
charris:backport-31193

Conversation

@charris
Copy link
Copy Markdown
Member

@charris charris commented Apr 25, 2026

Backport of #31193.

PR summary

can_elide_temp() in temp_elide.c incorrectly identifies new-style user-defined dtypes as numeric types eligible for in-place buffer reuse. For large arrays this silently rewrites a*a + b*b into (a*a) += (b*b), which raises a TypeError when the result dtype of the in-place add does not match the pre-allocated buffer.

I encountered this bug while implementing a custom fixed-point dtype, which automatically increases its bit width on every operation as required to avoid overflow. Since addition increases the bit width by one, attempting to reuse the temporary buffer results in an incompatible destination dtype being provided to the add ufunc and raises a TypeError.

The root cause is that PyArray_ISNUMBER evaluates to true for new-style user dtypes since it checks (type) <= NPY_CLONGDOUBLE and type_num = -1 for these types.

This could be fixed either in PyArray_ISNUMBER (probably more correct, but may have unexpected side effects) or with a minimal extra check in can_elide_temp (the approach used by the PR). Let me know if you'd like me to fix PyArray_ISNUMBER instead.

AI Disclosure

This bug was identified and fixed by Claude Sonnet 4.6, and then reviewed by me.
Full AI-generated explanation: https://gist.github.com/MaartenBaert/7540176c4005d26a0b292baefbec8519

@charris charris added this to the 2.4.5 release milestone Apr 25, 2026
@charris charris added 00 - Bug 08 - Backport Used to tag backport PRs labels Apr 25, 2026
@charris charris merged commit b3ecd15 into numpy:maintenance/2.4.x Apr 25, 2026
73 of 74 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 08 - Backport Used to tag backport PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants