Skip to content

BUG: Fix buffered iterator stride after removing multi-index - #32116

Merged
charris merged 1 commit into
numpy:maintenance/2.5.xfrom
charris:backport-31985
Jul 27, 2026
Merged

BUG: Fix buffered iterator stride after removing multi-index#32116
charris merged 1 commit into
numpy:maintenance/2.5.xfrom
charris:backport-31985

Conversation

@charris

@charris charris commented Jul 27, 2026

Copy link
Copy Markdown
Member

Backport of #31985.

PR summary

This PR fixes a critical bug where incorrect coordinates are returned after the iterator buffer boundary for large (N, 1) index arrays. As seen in real-world applications (e.g., MRI reconstruction in mckib2/pygrappa#107), this can silently produce incorrect results without raising any exceptions.

Root Cause:
The issue occurs because the transfer loop is fixed to a zero stride for a size-one inner axis, failing to reflect the updated runtime stride after remove_multi_index is applied.

Fix:
To resolve this, I modified numpy/_core/src/multiarray/nditer_constr.c to exclude mutable strides from the fixed-stride optimization.

Testing & Verification:

  • Added generic nditer regression tests in numpy/_core/tests/test_nditer.py.
  • Added public unravel_index regression tests in numpy/lib/tests/test_index_tricks.py.
  • I have manually run and confirmed that all core tests and linting pass successfully locally.

Closes #31980

First time committer introduction

Hi, I am snoopuppy582, a developer using NumPy. I investigated this issue because of its silent but critical impact on real-world calculations, and I hope this patch helps improve the stability of the library.

AI Disclosure

I used AI tools (OpenAI Codex / ChatGPT) strictly for review and verification purposes to ensure the quality of the fix.

I manually analyzed the root cause, wrote the C modifications and regression tests, and executed all local validations. I have fully reviewed and understood the entire diff. Also, this PR description, the commit messages, and all future communications are written entirely by me manually.

@charris charris added this to the 2.5.2 release milestone Jul 27, 2026
@charris charris added 00 - Bug 08 - Backport Used to tag backport PRs labels Jul 27, 2026
@charris
charris merged commit b63bc77 into numpy:maintenance/2.5.x Jul 27, 2026
84 of 85 checks passed
@charris
charris deleted the backport-31985 branch July 27, 2026 17:01
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