|
1 | 1 | """Test suite for the cProfile module.""" |
2 | | - |
3 | 2 | import sys |
4 | 3 | import unittest |
5 | 4 |
|
@@ -171,6 +170,40 @@ class Foo: |
171 | 170 | f.close() |
172 | 171 | assert_python_ok('-m', "cProfile", f.name) |
173 | 172 |
|
| 173 | + def _test_process_run_pickle(self, start_method): |
| 174 | + val = 10 |
| 175 | + with tempfile.NamedTemporaryFile("w+", delete_on_close=False) as f: |
| 176 | + f.write(textwrap.dedent( |
| 177 | + f'''\ |
| 178 | + import multiprocessing |
| 179 | +
|
| 180 | + def worker(x): |
| 181 | + print(__name__) |
| 182 | + exit(x ** 2) |
| 183 | +
|
| 184 | + if __name__ == "__main__": |
| 185 | + multiprocessing.set_start_method('{start_method}') |
| 186 | + p = multiprocessing.Process(target=worker, args=({val},)) |
| 187 | + p.start() |
| 188 | + p.join() |
| 189 | + print("p.exitcode =", p.exitcode) |
| 190 | + ''')) |
| 191 | + f.close() |
| 192 | + _, out, err = assert_python_ok('-m', "cProfile", f.name) |
| 193 | + self.assertIn(b"__mp_main__", out) |
| 194 | + self.assertIn(bytes(f"exitcode = {val**2}", encoding='utf8'), out) |
| 195 | + self.assertNotIn(b"Can't pickle", err) |
| 196 | + |
| 197 | + def test_process_spawn_pickle(self): |
| 198 | + # gh-140729: test use Process in cProfile. |
| 199 | + self._test_process_run_pickle('spawn') |
| 200 | + |
| 201 | + @unittest.skipIf(sys.platform == 'win32', |
| 202 | + "No 'forkserver' start method on Windows") |
| 203 | + def test_process_forkserver_pickle(self): |
| 204 | + # gh-140729: test use Process in cProfile. |
| 205 | + self._test_process_run_pickle('forkserver') |
| 206 | + |
174 | 207 |
|
175 | 208 | def main(): |
176 | 209 | if '-r' not in sys.argv: |
|
0 commit comments