Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit292a724

Browse files
MaximGit1picnixz
andauthored
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>
1 parent2aab2db commit292a724

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

‎Lib/test/test_metaclass.py‎

Lines changed: 2 additions & 0 deletions
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‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
Nick Mathewson
44
'''
55

6+
importimportlib.machinery
67
importsys
8+
fromcontextlibimportcontextmanager
79
fromtextwrapimportdedent
810
fromtypesimportFunctionType,MethodType,BuiltinFunctionType
911
importpyclbr
@@ -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+
deftemporary_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+
ifmain_modisNone:
36+
yield# Do nothing if __main__ is not present
37+
return
38+
39+
original_spec=getattr(main_mod,"__spec__",None)
40+
iforiginal_specisNone:
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
classPyclbrTest(TestCase):
2651

2752
defassertListEq(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+
withtemporary_main_spec():
174+
self.checkModule('doctest',ignore=("TestResults","_SpoofOut",
175+
"DocTestCase",'_DocTestSuite'))
150176
self.checkModule('difflib',ignore=("Match",))
151177

152178
deftest_cases(self):
@@ -223,12 +249,13 @@ def test_others(self):
223249
withwarnings.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+
withtemporary_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‎

Lines changed: 1 addition & 0 deletions
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()
Lines changed: 1 addition & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp