mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-13 09:03:54 +00:00
numpy: fix refcount leak to dtype singleton (#1860)
PyArray_DescrFromType returns a new reference, not borrowed one
This commit is contained in:
parent
4a3464fd88
commit
c9d32a81f4
@ -1044,7 +1044,7 @@ public:
|
|||||||
|
|
||||||
static pybind11::dtype dtype() {
|
static pybind11::dtype dtype() {
|
||||||
if (auto ptr = npy_api::get().PyArray_DescrFromType_(value))
|
if (auto ptr = npy_api::get().PyArray_DescrFromType_(value))
|
||||||
return reinterpret_borrow<pybind11::dtype>(ptr);
|
return reinterpret_steal<pybind11::dtype>(ptr);
|
||||||
pybind11_fail("Unsupported buffer format!");
|
pybind11_fail("Unsupported buffer format!");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -434,3 +434,14 @@ def test_array_create_and_resize(msg):
|
|||||||
def test_index_using_ellipsis():
|
def test_index_using_ellipsis():
|
||||||
a = m.index_using_ellipsis(np.zeros((5, 6, 7)))
|
a = m.index_using_ellipsis(np.zeros((5, 6, 7)))
|
||||||
assert a.shape == (6,)
|
assert a.shape == (6,)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.unsupported_on_pypy
|
||||||
|
def test_dtype_refcount_leak():
|
||||||
|
from sys import getrefcount
|
||||||
|
dtype = np.dtype(np.float_)
|
||||||
|
a = np.array([1], dtype=dtype)
|
||||||
|
before = getrefcount(dtype)
|
||||||
|
m.ndim(a)
|
||||||
|
after = getrefcount(dtype)
|
||||||
|
assert after == before
|
||||||
|
Loading…
Reference in New Issue
Block a user