summaryrefslogtreecommitdiff
path: root/coverage
diff options
context:
space:
mode:
Diffstat (limited to 'coverage')
-rw-r--r--coverage/control.py45
-rw-r--r--coverage/execfile.py16
2 files changed, 31 insertions, 30 deletions
diff --git a/coverage/control.py b/coverage/control.py
index 346f655f..bcb18231 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -129,6 +129,7 @@ class Coverage(object):
# The matchers for _should_trace.
self.source_match = None
+ self.source_pkgs_match = None
self.pylib_match = self.cover_match = None
self.include_match = self.omit_match = None
@@ -305,36 +306,36 @@ class Coverage(object):
filename = filename[:-9] + ".py"
return filename
- def _name_for_module(self, module_namespace, filename):
- """
- For configurability's sake, we allow __main__ modules to be matched by their importable name.
+ def _name_for_module(self, module_globals, filename):
+ """Get the name of the module for a set of globals and filename.
+
+ For configurability's sake, we allow __main__ modules to be matched by
+ their importable name.
+
+ If loaded via runpy (aka -m), we can usually recover the "original" full
+ dotted module name, otherwise, we resort to interpreting the filename to
+ get the module's name. In the case that the module name can't be
+ deteremined, None is returned.
- If loaded via runpy (aka -m), we can usually recover the "original" full dotted module name,
- otherwise, we resort to interpreting the filename to get the module's name.
- In the case that the module name can't be deteremined, None is returned.
"""
- # TODO: unit-test
- dunder_name = module_namespace.get('__name__', None)
+ dunder_name = module_globals.get('__name__', None)
if isinstance(dunder_name, str) and dunder_name != '__main__':
- # this is the usual case: an imported module
+ # This is the usual case: an imported module.
return dunder_name
- loader = module_namespace.get('__loader__', None)
+ loader = module_globals.get('__loader__', None)
for attrname in ('fullname', 'name'): # attribute renamed in py3.2
if hasattr(loader, attrname):
fullname = getattr(loader, attrname)
else:
continue
- if (
- isinstance(fullname, str) and
- fullname != '__main__'
- ):
- # module loaded via runpy -m
+ if isinstance(fullname, str) and fullname != '__main__':
+ # Module loaded via: runpy -m
return fullname
- # script as first argument to python cli
+ # Script as first argument to Python CLI.
inspectedname = inspect.getmodulename(filename)
if inspectedname is not None:
return inspectedname
@@ -675,12 +676,12 @@ class Coverage(object):
):
self._warn("Module %s has no Python source." % pkg)
else:
- raise AssertionError('''\
-Unexpected third case:
- name: %s
- object: %r
- __file__: %s''' % (pkg, sys.modules[pkg], sys.modules[pkg].__file__)
- )
+ raise AssertionError(
+ "Unexpected third case: name = %s, "
+ "object = %r, "
+ "__file__ = %s" % (
+ pkg, sys.modules[pkg], sys.modules[pkg].__file__
+ ))
# Find out if we got any data.
summary = self.data.summary()
diff --git a/coverage/execfile.py b/coverage/execfile.py
index 8965d207..f08b7589 100644
--- a/coverage/execfile.py
+++ b/coverage/execfile.py
@@ -7,18 +7,12 @@ 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):
+ def __init__(self, fullname, *_args):
self.fullname = fullname
@@ -109,7 +103,7 @@ def run_python_module(modulename, args):
run_python_file(pathname, args, package=packagename, modulename=modulename)
-def run_python_file(filename, args, package=None, modulename=DEFAULT_FULLNAME):
+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.
@@ -117,7 +111,13 @@ def run_python_file(filename, args, package=None, modulename=DEFAULT_FULLNAME):
element naming the file being executed. `package` is the name of the
enclosing package, if any.
+ `modulename` is the name of the module the file was run as.
+
"""
+ if modulename is None and sys.version_info >= (3, 3):
+ modulename = '__main__'
+
+
# Create a module to serve as __main__
old_main_mod = sys.modules['__main__']
main_mod = types.ModuleType('__main__')