summaryrefslogtreecommitdiff
path: root/coverage/control.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2017-05-04 21:18:43 -0400
committerNed Batchelder <ned@nedbatchelder.com>2017-05-04 21:18:43 -0400
commit403e51068a123733740a094e40cdd31ceb5e7c61 (patch)
treea27d665d77aeea97cec1d45e932347969dbc1930 /coverage/control.py
parent67f905e4b7002d71d7ab33b87089a876eb4ef66c (diff)
downloadpython-coveragepy-git-403e51068a123733740a094e40cdd31ceb5e7c61.tar.gz
Don't warn that namespace packages have no code. #572
--HG-- extra : amend_source : 68f6e0ab140e77ede11bb40dc2ac515cfb6f2333
Diffstat (limited to 'coverage/control.py')
-rw-r--r--coverage/control.py44
1 files changed, 32 insertions, 12 deletions
diff --git a/coverage/control.py b/coverage/control.py
index fb033610..2cbe491b 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -837,18 +837,7 @@ class Coverage(object):
# then we never encountered those packages.
if self._warn_unimported_source:
for pkg in self.source_pkgs_unmatched:
- if pkg not in sys.modules:
- self._warn("Module %s was never imported." % pkg, slug="module-not-imported")
- elif not (
- hasattr(sys.modules[pkg], '__file__') and
- os.path.exists(sys.modules[pkg].__file__)
- ):
- self._warn("Module %s has no Python source." % pkg, slug="module-not-python")
- else:
- self._warn(
- "Module %s was previously imported, but not measured." % pkg,
- slug="module-not-measured",
- )
+ self._warn_about_unmeasured_code(pkg)
# Find out if we got any data.
if not self.data and self._warn_no_data:
@@ -869,6 +858,37 @@ class Coverage(object):
if self.config.note:
self.data.add_run_info(note=self.config.note)
+ def _warn_about_unmeasured_code(self, pkg):
+ """Warn about a package or module that we never traced.
+
+ `pkg` is a string, the name of the package or module.
+
+ """
+ mod = sys.modules.get(pkg)
+ if mod is None:
+ self._warn("Module %s was never imported." % pkg, slug="module-not-imported")
+ return
+
+ is_namespace = hasattr(mod, '__path__') and not hasattr(mod, '__file__')
+ has_file = hasattr(mod, '__file__') and os.path.exists(mod.__file__)
+
+ if is_namespace:
+ # A namespace package. It's OK for this not to have been traced,
+ # since there is no code directly in it.
+ return
+
+ if not has_file:
+ self._warn("Module %s has no Python source." % pkg, slug="module-not-python")
+ return
+
+ # The module was in sys.modules, and seems like a module with code, but
+ # we never measured it. I guess that means it was imported before
+ # coverage even started.
+ self._warn(
+ "Module %s was previously imported, but not measured." % pkg,
+ slug="module-not-measured",
+ )
+
def _find_plugin_files(self, src_dir):
"""Get executable files from the plugins."""
for plugin in self.plugins: