8000 MAINT: Remove Python <= 3.4 logic and imports by mwtoews · Pull Request #13852 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Remove Python <= 3.4 logic and imports #13852

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

Closed
wants to merge 1 commit into from
Closed
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
MAINT: Remove Python <= 3.4 logic and imports
  • Loading branch information
mwtoews committed Jul 1, 2019
commit 6fa8279af7c167e1802b82bbdbee1d53e88e40ad
4 changes: 1 addition & 3 deletions benchmarks/benchmarks/bench_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import numpy as np

from six.moves import xrange


class LaplaceInplace(Benchmark):
params = ['inplace', 'normal']
Expand Down Expand Up @@ -61,7 +59,7 @@ def setup(self):
ntime = 200

self.arrays = [np.random.normal(size=(ntime, nfeat))
for i in xrange(nsubj)]
for i in range(nsubj)]

def maxes_of_dots(self, arrays):
"""
Expand Down
9 changes: 2 additions & 7 deletions benchmarks/benchmarks/bench_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

from os.path import join as pjoin
import shutil
import sys
import six
from numpy import memmap, float32, array
import numpy as np
from tempfile import mkdtemp
Expand All @@ -25,13 +23,10 @@ def setup(self, indexes, sel, op):
'indexes_': get_indexes_(),
'indexes_rand_': get_indexes_rand_()}

if sys.version_info[0] >= 3:
code = "def run():\n for a in squares_.values(): a[%s]%s"
else:
code = "def run():\n for a in squares_.itervalues(): a[%s]%s"
code = "def run():\n for a in squares_.values(): a[%s]%s"
code = code % (sel, op)

six.exec_(code, ns)
exec(code, ns)
self.func = ns['run']

def time_op(self, indexes, sel, op):
Expand Down
8 changes: 2 additions & 6 deletions doc/summarize.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@
from __future__ import division, absolute_import, print_function

import os, glob, re, sys, inspect, optparse
try:
# Accessing collections abstract classes from collections
# has been deprecated since Python 3.3
import collections.abc as collections_abc
except ImportError:
import collections as collections_abc
import collections.abc as collections_abc

sys.path.append(os.path.join(os.path.dirname(__file__), 'sphinxext'))
from sphinxext.phantom_import import import_phantom_module

Expand Down
7 changes: 2 additions & 5 deletions numpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,8 @@

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

from .core import round, abs, max, min
# now that numpy modules are imported, can initialize limits
Expand Down
160 changes: 49 additions & 111 deletions numpy/compat/py3k.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,76 +12,51 @@

import sys
import os
try:
from pathlib import Path, PurePath
except ImportError:
Path = PurePath = None

if sys.version_info[0] >= 3:
import io
import io

try:
import pickle5 as pickle
except ImportError:
import pickle
from pathlib import Path, PurePath

long = int
integer_types = (int,)
basestring = str
unicode = str
bytes = bytes

def asunicode(s):
if isinstance(s, bytes):
return s.decode('latin1')
return str(s)
try:
import pickle5 as pickle
except ImportError:
import pickle

def asbytes(s):
if isinstance(s, bytes):
return s
return str(s).encode('latin1')
long = int
integer_types = (int,)
basestring = str
unicode = str
bytes = bytes
strchar = 'U'

def asstr(s):
if isinstance(s, bytes):
return s.decode('latin1')
return str(s)

def isfileobj(f):
return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter))
def asunicode(s):
if isinstance(s, bytes):
return s.decode('latin1')
return str(s)

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

def sixu(s):
def asbytes(s):
if isinstance(s, bytes):
return s
return str(s).encode('latin1')

strchar = 'U'

else:
import cpickle as pickle
def asstr(s):
if isinstance(s, bytes):
return s.decode('latin1')
return str(s)


bytes = str
long = long
basestring = basestring
unicode = unicode
integer_types = (int, long)
asbytes = str
asstr = str
strchar = 'S'
def isfileobj(f):
return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter))

def isfileobj(f):
return isinstance(f, file)

def asunicode(s):
if isinstance(s, unicode):
return s
return str(s).decode('ascii')
def open_latin1(filename, mode='r'):
return open(filename, mode=mode, encoding='iso-8859-1')

def open_latin1(filename, mode='r'):
return open(filename, mode=mode)

def sixu(s):
return unicode(s, 'unicode_escape')
def sixu(s):
return s

def getexception():
return sys.exc_info()[1]
Expand Down Expand Up @@ -128,69 +103,32 @@ def __exit__(self, *excinfo):
pass


if sys.version_info[0] >= 3 and sys.version_info[1] >= 4:
def npy_load_module(name, fn, info=None):
"""
Load a module.

.. versionadded:: 1.11.2

Parameters
----------
name : str
Full module name.
fn : str
Path to module file.
info : tuple, optional
Only here for backward compatibility with Python 2.*.

Returns
-------
mod : module

