Skip to content

Commit c5d136c

Browse files
committed
Simplify preload regression test using __main__
With the fix for gh-126631 __main__ modules can be preloaded and the regression test for gh-135335 can be simplified to just use a self-contained script rather than requiring a module. Note this assumes and implicitly tests that __main__ is preloaded by default.
1 parent 22cb9ba commit c5d136c

File tree

2 files changed

+5
-24
lines changed

2 files changed

+5
-24
lines changed

Lib/test/_test_multiprocessing.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6939,28 +6939,13 @@ def test_std_streams_flushed_after_preload(self):
69396939
if multiprocessing.get_start_method() != "forkserver":
69406940
self.skipTest("forkserver specific test")
69416941

6942-
# Create a test module in the temporary directory on the child's path
6943-
# TODO: This can all be simplified once gh-126631 is fixed and we can
6944-
# use __main__ instead of a module.
6945-
dirname = os.path.join(self._temp_dir, 'preloaded_module')
6946-
init_name = os.path.join(dirname, '__init__.py')
6947-
os.mkdir(dirname)
6948-
with open(init_name, "w") as f:
6949-
cmd = '''if 1:
6950-
import sys
6951-
print('stderr', end='', file=sys.stderr)
6952-
print('stdout', end='', file=sys.stdout)
6953-
'''
6954-
f.write(cmd)
6955-
69566942
name = os.path.join(os.path.dirname(__file__), 'mp_preload_flush.py')
6957-
env = {'PYTHONPATH': self._temp_dir}
6958-
_, out, err = test.support.script_helper.assert_python_ok(name, **env)
6943+
_, out, err = test.support.script_helper.assert_python_ok(name)
69596944

69606945
# Check stderr first, as it is more likely to be useful to see in the
69616946
# event of a failure.
6962-
self.assertEqual(err.decode().rstrip(), 'stderr')
6963-
self.assertEqual(out.decode().rstrip(), 'stdout')
6947+
self.assertEqual(err.decode().rstrip(), '__main____mp_main__')
6948+
self.assertEqual(out.decode().rstrip(), '__main____mp_main__')
69646949

69656950

69666951
class MiscTestCase(unittest.TestCase):

Lib/test/mp_preload_flush.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import multiprocessing
22
import sys
33

4-
modname = 'preloaded_module'
4+
print(__name__, end='', file=sys.stderr)
5+
print(__name__, end='', file=sys.stdout)
56
if __name__ == '__main__':
6-
if modname in sys.modules:
7-
raise AssertionError(f'{modname!r} is not in sys.modules')
87
multiprocessing.set_start_method('forkserver')
9-
multiprocessing.set_forkserver_preload([modname])
108
for _ in range(2):
119
p = multiprocessing.Process()
1210
p.start()
1311
p.join()
14-
elif modname not in sys.modules:
15-
raise AssertionError(f'{modname!r} is not in sys.modules')

0 commit comments

Comments
 (0)