|
1 | 1 | """Test suite for the cProfile module.""" |
2 | | - |
3 | 2 | import sys |
4 | 3 | import unittest |
5 | 4 |
|
@@ -192,6 +191,40 @@ class Foo: |
192 | 191 | f.close() |
193 | 192 | assert_python_ok('-m', "cProfile", f.name) |
194 | 193 |
|
| 194 | + def _test_process_run_pickle(self, start_method): |
| 195 | + val = 10 |
| 196 | + with tempfile.NamedTemporaryFile("w+", delete_on_close=False) as f: |
| 197 | + f.write(textwrap.dedent( |
| 198 | + f'''\ |
| 199 | + import multiprocessing |
| 200 | +
|
| 201 | + def worker(x): |
| 202 | + print(__name__) |
| 203 | + exit(x ** 2) |
| 204 | +
|
| 205 | + if __name__ == "__main__": |
| 206 | + multiprocessing.set_start_method('{start_method}') |
| 207 | + p = multiprocessing.Process(target=worker, args=({val},)) |
| 208 | + p.start() |
| 209 | + p.join() |
| 210 | + print("p.exitcode =", p.exitcode) |
| 211 | + ''')) |
| 212 | + f.close() |
| 213 | + _, out, err = assert_python_ok('-m', "cProfile", f.name) |
| 214 | + self.assertIn(b"__mp_main__", out) |
| 215 | + self.assertIn(bytes(f"exitcode = {val**2}", encoding='utf8'), out) |
| 216 | + self.assertNotIn(b"Can't pickle", err) |
| 217 | + |
| 218 | + def test_process_spawn_pickle(self): |
| 219 | + # gh-140729: test use Process in cProfile. |
| 220 | + self._test_process_run_pickle('spawn') |
| 221 | + |
| 222 | + @unittest.skipIf(sys.platform == 'win32', |
| 223 | + "No 'forkserver' start method on Windows") |
| 224 | + def test_process_forkserver_pickle(self): |
| 225 | + # gh-140729: test use Process in cProfile. |
| 226 | + self._test_process_run_pickle('forkserver') |
| 227 | + |
195 | 228 |
|
196 | 229 | def main(): |
197 | 230 | if '-r' not in sys.argv: |
|
0 commit comments