]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-131290: ensure that test files can be executed as standalone scripts (#131371)
authorMaksim <130899353+MaximGit1@users.noreply.github.com>
Sat, 12 Apr 2025 07:46:19 +0000 (10:46 +0300)
committerGitHub <noreply@github.com>
Sat, 12 Apr 2025 07:46:19 +0000 (07:46 +0000)
---------

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Lib/test/test_metaclass.py
Lib/test/test_pyclbr.py
Lib/test/test_regrtest.py
Misc/NEWS.d/next/Tests/2025-03-17-19-47-27.gh-issue-131290.NyCIXR.rst [new file with mode: 0644]

index 07a333f98fa0a9393c785acc15ae798adffedb12..627c29fdb18ab3515bc274fb6208c8877e07b094 100644 (file)
@@ -297,4 +297,6 @@ def load_tests(loader, tests, pattern):
 
 
 if __name__ == "__main__":
+    # set __name__ to match doctest expectations
+    __name__ = "test.test_metaclass"
     unittest.main()
index 749109805677631cb05cc79ce39658480b118fad..a9ac13395a8fac69cc377dbf58b8d9db9d91e35a 100644 (file)
@@ -3,7 +3,9 @@
    Nick Mathewson
 '''
 
+import importlib.machinery
 import sys
+from contextlib import contextmanager
 from textwrap import dedent
 from types import FunctionType, MethodType, BuiltinFunctionType
 import pyclbr
@@ -22,6 +24,29 @@ ClassMethodType = type(classmethod(lambda c: None))
 # is imperfect (as designed), testModule is called with a set of
 # members to ignore.
 
+
+@contextmanager
+def temporary_main_spec():
+    """
+    A context manager that temporarily sets the `__spec__` attribute
+    of the `__main__` module if it's missing.
+    """
+    main_mod = sys.modules.get("__main__")
+    if main_mod is None:
+        yield  # Do nothing if __main__ is not present
+        return
+
+    original_spec = getattr(main_mod, "__spec__", None)
+    if original_spec is None:
+        main_mod.__spec__ = importlib.machinery.ModuleSpec(
+            name="__main__", loader=None, origin="built-in"
+        )
+    try:
+        yield
+    finally:
+        main_mod.__spec__ = original_spec
+
+
 class PyclbrTest(TestCase):
 
     def assertListEq(self, l1, l2, ignore):
@@ -145,8 +170,9 @@ class PyclbrTest(TestCase):
         self.checkModule('pyclbr')
         # XXX: Metaclasses are not supported
         # self.checkModule('ast')
-        self.checkModule('doctest', ignore=("TestResults", "_SpoofOut",
-                                            "DocTestCase", '_DocTestSuite'))
+        with temporary_main_spec():
+            self.checkModule('doctest', ignore=("TestResults", "_SpoofOut",
+                                                "DocTestCase", '_DocTestSuite'))
         self.checkModule('difflib', ignore=("Match",))
 
     def test_cases(self):
@@ -223,12 +249,13 @@ class PyclbrTest(TestCase):
         with warnings.catch_warnings():
             warnings.simplefilter('ignore', DeprecationWarning)
             cm('sre_parse', ignore=('dump', 'groups', 'pos')) # from sre_constants import *; property
-        cm(
-            'pdb',
-            # pyclbr does not handle elegantly `typing` or properties
-            ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'curframe_locals'),
-        )
-        cm('pydoc', ignore=('input', 'output',)) # properties
+        with temporary_main_spec():
+            cm(
+                'pdb',
+                # pyclbr does not handle elegantly `typing` or properties
+                ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'curframe_locals'),
+            )
+        cm('pydoc', ignore=('input', 'output',))  # properties
 
         # Tests for modules inside packages
         cm('email.parser')
index 510c8f69631bacdf56681deff0653eff0ce9f331..7e317d5ab943ff5ab62ea3538ed9168292aef78c 100644 (file)
@@ -2546,4 +2546,5 @@ class TestColorized(unittest.TestCase):
 
 
 if __name__ == '__main__':
+    setup.setup_process()
     unittest.main()
diff --git a/Misc/NEWS.d/next/Tests/2025-03-17-19-47-27.gh-issue-131290.NyCIXR.rst b/Misc/NEWS.d/next/Tests/2025-03-17-19-47-27.gh-issue-131290.NyCIXR.rst
new file mode 100644 (file)
index 0000000..c6f0e0c
--- /dev/null
@@ -0,0 +1 @@
+Tests in :file:`Lib/test` can now be correctly executed as standalone scripts.