]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-135335: Simplify preload regression test using __main__ (GH-138686) (#141887)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 24 Nov 2025 03:09:40 +0000 (04:09 +0100)
committerGitHub <noreply@github.com>
Mon, 24 Nov 2025 03:09:40 +0000 (19:09 -0800)
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 425f24e4fad672c211307a9f0018c8d39c4db9de)

Co-authored-by: Duane Griffin <duaneg@dghda.com>
Lib/test/_test_multiprocessing.py
Lib/test/mp_preload_flush.py

index c22ce769c48963ef5b55c6972d13d77e7990adb4..7dda7d236d9b2040b250a2ef66940f6f13bb0afd 100644 (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):
index 3501554d366a21ac789e57c6a2087c3dc1184332..c195a9ef6b26fea5f44bdf19b0de048bff402a3a 100644 (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')