diff options
author | Pauli Virtanen <pav@iki.fi> | 2018-03-12 22:47:12 +0000 |
---|---|---|
committer | Nathaniel J. Smith <njs@pobox.com> | 2018-03-12 15:47:12 -0700 |
commit | e97de95d4cae6805ed6c258655e7492a5f2ce863 (patch) | |
tree | ffea6426cc80a5f6f8f77a73b219ef3aac840c15 | |
parent | 5324067c702a41e901354a01a5f0d05ff49b6cb4 (diff) | |
download | python-numpy-e97de95d4cae6805ed6c258655e7492a5f2ce863.tar.gz python-numpy-e97de95d4cae6805ed6c258655e7492a5f2ce863.tar.bz2 python-numpy-e97de95d4cae6805ed6c258655e7492a5f2ce863.zip |
Fix low-hanging Pypy compatibility issues (#10737)
* TST: skip refcount-requiring tests if sys.refcount is missing
* ENH: io: add refcheck=False to a safe .resize() call
The array is allocated immediately above, and the resize always succeeds
so it is not necessary to check it. Fixes Pypy compatibility.
* TST: remove unused code
* TST: factor skipif(not HAS_REFCOUNT) into a separate decorator
-rw-r--r-- | numpy/core/tests/test_arrayprint.py | 3 | ||||
-rw-r--r-- | numpy/core/tests/test_indexing.py | 16 | ||||
-rw-r--r-- | numpy/core/tests/test_nditer.py | 2 | ||||
-rw-r--r-- | numpy/core/tests/test_numeric.py | 4 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 8 | ||||
-rw-r--r-- | numpy/lib/npyio.py | 2 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 4 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 3 | ||||
-rw-r--r-- | numpy/testing/nose_tools/decorators.py | 6 | ||||
-rw-r--r-- | numpy/testing/pytest_tools/decorators.py | 7 |
10 files changed, 24 insertions, 31 deletions
diff --git a/numpy/core/tests/test_arrayprint.py b/numpy/core/tests/test_arrayprint.py index 309df8545..6c2403e67 100644 --- a/numpy/core/tests/test_arrayprint.py +++ b/numpy/core/tests/test_arrayprint.py @@ -5,7 +5,7 @@ import sys, gc import numpy as np from numpy.testing import ( - run_module_suite, assert_, assert_equal, assert_raises, assert_warns, dec + run_module_suite, assert_, assert_equal, assert_raises, assert_warns, dec, ) import textwrap @@ -388,6 +388,7 @@ class TestArray2String(object): "[ 'xxxxx']" ) + @dec._needs_refcount def test_refcount(self): # make sure we do not hold references to the array due to a recursive # closure (gh-10620) diff --git a/numpy/core/tests/test_indexing.py b/numpy/core/tests/test_indexing.py index cd4ea611a..26882c593 100644 --- a/numpy/core/tests/test_indexing.py +++ b/numpy/core/tests/test_indexing.py @@ -14,20 +14,6 @@ from numpy.testing import ( ) -try: - cdll = None - if hasattr(sys, 'gettotalrefcount'): - try: - cdll = np.ctypeslib.load_library('multiarray_d', np.core.multiarray.__file__) - except OSError: - pass - if cdll is None: - cdll = np.ctypeslib.load_library('multiarray', np.core.multiarray.__file__) - _HAS_CTYPE = True -except ImportError: - _HAS_CTYPE = False - - class TestIndexing(object): def test_index_no_floats(self): a = np.array([[[5]]]) @@ -622,7 +608,7 @@ class TestSubclasses(object): assert_array_equal(new_s.finalize_status, new_s) assert_array_equal(new_s.old, s) - @dec.skipif(not HAS_REFCOUNT) + @dec._needs_refcount def test_slice_decref_getsetslice(self): # See gh-10066, a temporary slice object should be discarted. # This test is only really interesting on Python 2 since diff --git a/numpy/core/tests/test_nditer.py b/numpy/core/tests/test_nditer.py index f3f8706b5..1b2485a87 100644 --- a/numpy/core/tests/test_nditer.py +++ b/numpy/core/tests/test_nditer.py @@ -33,7 +33,7 @@ def iter_iterindices(i): i.iternext() return ret -@dec.skipif(not HAS_REFCOUNT, "python does not have sys.getrefcount") +@dec._needs_refcount def test_iter_refcount(): # Make sure the iterator doesn't leak diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 7c012e9e8..d7cdd77f1 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -12,7 +12,7 @@ from numpy.random import rand, randint, randn from numpy.testing import ( run_module_suite, assert_, assert_equal, assert_raises, assert_raises_regex, assert_array_equal, assert_almost_equal, - assert_array_almost_equal, dec, HAS_REFCOUNT, suppress_warnings + assert_array_almost_equal, dec, suppress_warnings ) @@ -2092,7 +2092,7 @@ class TestCreationFuncs(object): self.check_function(np.full, 0) self.check_function(np.full, 1) - @dec.skipif(not HAS_REFCOUNT, "python has no sys.getrefcount") + @dec._needs_refcount def test_for_reference_leak(self): # Make sure we have an object for reference dim = 1 diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index a3b011454..016b720f3 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -1424,7 +1424,7 @@ class TestRegression(object): x[x.nonzero()] = x.ravel()[:1] assert_(x[0, 1] == x[0, 0]) - @dec.skipif(not HAS_REFCOUNT, "python has no sys.getrefcount") + @dec._needs_refcount def test_structured_arrays_with_objects2(self): # Ticket #1299 second test stra = 'aaaa' @@ -1537,7 +1537,7 @@ class TestRegression(object): y = np.add(x, x, x) assert_equal(id(x), id(y)) - @dec.skipif(not HAS_REFCOUNT, "python has no sys.getrefcount") + @dec._needs_refcount def test_take_refcount(self): # ticket #939 a = np.arange(16, dtype=float) @@ -1937,7 +1937,7 @@ class TestRegression(object): a = np.empty((100000000,), dtype='i1') del a - @dec.skipif(not HAS_REFCOUNT, "python has no sys.getrefcount") + @dec._needs_refcount def test_ufunc_reduce_memoryleak(self): a = np.arange(6) acnt = sys.getrefcount(a) @@ -2167,7 +2167,7 @@ class TestRegression(object): assert_equal(uf(a), ()) assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]]) - @dec.skipif(not HAS_REFCOUNT, "python has no sys.getrefcount") + @dec._needs_refcount def test_leak_in_structured_dtype_comparison(self): # gh-6250 recordtype = np.dtype([('a', np.float64), diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 959574594..0f338d781 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1104,7 +1104,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, nshape = list(X.shape) pos = nshape[0] nshape[0] += len(x) - X.resize(nshape) + X.resize(nshape, refcheck=False) X[pos:, ...] = x finally: if fown: diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 49b450175..2b1ac1cc0 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -11,7 +11,7 @@ from numpy.testing import ( run_module_suite, assert_, assert_equal, assert_array_equal, assert_almost_equal, assert_array_almost_equal, assert_raises, assert_allclose, assert_array_max_ulp, assert_warns, assert_raises_regex, - dec, suppress_warnings, HAS_REFCOUNT, + dec, suppress_warnings, ) import numpy.lib.function_base as nfb from numpy.random import rand @@ -2141,7 +2141,7 @@ class TestBincount(object): "must not be negative", lambda: np.bincount(x, minlength=-1)) - @dec.skipif(not HAS_REFCOUNT, "python has no sys.getrefcount") + @dec._needs_refcount def test_dtype_reference_leaks(self): # gh-6805 intp_refcount = sys.getrefcount(np.dtype(np.intp)) diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index ae40cf73b..a0f256726 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -22,7 +22,7 @@ from numpy.ma.testutils import assert_equal from numpy.testing import ( run_module_suite, assert_warns, assert_, SkipTest, assert_raises_regex, assert_raises, assert_allclose, - assert_array_equal, temppath, tempdir, dec, IS_PYPY, suppress_warnings + assert_array_equal, temppath, tempdir, dec, IS_PYPY, suppress_warnings, ) @@ -2364,6 +2364,7 @@ def test_npzfile_dict(): assert_('x' in z.keys()) +@dec._needs_refcount def test_load_refcount(): # Check that objects returned by np.load are directly freed based on # their refcount, rather than needing the gc to collect them. diff --git a/numpy/testing/nose_tools/decorators.py b/numpy/testing/nose_tools/decorators.py index 243c0c8c1..dda2c1b74 100644 --- a/numpy/testing/nose_tools/decorators.py +++ b/numpy/testing/nose_tools/decorators.py @@ -17,10 +17,10 @@ from __future__ import division, absolute_import, print_function import collections -from .utils import SkipTest, assert_warns +from .utils import SkipTest, assert_warns, HAS_REFCOUNT __all__ = ['slow', 'setastest', 'skipif', 'knownfailureif', 'deprecated', - 'parametrize',] + 'parametrize', '_needs_refcount',] def slow(t): @@ -283,3 +283,5 @@ def parametrize(vars, input): from .parameterized import parameterized return parameterized(input) + +_needs_refcount = skipif(not HAS_REFCOUNT, "python has no sys.getrefcount") diff --git a/numpy/testing/pytest_tools/decorators.py b/numpy/testing/pytest_tools/decorators.py index 08a39e0c0..bbca34035 100644 --- a/numpy/testing/pytest_tools/decorators.py +++ b/numpy/testing/pytest_tools/decorators.py @@ -14,10 +14,10 @@ from __future__ import division, absolute_import, print_function import collections -from .utils import SkipTest, assert_warns +from .utils import SkipTest, assert_warns, HAS_REFCOUNT __all__ = ['slow', 'setastest', 'skipif', 'knownfailureif', 'deprecated', - 'parametrize',] + 'parametrize', '_needs_refcount',] def slow(t): @@ -276,3 +276,6 @@ def parametrize(vars, input): import pytest return pytest.mark.parametrize(vars, input) + + +_needs_refcount = skipif(not HAS_REFCOUNT, "python has no sys.getrefcount") |