Skip to content

Commit 47cb43c

Browse files
[3.13] gh-135335: Simplify preload regression test using __main__ (GH-138686) (#141887)
gh-135335: Simplify preload regression test using __main__ (GH-138686) 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. (cherry picked from commit 425f24e) Co-authored-by: Duane Griffin <[email protected]>
1 parent 2e8d4f1 commit 47cb43c

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
@@ -6457,28 +6457,13 @@ def test_std_streams_flushed_after_preload(self):
64576457
if multiprocessing.get_start_method() != "forkserver":
64586458
self.skipTest("forkserver specific test")
64596459

6460-
# Create a test module in the temporary directory on the child's path
6461-
# TODO: This can all be simplified once gh-126631 is fixed and we can
6462-
# use __main__ instead of a module.
6463-
dirname = os.path.join(self._temp_dir, 'preloaded_module')
6464-
init_name = os.path.join(dirname, '__init__.py')
6465-
os.mkdir(dirname)
6466-
with open(init_name, "w") as f:
6467-
cmd = '''if 1:
6468-
import sys
6469-
print('stderr', end='', file=sys.stderr)
6470-
print('stdout', end='', file=sys.stdout)
6471-
'''
6472-
f.write(cmd)
6473-
64746460
name = os.path.join(os.path.dirname(__file__), 'mp_preload_flush.py')
6475-
env = {'PYTHONPATH': self._temp_dir}
6476-
_, out, err = test.support.script_helper.assert_python_ok(name, **env)
6461+
_, out, err = test.support.script_helper.assert_python_ok(name)
64776462

64786463
# Check stderr first, as it is more likely to be useful to see in the
64796464
# event of a failure.
6480-
self.assertEqual(err.decode().rstrip(), 'stderr')
6481-
self.assertEqual(out.decode().rstrip(), 'stdout')
6465+
self.assertEqual(err.decode().rstrip(), '__main____mp_main__')
6466+
self.assertEqual(out.decode().rstrip(), '__main____mp_main__')
64826467

64836468

64846469
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)