Skip to content

Reference count issue #521

@SUSYUSTC

Description

@SUSYUSTC

I expected that numexpr.evaluate(xxx, out=array) should write data to array, and eventually it like a fast version of array[:] = xxx, so it should not change the reference count. However, in the following case I expect that del B in the second case should decrease the reference count by 1, but it's not changed. Why does it happen and how to fix it?

import sys
import numpy as np
import numexpr

A = np.zeros(10000)
B = A.reshape((100, 100))
B[:] = 1
print(sys.getrefcount(A), np.sum(A))
del B
print(sys.getrefcount(A), np.sum(A))

r = np.random.random((100, 100))
A = np.zeros(10000)
B = A.reshape((100, 100))
numexpr.evaluate("r + 1", local_dict={'r': r}, global_dict={}, out=B)
print(sys.getrefcount(A), np.sum(A))
del B
print(sys.getrefcount(A), np.sum(A))

The output is

3 10000.0
2 10000.0
3 14951.218372702608
3 14951.218372702608

versions:
numexpr: 2.11.0
numpy: 2.2.0
python: 3.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions