10000 MAINT: Remove Python <= 3.4 logic and imports · numpy/numpy@6fa8279 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6fa8279

Browse files
committed
MAINT: Remove Python <= 3.4 logic and imports
1 parent b4105bf commit 6fa8279

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+596
-1573
lines changed

benchmarks/benchmarks/bench_app.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
import numpy as np
66

7-
from six.moves import xrange
8-
97

108
class LaplaceInplace(Benchmark):
119
params = ['inplace', 'normal']
@@ -61,7 +59,7 @@ def setup(self):
6159
ntime = 200
6260

6361
self.arrays = [np.random.normal(size=(ntime, nfeat))
64-
for i in xrange(nsubj)]
62+
for i in range(nsubj)]
6563

6664
def maxes_of_dots(self, arrays):
6765
"""

benchmarks/benchmarks/bench_indexing.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
from os.path import join as pjoin
66
import shutil
7-
import sys
8-
import six
97
from numpy import memmap, float32, array
108
import numpy as np
119
from tempfile import mkdtemp
@@ -25,13 +23,10 @@ def setup(self, indexes, sel, op):
2523
'indexes_': get_indexes_(),
2624
'indexes_rand_': get_indexes_rand_()}
2725

28-
if sys.version_info[0] >= 3:
29-
code = "def run():\n for a in squares_.values(): a[%s]%s"
30-
else:
31-
code = "def run():\n for a in squares_.itervalues(): a[%s]%s"
26+
code = "def run():\n for a in squares_.values(): a[%s]%s"
3227
code = code % (sel, op)
3328

34-
six.exec_(code, ns)
29+
exec(code, ns)
3530
self.func = ns['run']
3631

3732
def time_op(self, indexes, sel, op):

doc/summarize.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@
88
from __future__ import division, absolute_import, print_function
99

1010
import os, glob, re, sys, inspect, optparse
11-
try:
12-
# Accessing collections abstract classes from collections
13-
# has been deprecated since Python 3.3
14-
import collections.abc as collections_abc
15-
except ImportError:
16-
import collections as collections_abc
11+
import collections.abc as collections_abc
12+
1713
sys.path.append(os.path.join(os.path.dirname(__file__), 'sphinxext'))
1814
from sphinxext.phantom_import import import_phantom_module
1915

numpy/__init__.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,8 @@
156156

157157
# Make these accessible from numpy name-space
158158
# but not imported in from numpy import *
159-
if sys.version_info[0] >= 3:
160-
from builtins import bool, int, float, complex, object, str
161-
unicode = str
162-
else:
163-
from __builtin__ import bool, int, float, complex, object, unicode, str
159+
from builtins import bool, int, float, complex, object, str
160+
unicode = str
164161

165162
from .core import round, abs, max, min
166163
# now that numpy modules are imported, can initialize limits

numpy/compat/py3k.py

Lines changed: 49 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -12,76 +12,51 @@
1212

1313
import sys
1414
import os
15-
try:
16-
from pathlib import Path, PurePath
17-
except ImportError:
18-
Path = PurePath = None
19-
20-
if sys.version_info[0] >= 3:
21-
import io
15+
import io
2216

23-
try:
24-
import pickle5 as pickle
25-
except ImportError:
26-
import pickle
17+
from pathlib import Path, PurePath
2718

28-
long = int
29-
integer_types = (int,)
30-
basestring = str
31-
unicode = str
32-
bytes = bytes
33-
34-
def asunicode(s):
35-
if isinstance(s, bytes):
36-
return s.decode('latin1')
37-
return str(s)
19+
try:
20+
import pickle5 as pickle
21+
except ImportError:
22+
import pickle
3823

39-
def asbytes(s):
40-
if isinstance(s, bytes):
41-
return s
42-
return str(s).encode('latin1')
24+
long = int
25+
integer_types = (int,)
26+
basestring = str
27+
unicode = str
28+
bytes = bytes
29+
strchar = 'U'
4330

44-
def asstr(s):
45-
if isinstance(s, bytes):
46-
return s.decode('latin1')
47-
return str(s)
4831

49-
def isfileobj(f):
50-
return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter))
32+
def asunicode(s):
33+
if isinstance(s, bytes):
34+
return s.decode('latin1')
35+
return str(s)
5136

52-
def open_latin1(filename, mode='r'):
53-
return open(filename, mode=mode, encoding='iso-8859-1')
5437

55-
def sixu(s):
38+
def asbytes(s):
39+
if isinstance(s, bytes):
5640
return s
41+
return str(s).encode('latin1')
5742

58-
strchar = 'U'
5943

60-
else:
61-
import cpickle as pickle
44+
def asstr(s):
45+
if isinstance(s, bytes):
46+
return s.decode('latin1')
47+
return str(s)
48+
6249

63-
bytes = str
64-
long = long
65-
basestring = basestring
66-
unicode = unicode
67-
integer_types = (int, long)
68-
asbytes = str
69-
asstr = str
70-
strchar = 'S'
50+
def isfileobj(f):
51+
return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter))
7152

72-
def isfileobj(f):
73-
return isinstance(f, file)
7453

75-
def asunicode(s):
76-
if isinstance(s, unicode):
77-
return s
78-
return str(s).decode('ascii')
54+
def open_latin1(filename, mode='r'):
55+
return open(filename, mode=mode, encoding='iso-8859-1')
7956

80-
def open_latin1(filename, mode='r'):
81-
return open(filename, mode=mode)
8257

83-
def sixu(s):
84-
return unicode(s, 'unicode_escape')
58+
def sixu(s):
59+
return s
8560

8661
def getexception():
8762
return sys.exc_info()[1]
@@ -128,69 +103,32 @@ def __exit__(self, *excinfo):
128103
pass
129104

130105

131-
if sys.version_info[0] >= 3 and sys.version_info[1] >= 4:
132-
def npy_load_module(name, fn, info=None):
133-
"""
134-
Load a module.
135-
136-
.. versionadded:: 1.11.2
137-
138-
Parameters
139-
----------
140-
name : str
141-
Full module name.
142-
fn : str
143-
Path to module file.
144-
info : tuple, optional
145-
Only here for backward compatibility with Python 2.*.
146-
147-
Returns
148-
-------
149-
mod : module
150-
151-
"""
152-
import importlib.machinery
153-
return importlib.machinery.SourceFileLoader(name, fn).load_module()
154-
else:
155-
def npy_load_module(name, fn, info=None):
156-
"""
157-
Load a module.
106+
def npy_load_module(name, fn, info=None):
107+
"""
108+
Load a module.
158109
159-
.. versionadded:: 1.11.2
110+
.. versionadded:: 1.11.2
160111
161-
Parameters
162-
----------
163-
name : str
164-
Full module name.
165-
fn : str
166-
Path to module file.
167-
info : tuple, optional
168-
Information as returned by `imp.find_module`
169-
(suffix, mode, type).
112+
Parameters
113+
----------
114+
name : str
115+
Full module name.
116+
fn : str
117+
Path to module file.
118+
info : tuple, optional
119+
Only here for backward compatibility with Python 2.*.
170120
171-
Returns
172-
-------
173-
mod : module
121+
Returns
122+
-------
123+
mod : module
174124
175-
"""
176-
import imp
177-
if info is None:
178-
path = os.path.dirname(fn)
179-
fo, fn, info = imp.find_module(name, [path])
180-
else:
181-
fo = open(fn, info[1])
182-
try:
183-
mod = imp.load_module(name, fo, fn, info)
184-
finally:
185-
fo.close()
186-
return mod
125+
"""
126+
import importlib.machinery
127+
return importlib.machinery.SourceFileLoader(name, fn).load_module()
187128

188129
# backport abc.ABC
189130
import abc
190-
if sys.version_info[:2] >= (3, 4):
191-
abc_ABC = abc.ABC
192-
else:
193-
abc_ABC = abc.ABCMeta('ABC', (object,), {'__slots__': ()})
131+
abc_ABC = abc.ABC
194132

195133

196134
# Backport os.fs_path, os.PathLike, and PurePath.__fspath__

numpy/core/__init__.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,11 @@ def _ufunc_reduce(func):
130130
return _ufunc_reconstruct, (whichmodule(func, name), name)
131131

132132

133-
import sys
134-
if sys.version_info[0] >= 3:
135-
import copyreg
136-
else:
137-
import copy_reg as copyreg
133+
import copyreg
138134

139135
copyreg.pickle(ufunc, _ufunc_reduce, _ufunc_reconstruct)
140136
# Unclutter namespace (must keep _ufunc_reconstruct for unpickling)
141137
del copyreg
142-
del sys
143138
del _ufunc_reduce
144139

145140
from numpy._pytesttester import PytestTester

numpy/core/_add_newdocs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2082,7 +2082,7 @@
20822082
>>> x.ctypes.data_as(ctypes.POINTER(ctypes.c_long)).contents
20832083
c_long(0)
20842084
>>> x.ctypes.data_as(ctypes.POINTER(ctypes.c_longlong)).contents
2085-
c_longlong(4294967296L)
2085+
c_longlong(0)
20862086
>>> x.ctypes.shape
20872087
<numpy.core._internal.c_long_Array_2 object at 0x01FFD580>
20882088
>>> x.ctypes.shape_as(ctypes.c_long)

numpy/core/_dtype.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,10 @@
1919
'V': 'void',
2020
'O': 'object',
2121
'M': 'datetime',
22-
'm': 'timedelta'
22+
'm': 'timedelta',
23+
'S': 'bytes',
24+
'U': 'str',
2325
}
24-
if sys.version_info[0] >= 3:
25-
_kind_to_stem.update({
26-
'S': 'bytes',
27-
'U': 'str'
28-
})
29-
else:
30-
_kind_to_stem.update({
31-
'S': 'string',
32-
'U': 'unicode'
33-
})
3426

3527

3628
def _kind_name(dtype):

numpy/core/_type_aliases.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
2424
"""
2525
import warnings
26-
import sys
2726

2827
from numpy.compat import unicode
2928
from numpy._globals import VisibleDeprecationWarning
@@ -203,22 +202,15 @@ def _set_up_aliases():
203202
('bool_', 'bool'),
204203
('bytes_', 'string'),
205204
('string_', 'string'),
205+
('str_', 'unicode'),
206206
('unicode_', 'unicode'),
207207
('object_', 'object')]
208-
if sys.version_info[0] >= 3:
209-
type_pairs.extend([('str_', 'unicode')])
210-
else:
211-
type_pairs.extend([('str_', 'string')])
212208
for alias, t in type_pairs:
213209
allTypes[alias] = allTypes[t]
214210
sctypeDict[alias] = sctypeDict[t]
215211
# Remove aliases overriding python types and modules
216-
to_remove = ['ulong', 'object', 'int', 'float',
217-
'complex', 'bool', 'string', 'datetime', 'timedelta']
218-
if sys.version_info[0] >= 3:
219-
to_remove.extend(['bytes', 'str'])
220-
else:
221-
to_remove.extend(['unicode', 'long'])
212+
to_remove = ['ulong', 'object', 'int', 'float', 'complex', 'bool',
213+
'string', 'bytes', 'str', 'datetime', 'timedelta']
222214

223215
for t in to_remove:
224216
try:
@@ -267,11 +259,8 @@ def _set_array_types():
267259

268260

269261
# Add additional strings to the sctypeDict
270-
_toadd = ['int', 'float', 'complex', 'bool', 'object']
271-
if sys.version_info[0] >= 3:
272-
_toadd.extend(['str', 'bytes', ('a', 'bytes_')])
273-
else:
274-
_toadd.extend(['string', ('str', 'string_'), 'unicode', ('a', 'string_')])
262+
_toadd = ['int', 'float', 'complex', 'bool', 'object',
263+
'str', 'bytes', ('a', 'bytes_')]
275264

276265
for name in _toadd:
277266
if isinstance(name, tuple):

numpy/core/_ufunc_config.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55
"""
66
from __future__ import division, absolute_import, print_function
77

8-
try:
9-
# Accessing collections abstract classes from collections
10-
# has been deprecated since Python 3.3
11-
import collections.abc as collections_abc
12-
except ImportError:
13-
import collections as collections_abc
8+
import collections.abc as collections_abc
149
import contextlib
1510

1611
from .overrides import set_module

0 commit comments

Comments
 (0)
0