summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES.rst5
-rw-r--r--coverage/execfile.py2
-rw-r--r--tests/modules/pkg1/__init__.py1
-rw-r--r--tests/test_execfile.py13
-rw-r--r--tests/test_summary.py2
5 files changed, 17 insertions, 6 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 4329615d..f60e502d 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -29,6 +29,10 @@ Version 4.0.1
to coverage.py using their mock inadvertently. This is now fixed, closing
`issue 416`_.
+- Importing a ``__init__`` module explicitly would lead to an error:
+ ``AttributeError: 'module' object has no attribute '__path__'``, as reported
+ in `issue 410`_. This is now fixed.
+
- Code that uses ``sys.settrace(sys.gettrace())`` used to incur a more than 2x
speed penalty. Now there's no penalty at all. Fixes `issue 397`_.
@@ -40,6 +44,7 @@ Version 4.0.1
.. _issue 137: https://bitbucket.org/ned/coveragepy/issues/137/provide-docs-with-source-distribution
.. _issue 397: https://bitbucket.org/ned/coveragepy/issues/397/stopping-and-resuming-coverage-with
+.. _issue 410: https://bitbucket.org/ned/coveragepy/issues/410/attributeerror-module-object-has-no
.. _issue 415: https://bitbucket.org/ned/coveragepy/issues/415/repeated-coveragedataupdates-cause
.. _issue 416: https://bitbucket.org/ned/coveragepy/issues/416/mocking-ospathexists-causes-failures
.. _issue 418: https://bitbucket.org/ned/coveragepy/issues/418/json-parse-error
diff --git a/coverage/execfile.py b/coverage/execfile.py
index 844c11bc..3e20a527 100644
--- a/coverage/execfile.py
+++ b/coverage/execfile.py
@@ -41,7 +41,7 @@ if importlib_util_find_spec:
raise NoSource("No module named %r" % (modulename,))
pathname = spec.origin
packagename = spec.name
- if pathname.endswith("__init__.py"):
+ if pathname.endswith("__init__.py") and not modulename.endswith("__init__"):
mod_main = modulename + ".__main__"
spec = importlib_util_find_spec(mod_main)
if not spec:
diff --git a/tests/modules/pkg1/__init__.py b/tests/modules/pkg1/__init__.py
index 2dfeb9c1..e2e4af5f 100644
--- a/tests/modules/pkg1/__init__.py
+++ b/tests/modules/pkg1/__init__.py
@@ -1,3 +1,4 @@
# This __init__.py has a module-level docstring, which is counted as a
# statement.
"""A simple package for testing with."""
+print("pkg1.__init__: %s" % (__name__,))
diff --git a/tests/test_execfile.py b/tests/test_execfile.py
index a3ea1153..135240f9 100644
--- a/tests/test_execfile.py
+++ b/tests/test_execfile.py
@@ -165,22 +165,27 @@ class RunModuleTest(CoverageTest):
def test_runmod2(self):
run_python_module("pkg1.runmod2", ["runmod2", "hello"])
self.assertEqual(self.stderr(), "")
- self.assertEqual(self.stdout(), "runmod2: passed hello\n")
+ self.assertEqual(self.stdout(), "pkg1.__init__: pkg1\nrunmod2: passed hello\n")
def test_runmod3(self):
run_python_module("pkg1.sub.runmod3", ["runmod3", "hello"])
self.assertEqual(self.stderr(), "")
- self.assertEqual(self.stdout(), "runmod3: passed hello\n")
+ self.assertEqual(self.stdout(), "pkg1.__init__: pkg1\nrunmod3: passed hello\n")
def test_pkg1_main(self):
run_python_module("pkg1", ["pkg1", "hello"])
self.assertEqual(self.stderr(), "")
- self.assertEqual(self.stdout(), "pkg1.__main__: passed hello\n")
+ self.assertEqual(self.stdout(), "pkg1.__init__: pkg1\npkg1.__main__: passed hello\n")
def test_pkg1_sub_main(self):
run_python_module("pkg1.sub", ["pkg1.sub", "hello"])
self.assertEqual(self.stderr(), "")
- self.assertEqual(self.stdout(), "pkg1.sub.__main__: passed hello\n")
+ self.assertEqual(self.stdout(), "pkg1.__init__: pkg1\npkg1.sub.__main__: passed hello\n")
+
+ def test_pkg1_init(self):
+ run_python_module("pkg1.__init__", ["pkg1.__init__", "wut?"])
+ self.assertEqual(self.stderr(), "")
+ self.assertEqual(self.stdout(), "pkg1.__init__: pkg1\npkg1.__init__: __main__\n")
def test_no_such_module(self):
with self.assertRaises(NoSource):
diff --git a/tests/test_summary.py b/tests/test_summary.py
index cc6b3204..cf55130a 100644
--- a/tests/test_summary.py
+++ b/tests/test_summary.py
@@ -588,7 +588,7 @@ class SummaryTest2(CoverageTest):
report = repout.getvalue().replace('\\', '/')
report = re.sub(r"\s+", " ", report)
- self.assertIn("tests/modules/pkg1/__init__.py 1 0 100%", report)
+ self.assertIn("tests/modules/pkg1/__init__.py 2 0 100%", report)
self.assertIn("tests/modules/pkg2/__init__.py 0 0 100%", report)