"""
import importlib.machinery
return importlib.machinery.SourceFileLoader(name, fn).load_module()
else:
def npy_load_module(name, fn, info=None):
"""
Load a module.
def npy_load_module(name, fn, info=None):
"""
Load a module.

.. versionadded:: 1.11.2
.. versionadded:: 1.11.2

Parameters
----------
name : str
Full module name.
fn : str
Path to module file.
info : tuple, optional
Information as returned by `imp.find_module`
(suffix, mode, type).
Parameters
----------
name : str
Full module name.
fn : str
Path to module file.
info : tuple, optional
Only here for backward compatibility with Python 2.*.

Returns
-------
mod : module
Returns
-------
mod : module

"""
import imp
if info is None:
path = os.path.dirname(fn)
fo, fn, info = imp.find_module(name, [path])
else:
fo = open(fn, info[1])
try:
mod = imp.load_module(name, fo, fn, info)
finally:
fo.close()
return mod
"""
import importlib.machinery
return importlib.machinery.SourceFileLoader(name, fn).load_module()

# backport abc.ABC
import abc
if sys.version_info[:2] >= (3, 4):
abc_ABC = abc.ABC
else:
abc_ABC = abc.ABCMeta('ABC', (object,), {'__slots__': ()})
abc_ABC = abc.ABC


# Backport os.fs_path, os.PathLike, and PurePath.__fspath__
Expand Down
7 changes: 1 addition & 6 deletions numpy/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,11 @@ def _ufunc_reduce(func):
return _ufunc_reconstruct, (whichmodule(func, name), name)


import sys
if sys.version_info[0] >= 3:
import copyreg
else:
import copy_reg as copyreg
import copyreg

copyreg.pickle(ufunc, _ufunc_reduce, _ufunc_reconstruct)
# Unclutter namespace (must keep _ufunc_reconstruct for unpickling)
del copyreg
del sys
del _ufunc_reduce

from numpy._pytesttester import PytestTester
Expand Down
2 changes: 1 addition & 1 deletion numpy/core/_add_newdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,7 @@
>>> x.ctypes.data_as(ctypes.POINTER(ctypes.c_long)).contents
c_long(0)
>>> x.ctypes.data_as(ctypes.POINTER(ctypes.c_longlong)).contents
c_longlong(4294967296L)
c_longlong(0)
>>> x.ctypes.shape
<numpy.core._internal.c_long_Array_2 object at 0x01FFD580>
>>> x.ctypes.shape_as(ctypes.c_long)
Expand Down
14 changes: 3 additions & 11 deletions numpy/core/_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,10 @@
'V': 'void',
'O': 'object',
'M': 'datetime',
'm': 'timedelta'
'm': 'timedelta',
'S': 'bytes',
'U': 'str',
}
if sys.version_info[0] >= 3:
_kind_to_stem.update({
'S': 'bytes',
'U': 'str'
})
else:
_kind_to_stem.update({
'S': 'string',
'U': 'unicode'
})


def _kind_name(dtype):
Expand Down
21 changes: 5 additions & 16 deletions numpy/core/_type_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

"""
import warnings
import sys

from numpy.compat import unicode
from numpy._globals import VisibleDeprecationWarning
Expand Down Expand Up @@ -203,22 +202,15 @@ def _set_up_aliases():
('bool_', 'bool'),
('bytes_', 'string'),
('string_', 'string'),
('str_', 'unicode'),
('unicode_', 'unicode'),
('object_', 'object')]
if sys.version_info[0] >= 3:
type_pairs.extend([('str_', 'unicode')])
else:
type_pairs.extend([('str_', 'string')])
for alias, t in type_pairs:
allTypes[alias] = allTypes[t]
sctypeDict[alias] = sctypeDict[t]
# Remove aliases overriding python types and modules
to_remove = ['ulong', 'object', 'int', 'float',
'complex', 'bool', 'string', 'datetime', 'timedelta']
if sys.version_info[0] >= 3:
to_remove.extend(['bytes', 'str'])
else:
to_remove.extend(['unicode', 'long'])
to_remove = ['ulong', 'object', 'int', 'float', 'complex', 'bool',
'string', 'bytes', 'str', 'datetime', 'timedelta']

for t in to_remove:
try:
Expand Down Expand Up @@ -267,11 +259,8 @@ def _set_array_types():


# Add additional strings to the sctypeDict
_toadd = ['int', 'float', 'complex', 'bool', 'object']
if sys.version_info[0] >= 3:
_toadd.extend(['str', 'bytes', ('a', 'bytes_')])
else:
_toadd.extend(['string', ('str', 'string_'), 'unicode', ('a', 'string_')])
_toadd = ['int', 'float', 'complex', 'bool', 'object',
'str', 'bytes', ('a', 'bytes_')]

for name in _toadd:
if isinstance(name, tuple):
Expand Down
7 changes: 1 addition & 6 deletions numpy/core/_ufunc_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
"""
from __future__ import division, absolute_import, print_function

try:
# Accessing collections abstract classes from collections
# has been deprecated since Python 3.3
import collections.abc as collections_abc
except ImportError:
import collections as collections_abc
import collections.abc as collections_abc
import contextlib

from .overrides import set_module
Expand Down
Loading
0