8000 BUG: shrinking an object array with .resize does not decref the shrunk items · Issue #29887 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: shrinking an object array with .resize does not decref the shrunk items  #29887

@mhvk

Description

@mhvk

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0