From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Fri, 13 Dec 2019 23:30:04 +0000 (-0800) Subject: bpo-36406: Handle namespace packages in doctest (GH-12520) (GH-17592) X-Git-Tag: v3.7.7rc1~138 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d3212036aa2510b9e133ba4bfaf1262d6bcbe7f0;p=thirdparty%2FPython%2Fcpython.git bpo-36406: Handle namespace packages in doctest (GH-12520) (GH-17592) (cherry picked from commit 8289e27393395ee903bd096d42e07c112d7f15c6) Co-authored-by: Xtreak --- diff --git a/Lib/doctest.py b/Lib/doctest.py index 79d91a040c2e..2a4b8c7dae01 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -1059,7 +1059,8 @@ class DocTestFinder: 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 4a3c488738d3..5f891c50d8fc 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -697,8 +697,12 @@ class TestDocTestFinder(unittest.TestCase): 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 000000000000..3d81eb50418b --- /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.