|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import unittest |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +from multiprocessing import forkserver |
| 7 | + |
| 8 | + |
| 9 | +class TestForkserverMain(unittest.TestCase): |
| 10 | + |
| 11 | + def setUp(self): |
| 12 | + self._orig_sys_path = list(sys.path) |
| 13 | + |
| 14 | + def tearDown(self): |
| 15 | + sys.path[:] = self._orig_sys_path |
| 16 | + |
| 17 | + @mock.patch("multiprocessing.process.current_process") |
| 18 | + @mock.patch("multiprocessing.spawn.import_main_path") |
| 19 | + @mock.patch("multiprocessing.util._close_stdin") |
| 20 | + def test_preload_kwargs( |
| 21 | + self, |
| 22 | + mock_close_stdin, |
| 23 | + mock_import_main_path, |
| 24 | + mock_current_process, |
| 25 | + ): |
| 26 | + # Very much a whitebox test of the first stanza of main before |
| 27 | + # we start diddling with file descriptors and pipes. |
| 28 | + mock_close_stdin.side_effect = RuntimeError("stop test") |
| 29 | + self.assertNotIn( |
| 30 | + "colorsys", |
| 31 | + sys.modules.keys(), |
| 32 | + msg="Thie test requires a module that has not yet been imported.", |
| 33 | + ) |
| 34 | + |
| 35 | + with self.assertRaisesRegex(RuntimeError, "stop test"): |
| 36 | + forkserver.main(None, None, ["sys", "colorsys"]) |
| 37 | + mock_current_process.assert_not_called() |
| 38 | + mock_import_main_path.assert_not_called() |
| 39 | + self.assertIn("colorsys", sys.modules.keys()) |
| 40 | + self.assertEqual(sys.path, self._orig_sys_path) # unmodified |
| 41 | + |
| 42 | + del sys.modules["colorsys"] # unimport |
| 43 | + fake_path = os.path.dirname(__file__) |
| 44 | + with self.assertRaisesRegex(RuntimeError, "stop test"): |
| 45 | + forkserver.main(None, None, ["sys", "colorsys"], sys_path=[fake_path]) |
| 46 | + self.assertEqual( |
| 47 | + sys.path, [fake_path], msg="sys.path should have been overridden" |
| 48 | + ) |
| 49 | + self.assertNotIn( |
| 50 | + "colorsys", |
| 51 | + sys.modules.keys(), |
| 52 | + msg="import of colorsys should have failed with unusual sys.path", |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + unittest.main() |
0 commit comments