diff options
author | T. Wouters <thomas@python.org> | 2025-01-17 22:43:17 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-17 22:43:17 (GMT) |
commit | d4544cb232dee5f836a64b9126fbbefcbb6099de (patch) | |
tree | 28f8845a69e6c3fca656674d98b17616cd29715e | |
parent | 13c4def692228f09df0b30c5f93bc515e89fc77f (diff) | |
download | cpython-d4544cb232dee5f836a64b9126fbbefcbb6099de.zip cpython-d4544cb232dee5f836a64b9126fbbefcbb6099de.tar.gz cpython-d4544cb232dee5f836a64b9126fbbefcbb6099de.tar.bz2 |
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`.
-rw-r--r-- | Lib/pydoc.py | 3 | ||||
-rw-r--r-- | Lib/test/test_pydoc/test_pydoc.py | 8 |
2 files changed, 10 insertions, 1 deletions
diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 9e84292..922946e 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1435,7 +1435,8 @@ location listed above. # List the built-in subclasses, if any: subclasses = sorted( (str(cls.__name__) for cls in type.__subclasses__(object) - if not cls.__name__.startswith("_") and cls.__module__ == "builtins"), + if (not cls.__name__.startswith("_") and + getattr(cls, '__module__', '') == "builtins")), key=str.lower ) no_of_subclasses = len(subclasses) diff --git a/Lib/test/test_pydoc/test_pydoc.py b/Lib/test/test_pydoc/test_pydoc.py index cec18aa..b02ba3a 100644 --- a/Lib/test/test_pydoc/test_pydoc.py +++ b/Lib/test/test_pydoc/test_pydoc.py @@ -556,6 +556,14 @@ class PydocDocTest(unittest.TestCase): | ... and 82 other subclasses """ doc = pydoc.TextDoc() + try: + # Make sure HeapType, which has no __module__ attribute, is one + # of the known subclasses of object. (doc.docclass() used to + # fail if HeapType was imported before running this test, like + # when running tests sequentially.) + from _testcapi import HeapType + except ImportError: + pass text = doc.docclass(object) snip = (" | Built-in subclasses:\n" " | async_generator\n" |