diff options
-rw-r--r-- | coverage/backward.py | 8 | ||||
-rw-r--r-- | tests/coveragetest.py | 14 |
2 files changed, 13 insertions, 9 deletions
diff --git a/coverage/backward.py b/coverage/backward.py index 81ca342f..4fc72215 100644 --- a/coverage/backward.py +++ b/coverage/backward.py @@ -151,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: @@ -163,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: diff --git a/tests/coveragetest.py b/tests/coveragetest.py index 1c0dd681..3468b794 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -81,20 +81,22 @@ class CoverageTest( if os.path.exists("__pycache__"): shutil.rmtree("__pycache__") - def import_local_file(self, modname): + def import_local_file(self, 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. """ - return import_local_file(modname) + return import_local_file(modname, modfile) - def start_import_stop(self, cov, modname): + def start_import_stop(self, cov, modname, modfile=None): """Start coverage, import a file, then stop coverage. `cov` is started and stopped, with an `import_local_file` of - `modname` in the middle. + `modname` in the middle. `modfile` is the file to import as `modname` + if it isn't in the current directory. The imported module is returned. @@ -102,7 +104,7 @@ class CoverageTest( cov.start() try: # pragma: nested # Import the Python file, executing it. - mod = self.import_local_file(modname) + mod = self.import_local_file(modname, modfile) finally: # pragma: nested # Stop coverage.py. cov.stop() |