-
-
Notifications
You must be signed in to change notification settings - Fork 11.6k
Open
Description
Describe the issue:
While investigating array.resize
in the context of #29878, I noticed that it seemed not to worry at all about the array containing references. This is fine if the array is expanded, but not if it is shrunk.
I think in principle, it is not difficult to solve, though definitely annoying, since an object can be inside a structured array too. Possibly, it makes more sense just to forbid resizing of arrays that hold references.
Reproduce the code example:
import numpy as np
import sys
import gc
a = object()
b = np.array([a, a])
sys.getrefcount(a)
# 4
b.resize(1)
gc.collect()
sys.getrefcount(a)
# 4
del b
gc.collect()
sys.getrefcount(a)
# 3 (without the resize, this correctly states 2)
Error message:
Python and NumPy Versions:
2.2.4
3.13.7 (main, Aug 20 2025, 22:17:40) [GCC 14.3.0]
Runtime Environment:
Not relevant
Context for the issue:
No response