Skip to content

Commit 40fd66b

Browse files
[3.14] gh-135335: Simplify preload regression test using __main__ (GH-138686) (#141886)
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 <duaneg@dghda.com>
1 parent 394db66 commit 40fd66b

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
@@ -6812,28 +6812,13 @@ def test_std_streams_flushed_after_preload(self):
68126812
if multiprocessing.get_start_method() != "forkserver":
68136813
self.skipTest("forkserver specific test")
68146814

6815-
# Create a test module in the temporary directory on the child's path
6816-
# TODO: This can all be simplified once gh-126631 is fixed and we can
6817-
# use __main__ instead of a module.
6818-
dirname = os.path.join(self._temp_dir, 'preloaded_module')
6819-
init_name = os.path.join(dirname, '__init__.py')
6820-
os.mkdir(dirname)
6821-
with open(init_name, "w") as f:
6822-
cmd = '''if 1:
6823-
import sys
6824-
print('stderr', end='', file=sys.stderr)
6825-
print('stdout', end='', file=sys.stdout)
6826-
'''
6827-
f.write(cmd)
6828-
68296815
name = os.path.join(os.path.dirname(__file__), 'mp_preload_flush.py')
6830-
env = {'PYTHONPATH': self._temp_dir}
6831-
_, out, err = test.support.script_helper.assert_python_ok(name, **env)
6816+
_, out, err = test.support.script_helper.assert_python_ok(name)
68326817

68336818
# Check stderr first, as it is more likely to be useful to see in the
68346819
# event of a failure.
6835-
self.assertEqual(err.decode().rstrip(), 'stderr')
6836-
self.assertEqual(out.decode().rstrip(), 'stdout')
6820+
self.assertEqual(err.decode().rstrip(), '__main____mp_main__')
6821+
self.assertEqual(out.decode().rstrip(), '__main____mp_main__')
68376822

68386823

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