Skip to content

Commit 292a724

Browse files
MaximGit1picnixz
andauthored
gh-131290: ensure that test files can be executed as standalone scripts (#131371)
--------- Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 2aab2db commit 292a724

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

Lib/test/test_metaclass.py

+2
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,6 @@ def load_tests(loader, tests, pattern):
297297

298298

299299
if __name__ == "__main__":
300+
# set __name__ to match doctest expectations
301+
__name__ = "test.test_metaclass"
300302
unittest.main()

Lib/test/test_pyclbr.py

+35-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
Nick Mathewson
44
'''
55

6+
import importlib.machinery
67
import sys
8+
from contextlib import contextmanager
79
from textwrap import dedent
810
from types import FunctionType, MethodType, BuiltinFunctionType
911
import pyclbr
@@ -22,6 +24,29 @@
2224
# is imperfect (as designed), testModule is called with a set of
2325
# members to ignore.
2426

27+
28+
@contextmanager
29+
def temporary_main_spec():
30+
"""
31+
A context manager that temporarily sets the `__spec__` attribute
32+
of the `__main__` module if it's missing.
33+
"""
34+
main_mod = sys.modules.get("__main__")
35+
if main_mod is None:
36+
yield # Do nothing if __main__ is not present
37+
return
38+
39+
original_spec = getattr(main_mod, "__spec__", None)
40+
if original_spec is None:
41+
main_mod.__spec__ = importlib.machinery.ModuleSpec(
42+
name="__main__", loader=None, origin="built-in"
43+
)
44+
try:
45+
yield
46+
finally:
47+
main_mod.__spec__ = original_spec
48+
49+
2550
class PyclbrTest(TestCase):
2651

2752
def assertListEq(self, l1, l2, ignore):
@@ -145,8 +170,9 @@ def test_easy(self):
145170
self.checkModule('pyclbr')
146171
# XXX: Metaclasses are not supported
147172
# self.checkModule('ast')
148-
self.checkModule('doctest', ignore=("TestResults", "_SpoofOut",
149-
"DocTestCase", '_DocTestSuite'))
173+
with temporary_main_spec():
174+
self.checkModule('doctest', ignore=("TestResults", "_SpoofOut",
175+
"DocTestCase", '_DocTestSuite'))
150176
self.checkModule('difflib', ignore=("Match",))
151177

152178
def test_cases(self):
@@ -223,12 +249,13 @@ def test_others(self):
223249
with warnings.catch_warnings():
224250
warnings.simplefilter('ignore', DeprecationWarning)
225251
cm('sre_parse', ignore=('dump', 'groups', 'pos')) # from sre_constants import *; property
226-
cm(
227-
'pdb',
228-
# pyclbr does not handle elegantly `typing` or properties
229-
ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'curframe_locals'),
230-
)
231-
cm('pydoc', ignore=('input', 'output',)) # properties
252+
with temporary_main_spec():
253+
cm(
254+
'pdb',
255+
# pyclbr does not handle elegantly `typing` or properties
256+
ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'curframe_locals'),
257+
)
258+
cm('pydoc', ignore=('input', 'output',)) # properties
232259

233260
# Tests for modules inside packages
234261
cm('email.parser')

Lib/test/test_regrtest.py

+1
Original file line numberDiff line numberDiff line change
@@ -2546,4 +2546,5 @@ def test_test_result_get_state(self):
25462546

25472547

25482548
if __name__ == '__main__':
2549+
setup.setup_process()
25492550
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Tests in :file:`Lib/test` can now be correctly executed as standalone scripts.

0 commit comments

Comments
 (0)