]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36406: Handle namespace packages in doctest (GH-12520)
authorXtreak <tir.karthi@gmail.com>
Fri, 13 Dec 2019 18:06:53 +0000 (23:36 +0530)
committerBrett Cannon <54418+brettcannon@users.noreply.github.com>
Fri, 13 Dec 2019 18:06:53 +0000 (10:06 -0800)
Lib/doctest.py
Lib/test/test_doctest.py
Misc/NEWS.d/next/Library/2019-03-24-12-12-27.bpo-36406.mCEkOl.rst [new file with mode: 0644]

index 8fca6280b8aa6bca3bb8ea9b2afc9bf82ec314c0..02299514bdb5937bc783cba0c1d5a8a6185bf876 100644 (file)
@@ -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,
index f7c399e526d17fda54647d2cbc70cb81e0270119..aa92777efc3c7a815d967946ff77c38b198a7b63 100644 (file)
@@ -701,8 +701,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 (file)
index 0000000..3d81eb5
--- /dev/null
@@ -0,0 +1 @@
+Handle namespace packages in :mod:`doctest`. Patch by Karthikeyan Singaravelan.