Skip to content

Commit 5a8c1f2

Browse files
committed
pythongh-113360: Fix doctest failure when __test__ is set to falsy value
1 parent c31943a commit 5a8c1f2

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/doctest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ def _find(self, tests, obj, name, module, source_lines, globs, seen):
10401040

10411041
# Look for tests in a module's __test__ dictionary.
10421042
if inspect.ismodule(obj) and self._recurse:
1043-
for valname, val in getattr(obj, '__test__', {}).items():
1043+
for valname, val in (getattr(obj, '__test__', None) or {}).items():
10441044
if not isinstance(valname, str):
10451045
raise ValueError("DocTestFinder.find: __test__ keys "
10461046
"must be strings: %r" %

Lib/test/test_doctest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,24 @@ def basics(): r"""
597597
2 some_module.__test__.d
598598
1 some_module.sample_func
599599
600+
When ``__test__`` is falsy, this value should be ignored:
601+
602+
>>> import types
603+
>>> m = types.ModuleType('falsy_magic_test')
604+
>>> m.__dict__.update({'__test__': None})
605+
606+
>>> finder = doctest.DocTestFinder()
607+
>>> # Use module=test.test_doctest, to prevent doctest from
608+
>>> # ignoring the objects since they weren't defined in m.
609+
>>> import test.test_doctest
610+
>>> finder.find(m, module=test.test_doctest)
611+
[]
612+
613+
>>> m.__dict__.update({'__test__': False})
614+
>>> finder.find(m, module=test.test_doctest)
615+
[]
616+
617+
600618
Duplicate Removal
601619
~~~~~~~~~~~~~~~~~
602620
If a single object is listed twice (under different names), then tests
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix doctest failure when ``__test__`` magic var is set to a falsy value.

0 commit comments

Comments
 (0)