[3.13] gh-135335: Simplify preload regression test using __main__ (GH-138686) (#141887)
Some checks are pending
Tests / (push) Blocked by required conditions
Tests / Android (aarch64) (push) Blocked by required conditions
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / Check if the ABI has changed (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / Android (x86_64) (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Sanitizers (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run

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 425f24e4fa)

Co-authored-by: Duane Griffin <duaneg@dghda.com>
This commit is contained in:
Miss Islington (bot) 2025-11-24 04:09:40 +01:00 committed by GitHub
parent 2e8d4f13a8
commit 47cb43c0f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 24 deletions

View file

@ -6457,28 +6457,13 @@ class _TestSpawnedSysPath(BaseTestCase):
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):

View file

@ -1,15 +1,11 @@
import multiprocessing
import sys
modname = 'preloaded_module'
print(__name__, end='', file=sys.stderr)
print(__name__, end='', file=sys.stdout)
if __name__ == '__main__':
if modname in sys.modules:
raise AssertionError(f'{modname!r} is not in sys.modules')
multiprocessing.set_start_method('forkserver')
multiprocessing.set_forkserver_preload([modname])
for _ in range(2):
p = multiprocessing.Process()
p.start()
p.join()
elif modname not in sys.modules:
raise AssertionError(f'{modname!r} is not in sys.modules')