[Python-checkins] gh-90844: Allow virtual environments to correctly launch when they have spaces in the path (GH-94903)

miss-islington webhook-mailer at python.org
Sat Jul 16 13:11:38 EDT 2022


https://github.com/python/cpython/commit/8c08e86d8d44272a18ae9254d4e52f490a5ea491
commit: 8c08e86d8d44272a18ae9254d4e52f490a5ea491
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-07-16T10:11:34-07:00
summary:

gh-90844: Allow virtual environments to correctly launch when they have spaces in the path (GH-94903)

(cherry picked from commit 4b4439daed3992a5c5a83b86596d6e00ac3c1203)

Co-authored-by: Steve Dower <steve.dower at python.org>

files:
A Misc/NEWS.d/next/Windows/2022-07-16-16-18-32.gh-issue-90844.vwITT3.rst
M PC/launcher.c

diff --git a/Misc/NEWS.d/next/Windows/2022-07-16-16-18-32.gh-issue-90844.vwITT3.rst b/Misc/NEWS.d/next/Windows/2022-07-16-16-18-32.gh-issue-90844.vwITT3.rst
new file mode 100644
index 0000000000000..8d9e850a5b5b7
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2022-07-16-16-18-32.gh-issue-90844.vwITT3.rst
@@ -0,0 +1,2 @@
+Allow virtual environments to correctly launch when they have spaces in the
+path.
diff --git a/PC/launcher.c b/PC/launcher.c
index de7abeb4e86ab..da566a180168c 100644
--- a/PC/launcher.c
+++ b/PC/launcher.c
@@ -1926,27 +1926,35 @@ process(int argc, wchar_t ** argv)
         if (!cch) {
             error(0, L"Cannot determine memory for home path");
         }
-        cch += (DWORD)wcslen(PYTHON_EXECUTABLE) + 1 + 1; /* include sep and null */
+        cch += (DWORD)wcslen(PYTHON_EXECUTABLE) + 4; /* include sep, null and quotes */
         executable = (wchar_t *)malloc(cch * sizeof(wchar_t));
         if (executable == NULL) {
             error(RC_NO_MEMORY, L"A memory allocation failed");
         }
-        cch_actual = MultiByteToWideChar(CP_UTF8, 0, start, len, executable, cch);
+        /* start with a quote - we'll skip this ahead, but want it for the final string */
+        executable[0] = L'"';
+        cch_actual = MultiByteToWideChar(CP_UTF8, 0, start, len, &executable[1], cch - 1);
         if (!cch_actual) {
             error(RC_BAD_VENV_CFG, L"Cannot decode home path in '%ls'",
                   venv_cfg_path);
         }
+        cch_actual += 1; /* account for the first quote */
+        executable[cch_actual] = L'\0';
         if (executable[cch_actual - 1] != L'\\') {
             executable[cch_actual++] = L'\\';
             executable[cch_actual] = L'\0';
         }
-        if (wcscat_s(executable, cch, PYTHON_EXECUTABLE)) {
+        if (wcscat_s(&executable[1], cch - 1, PYTHON_EXECUTABLE)) {
             error(RC_BAD_VENV_CFG, L"Cannot create executable path from '%ls'",
                   venv_cfg_path);
         }
-        if (GetFileAttributesW(executable) == INVALID_FILE_ATTRIBUTES) {
+        /* there's no trailing quote, so we only have to skip one character for the test */
+        if (GetFileAttributesW(&executable[1]) == INVALID_FILE_ATTRIBUTES) {
             error(RC_NO_PYTHON, L"No Python at '%ls'", executable);
         }
+        /* now append the final quote */
+        wcscat_s(executable, cch, L"\"");
+        /* smuggle our original path through */
         if (!SetEnvironmentVariableW(L"__PYVENV_LAUNCHER__", argv0)) {
             error(0, L"Failed to set launcher environment");
         }



More information about the Python-checkins mailing list