Skip to content

Commit 4e02f8f

Browse files
authored
bpo-35797: Fix default executable used by the multiprocessing module (GH-11676)
1 parent 3bab40d commit 4e02f8f

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/multiprocessing/spawn.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@
2929
if sys.platform != 'win32':
3030
WINEXE = False
3131
WINSERVICE = False
32+
_WINENV = False
3233
else:
33-
WINEXE = (sys.platform == 'win32' and getattr(sys, 'frozen', False))
34+
WINEXE = getattr(sys, 'frozen', False)
3435
WINSERVICE = sys.executable.lower().endswith("pythonservice.exe")
36+
_WINENV = '__PYVENV_LAUNCHER__' in os.environ
3537

3638
if WINSERVICE:
3739
_python_exe = os.path.join(sys.exec_prefix, 'python.exe')
40+
elif _WINENV:
41+
# bpo-35797: When running in a venv, we need to bypass the redirect
42+
# executor and launch our base Python.
43+
import _winapi
44+
_python_exe = _winapi.GetModuleFileName(0)
3845
else:
3946
_python_exe = sys.executable
4047

Lib/test/test_venv.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,19 @@ def test_unicode_in_batch_file(self):
306306
)
307307
self.assertEqual(out.strip(), '0')
308308

309+
def test_multiprocessing(self):
310+
"""
311+
Test that the multiprocessing is able to spawn.
312+
"""
313+
rmtree(self.env_dir)
314+
self.run_with_capture(venv.create, self.env_dir)
315+
envpy = os.path.join(os.path.realpath(self.env_dir),
316+
self.bindir, self.exe)
317+
out, err = check_output([envpy, '-c',
318+
'from multiprocessing import Pool; ' +
319+
'print(Pool(1).apply_async("Python".lower).get(3))'])
320+
self.assertEqual(out.strip(), "python".encode())
321+
309322
@skipInVenv
310323
class EnsurePipTest(BaseTest):
311324
"""Test venv module installation of pip."""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix default executable used by the multiprocessing module

0 commit comments

Comments
 (0)