Skip to content

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

Closed

Description

@Ijtihed

Describe the issue:

If arr[idx] = values needs a buffered cast and the cast fails in a buffer chunk after the first one (array > 8192 elements), the error is set but the assignment reports success. Result: the statement silently "succeeds", dst is partially written, and the leaked exception surfaces at a later unrelated call (or as SystemError: ... returned a result with an exception set when it crosses a function-call boundary, e.g. inside np.put_along_axis).

Small arrays raise a clean TypeError for the same assignment, so behavior is size-dependent. Not object-specific: a failing string cast (svals[9000] = "xx" into an int array) leaks the same way.

Root cause: a cast failure during buffer refill makes npyiter_buffered_iternext return 0, indistinguishable from normal end of iteration (nditer_templ.c.src ~line 248). mapiter_set loops do {...} while (mit->outer_next(...)) and only checks PyErr_Occurred() at the top of the loop body, so the error set by the final iternext is never seen; the function returns 0 (lowlevel_strided_loops.c.src:2010-2157), and array_assign_subscript (mapping.c:2175) reports success.

Reproduce the code example:

import numpy as np

N = 20000  # > 8192 (one buffer chunk)
dst = np.zeros(N, dtype=int)
vals = np.arange(N, dtype=object)
vals[9000] = None  # in the second chunk

dst[np.arange(N)] = vals   # does NOT raise
print("got here")          # leaked TypeError detonates here or later

Error message:

got here
Traceback (most recent call last):
  File "repro.py", line 9, in <module>
    print("got here")
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

Same assignment via np.put_along_axis raises immediately instead, but as:
SystemError: <function put_along_axis at 0x...> returned a result with an exception set

dst has the first 8192 elements written, rest untouched. The partial write is
expected (see gh-31965 / gh-31940); this issue is only the swallowed exception.

Python and NumPy Versions:

NumPy 2.2.4, 2.5.1, and main @ 9ef40d3. Python 3.13.5, Linux x86_64 (Debian 13).

Runtime Environment:

No response

How does this issue affect you or how did you find it:

Found while auditing partial-write behavior for gh-31965.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions