diff options
author | Buck Golemon <buck@yelp.com> | 2014-11-14 14:00:48 -0800 |
---|---|---|
committer | Buck Golemon <buck@yelp.com> | 2014-11-14 14:00:48 -0800 |
commit | eafbacf36a5ab26e44666c8477f7fdc0284bd068 (patch) | |
tree | 508557e484919c729a4d11b9f5c3309db17c8b83 /coverage/execfile.py | |
parent | f8b9153791455b6d09ec80d507c300f33b1da5a1 (diff) | |
download | python-coveragepy-eafbacf36a5ab26e44666c8477f7fdc0284bd068.tar.gz |
shim for pep302 __loader__
Diffstat (limited to 'coverage/execfile.py')
-rw-r--r-- | coverage/execfile.py | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/coverage/execfile.py b/coverage/execfile.py index 82cc221..f03713e 100644 --- a/coverage/execfile.py +++ b/coverage/execfile.py @@ -7,6 +7,15 @@ from coverage.backward import PYC_MAGIC_NUMBER, imp, importlib_util_find_spec from coverage.misc import ExceptionDuringRun, NoCode, NoSource +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`. @@ -92,10 +101,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=None): """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. @@ -111,6 +120,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. |