Skip to content

DOC: document caveats of ndarray.resize on 3.14 and newer#31021

Merged
charris merged 8 commits into
numpy:mainfrom
ngoldbaum:resize-refcheck-fix
Mar 24, 2026
Merged

DOC: document caveats of ndarray.resize on 3.14 and newer#31021
charris merged 8 commits into
numpy:mainfrom
ngoldbaum:resize-refcheck-fix

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Mar 16, 2026

Copy link
Copy Markdown
Member

Closes #30991.

This doesn't "fix" the regression, because I don't think there's a way to do that without introducing new bugs or depending on interpreter internals and/or trying to argue for yet another PyUnstable C API function we can use.

I'm also a little concerned that the old check is actually incorrect for arrays created by C extensions: the check on 3.13 and older allows resizing an array created directly via e.g. PyArray_FromAny with exactly one reference.

IMO it's better to keep the logic of the check the same and simply improve the documentation and error messages.

Now when the reference count is exactly 2, the error message on 3.14 and newer explains that this might be a false positive.

I added tests which trigger the behaviors we expect to happen on different Python versions. I also deleted some out-of-date docs about an unused order argument and added some notes that resizing in-place can lead to memory fragmentation and worse overall memory usage.

Also looking for any ideas for ways to make the error messages and docstrings less wordy.

@ngoldbaum ngoldbaum requested a review from mattip March 16, 2026 18:59
@ngoldbaum ngoldbaum added 04 - Documentation 09 - Backport-Candidate PRs tagged should be backported labels Mar 16, 2026
@ngoldbaum ngoldbaum marked this pull request as draft March 16, 2026 20:03
@ngoldbaum

Copy link
Copy Markdown
Member Author

Marking as a draft while I consider Sam's suggestion: #30991 (comment)

@mattip mattip left a comment

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.

Thanks. I think this is useful even if it turns out we can work around CPython refcounting fragility. We may need to adjust the post-3.13 comment depending on the outcome of the other work.

Comment thread doc/source/reference/c-api/array.rst Outdated
reference to be resized, because it is impossible to differentiate between
an array with one reference created via an extension and a uniquely
referenced array defined in a Python function. On Python 3.14 and newer,
the array must be uniquely referenced.

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.

I find this a bit confusing. Maybe "On Python 3.13 and older, the check allows objects with exactly one reference to be reallocated in-place. On Python 3.14 and newer, the array must be uniquely referenced, which is getting progressively more complicated to detect.

Comment thread doc/source/reference/c-api/array.rst Outdated
Comment thread doc/source/reference/c-api/array.rst Outdated

@mattip mattip left a comment

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.

Thanks!

Comment thread numpy/_core/src/multiarray/shape.c Outdated
Comment thread numpy/_core/src/multiarray/shape.c Outdated
* On Python 3.13 and older, the check allows objects with exactly one
* reference to be resized, because it is impossible to differentiate between
* an array with one reference created via an extension and a uniquely
* referenced array defined in a Python function.

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.

Maybe like in the docs

On Python 3.13 and older, the check allows objects with exactly one reference to be reallocate in-place. On newer versions ...

Comment thread numpy/_core/_add_newdocs.py
ngoldbaum and others added 4 commits March 20, 2026 13:23
Co-authored-by: Matti Picus <matti.picus@gmail.com>
Co-authored-by: Matti Picus <matti.picus@gmail.com>
@ngoldbaum ngoldbaum marked this pull request as ready for review March 20, 2026 20:02
@ngoldbaum

Copy link
Copy Markdown
Member Author

I think I responded to all your comments @mattip

@ngoldbaum ngoldbaum added this to the 2.4.4 release milestone Mar 20, 2026

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, I think documenting behaviour is preferred over trying to make .resize work magically. Some small comments in-line.

Comment thread doc/source/reference/c-api/array.rst
Comment thread numpy/_core/src/multiarray/shape.c Outdated
* must be uniquely referenced. In some cases this can lead to spurious
* ValueErrors on Python 3.14.
*
* On Python 3.13 and older, the check allows objects with exactly one

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Strange partial duplication with text above. Merge?

Comment thread numpy/_core/tests/test_multiarray.py
@ngoldbaum

Copy link
Copy Markdown
Member Author

I think I responded to all review comments.

@mattip mattip left a comment

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.

On tiny nit, but can be merged without it.

Comment thread numpy/_core/src/multiarray/shape.c Outdated
Co-authored-by: Matti Picus <matti.picus@gmail.com>
@charris charris merged commit a445b49 into numpy:main Mar 24, 2026
86 of 87 checks passed
@charris

charris commented Mar 24, 2026

Copy link
Copy Markdown
Member

Thanks Nathan.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: ndarray.resize() recheck argument sometimes causes incorrect ValueError with Python 3.14

4 participants