Debian Codegraph @

Lib/test/test_interpreters/test_lifecycle.py

raw
1 import contextlib
2 import json
3 import os
4 import os.path
5 import sys
6 from textwrap import dedent
7 import unittest
8
9 from test import support
10 from test.support import import_helper
11 from test.support import os_helper
12 # Raise SkipTest if subinterpreters not supported.
13 import_helper.import_module('_interpreters')
14 from .utils import TestBase
15
16
17 class StartupTests(TestBase):
18
19 # We want to ensure the initial state of subinterpreters
20 # matches expectations.
21
22 _subtest_count = 0
23
24 @contextlib.contextmanager
25 def subTest(self, *args):
26 with super().subTest(*args) as ctx:
27 self._subtest_count += 1
28 try:
29 yield ctx
30 finally:
31 if self._debugged_in_subtest:
32 if self._subtest_count == 1:
33 # The first subtest adds a leading newline, so we
34 # compensate here by not printing a trailing newline.
35 print('### end subtest debug ###', end='')
36 else:
37 print('### end subtest debug ###')
38 self._debugged_in_subtest = False
39
40 def debug(self, msg, *, header=None):
41 if header:
42 self._debug(f'--- {header} ---')
43 if msg:
44 if msg.endswith(os.linesep):
45 self._debug(msg[:-len(os.linesep)])
46 else:
47 self._debug(msg)
48 self._debug('<no newline>')
49 self._debug('------')
50 else:
51 self._debug(msg)
52
53 _debugged = False
54 _debugged_in_subtest = False
55 def _debug(self, msg):
56 if not self._debugged:
57 print()
58 self._debugged = True
59 if self._subtest is not None:
60 if True:
61 if not self._debugged_in_subtest:
62 self._debugged_in_subtest = True
63 print('### start subtest debug ###')
64 print(msg)
65 else:
66 print(msg)
67
68 def create_temp_dir(self):
69 import tempfile
70 tmp = tempfile.mkdtemp(prefix='test_interpreters_')
71 tmp = os.path.realpath(tmp)
72 self.addCleanup(os_helper.rmtree, tmp)
73 return tmp
74
75 def write_script(self, *path, text):
76 filename = os.path.join(*path)
77 dirname = os.path.dirname(filename)
78 if dirname:
79 os.makedirs(dirname, exist_ok=True)
80 with open(filename, 'w', encoding='utf-8') as outfile:
81 outfile.write(dedent(text))
82 return filename
83
84 @support.requires_subprocess()
85 def run_python(self, argv, *, cwd=None):
86 # This method is inspired by
87 # EmbeddingTestsMixin.run_embedded_interpreter() in test_embed.py.
88 import shlex
89 import subprocess
90 if isinstance(argv, str):
91 argv = shlex.split(argv)
92 argv = [sys.executable, *argv]
93 try:
94 proc = subprocess.run(
95 argv,
96 cwd=cwd,
97 capture_output=True,
98 text=True,
99 )
100 except Exception as exc:
101 self.debug(f'# cmd: {shlex.join(argv)}')
102 if isinstance(exc, FileNotFoundError) and not exc.filename:
103 if os.path.exists(argv[0]):
104 exists = 'exists'
105 else:
106 exists = 'does not exist'
107 self.debug(f'{argv[0]} {exists}')
108 raise # re-raise
109 assert proc.stderr == '' or proc.returncode != 0, proc.stderr
110 if proc.returncode != 0 and support.verbose:
111 self.debug(f'# python3 {shlex.join(argv[1:])} failed:')
112 self.debug(proc.stdout, header='stdout')
113 self.debug(proc.stderr, header='stderr')
114 self.assertEqual(proc.returncode, 0)
115 self.assertEqual(proc.stderr, '')
116 return proc.stdout
117
118 def test_sys_path_0(self):
119 # The main interpreter's sys.path[0] should be used by subinterpreters.
120 script = '''
121 import sys
122 from concurrent import interpreters
123
124 orig = sys.path[0]
125
126 interp = interpreters.create()
127 interp.exec(f"""if True:
128 import json
129 import sys
130 print(json.dumps({{
131 'main': {orig!r},
132 'sub': sys.path[0],
133 }}, indent=4), flush=True)
134 """)
135 interp.close()
136 '''
137 # <tmp>/
138 # pkg/
139 # __init__.py
140 # __main__.py
141 # script.py
142 # script.py
143 cwd = self.create_temp_dir()
144 self.write_script(cwd, 'pkg', '__init__.py', text='')
145 self.write_script(cwd, 'pkg', '__main__.py', text=script)
146 self.write_script(cwd, 'pkg', 'script.py', text=script)
147 self.write_script(cwd, 'script.py', text=script)
148
149 cases = [
150 ('script.py', cwd),
151 ('-m script', cwd),
152 ('-m pkg', cwd),
153 ('-m pkg.script', cwd),
154 ('-c "import script"', ''),
155 ]
156 for argv, expected in cases:
157 with self.subTest(f'python3 {argv}'):
158 out = self.run_python(argv, cwd=cwd)
159 data = json.loads(out)
160 sp0_main, sp0_sub = data['main'], data['sub']
161 self.assertEqual(sp0_sub, sp0_main)
162 self.assertEqual(sp0_sub, expected)
163 # XXX Also check them all with the -P cmdline flag?
164
165
166 class FinalizationTests(TestBase):
167
168 @support.requires_subprocess()
169 def test_gh_109793(self):
170 # Make sure finalization finishes and the correct error code
171 # is reported, even when subinterpreters get cleaned up at the end.
172 import subprocess
173 argv = [sys.executable, '-c', '''if True:
174 from concurrent import interpreters
175 interp = interpreters.create()
176 try:
177 raise Exception
178 finally:
179 interp.close()
180 ''']
181 proc = subprocess.run(argv, capture_output=True, text=True)
182 self.assertIn('Traceback', proc.stderr)
183 if proc.returncode == 0 and support.verbose:
184 print()
185 print("--- cmd unexpected succeeded ---")
186 print(f"stdout:\n{proc.stdout}")
187 print(f"stderr:\n{proc.stderr}")
188 print("------")
189 self.assertEqual(proc.returncode, 1)
190
191
192 if __name__ == '__main__':
193 # Test needs to be a package, so we can do relative imports.
194 unittest.main()