Skip to content

BUG: Choose (and probably more item selection) leaks references with out= #22683

@seberg

Description

@seberg

Describe the issue:

np.choose internally uses raw access to the array data and a final PyArray_INCREF. That somewhat works generally, but if out= is specified, that result array would need to be cleared first.

The short-term solution here is likely that we need to use the element copy function rather than memcpy when necessary (elsewhere, we currently use copyswap if the dtype flags "needs API").

I suspect, we need to keep something like copyswap, but I need to also think about how to evolve that function.

Reproduce the code example:

import sys
import numpy as np
a = np.ones(10000, dtype=object)
sys.getrefcount(1)
np.choose(np.zeros(10000, dtype=int), [a], out=a)
sys.getrefcount(1)
np.choose(np.zeros(10000, dtype=int), [a], out=a)
sys.getrefcount(1)

# Prints (increasing by 10000 every time):
# 290281
# 300281
# 310281

Error message:

No response

NumPy/Python version information:

NumPy 1.25 and earlier

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions