diff options
author | Seth Troisi <sethtroisi@google.com> | 2020-01-08 23:49:17 -0800 |
---|---|---|
committer | Seth Troisi <sethtroisi@google.com> | 2020-01-15 13:19:56 -0800 |
commit | 1427484e9015e73b7017ee9336ce914a6f15187b (patch) | |
tree | 2db89078addee6a01697ab8bd4160c25d59fffc4 /numpy/lib | |
parent | b6bc0941d4f07310456079ab2497c3d1bde4a5e7 (diff) | |
download | numpy-1427484e9015e73b7017ee9336ce914a6f15187b.tar.gz |
MAINT: Remove sys.version checks in tests
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/tests/test__datasource.py | 29 | ||||
-rw-r--r-- | numpy/lib/tests/test_format.py | 59 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 9 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 15 | ||||
-rw-r--r-- | numpy/lib/tests/test_mixins.py | 12 | ||||
-rw-r--r-- | numpy/lib/tests/test_regression.py | 5 | ||||
-rw-r--r-- | numpy/lib/tests/test_utils.py | 5 |
7 files changed, 38 insertions, 96 deletions
diff --git a/numpy/lib/tests/test__datasource.py b/numpy/lib/tests/test__datasource.py index fdd22347d..d3bd88d7f 100644 --- a/numpy/lib/tests/test__datasource.py +++ b/numpy/lib/tests/test__datasource.py @@ -9,14 +9,9 @@ from numpy.testing import ( assert_, assert_equal, assert_raises, assert_warns ) -if sys.version_info[0] >= 3: - import urllib.request as urllib_request - from urllib.parse import urlparse - from urllib.error import URLError -else: - import urllib2 as urllib_request - from urlparse import urlparse - from urllib2 import URLError +import urllib.request as urllib_request +from urllib.parse import urlparse +from urllib.error import URLError def urlopen_stub(url, data=None): @@ -162,24 +157,6 @@ class TestDataSourceOpen: fp.close() assert_equal(magic_line, result) - @pytest.mark.skipif(sys.version_info[0] >= 3, reason="Python 2 only") - def test_Bz2File_text_mode_warning(self): - try: - import bz2 - except ImportError: - # We don't have the bz2 capabilities to test. - pytest.skip() - # Test datasource's internal file_opener for BZip2 files. - filepath = os.path.join(self.tmpdir, 'foobar.txt.bz2') - fp = bz2.BZ2File(filepath, 'w') - fp.write(magic_line) - fp.close() - with assert_warns(RuntimeWarning): - fp = self.ds.open(filepath, 'rt') - result = fp.readline() - fp.close() - assert_equal(magic_line, result) - class TestDataSourceExists: def setup(self): diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py index 28ce038ae..24593d7b3 100644 --- a/numpy/lib/tests/test_format.py +++ b/numpy/lib/tests/test_format.py @@ -548,10 +548,7 @@ def test_load_padded_dtype(dt): def test_python2_python3_interoperability(): - if sys.version_info[0] >= 3: - fname = 'win64python2.npy' - else: - fname = 'python3.npy' + fname = 'win64python2.npy' path = os.path.join(os.path.dirname(__file__), 'data', fname) data = np.load(path) assert_array_equal(data, np.ones(2)) @@ -561,13 +558,7 @@ def test_pickle_python2_python3(): # Python 2 and Python 3 and vice versa data_dir = os.path.join(os.path.dirname(__file__), 'data') - if sys.version_info[0] >= 3: - xrange = range - else: - import __builtin__ - xrange = __builtin__.xrange - - expected = np.array([None, xrange, u'\u512a\u826f', + expected = np.array([None, range, u'\u512a\u826f', b'\xe4\xb8\x8d\xe8\x89\xaf'], dtype=object) @@ -583,34 +574,30 @@ def test_pickle_python2_python3(): else: data = data_f - if sys.version_info[0] >= 3: - if encoding == 'latin1' and fname.startswith('py2'): - assert_(isinstance(data[3], str)) - assert_array_equal(data[:-1], expected[:-1]) - # mojibake occurs - assert_array_equal(data[-1].encode(encoding), expected[-1]) - else: - assert_(isinstance(data[3], bytes)) - assert_array_equal(data, expected) + if encoding == 'latin1' and fname.startswith('py2'): + assert_(isinstance(data[3], str)) + assert_array_equal(data[:-1], expected[:-1]) + # mojibake occurs + assert_array_equal(data[-1].encode(encoding), expected[-1]) else: + assert_(isinstance(data[3], bytes)) assert_array_equal(data, expected) - if sys.version_info[0] >= 3: - if fname.startswith('py2'): - if fname.endswith('.npz'): - data = np.load(path, allow_pickle=True) - assert_raises(UnicodeError, data.__getitem__, 'x') - data.close() - data = np.load(path, allow_pickle=True, fix_imports=False, - encoding='latin1') - assert_raises(ImportError, data.__getitem__, 'x') - data.close() - else: - assert_raises(UnicodeError, np.load, path, - allow_pickle=True) - assert_raises(ImportError, np.load, path, - allow_pickle=True, fix_imports=False, - encoding='latin1') + if fname.startswith('py2'): + if fname.endswith('.npz'): + data = np.load(path, allow_pickle=True) + assert_raises(UnicodeError, data.__getitem__, 'x') + data.close() + data = np.load(path, allow_pickle=True, fix_imports=False, + encoding='latin1') + assert_raises(ImportError, data.__getitem__, 'x') + data.close() + else: + assert_raises(UnicodeError, np.load, path, + allow_pickle=True) + assert_raises(ImportError, np.load, path, + allow_pickle=True, fix_imports=False, + encoding='latin1') def test_pickle_disallow(): diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index dfce2d55d..7953de15d 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -23,8 +23,6 @@ from numpy.lib import ( from numpy.compat import long -PY2 = sys.version_info[0] == 2 - def get_mat(n): data = np.arange(n) data = np.add.outer(data, data) @@ -1568,11 +1566,8 @@ class TestLeaks: 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) + # 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) diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index ba27eea6c..6460741b5 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -44,7 +44,6 @@ class TextIO(BytesIO): BytesIO.writelines(self, [asbytes(s) for s in lines]) -MAJVER, MINVER = sys.version_info[:2] IS_64BIT = sys.maxsize > 2**32 try: import bz2 @@ -528,12 +527,10 @@ class TestSaveTxt: a = np.array([utf8], dtype=np.unicode_) # our gz wrapper support encoding suffixes = ['', '.gz'] - # stdlib 2 versions do not support encoding - if MAJVER > 2: - if HAS_BZ2: - suffixes.append('.bz2') - if HAS_LZMA: - suffixes.extend(['.xz', '.lzma']) + if HAS_BZ2: + suffixes.append('.bz2') + if HAS_LZMA: + suffixes.extend(['.xz', '.lzma']) with tempdir() as tmpdir: for suffix in suffixes: np.savetxt(os.path.join(tmpdir, 'test.csv' + suffix), a, @@ -599,18 +596,14 @@ class LoadTxtBase: res = self.loadfunc(f) assert_array_equal(res, wanted) - # Python2 .open does not support encoding - @pytest.mark.skipif(MAJVER == 2, reason="Needs Python version >= 3") def test_compressed_gzip(self): self.check_compressed(gzip.open, ('.gz',)) @pytest.mark.skipif(not HAS_BZ2, reason="Needs bz2") - @pytest.mark.skipif(MAJVER == 2, reason="Needs Python version >= 3") def test_compressed_bz2(self): self.check_compressed(bz2.open, ('.bz2',)) @pytest.mark.skipif(not HAS_LZMA, reason="Needs lzma") - @pytest.mark.skipif(MAJVER == 2, reason="Needs Python version >= 3") def test_compressed_lzma(self): self.check_compressed(lzma.open, ('.xz', '.lzma')) diff --git a/numpy/lib/tests/test_mixins.py b/numpy/lib/tests/test_mixins.py index 7c22dae94..e184ffe19 100644 --- a/numpy/lib/tests/test_mixins.py +++ b/numpy/lib/tests/test_mixins.py @@ -6,9 +6,6 @@ import numpy as np from numpy.testing import assert_, assert_equal, assert_raises -PY2 = sys.version_info.major < 3 - - # NOTE: This class should be kept as an exact copy of the example from the # docstring for NDArrayOperatorsMixin. @@ -202,11 +199,10 @@ class TestNDArrayOperatorsMixin: array_like = ArrayLike(array) expected = ArrayLike(np.float64(5)) _assert_equal_type_and_value(expected, np.matmul(array_like, array)) - if not PY2: - _assert_equal_type_and_value( - expected, operator.matmul(array_like, array)) - _assert_equal_type_and_value( - expected, operator.matmul(array, array_like)) + _assert_equal_type_and_value( + expected, operator.matmul(array_like, array)) + _assert_equal_type_and_value( + expected, operator.matmul(array, array_like)) def test_ufunc_at(self): array = ArrayLike(np.array([1, 2, 3, 4])) diff --git a/numpy/lib/tests/test_regression.py b/numpy/lib/tests/test_regression.py index 019b7595e..37cc158ba 100644 --- a/numpy/lib/tests/test_regression.py +++ b/numpy/lib/tests/test_regression.py @@ -206,10 +206,7 @@ class TestRegression: def test_loadtxt_fields_subarrays(self): # For ticket #1936 - if sys.version_info[0] >= 3: - from io import StringIO - else: - from StringIO import StringIO + from io import StringIO dt = [("a", 'u1', 2), ("b", 'u1', 2)] x = np.loadtxt(StringIO("0 1 2 3"), dtype=dt) diff --git a/numpy/lib/tests/test_utils.py b/numpy/lib/tests/test_utils.py index 57c840342..c96bf795a 100644 --- a/numpy/lib/tests/test_utils.py +++ b/numpy/lib/tests/test_utils.py @@ -7,10 +7,7 @@ from numpy.testing import assert_, assert_equal, assert_raises_regex from numpy.lib import deprecate import numpy.lib.utils as utils -if sys.version_info[0] >= 3: - from io import StringIO -else: - from StringIO import StringIO +from io import StringIO @pytest.mark.skipif(sys.flags.optimize == 2, reason="Python running -OO") |