Skip to content

Commit 425f24e

Browse files
authored
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.
1 parent 23b67aa commit 425f24e

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
@@ -6956,28 +6956,13 @@ def test_std_streams_flushed_after_preload(self):
69566956
if multiprocessing.get_start_method() != "forkserver":
69576957
self.skipTest("forkserver specific test")
69586958

6959-
# Create a test module in the temporary directory on the child's path
6960-
# TODO: This can all be simplified once gh-126631 is fixed and we can
6961-
# use __main__ instead of a module.
6962-
dirname = os.path.join(self._temp_dir, 'preloaded_module')
6963-
init_name = os.path.join(dirname, '__init__.py')
6964-
os.mkdir(dirname)
6965-
with open(init_name, "w") as f:
6966-
cmd = '''if 1:
6967-
import sys
6968-
print('stderr', end='', file=sys.stderr)
6969-
print('stdout', end='', file=sys.stdout)
6970-
'''
6971-
f.write(cmd)
6972-
69736959
name = os.path.join(os.path.dirname(__file__), 'mp_preload_flush.py')
6974-
env = {'PYTHONPATH': self._temp_dir}
6975-
_, out, err = test.support.script_helper.assert_python_ok(name, **env)
6960+
_, out, err = test.support.script_helper.assert_python_ok(name)
69766961

69776962
# Check stderr first, as it is more likely to be useful to see in the
69786963
# event of a failure.
6979-
self.assertEqual(err.decode().rstrip(), 'stderr')
6980-
self.assertEqual(out.decode().rstrip(), 'stdout')
6964+
self.assertEqual(err.decode().rstrip(), '__main____mp_main__')
6965+
self.assertEqual(out.decode().rstrip(), '__main____mp_main__')
69816966

69826967

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