Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: reference cycle in np.vectorize #11977

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 5 commits into from
Jan 9, 2019
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
8 changes: 6 additions & 2 deletions numpy/core/include/numpy/ufuncobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ typedef struct _tagPyUFuncObject {
*/
int nin, nout, nargs;

/* Identity for reduction, either PyUFunc_One or PyUFunc_Zero */
/*
* Identity for reduction, any of PyUFunc_One, PyUFunc_Zero
* PyUFunc_MinusOne, PyUFunc_None, PyUFunc_ReorderableNone,
* PyUFunc_IdentityValue.
*/
int identity;

/* Array of one-dimensional core loops */
Expand Down Expand Up @@ -301,7 +305,7 @@ typedef struct _tagPyUFuncObject {
*/
#define PyUFunc_ReorderableNone -2
/*
* UFunc unit is in identity_value, and the order of operations can be reordered
* UFunc unit is an identity_value, and the order of operations can be reordered
* This case allows reduction with multiple axes at once.
*/
#define PyUFunc_IdentityValue -3
Expand Down
48 changes: 40 additions & 8 deletions numpy/core/src/umath/ufunc_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -4892,26 +4892,46 @@ PyUFunc_FromFuncAndDataAndSignatureAndIdentity(PyUFuncGenericFunction *func, voi
return NULL;
}

ufunc = PyArray_malloc(sizeof(PyUFuncObject));
ufunc = PyObject_GC_New(PyUFuncObject, &PyUFunc_Type);
/*
* We use GC_New here for ufunc->obj, but do not use GC_Track since
* ufunc->obj is still NULL at the end of this function.
* See ufunc_frompyfunc where ufunc->obj is set and GC_Track is called.
*/
if (ufunc == NULL) {
return NULL;
}
memset(ufunc, 0, sizeof(PyUFuncObject));
PyObject_Init((PyObject *)ufunc, &PyUFunc_Type);

ufunc->nin = nin;
ufunc->nout = nout;
ufunc->nargs = nin+nout;
ufunc->identity = identity;
if (ufunc->identity == PyUFunc_IdentityValue) {
Py_INCREF(identity_value);
ufunc->identity_value = identity_value;
}
else {
ufunc->identity_value = NULL;
}
ufunc->identity_value = identity_value;

ufunc->functions = func;
ufunc->data = data;
ufunc->types = types;
ufunc->ntypes = ntypes;
ufunc->core_signature = NULL;
ufunc->core_enabled = 0;
ufunc->obj = NULL;
ufunc->core_num_dims = NULL;
ufunc->core_num_dim_ix = 0;
ufunc->core_offsets = NULL;
ufunc->core_dim_ixs = NULL;
ufunc->core_dim_sizes = NULL;
ufunc->core_dim_flags = NULL;
ufunc->userloops = NULL;
ufunc->ptr = NULL;
ufunc->reserved2 = NULL;
ufunc->reserved1 = 0;
ufunc->iter_flags = 0;

/* Type resolution and inner loop selection functions */
ufunc->type_resolver = &PyUFunc_DefaultTypeResolver;
Expand Down Expand Up @@ -5277,18 +5297,21 @@ PyUFunc_RegisterLoopForType(PyUFuncObject *ufunc,
static void
ufunc_dealloc(PyUFuncObject *ufunc)
{
PyObject_GC_UnTrack((PyObject *)ufunc);
PyArray_free(ufunc->core_num_dims);
PyArray_free(ufunc->core_dim_ixs);
PyArray_free(ufunc->core_offsets);
PyArray_free(ufunc->core_signature);
PyArray_free(ufunc->ptr);
PyArray_free(ufunc->op_flags);
Py_XDECREF(ufunc->userloops);
Py_XDECREF(ufunc->obj);
if (ufunc->identity == PyUFunc_IdentityValue) {
Py_DECREF(ufunc->identity_value);
}
PyArray_free(ufunc);
if (ufunc->obj != NULL) {
Py_DECREF(ufunc->obj);
}
PyObject_GC_Del(ufunc);
}

static PyObject *
Expand All @@ -5297,6 +5320,15 @@ ufunc_repr(PyUFuncObject *ufunc)
return PyUString_FromFormat("<ufunc '%s'>", ufunc->name);
}

static int
ufunc_traverse(PyUFuncObject *self, visitproc visit, void *arg)
{
Py_VISIT(self->obj);
if (self->identity == PyUFunc_IdentityValue) {
Py_VISIT(self->identity_value);
}
return 0;
}

/******************************************************************************
*** UFUNC METHODS ***
Expand Down Expand Up @@ -6013,9 +6045,9 @@ NPY_NO_EXPORT PyTypeObject PyUFunc_Type = {
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
(traverseproc)ufunc_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
Expand Down
1 change: 1 addition & 0 deletions numpy/core/src/umath/umathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *NPY_UNUS

self->type_resolver = &object_ufunc_type_resolver;
self->legacy_inner_loop_selector = &object_ufunc_loop_selector;
PyObject_GC_Track(self);

return (PyObject *)self;
}
Expand Down
49 changes: 47 additions & 2 deletions numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import warnings
import sys
import decimal
import types
import pytest

import numpy as np
Expand All @@ -24,6 +25,7 @@

from numpy.compat import long

PY2 = sys.version_info[0] == 2

def get_mat(n):
data = np.arange(n)
Expand Down Expand Up @@ -353,9 +355,9 @@ class subclass(np.ndarray):
assert_equal(type(np.average(a, weights=w)), subclass)

def test_upcasting(self):
types = [('i4', 'i4', 'f8'), ('i4', 'f4', 'f8'), ('f4', 'i4', 'f8'),
typs = [('i4', 'i4', 'f8'), ('i4', 'f4', 'f8'), ('f4', 'i4', 'f8'),
('f4', 'f4', 'f4'), ('f4', 'f8', 'f8')]
for at, wt, rt in types:
for at, wt, rt in typs:
a = np.array([[1,2],[3,4]], dtype=at)
w = np.array([[1,2],[3,4]], dtype=wt)
assert_equal(np.average(a, weights=w).dtype, np.dtype(rt))
Expand Down Expand Up @@ -1498,6 +1500,49 @@ def test_size_zero_output(self):
f(x)


class TestLeaks(object):
class A(object):
iters = 20

def bound(self, *args):
return 0

@staticmethod
def unbound(*args):
return 0

@pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
@pytest.mark.parametrize('name, incr', [
('bound', A.iters),
('unbound', 0),
])
def test_frompyfunc_leaks(self, name, incr):
# exposed in gh-11867 as np.vectorized, but the problem stems from
# frompyfunc.
# class.attribute = np.frompyfunc(<method>) creates a
# reference cycle if <method> is a bound class method. It requires a
# gc collection cycle to break the cycle (on CPython 3)
import gc
A_func = getattr(self.A, name)
gc.disable()
try:
refcount = sys.getrefcount(A_func)
for i in range(self.A.iters):
a = self.A()
a.f = np.frompyfunc(getattr(a, name), 1, 1)
out = a.f(np.arange(10))
a = None
if PY2:
assert_equal(sys.getrefcount(A_func), refcount)
else:
# A.func is part of a reference cycle if incr is non-zero
assert_equal(sys.getrefcount(A_func), refcount + incr)
for i in range(5):
gc.collect()
assert_equal(sys.getrefcount(A_func), refcount)
finally:
gc.enable()

class TestDigitize(object):

def test_forward(self):
Expand Down