GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: Fix buffered iterator stride after removing multi-index by snoopuppy582 · Pull Request #31985 · numpy/numpy

Conversation

@snoopuppy582

@snoopuppy582 snoopuppy582 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

seberg reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@snoopuppy582
BUG: Fix buffered iterator stride after removing multi-index
fdfde66
@tylerjereddy tylerjereddy added the 00 - Bug label Jul 13, 2026
tylerjereddy
tylerjereddy reviewed Jul 14, 2026
View reviewed changes
Comment thread numpy/_core/src/multiarray/nditer_constr.c
if ((itflags & NPY_ITFLAG_HASMULTIINDEX) &&
NAD_SHAPE(axisdata) == 1 && op_stride == 0) {
op_stride = NPY_MAX_INTP;
}

@tylerjereddy tylerjereddy Jul 14, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Just for a small contribution to the code review here -- I confirmed that both of the added regression tests fail locally on ARM Mac when I revert this source patch. The full testsuite is passing for me locally on this branch.

The patch does appear to be focused around a real world failure scenario, though it does seem a little obscure and hard to follow.

Probably a more active NumPy core dev should take a look--the patch is relatively small, but the team is getting pretty inundated at the moment.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

seberg and snoopuppy582 reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
@seberg seberg added the 09 - Backport-Candidate PRs tagged should be backported label Jul 15, 2026
seberg
seberg approved these changes Jul 15, 2026
View reviewed changes

@seberg seberg left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks, looks right (wonder if we could merge it with the if/else above, but doesn't matter). Pretty bad embarrassing issue :(.

I'll approve, please go ahead to merge anyone (I'll very soon). I wish we could delay this part, but I don't think it is obvious.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mattip

mattip commented Jul 15, 2026

Copy link
Copy Markdown
Member

I wish we could go back in time and remove or at least vastly simplify the python-level nditer. But we can't so in it goes.

seberg reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mattip
mattip merged commit d762125 into numpy:main Jul 15, 2026
86 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@mattip

mattip commented Jul 15, 2026

Copy link
Copy Markdown
Member

Thanks @snoopuppy582

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@charris charris mentioned this pull request Jul 27, 2026
BUG: Fix buffered iterator stride after removing multi-index #32116
Merged
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@tylerjereddy tylerjereddy tylerjereddy left review comments
@seberg seberg seberg approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

00 - Bug

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

BUG: unravel_index returns incorrect coordinates for large (N, 1) index arrays across an iterator buffer boundary

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@snoopuppy582 @mattip @seberg @tylerjereddy @charris
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.