summaryrefslogtreecommitdiff
path: root/coverage/execfile.py
diff options
context:
space:
mode:
authorBuck Golemon <buck@yelp.com>2014-11-14 14:00:48 -0800
committerBuck Golemon <buck@yelp.com>2014-11-14 14:00:48 -0800
commit7ca11054c5b0f91bd77c16923ec443ccee6f2dd9 (patch)
treeac1ca3c09b68289bf9e9bb63b58e6e1989d331bb /coverage/execfile.py
parentd05360a060187452f49302467f87ead09d27c9ba (diff)
downloadpython-coveragepy-git-7ca11054c5b0f91bd77c16923ec443ccee6f2dd9.tar.gz
shim for pep302 __loader__
--HG-- branch : __main__-support extra : histedit_source : f07b26f0cfc575d81972546f1b7ae2ece84e2d87
Diffstat (limited to 'coverage/execfile.py')
-rw-r--r--coverage/execfile.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/coverage/execfile.py b/coverage/execfile.py
index 82cc2217..f03713ec 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.