Skip to content

Commit

Permalink
Merge pull request #25950 from seberg/clean-field-userdtype
Browse files Browse the repository at this point in the history
BUG: Fix reference leak in niche user old user dtypes
  • Loading branch information
ngoldbaum committed Mar 6, 2024
2 parents 955d980 + 65af6e6 commit dff8cbb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion numpy/_core/src/multiarray/usertypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ PyArray_RegisterDataType(PyArray_DescrProto *descr_proto)
NPY_DT_SLOTS(NPY_DTYPE(descr))->get_clear_loop = (
(PyArrayMethod_GetTraverseLoop *)&npy_get_clear_void_and_legacy_user_dtype_loop);
/* Also use the void zerofill since there may be objects */
NPY_DT_SLOTS(NPY_DTYPE(descr))->get_clear_loop = (
NPY_DT_SLOTS(NPY_DTYPE(descr))->get_fill_zero_loop = (
(PyArrayMethod_GetTraverseLoop *)&npy_get_zerofill_void_and_legacy_user_dtype_loop);
}

Expand Down
7 changes: 7 additions & 0 deletions numpy/_core/tests/test_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -1897,6 +1897,13 @@ class mytype:
# unnecessary restriction, but one that has been around forever:
assert np.dtype(mytype) == np.dtype("O")

if HAS_REFCOUNT:
# Create an array and test that memory gets cleaned up (gh-25949)
o = object()
a = np.array([o], dtype=dt)
del a
assert sys.getrefcount(o) == 2

def test_custom_structured_dtype_errors(self):
class mytype:
pass
Expand Down

0 comments on commit dff8cbb

Please sign in to comment.