diff options
Diffstat (limited to 'coverage/backward.py')
-rw-r--r-- | coverage/backward.py | 52 |
1 files changed, 35 insertions, 17 deletions
diff --git a/coverage/backward.py b/coverage/backward.py index 58d9cfea..8d2218d2 100644 --- a/coverage/backward.py +++ b/coverage/backward.py @@ -1,9 +1,10 @@ +# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 +# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt + """Add things to old Pythons so I can pretend they are newer.""" -# This file does lots of tricky stuff, so disable a bunch of pylint warnings. -# pylint: disable=redefined-builtin +# This file does tricky stuff, so disable a pylint warning. # pylint: disable=unused-import -# pylint: disable=no-name-in-module import sys @@ -16,6 +17,15 @@ try: except ImportError: from io import StringIO +# In py3, ConfigParser was renamed to the more-standard configparser. +# But there's a py3 backport that installs "configparser" in py2, and I don't +# want it because it has annoying deprecation warnings. So try the real py2 +# import first. +try: + import ConfigParser as configparser +except ImportError: + import configparser + # What's a string called? try: string_class = basestring @@ -36,9 +46,18 @@ except ImportError: # range or xrange? try: - range = xrange + range = xrange # pylint: disable=redefined-builtin except NameError: - range = range + range = range # pylint: disable=redefined-variable-type + +# shlex.quote is new, but there's an undocumented implementation in "pipes", +# who knew!? +try: + from shlex import quote as shlex_quote +except ImportError: + # Useful function, available under a different (undocumented) name + # in Python versions earlier than 3.3. + from pipes import quote as shlex_quote # A function to iterate listlessly over a dict's items. try: @@ -75,10 +94,6 @@ if env.PY3: """Produce a byte string with the ints from `byte_values`.""" return bytes(byte_values) - def byte_to_int(byte_value): - """Turn an element of a bytes object into an int.""" - return byte_value - def bytes_to_ints(bytes_value): """Turn a bytes object into a sequence of ints.""" # In Python 3, iterating bytes gives ints. @@ -93,10 +108,6 @@ else: """Produce a byte string with the ints from `byte_values`.""" return "".join(chr(b) for b in byte_values) - def byte_to_int(byte_value): - """Turn an element of a bytes object into an int.""" - return ord(byte_value) - def bytes_to_ints(bytes_value): """Turn a bytes object into a sequence of ints.""" for byte in bytes_value: @@ -133,11 +144,18 @@ except AttributeError: PYC_MAGIC_NUMBER = imp.get_magic() -def import_local_file(modname): +def invalidate_import_caches(): + """Invalidate any import caches that may or may not exist.""" + if importlib and hasattr(importlib, "invalidate_caches"): + importlib.invalidate_caches() + + +def import_local_file(modname, modfile=None): """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. + as `modname`, and returns the module object. `modfile` is the file to + import if it isn't in the current directory. """ try: @@ -145,7 +163,8 @@ def import_local_file(modname): except ImportError: SourceFileLoader = None - modfile = modname + '.py' + if modfile is None: + modfile = modname + '.py' if SourceFileLoader: mod = SourceFileLoader(modname, modfile).load_module() else: @@ -155,7 +174,6 @@ def import_local_file(modname): with open(modfile, 'r') as f: # pylint: disable=undefined-loop-variable - # (Using possibly undefined loop variable 'suff') mod = imp.load_module(modname, f, modfile, suff) return mod |