[Python-checkins] bpo-36406: Handle namespace packages in doctest (GH-12520)

Brett Cannon webhook-mailer at python.org
Fri Dec 13 13:07:02 EST 2019


https://github.com/python/cpython/commit/8289e27393395ee903bd096d42e07c112d7f15c6
commit: 8289e27393395ee903bd096d42e07c112d7f15c6
branch: master
author: Xtreak <tir.karthi at gmail.com>
committer: Brett Cannon <54418+brettcannon at users.noreply.github.com>
date: 2019-12-13T10:06:53-08:00
summary:

bpo-36406: Handle namespace packages in doctest (GH-12520)

files:
A Misc/NEWS.d/next/Library/2019-03-24-12-12-27.bpo-36406.mCEkOl.rst
M Lib/doctest.py
M Lib/test/test_doctest.py

diff --git a/Lib/doctest.py b/Lib/doctest.py
index 8fca6280b8aa6..02299514bdb59 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -1059,7 +1059,8 @@ def _get_test(self, obj, name, module, globs, source_lines):
         if module is None:
             filename = None
         else:
-            filename = getattr(module, '__file__', module.__name__)
+            # __file__ can be None for namespace packages.
+            filename = getattr(module, '__file__', None) or module.__name__
             if filename[-4:] == ".pyc":
                 filename = filename[:-1]
         return self._parser.get_doctest(docstring, globs, name,
diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py
index f7c399e526d17..aa92777efc3c7 100644
--- a/Lib/test/test_doctest.py
+++ b/Lib/test/test_doctest.py
@@ -701,8 +701,12 @@ def test_empty_namespace_package(self):
             finally:
                 support.forget(pkg_name)
                 sys.path.pop()
-            assert doctest.DocTestFinder().find(mod) == []
 
+            include_empty_finder = doctest.DocTestFinder(exclude_empty=False)
+            exclude_empty_finder = doctest.DocTestFinder(exclude_empty=True)
+
+            self.assertEqual(len(include_empty_finder.find(mod)), 1)
+            self.assertEqual(len(exclude_empty_finder.find(mod)), 0)
 
 def test_DocTestParser(): r"""
 Unit tests for the `DocTestParser` class.
diff --git a/Misc/NEWS.d/next/Library/2019-03-24-12-12-27.bpo-36406.mCEkOl.rst b/Misc/NEWS.d/next/Library/2019-03-24-12-12-27.bpo-36406.mCEkOl.rst
new file mode 100644
index 0000000000000..3d81eb50418b0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-03-24-12-12-27.bpo-36406.mCEkOl.rst
@@ -0,0 +1 @@
+Handle namespace packages in :mod:`doctest`. Patch by Karthikeyan Singaravelan.



More information about the Python-checkins mailing list