summaryrefslogtreecommitdiff
path: root/coverage
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2018-03-05 18:19:43 -0500
committerNed Batchelder <ned@nedbatchelder.com>2018-03-05 18:19:43 -0500
commit421062f1ff81dafa8a391458c753f3608035c342 (patch)
treea0197c6281b199b3c5b4d6b16b90a7cd3ac0afbf /coverage
parentcff5b3620b9e7d0a40445f5dfe3e9fb74e794979 (diff)
downloadpython-coveragepy-421062f1ff81dafa8a391458c753f3608035c342.tar.gz
In 3.7, namespace modules can have: mod.__file__ is None
Diffstat (limited to 'coverage')
-rw-r--r--coverage/inorout.py23
-rw-r--r--coverage/python.py2
2 files changed, 17 insertions, 8 deletions
diff --git a/coverage/inorout.py b/coverage/inorout.py
index 7bb89f1..640b4e6 100644
--- a/coverage/inorout.py
+++ b/coverage/inorout.py
@@ -94,6 +94,19 @@ def name_for_module(filename, frame):
return dunder_name
+def module_is_namespace(mod):
+ """Is the module object `mod` a PEP420 namespace module?"""
+ return hasattr(mod, '__path__') and getattr(mod, '__file__', None) is None
+
+
+def module_has_file(mod):
+ """Does the module object `mod` have an existing __file__ ?"""
+ mod__file__ = getattr(mod, '__file__', None)
+ if mod__file__ is None:
+ return False
+ return os.path.exists(mod__file__)
+
+
class InOrOut(object):
def __init__(self, warn):
self.warn = warn
@@ -359,15 +372,12 @@ class InOrOut(object):
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:
+ if module_is_namespace(mod):
# 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:
+ if not module_has_file(mod):
self.warn("Module %s has no Python source." % pkg, slug="module-not-python")
return
@@ -382,8 +392,7 @@ class InOrOut(object):
def find_unexecuted_files(self):
for pkg in self.source_pkgs:
if (not pkg in sys.modules or
- not hasattr(sys.modules[pkg], '__file__') or
- not os.path.exists(sys.modules[pkg].__file__)):
+ not module_has_file(sys.modules[pkg])):
continue
pkg_file = source_for_file(sys.modules[pkg].__file__)
for ret in self._find_unexecuted_files(canonical_path(pkg_file)):
diff --git a/coverage/python.py b/coverage/python.py
index 5edfc54..834bc33 100644
--- a/coverage/python.py
+++ b/coverage/python.py
@@ -131,7 +131,7 @@ def source_for_file(filename):
def source_for_morf(morf):
"""Get the source filename for the module-or-file `morf`."""
- if hasattr(morf, '__file__'):
+ if hasattr(morf, '__file__') and morf.__file__:
filename = morf.__file__
elif isinstance(morf, types.ModuleType):
# A module should have had .__file__, otherwise we can't use it.