summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_pyclbr.py
diff options
context:
space:
mode:
authorMaksim <130899353+MaximGit1@users.noreply.github.com>2025-04-12 07:46:19 (GMT)
committerGitHub <noreply@github.com>2025-04-12 07:46:19 (GMT)
commit292a7248cda89f497e06eff4aa0147d6ff22f6bb (patch)
tree357a5ab5bb610e129253163a8d5019213c03f35b /Lib/test/test_pyclbr.py
parent2aab2db1461ef49b42549255af16a74b1bf8a5ef (diff)
downloadcpython-292a7248cda89f497e06eff4aa0147d6ff22f6bb.zip
cpython-292a7248cda89f497e06eff4aa0147d6ff22f6bb.tar.gz
cpython-292a7248cda89f497e06eff4aa0147d6ff22f6bb.tar.bz2
gh-131290: ensure that test files can be executed as standalone scripts (#131371)
--------- Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Diffstat (limited to 'Lib/test/test_pyclbr.py')
-rw-r--r--Lib/test/test_pyclbr.py43
1 files changed, 35 insertions, 8 deletions
diff --git a/Lib/test/test_pyclbr.py b/Lib/test/test_pyclbr.py
index 7491098..a9ac133 100644
--- a/Lib/test/test_pyclbr.py
+++ b/Lib/test/test_pyclbr.py
@@ -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')