From 17e0a26b210b149c56c2fd3c86d45fb4cb65a48e Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Fri, 25 Jan 2019 12:29:27 -0800 Subject: [PATCH 1/2] bpo-35797: Fix default executable used by the multiprocessing module --- Lib/multiprocessing/spawn.py | 8 ++++++++ Lib/test/test_venv.py | 13 +++++++++++++ .../2019-01-25-12-29-14.bpo-35797.MzyOK9.rst | 1 + 3 files changed, 22 insertions(+) create mode 100644 Misc/NEWS.d/next/Windows/2019-01-25-12-29-14.bpo-35797.MzyOK9.rst diff --git a/Lib/multiprocessing/spawn.py b/Lib/multiprocessing/spawn.py index 73aa69471f29c3a..cf3dd7ccd29168c 100644 --- a/Lib/multiprocessing/spawn.py +++ b/Lib/multiprocessing/spawn.py @@ -35,6 +35,14 @@ if WINSERVICE: _python_exe = os.path.join(sys.exec_prefix, 'python.exe') +elif (sys.platform == 'win32' and sys.base_exec_prefix != sys.exec_prefix + and '__PYVENV_LAUNCHER__' in os.environ): + # bpo-35797: When running in a venv, we need to bypass the redirect + # executor and launch our original Python. + _python_exe = os.path.join( + sys.base_exec_prefix, + os.path.split(sys.executable)[-1] + ) else: _python_exe = sys.executable diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py index 22a3b78852f8c09..34c2234493bcf35 100644 --- a/Lib/test/test_venv.py +++ b/Lib/test/test_venv.py @@ -306,6 +306,19 @@ def test_unicode_in_batch_file(self): ) self.assertEqual(out.strip(), '0') + def test_multiprocessing(self): + """ + Test that the multiprocessing is able to spawn. + """ + rmtree(self.env_dir) + self.run_with_capture(venv.create, self.env_dir) + envpy = os.path.join(os.path.realpath(self.env_dir), + self.bindir, self.exe) + out, err = check_output([envpy, '-c', + 'from multiprocessing import Pool; ' + + 'print(Pool(1).apply_async("Python".lower).get(3))']) + self.assertEqual(out.strip(), "python".encode()) + @skipInVenv class EnsurePipTest(BaseTest): """Test venv module installation of pip.""" diff --git a/Misc/NEWS.d/next/Windows/2019-01-25-12-29-14.bpo-35797.MzyOK9.rst b/Misc/NEWS.d/next/Windows/2019-01-25-12-29-14.bpo-35797.MzyOK9.rst new file mode 100644 index 000000000000000..a0745f500b13661 --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2019-01-25-12-29-14.bpo-35797.MzyOK9.rst @@ -0,0 +1 @@ +Fix default executable used by the multiprocessing module From d03c1ff8cbe8570ed1df921a544f706eac2af664 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Fri, 25 Jan 2019 13:09:32 -0800 Subject: [PATCH 2/2] Fix getting executable path to work with source builds --- Lib/multiprocessing/spawn.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Lib/multiprocessing/spawn.py b/Lib/multiprocessing/spawn.py index cf3dd7ccd29168c..860fa4ceb5ced51 100644 --- a/Lib/multiprocessing/spawn.py +++ b/Lib/multiprocessing/spawn.py @@ -29,20 +29,19 @@ if sys.platform != 'win32': WINEXE = False WINSERVICE = False + _WINENV = False else: - WINEXE = (sys.platform == 'win32' and getattr(sys, 'frozen', False)) + WINEXE = getattr(sys, 'frozen', False) WINSERVICE = sys.executable.lower().endswith("pythonservice.exe") + _WINENV = '__PYVENV_LAUNCHER__' in os.environ if WINSERVICE: _python_exe = os.path.join(sys.exec_prefix, 'python.exe') -elif (sys.platform == 'win32' and sys.base_exec_prefix != sys.exec_prefix - and '__PYVENV_LAUNCHER__' in os.environ): +elif _WINENV: # bpo-35797: When running in a venv, we need to bypass the redirect - # executor and launch our original Python. - _python_exe = os.path.join( - sys.base_exec_prefix, - os.path.split(sys.executable)[-1] - ) + # executor and launch our base Python. + import _winapi + _python_exe = _winapi.GetModuleFileName(0) else: _python_exe = sys.executable