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.
This commit is contained in:
Duane Griffin 2025-11-24 12:37:15 +13:00 committed by GitHub
parent 23b67aa037
commit 425f24e4fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 24 deletions

View file

@ -6956,28 +6956,13 @@ def test_std_streams_flushed_after_preload(self):
if multiprocessing.get_start_method() != "forkserver":
self.skipTest("forkserver specific test")
# Create a test module in the temporary directory on the child's path
# TODO: This can all be simplified once gh-126631 is fixed and we can
# use __main__ instead of a module.
dirname = os.path.join(self._temp_dir, 'preloaded_module')
init_name = os.path.join(dirname, '__init__.py')
os.mkdir(dirname)
with open(init_name, "w") as f:
cmd = '''if 1:
import sys
print('stderr', end='', file=sys.stderr)
print('stdout', end='', file=sys.stdout)
'''
f.write(cmd)
name = os.path.join(os.path.dirname(__file__), 'mp_preload_flush.py')
env = {'PYTHONPATH': self._temp_dir}
_, out, err = test.support.script_helper.assert_python_ok(name, **env)
_, out, err = test.support.script_helper.assert_python_ok(name)
# Check stderr first, as it is more likely to be useful to see in the
# event of a failure.
self.assertEqual(err.decode().rstrip(), 'stderr')
self.assertEqual(out.decode().rstrip(), 'stdout')
self.assertEqual(err.decode().rstrip(), '__main____mp_main__')
self.assertEqual(out.decode().rstrip(), '__main____mp_main__')
class MiscTestCase(unittest.TestCase):