MCPcopy Create free account
hub / github.com/numpy/numpy / test_frompyfunc_leaks

Method test_frompyfunc_leaks

numpy/lib/tests/test_function_base.py:1871–1893  ·  view source on GitHub ↗
(self, name, incr)

Source from the content-addressed store, hash-verified

1869 ('unbound', 0),
1870 ])
1871 def test_frompyfunc_leaks(self, name, incr):
1872 # exposed in gh-11867 as np.vectorized, but the problem stems from
1873 # frompyfunc.
1874 # class.attribute = np.frompyfunc(<method>) creates a
1875 # reference cycle if <method> is a bound class method. It requires a
1876 # gc collection cycle to break the cycle (on CPython 3)
1877 import gc
1878 A_func = getattr(self.A, name)
1879 gc.disable()
1880 try:
1881 refcount = sys.getrefcount(A_func)
1882 for i in range(self.A.iters):
1883 a = self.A()
1884 a.f = np.frompyfunc(getattr(a, name), 1, 1)
1885 out = a.f(np.arange(10))
1886 a = None
1887 # A.func is part of a reference cycle if incr is non-zero
1888 assert_equal(sys.getrefcount(A_func), refcount + incr)
1889 for i in range(5):
1890 gc.collect()
1891 assert_equal(sys.getrefcount(A_func), refcount)
1892 finally:
1893 gc.enable()
1894
1895
1896class TestDigitize:

Callers

nothing calls this directly

Calls 4

assert_equalFunction · 0.90
AMethod · 0.80
enableMethod · 0.80
fMethod · 0.45

Tested by

no test coverage detected