8000 BUG: reference count leak in bincount, fixes #6805 by jaimefrio · Pull Request #7445 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: reference count leak in bincount, fixes #6805 #7445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions numpy/core/src/multiarray/compiled_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,9 @@ minmax(const npy_intp *data, npy_intp data_len, npy_intp *mn, npy_intp *mx)
NPY_NO_EXPORT PyObject *
arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
{
PyArray_Descr *type;
PyObject *list = NULL, *weight=Py_None, *mlength=Py_None;
PyArrayObject *lst=NULL, *ans=NULL, *wts=NULL;
npy_intp *numbers, *ians, len , mx, mn, ans_size, minlength;
PyObject *list = NULL, *weight = Py_None, *mlength = Py_None;
PyArrayObject *lst = NULL, *ans = NULL, *wts = NULL;
npy_intp *numbers, *ians, len, mx, mn, ans_size, minlength;
npy_intp i;
double *weights , *dans;
static char *kwlist[] = {"list", "weights", "minlength", NULL};
Expand All @@ -110,7 +109,6 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
goto fail;
}
len = PyArray_SIZE(lst);
type = PyArray_DescrFromType(NPY_INTP);

if (mlength == Py_None) {
minlength = 0;
Expand All @@ -128,14 +126,15 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)

/* handle empty list */
if (len == 0) {
if (!(ans = (PyArrayObject *)PyArray_Zeros(1, &minlength, type, 0))){
ans = (PyArrayObject *)PyArray_ZEROS(1, &minlength, NPY_INTP, 0);
if (ans == NULL){
goto fail;
}
Py_DECREF(lst);
return (PyObject *)ans;
}

numbers = (npy_intp *) PyArray_DATA(lst);
numbers = (npy_intp *)PyArray_DATA(lst);
minmax(numbers, len, &mn, &mx);
if (mn < 0) {
PyErr_SetString(PyExc_ValueError,
Expand All @@ -149,11 +148,11 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
}
}
if (weight == Py_None) {
ans = (PyArrayObject *)PyArray_Zeros(1, &ans_size, type, 0);
ans = (PyArrayObject *)PyArray_ZEROS(1, &ans_size, NPY_INTP, 0);
if (ans == NULL) {
goto fail;
}
ians = (npy_intp *)(PyArray_DATA(ans));
ians = (npy_intp *)PyArray_DATA(ans);
NPY_BEGIN_ALLOW_THREADS;
for (i = 0; i < len; i++)
ians[numbers[i]] += 1;
Expand All @@ -166,14 +165,13 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
if (wts == NULL) {
goto fail;
}
weights = (double *)PyArray_DATA (wts);
weights = (double *)PyArray_DATA(wts);
if (PyArray_SIZE(wts) != len) {
PyErr_SetString(PyExc_ValueError,
"The weights and list don't have the same length.");
goto fail;
}
type = PyArray_DescrFromType(NPY_DOUBLE);
ans = (PyArrayObject *)PyArray_Zeros(1, &ans_size, type, 0);
ans = (PyArrayObject *)PyArray_ZEROS(1, &ans_size, NPY_DOUBLE, 0);
if (ans == NULL) {
goto fail;
}
Expand Down
23 changes: 19 additions & 4 deletions numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,7 @@ def test_finite_range(self):
histogram(vals, range=[0.25,0.75])
assert_raises(ValueError, histogram, vals, range=[np.nan,0.75])
assert_raises(ValueError, histogram, vals, range=[0.25,np.inf])


class TestHistogramOptimBinNums(TestCase):
"""
Expand All @@ -1428,7 +1428,7 @@ def test_simple(self):
"""
# Some basic sanity checking, with some fixed data.
# Checking for the correct number of bins
basic_test = {50: {'fd': 4, 'scott': 4, 'rice': 8, 'sturges': 7,
basic_test = {50: {'fd': 4, 'scott': 4, 'rice': 8, 'sturges': 7,
'doane': 8, 'sqrt': 8, 'auto': 7},
500: {'fd': 8, 'scott': 8, 'rice': 16, 'sturges': 10,
'doane': 12, 'sqrt': 23, 'auto': 10},
Expand Down Expand Up @@ -1674,9 +1674,9 @@ def test_rightmost_binedge(self):
def test_finite_range(self):
vals = np.random.random((100, 3))
histogramdd(vals, range=[[0.0, 1.0], [0.25, 0.75], [0.25, 0.5]])
assert_raises(ValueError, histogramdd, vals,
assert_raises(ValueError, histogramdd, vals,
range=[[0.0, 1.0], [0.25, 0.75], [0.25, np.inf]])
assert_raises(ValueError, histogramdd, vals,
assert_raises(ValueError, histogramdd, vals,
range=[[0.0, 1.0], [np.nan, 0.75], [0.25, 0.5]])


Expand Down Expand Up @@ -2150,6 +2150,21 @@ def test_with_incorrect_minlength(self):
"minlength must be positive",
lambda: np.bincount(x, minlength=0))

def test_dtype_reference_leaks(self):
# gh-6805
intp_refcount = sys.getrefcount(np.dtype(np.intp))
double_refcount = sys.getrefcount(np.dtype(np.double))

for j in range(10):
np.bincount([1, 2, 3])
assert_equal(sys.getrefcount(np.dtype(np.intp)), intp_refcount)
assert_equal(sys.getrefcount(np.dtype(np.double)), double_refcount)

for j in range(10):
np.bincount([1, 2, 3], [4, 5, 6])
assert_equal(sys.getrefcount(np.dtype(np.intp)), intp_refcount)
assert_equal(sys.getrefcount(np.dtype(np.double)), double_refcount)


class TestInterp(TestCase):

Expand Down
0