diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2014-11-24 21:30:04 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2014-11-24 21:30:04 -0500 |
commit | 414941cd8cb1e157eb1d5f629958f03c49e6be93 (patch) | |
tree | 22d3bd825e2561f80c91562379541b73a6f3edd5 /coverage/execfile.py | |
parent | d182230b96de38b3cd318cf74a84787e1fc9b90d (diff) | |
parent | 84505f77650e7c62ba47da5c2b93d291885e7a9b (diff) | |
download | python-coveragepy-414941cd8cb1e157eb1d5f629958f03c49e6be93.tar.gz |
Merged pull request 42, fixing issue #328.
Diffstat (limited to 'coverage/execfile.py')
-rw-r--r-- | coverage/execfile.py | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/coverage/execfile.py b/coverage/execfile.py index e7e2071..8965d20 100644 --- a/coverage/execfile.py +++ b/coverage/execfile.py @@ -7,6 +7,21 @@ from coverage.backward import PYC_MAGIC_NUMBER, imp, importlib_util_find_spec from coverage.misc import ExceptionDuringRun, NoCode, NoSource +if sys.version_info >= (3, 3): + DEFAULT_FULLNAME = '__main__' +else: + DEFAULT_FULLNAME = None + + +class DummyLoader(object): + """A shim for the pep302 __loader__, emulating pkgutil.ImpLoader. + + Currently only implements the .fullname attribute + """ + def __init__(self, fullname, *args): + self.fullname = fullname + + if importlib_util_find_spec: def find_module(modulename): """Find the module named `modulename`. @@ -91,10 +106,10 @@ def run_python_module(modulename, args): pathname = os.path.abspath(pathname) args[0] = pathname - run_python_file(pathname, args, package=packagename) + run_python_file(pathname, args, package=packagename, modulename=modulename) -def run_python_file(filename, args, package=None): +def run_python_file(filename, args, package=None, modulename=DEFAULT_FULLNAME): """Run a python file as if it were the main program on the command line. `filename` is the path to the file to execute, it need not be a .py file. @@ -110,6 +125,9 @@ def run_python_file(filename, args, package=None): main_mod.__file__ = filename if package: main_mod.__package__ = package + if modulename: + main_mod.__loader__ = DummyLoader(modulename) + main_mod.__builtins__ = BUILTINS # Set sys.argv properly. |