From addf86ba5ec6d0038993f00d782101f365ddeb6d Mon Sep 17 00:00:00 2001 From: Seth Troisi Date: Thu, 9 Jan 2020 18:19:40 -0800 Subject: MAINT: cleanup sys.version dependant code --- numpy/core/_internal.py | 1 - numpy/core/defchararray.py | 25 +++----- numpy/core/tests/test_api.py | 71 +++++++++-------------- numpy/core/tests/test_multiarray.py | 100 +++++++++++++------------------- numpy/core/tests/test_numeric.py | 2 - numpy/distutils/ccompiler.py | 7 +-- numpy/distutils/extension.py | 5 +- numpy/lib/format.py | 7 +-- numpy/testing/_private/parameterized.py | 57 +++--------------- 9 files changed, 88 insertions(+), 187 deletions(-) (limited to 'numpy') diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py index 105b5d24a..836056f82 100644 --- a/numpy/core/_internal.py +++ b/numpy/core/_internal.py @@ -555,7 +555,6 @@ class _Stream: def __bool__(self): return bool(self.s) - __nonzero__ = __bool__ def _dtype_from_pep3118(spec): diff --git a/numpy/core/defchararray.py b/numpy/core/defchararray.py index ff1474d9d..4d7781317 100644 --- a/numpy/core/defchararray.py +++ b/numpy/core/defchararray.py @@ -40,13 +40,6 @@ __all__ = [ _globalvar = 0 -if sys.version_info[0] >= 3: - _unicode = str - _bytes = bytes -else: - _unicode = unicode - _bytes = str -_len = len array_function_dispatch = functools.partial( overrides.array_function_dispatch, module='numpy.char') @@ -61,7 +54,7 @@ def _use_unicode(*args): result should be unicode. """ for x in args: - if (isinstance(x, _unicode) or + if (isinstance(x, str) or issubclass(numpy.asarray(x).dtype.type, unicode_)): return unicode_ return string_ @@ -1960,7 +1953,7 @@ class chararray(ndarray): # strings in the new array. itemsize = long(itemsize) - if sys.version_info[0] >= 3 and isinstance(buffer, _unicode): + if sys.version_info[0] >= 3 and isinstance(buffer, str): # On Py3, unicode objects do not have the buffer interface filler = buffer buffer = None @@ -1991,7 +1984,7 @@ class chararray(ndarray): if isinstance(val, character): temp = val.rstrip() - if _len(temp) == 0: + if len(temp) == 0: val = '' else: val = temp @@ -2675,16 +2668,16 @@ def array(obj, itemsize=None, copy=True, unicode=None, order=None): be in any order (either C-, Fortran-contiguous, or even discontiguous). """ - if isinstance(obj, (_bytes, _unicode)): + if isinstance(obj, (bytes, str)): if unicode is None: - if isinstance(obj, _unicode): + if isinstance(obj, str): unicode = True else: unicode = False if itemsize is None: - itemsize = _len(obj) - shape = _len(obj) // itemsize + itemsize = len(obj) + shape = len(obj) // itemsize if unicode: if sys.maxunicode == 0xffff: @@ -2699,11 +2692,11 @@ def array(obj, itemsize=None, copy=True, unicode=None, order=None): # should happen in native endianness. obj = obj.encode('utf_32') else: - obj = _unicode(obj) + obj = str(obj) else: # Let the default Unicode -> string encoding (if any) take # precedence. - obj = _bytes(obj) + obj = bytes(obj) return chararray(shape, itemsize=itemsize, unicode=unicode, buffer=obj, order=order) diff --git a/numpy/core/tests/test_api.py b/numpy/core/tests/test_api.py index 3f0a59eec..71b46e551 100644 --- a/numpy/core/tests/test_api.py +++ b/numpy/core/tests/test_api.py @@ -39,57 +39,38 @@ def test_array_array(): assert_equal(old_refcount, sys.getrefcount(np.float64)) # test string - S2 = np.dtype((str, 2)) - S3 = np.dtype((str, 3)) - S5 = np.dtype((str, 5)) + S2 = np.dtype((bytes, 2)) + S3 = np.dtype((bytes, 3)) + S5 = np.dtype((bytes, 5)) + assert_equal(np.array(b"1.0", dtype=np.float64), + np.ones((), dtype=np.float64)) + assert_equal(np.array(b"1.0").dtype, S3) + assert_equal(np.array(b"1.0", dtype=bytes).dtype, S3) + assert_equal(np.array(b"1.0", dtype=S2), np.array(b"1.")) + assert_equal(np.array(b"1", dtype=S5), np.ones((), dtype=S5)) + + # test string + U2 = np.dtype((str, 2)) + U3 = np.dtype((str, 3)) + U5 = np.dtype((str, 5)) assert_equal(np.array("1.0", dtype=np.float64), np.ones((), dtype=np.float64)) - assert_equal(np.array("1.0").dtype, S3) - assert_equal(np.array("1.0", dtype=str).dtype, S3) - assert_equal(np.array("1.0", dtype=S2), np.array("1.")) - assert_equal(np.array("1", dtype=S5), np.ones((), dtype=S5)) - - # test unicode - _unicode = globals().get("unicode") - if _unicode: - U2 = np.dtype((_unicode, 2)) - U3 = np.dtype((_unicode, 3)) - U5 = np.dtype((_unicode, 5)) - assert_equal(np.array(_unicode("1.0"), dtype=np.float64), - np.ones((), dtype=np.float64)) - assert_equal(np.array(_unicode("1.0")).dtype, U3) - assert_equal(np.array(_unicode("1.0"), dtype=_unicode).dtype, U3) - assert_equal(np.array(_unicode("1.0"), dtype=U2), - np.array(_unicode("1."))) - assert_equal(np.array(_unicode("1"), dtype=U5), - np.ones((), dtype=U5)) + assert_equal(np.array("1.0").dtype, U3) + assert_equal(np.array("1.0", dtype=str).dtype, U3) + assert_equal(np.array("1.0", dtype=U2), np.array(str("1."))) + assert_equal(np.array("1", dtype=U5), np.ones((), dtype=U5)) builtins = getattr(__builtins__, '__dict__', __builtins__) assert_(hasattr(builtins, 'get')) - # test buffer - _buffer = builtins.get("buffer") - if _buffer and sys.version_info[:3] >= (2, 7, 5): - # This test fails for earlier versions of Python. - # Evidently a bug got fixed in 2.7.5. - dat = np.array(_buffer('1.0'), dtype=np.float64) - assert_equal(dat, [49.0, 46.0, 48.0]) - assert_(dat.dtype.type is np.float64) - - dat = np.array(_buffer(b'1.0')) - assert_equal(dat, [49, 46, 48]) - assert_(dat.dtype.type is np.uint8) - - # test memoryview, new version of buffer - _memoryview = builtins.get("memoryview") - if _memoryview: - dat = np.array(_memoryview(b'1.0'), dtype=np.float64) - assert_equal(dat, [49.0, 46.0, 48.0]) - assert_(dat.dtype.type is np.float64) - - dat = np.array(_memoryview(b'1.0')) - assert_equal(dat, [49, 46, 48]) - assert_(dat.dtype.type is np.uint8) + # test memoryview + dat = np.array(memoryview(b'1.0'), dtype=np.float64) + assert_equal(dat, [49.0, 46.0, 48.0]) + assert_(dat.dtype.type is np.float64) + + dat = np.array(memoryview(b'1.0')) + assert_equal(dat, [49, 46, 48]) + assert_(dat.dtype.type is np.uint8) # test array interface a = np.array(100.0, dtype=np.float64) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 85910886a..62e2920d8 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -50,15 +50,6 @@ from numpy.core.tests._locales import CommaDecimalPointLocale from datetime import timedelta, datetime -if sys.version_info[:2] > (3, 2): - # In Python 3.3 the representation of empty shape, strides and sub-offsets - # is an empty tuple instead of None. - # https://docs.python.org/dev/whatsnew/3.3.html#api-changes - EMPTY = () -else: - EMPTY = None - - def _aligned_zeros(shape, dtype=float, order="C", align=None): """ Allocate a new ndarray with aligned memory. @@ -5288,51 +5279,41 @@ class TestRecord: a = np.zeros((1,), dtype=[('f1', 'i4'), ('f2', 'i4'), ('f3', [('sf1', 'i4')])]) - is_py3 = sys.version_info[0] >= 3 - if is_py3: - funcs = (str,) - # byte string indexing fails gracefully - assert_raises(IndexError, a.__setitem__, b'f1', 1) - assert_raises(IndexError, a.__getitem__, b'f1') - assert_raises(IndexError, a['f1'].__setitem__, b'sf1', 1) - assert_raises(IndexError, a['f1'].__getitem__, b'sf1') - else: - funcs = (str, unicode) - for func in funcs: - b = a.copy() - fn1 = func('f1') - b[fn1] = 1 - assert_equal(b[fn1], 1) - fnn = func('not at all') - assert_raises(ValueError, b.__setitem__, fnn, 1) - assert_raises(ValueError, b.__getitem__, fnn) - b[0][fn1] = 2 - assert_equal(b[fn1], 2) - # Subfield - assert_raises(ValueError, b[0].__setitem__, fnn, 1) - assert_raises(ValueError, b[0].__getitem__, fnn) - # Subfield - fn3 = func('f3') - sfn1 = func('sf1') - b[fn3][sfn1] = 1 - assert_equal(b[fn3][sfn1], 1) - assert_raises(ValueError, b[fn3].__setitem__, fnn, 1) - assert_raises(ValueError, b[fn3].__getitem__, fnn) - # multiple subfields - fn2 = func('f2') - b[fn2] = 3 - - assert_equal(b[['f1', 'f2']][0].tolist(), (2, 3)) - assert_equal(b[['f2', 'f1']][0].tolist(), (3, 2)) - assert_equal(b[['f1', 'f3']][0].tolist(), (2, (1,))) + # byte string indexing fails gracefully + assert_raises(IndexError, a.__setitem__, b'f1', 1) + assert_raises(IndexError, a.__getitem__, b'f1') + assert_raises(IndexError, a['f1'].__setitem__, b'sf1', 1) + assert_raises(IndexError, a['f1'].__getitem__, b'sf1') + b = a.copy() + fn1 = str('f1') + b[fn1] = 1 + assert_equal(b[fn1], 1) + fnn = str('not at all') + assert_raises(ValueError, b.__setitem__, fnn, 1) + assert_raises(ValueError, b.__getitem__, fnn) + b[0][fn1] = 2 + assert_equal(b[fn1], 2) + # Subfield + assert_raises(ValueError, b[0].__setitem__, fnn, 1) + assert_raises(ValueError, b[0].__getitem__, fnn) + # Subfield + fn3 = str('f3') + sfn1 = str('sf1') + b[fn3][sfn1] = 1 + assert_equal(b[fn3][sfn1], 1) + assert_raises(ValueError, b[fn3].__setitem__, fnn, 1) + assert_raises(ValueError, b[fn3].__getitem__, fnn) + # multiple subfields + fn2 = str('f2') + b[fn2] = 3 + + assert_equal(b[['f1', 'f2']][0].tolist(), (2, 3)) + assert_equal(b[['f2', 'f1']][0].tolist(), (3, 2)) + assert_equal(b[['f1', 'f3']][0].tolist(), (2, (1,))) # non-ascii unicode field indexing is well behaved - if not is_py3: - pytest.skip('non ascii unicode field indexing skipped; ' - 'raises segfault on python 2.x') - else: - assert_raises(ValueError, a.__setitem__, u'\u03e0', 1) - assert_raises(ValueError, a.__getitem__, u'\u03e0') + assert_raises(ValueError, a.__setitem__, u'\u03e0', 1) + assert_raises(ValueError, a.__getitem__, u'\u03e0') def test_record_hash(self): a = np.array([(1, 2), (1, 2)], dtype='i1,i2') @@ -7056,7 +7037,7 @@ class TestNewBufferProtocol: assert_equal(y.shape, (5,)) assert_equal(y.ndim, 1) assert_equal(y.strides, (4,)) - assert_equal(y.suboffsets, EMPTY) + assert_equal(y.suboffsets, ()) assert_equal(y.itemsize, 4) def test_export_simple_nd(self): @@ -7066,7 +7047,7 @@ class TestNewBufferProtocol: assert_equal(y.shape, (2, 2)) assert_equal(y.ndim, 2) assert_equal(y.strides, (16, 8)) - assert_equal(y.suboffsets, EMPTY) + assert_equal(y.suboffsets, ()) assert_equal(y.itemsize, 8) def test_export_discontiguous(self): @@ -7076,7 +7057,7 @@ class TestNewBufferProtocol: assert_equal(y.shape, (3, 3)) assert_equal(y.ndim, 2) assert_equal(y.strides, (36, 4)) - assert_equal(y.suboffsets, EMPTY) + assert_equal(y.suboffsets, ()) assert_equal(y.itemsize, 4) def test_export_record(self): @@ -7109,7 +7090,7 @@ class TestNewBufferProtocol: y = memoryview(x) assert_equal(y.shape, (1,)) assert_equal(y.ndim, 1) - assert_equal(y.suboffsets, EMPTY) + assert_equal(y.suboffsets, ()) sz = sum([np.dtype(b).itemsize for a, b in dt]) if np.dtype('l').itemsize == 4: @@ -7125,10 +7106,10 @@ class TestNewBufferProtocol: x = np.array(([[1, 2], [3, 4]],), dtype=[('a', ('i', (2, 2)))]) y = memoryview(x) assert_equal(y.format, 'T{(2,2)i:a:}') - assert_equal(y.shape, EMPTY) + assert_equal(y.shape, ()) assert_equal(y.ndim, 0) - assert_equal(y.strides, EMPTY) - assert_equal(y.suboffsets, EMPTY) + assert_equal(y.strides, ()) + assert_equal(y.suboffsets, ()) assert_equal(y.itemsize, 16) def test_export_endian(self): @@ -7556,7 +7537,6 @@ class TestConversion: class NotConvertible: def __bool__(self): raise NotImplementedError - __nonzero__ = __bool__ # python 2 assert_raises(NotImplementedError, bool, np.array(NotConvertible())) assert_raises(NotImplementedError, bool, np.array([NotConvertible()])) diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 934f0a2fd..210e86d36 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -1215,8 +1215,6 @@ class TestNonzero: class BoolErrors: def __bool__(self): raise ValueError("Not allowed") - def __nonzero__(self): - raise ValueError("Not allowed") assert_raises(ValueError, np.nonzero, np.array([BoolErrors()])) diff --git a/numpy/distutils/ccompiler.py b/numpy/distutils/ccompiler.py index 5e3cd0e74..35cf9df81 100644 --- a/numpy/distutils/ccompiler.py +++ b/numpy/distutils/ccompiler.py @@ -82,11 +82,8 @@ def _needs_build(obj, cc_args, extra_postargs, pp_opts): def replace_method(klass, method_name, func): - if sys.version_info[0] < 3: - m = types.MethodType(func, None, klass) - else: - # Py3k does not have unbound method anymore, MethodType does not work - m = lambda self, *args, **kw: func(self, *args, **kw) + # Py3k does not have unbound method anymore, MethodType does not work + m = lambda self, *args, **kw: func(self, *args, **kw) setattr(klass, method_name, m) diff --git a/numpy/distutils/extension.py b/numpy/distutils/extension.py index 3b5c3db35..704f1e7aa 100644 --- a/numpy/distutils/extension.py +++ b/numpy/distutils/extension.py @@ -10,9 +10,6 @@ import sys import re from distutils.extension import Extension as old_Extension -if sys.version_info[0] >= 3: - basestring = str - cxx_ext_re = re.compile(r'.*[.](cpp|cxx|cc)\Z', re.I).match fortran_pyf_ext_re = re.compile(r'.*[.](f90|f95|f77|for|ftn|f|pyf)\Z', re.I).match @@ -74,7 +71,7 @@ class Extension(old_Extension): self.swig_opts = swig_opts or [] # swig_opts is assumed to be a list. Here we handle the case where it # is specified as a string instead. - if isinstance(self.swig_opts, basestring): + if isinstance(self.swig_opts, str): import warnings msg = "swig_opts is specified as a string instead of a list" warnings.warn(msg, SyntaxWarning, stacklevel=2) diff --git a/numpy/lib/format.py b/numpy/lib/format.py index 2ee43637c..15a74518b 100644 --- a/numpy/lib/format.py +++ b/numpy/lib/format.py @@ -549,9 +549,7 @@ def _filter_header(s): tokens = [] last_token_was_number = False - # adding newline as python 2.7.5 workaround - string = s + "\n" - for token in tokenize.generate_tokens(StringIO(string).readline): + for token in tokenize.generate_tokens(StringIO(s).readline): token_type = token[0] token_string = token[1] if (last_token_was_number and @@ -561,8 +559,7 @@ def _filter_header(s): else: tokens.append(token) last_token_was_number = (token_type == tokenize.NUMBER) - # removing newline (see above) as python 2.7.5 workaround - return tokenize.untokenize(tokens)[:-1] + return tokenize.untokenize(tokens) def _read_array_header(fp, version): diff --git a/numpy/testing/_private/parameterized.py b/numpy/testing/_private/parameterized.py index dbfb4807c..086b292e2 100644 --- a/numpy/testing/_private/parameterized.py +++ b/numpy/testing/_private/parameterized.py @@ -35,7 +35,7 @@ import sys import inspect import warnings from functools import wraps -from types import MethodType as MethodType +from types import MethodType from collections import namedtuple try: @@ -45,30 +45,6 @@ except ImportError: from unittest import TestCase -PY2 = sys.version_info[0] == 2 - - -if PY2: - from types import InstanceType - lzip = zip - text_type = unicode - bytes_type = str - string_types = basestring, - def make_method(func, instance, type): - return MethodType(func, instance, type) -else: - # Python 3 doesn't have an InstanceType, so just use a dummy type. - class InstanceType(): - pass - lzip = lambda *a: list(zip(*a)) - text_type = str - string_types = str, - bytes_type = bytes - def make_method(func, instance, type): - if instance is None: - return func - return MethodType(func, instance) - _param = namedtuple("param", "args kwargs") class param(_param): @@ -122,7 +98,7 @@ class param(_param): """ if isinstance(args, param): return args - elif isinstance(args, string_types): + elif isinstance(args, (str,)): args = (args, ) try: return cls(*args) @@ -179,7 +155,7 @@ def parameterized_argument_value_pairs(func, p): named_args = argspec.args[arg_offset:] - result = lzip(named_args, p.args) + result = list(zip(named_args, p.args)) named_args = argspec.args[len(result) + arg_offset:] varargs = p.args[len(result):] @@ -214,11 +190,11 @@ def short_repr(x, n=64): """ x_repr = repr(x) - if isinstance(x_repr, bytes_type): + if isinstance(x_repr, bytes): try: - x_repr = text_type(x_repr, "utf-8") + x_repr = str(x_repr, "utf-8") except UnicodeDecodeError: - x_repr = text_type(x_repr, "latin1") + x_repr = str(x_repr, "latin1") if len(x_repr) > n: x_repr = x_repr[:n//2] + "..." + x_repr[len(x_repr) - n//2:] return x_repr @@ -246,7 +222,7 @@ def default_doc_func(func, num, p): def default_name_func(func, num, p): base_name = func.__name__ name_suffix = "_%s" %(num, ) - if len(p.args) > 0 and isinstance(p.args[0], string_types): + if len(p.args) > 0 and isinstance(p.args[0], (str,)): name_suffix += "_" + parameterized.to_safe_name(p.args[0]) return base_name + name_suffix @@ -324,15 +300,6 @@ class parameterized: @wraps(test_func) def wrapper(test_self=None): test_cls = test_self and type(test_self) - if test_self is not None: - if issubclass(test_cls, InstanceType): - raise TypeError(( - "@parameterized can't be used with old-style classes, but " - "%r has an old-style class. Consider using a new-style " - "class, or '@parameterized.expand' " - "(see http://stackoverflow.com/q/54867/71522 for more " - "information on old-style classes)." - ) %(test_self, )) original_doc = wrapper.__doc__ for num, args in enumerate(wrapper.parameterized_input): @@ -365,15 +332,7 @@ class parameterized: # Python 3 doesn't let us pull the function out of a bound method. unbound_func = nose_func if test_self is not None: - # Under nose on Py2 we need to return an unbound method to make - # sure that the `self` in the method is properly shared with the - # `self` used in `setUp` and `tearDown`. But only there. Everyone - # else needs a bound method. - func_self = ( - None if PY2 and detect_runner() == "nose" else - test_self - ) - nose_func = make_method(nose_func, func_self, type(test_self)) + nose_func = MethodType(nose_func, test_self) return unbound_func, (nose_func, ) + p.args + (p.kwargs or {}, ) def assert_not_in_testcase_subclass(self): -- cgit v1.2.1