diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2014-05-17 14:56:40 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2014-05-17 14:56:40 -0400 |
commit | 147c3562631c64066a2da964522a66fb369841c4 (patch) | |
tree | d346f6567216c2c864aea1a39f637a6ee89110f9 /coverage/backward.py | |
parent | 503cdc25c6e5410e920053e3c4a819e962c4f6e8 (diff) | |
download | python-coveragepy-147c3562631c64066a2da964522a66fb369841c4.tar.gz |
Avoid a bunch of deprecated functions.
Diffstat (limited to 'coverage/backward.py')
-rw-r--r-- | coverage/backward.py | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/coverage/backward.py b/coverage/backward.py index 98407e3..a7888a2 100644 --- a/coverage/backward.py +++ b/coverage/backward.py @@ -135,7 +135,7 @@ except KeyError: BUILTINS = sys.modules['builtins'] -# imp was deprecated in Python 3.4 +# imp was deprecated in Python 3.3 try: import importlib, importlib.util imp = None @@ -144,12 +144,40 @@ except ImportError: # we only want to use importlib if it has everything we need. try: - importlib.util.find_spec + importlib_util_find_spec = importlib.util.find_spec except Exception: import imp - importlib = None + importlib_util_find_spec = None try: PYC_MAGIC_NUMBER = importlib.util.MAGIC_NUMBER except AttributeError: PYC_MAGIC_NUMBER = imp.get_magic() + + +def import_local_file(modname): + """Import a local file as a module. + + Opens a file in the current directory named `modname`.py, imports it + as `modname`, and returns the module object. + + """ + try: + from importlib.machinery import SourceFileLoader + except ImportError: + SourceFileLoader = None + + modfile = modname + '.py' + if SourceFileLoader: + mod = SourceFileLoader(modname, modfile).load_module() + else: + for suff in imp.get_suffixes(): + if suff[0] == '.py': + break + + with open(modfile, 'r') as f: + # pylint: disable=W0631 + # (Using possibly undefined loop variable 'suff') + mod = imp.load_module(modname, f, modfile, suff) + + return mod |