]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-128923: fix test_pydoc for object subclasses without `__module__` (#128951)
authorT. Wouters <thomas@python.org>
Fri, 17 Jan 2025 22:43:17 +0000 (23:43 +0100)
committerGitHub <noreply@github.com>
Fri, 17 Jan 2025 22:43:17 +0000 (23:43 +0100)
Fix pydoc's docclass() for classes inheriting from object without the `__module__` attribute, like `_testcapi.HeapType`.

Lib/pydoc.py
Lib/test/test_pydoc/test_pydoc.py

index 9e84292aaf825facf023cc2e952c12c4c962db5d..922946e5fa7ddb1307d3fb7a86fac93249cc5ec2 100644 (file)
@@ -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)
index cec18aa9440c9e4116a79b593e61f0e88b8ade0c..b02ba3aafd4d20bcc6e773543b00890dade326e2 100644 (file)
@@ -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"