Skip to content

Commit d4544cb

Browse files
authored
gh-128923: fix test_pydoc for object subclasses without __module__ (#128951)
Fix pydoc's docclass() for classes inheriting from object without the `__module__` attribute, like `_testcapi.HeapType`.
1 parent 13c4def commit d4544cb

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Lib/pydoc.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,8 @@ def makename(c, m=object.__module__):
14351435
# List the built-in subclasses, if any:
14361436
subclasses = sorted(
14371437
(str(cls.__name__) for cls in type.__subclasses__(object)
1438-
if not cls.__name__.startswith("_") and cls.__module__ == "builtins"),
1438+
if (not cls.__name__.startswith("_") and
1439+
getattr(cls, '__module__', '') == "builtins")),
14391440
key=str.lower
14401441
)
14411442
no_of_subclasses = len(subclasses)

Lib/test/test_pydoc/test_pydoc.py

+8
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,14 @@ class object
556556
| ... and 82 other subclasses
557557
"""
558558
doc = pydoc.TextDoc()
559+
try:
560+
# Make sure HeapType, which has no __module__ attribute, is one
561+
# of the known subclasses of object. (doc.docclass() used to
562+
# fail if HeapType was imported before running this test, like
563+
# when running tests sequentially.)
564+
from _testcapi import HeapType
565+
except ImportError:
566+
pass
559567
text = doc.docclass(object)
560568
snip = (" | Built-in subclasses:\n"
561569
" | async_generator\n"

0 commit comments

Comments
 (0)