8000 MAINT: cleanup sys.version dependant code by sethtroisi · Pull Request #15307 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: cleanup sys.version dependant code #15307

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 1 commit into from
Jan 15, 2020
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
1 change: 0 additions & 1 deletion numpy/core/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,6 @@ def next(self):

def __bool__(self):
return bool(self.s)
__nonzero__ = __bool__


def _dtype_from_pep3118(spec):
Expand Down
25 changes: 9 additions & 16 deletions numpy/core/defchararray.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@


_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')
Expand All @@ -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_
Expand Down Expand Up @@ -1960,7 +1953,7 @@ def __new__(subtype, shape, itemsize=1, unicode=False, buffer=None,
# 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
Expand Down Expand Up @@ -1991,7 +1984,7 @@ def __getitem__(self, obj):

if isinstance(val, character):
temp = val.rstrip()
if _len(temp) == 0:
if len(temp) == 0:
val = ''
else:
val = temp
Expand Down Expand Up @@ -2675,16 +2668,16 @@ class adds the following functionality:
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:
Expand All @@ -2699,11 +2692,11 @@ class adds the following functionality:
# 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)
Expand Down
71 changes: 26 additions & 45 deletions numpy/core/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
100 changes: 40 additions & 60 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@
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.
Expand Down Expand Up @@ -5288,51 +5279,41 @@ def test_field_names(self):
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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
F438
fn2 = str('f2')
fn2 = 'f2'

Same for the ones up to line 5288.

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')
Expand Down Expand Up @@ -7056,7 +7037,7 @@ def test_export_simple_1d(self):
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):
Expand All @@ -7066,7 +7047,7 @@ def test_export_simple_nd(self):
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):
Expand All @@ -7076,7 +7057,7 @@ def test_export_discontiguous(self):
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):
Expand Down Expand Up @@ -7109,7 +7090,7 @@ def test_export_record(self):
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:
Expand All @@ -7125,10 +7106,10 @@ def test_export_subarray(self):
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):
Expand Down Expand Up @@ -7556,7 +7537,6 @@ def test_to_bool_scalar(self):
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()]))
Expand Down
2 changes: 0 additions & 2 deletions numpy/core/tests/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -1215,8 +1215,6 @@ def test_nonzero_invalid_object(self):
class BoolErrors:
def __bool__(self):
raise ValueError("Not allowed")
def __nonzero__(self):
raise ValueError("Not allowed")

assert_raises(ValueError, np.nonzero, np.array([BoolErrors()]))

Expand Down
7 changes: 2 additions & 5 deletions numpy/distutils/ccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
5 changes: 1 addition & 4 deletions numpy/distutils/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
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
Expand Down Expand Up @@ -74,7 +71,7 @@ def __init__(
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)
Expand Down
7 changes: 2 additions & 5 deletions numpy/lib/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
Loading
0