diff options
Diffstat (limited to 'coverage/backward.py')
-rw-r--r-- | coverage/backward.py | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/coverage/backward.py b/coverage/backward.py index 58d9cfea..4fc72215 100644 --- a/coverage/backward.py +++ b/coverage/backward.py @@ -1,3 +1,6 @@ +# 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. @@ -16,6 +19,12 @@ try: except ImportError: from io import StringIO +# In py3, ConfigParser was renamed to the more-standard configparser +try: + import configparser +except ImportError: + import ConfigParser as configparser + # What's a string called? try: string_class = basestring @@ -40,6 +49,15 @@ try: except NameError: range = range +# 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: {}.iteritems @@ -133,11 +151,12 @@ except AttributeError: PYC_MAGIC_NUMBER = imp.get_magic() -def import_local_file(modname): +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 +164,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 +175,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 |