diff options
-rw-r--r-- | numpy/compat/py3k.py | 169 |
1 files changed, 51 insertions, 118 deletions
diff --git a/numpy/compat/py3k.py b/numpy/compat/py3k.py index f24a8af27..19d219ed5 100644 --- a/numpy/compat/py3k.py +++ b/numpy/compat/py3k.py @@ -18,76 +18,48 @@ __all__ = ['bytes', 'asbytes', 'isfileobj', 'getexception', 'strchar', import sys import os -try: - from pathlib import Path, PurePath -except ImportError: - Path = PurePath = None - -if sys.version_info[0] >= 3: - import io - - try: - import pickle5 as pickle - except ImportError: - import pickle +from pathlib import Path, PurePath +import io - 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) - - def asbytes(s): - if isinstance(s, bytes): - return s - return str(s).encode('latin1') +import abc +from abc import ABC as abc_ABC - def asstr(s): - if isinstance(s, bytes): - return s.decode('latin1') - return str(s) +try: + import pickle5 as pickle +except ImportError: + import pickle - def isfileobj(f): - return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter)) +long = int +integer_types = (int,) +basestring = str +unicode = str +bytes = bytes - def open_latin1(filename, mode='r'): - return open(filename, mode=mode, encoding='iso-8859-1') +def asunicode(s): + if isinstance(s, bytes): + return s.decode('latin1') + return str(s) - 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 open_latin1(filename, mode='r'): + return open(filename, mode=mode, encoding='iso-8859-1') - def asunicode(s): - if isinstance(s, unicode): - return s - return str(s).decode('ascii') +def sixu(s): + return s - def open_latin1(filename, mode='r'): - return open(filename, mode=mode) - - def sixu(s): - return unicode(s, 'unicode_escape') +strchar = 'U' def getexception(): return sys.exc_info()[1] @@ -134,69 +106,30 @@ class contextlib_nullcontext(object): 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. - - .. versionadded:: 1.11.2 +def npy_load_module(name, fn, info=None): + """ + Load a module. - 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). + .. versionadded:: 1.11.2 - Returns - ------- - mod : module + Parameters + ---------- + name : str + Full module name. + fn : str + Path to module file. + info : tuple, optional + Only here for backward compatibility with Python 2.*. - """ - 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 + Returns + ------- + mod : 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__': ()}) + """ + # Explicitly lazy import this to avoid paying the cost + # of importing importlib at startup + from importlib.machinery import SourceFileLoader + return SourceFileLoader(name, fn).load_module() # Backport os.fs_path, os.PathLike, and PurePath.__fspath__ |