Skip to content

BUG: fix swallowed cast error in fancy indexing assignment#31975

Open
Ijtihed wants to merge 1 commit into
numpy:mainfrom
Ijtihed:fix/mapiter-buffered-cast-error-leak
Open

BUG: fix swallowed cast error in fancy indexing assignment#31975
Ijtihed wants to merge 1 commit into
numpy:mainfrom
Ijtihed:fix/mapiter-buffered-cast-error-leak

Conversation

@Ijtihed

@Ijtihed Ijtihed commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

PR summary

Fixes #31974.

arr[idx] = vals can swallow a cast error when the values are buffered and the cast fails while the iterator refills a buffer chunk (array bigger than one 8192-element chunk). the buffered iternext returns 0 on error which looks exactly like a normal end of iteration, so mapiter_set/mapiter_get returned success with the exception still set. depending on what runs next that's either a silent bogus success (the error surfaces at a later unrelated call) or SystemError: ... returned a result with an exception set (e.g. via np.put_along_axis).

this adds a PyErr_Occurred() check after the iteration loops in mapiter_@name@. small arrays already raise cleanly since first-chunk failures are caught at NpyIter_Reset so this just makes big arrays consistent. kept the check unconditional (not needs_api-gated) because non-object casts can fail too, e.g. a string -> int cast hits the same leak

AI Disclosure

Used Claude to double check my work and mae sure I didn't miss anything

* (the buffered iternext), which then returns 0 exactly as on a
* normal end of iteration, so the loops above can finish "cleanly"
* with the error still set. Check for it here.
*/

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 sort of comment should be maximum one-line. AI models like to memorialize bugs and if we add comments like this for a few years then the codebase will be riddled with references to old bugs for no benefit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: cast error during buffered fancy-index assignment is swallowed (SystemError or silently leaked exception)

2 participants