https://github.com/python/cpython/commit/d4544cb232dee5f836a64b9126fbbefcbb6099de
commit: d4544cb232dee5f836a64b9126fbbefcbb6099de
branch: main
author: T. Wouters <[email protected]>
committer: Yhg1s <[email protected]>
date: 2025-01-17T23:43:17+01:00
summary:

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`.

files:
M Lib/pydoc.py
M Lib/test/test_pydoc/test_pydoc.py

diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index 9e84292aaf825f..922946e5fa7ddb 100644
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -1435,7 +1435,8 @@ def makename(c, m=object.__module__):
         # 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 cec18aa9440c9e..b02ba3aafd4d20 100644
--- a/Lib/test/test_pydoc/test_pydoc.py
+++ b/Lib/test/test_pydoc/test_pydoc.py
@@ -556,6 +556,14 @@ class object
          |      ... 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"

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to