[Python-checkins] gh-103088: Sanitize venv paths when using MSYS or Cygwin Bash (GH-103325)

zooba webhook-mailer at python.org
Tue Apr 11 15:20:54 EDT 2023


https://github.com/python/cpython/commit/ebc81034278ea186b4110c7dbf6d1c2a0ada9398
commit: ebc81034278ea186b4110c7dbf6d1c2a0ada9398
branch: main
author: Stanislav Syekirin <syekirin at gmail.com>
committer: zooba <steve.dower at microsoft.com>
date: 2023-04-11T20:20:46+01:00
summary:

gh-103088: Sanitize venv paths when using MSYS or Cygwin Bash (GH-103325)

files:
A Misc/NEWS.d/next/Windows/2023-04-11-09-22-22.gh-issue-103088.6AJEuR.rst
M Lib/venv/scripts/common/activate
M PCbuild/find_python.bat

diff --git a/Lib/venv/scripts/common/activate b/Lib/venv/scripts/common/activate
index 6fbc2b8801da..cb898b39670c 100644
--- a/Lib/venv/scripts/common/activate
+++ b/Lib/venv/scripts/common/activate
@@ -38,8 +38,15 @@ deactivate () {
 # unset irrelevant variables
 deactivate nondestructive
 
-VIRTUAL_ENV="__VENV_DIR__"
-export VIRTUAL_ENV
+# on Windows, a path can contain colons and backslashes and has to be converted:
+if [ "$OSTYPE" = "cygwin" ] || [ "$OSTYPE" = "msys" ] ; then
+    # transform D:\path\to\venv to /d/path/to/venv on MSYS
+    # and to /cygdrive/d/path/to/venv on Cygwin
+    export VIRTUAL_ENV=$(cygpath "__VENV_DIR__")
+else
+    # use the path as-is
+    export VIRTUAL_ENV="__VENV_DIR__"
+fi
 
 _OLD_VIRTUAL_PATH="$PATH"
 PATH="$VIRTUAL_ENV/__VENV_BIN_NAME__:$PATH"
diff --git a/Misc/NEWS.d/next/Windows/2023-04-11-09-22-22.gh-issue-103088.6AJEuR.rst b/Misc/NEWS.d/next/Windows/2023-04-11-09-22-22.gh-issue-103088.6AJEuR.rst
new file mode 100644
index 000000000000..f9f5343f4210
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2023-04-11-09-22-22.gh-issue-103088.6AJEuR.rst
@@ -0,0 +1 @@
+Fixes venvs not working in bash on Windows across different disks
diff --git a/PCbuild/find_python.bat b/PCbuild/find_python.bat
index 11d6cba7a172..7af5503d80a0 100644
--- a/PCbuild/find_python.bat
+++ b/PCbuild/find_python.bat
@@ -42,7 +42,7 @@
 @if NOT "%HOST_PYTHON%"=="" @%HOST_PYTHON% -Ec "import sys; assert sys.version_info[:2] >= (3, 9)" >nul 2>nul && (set PYTHON="%HOST_PYTHON%") && (set _Py_Python_Source=found as HOST_PYTHON) && goto :found
 
 @rem If py.exe finds a recent enough version, use that one
- at for %%p in (3.10 3.9) do @py -%%p -EV >nul 2>&1 && (set PYTHON=py -%%p) && (set _Py_Python_Source=found %%p with py.exe) && goto :found
+ at for %%p in (3.11 3.10 3.9) do @py -%%p -EV >nul 2>&1 && (set PYTHON=py -%%p) && (set _Py_Python_Source=found %%p with py.exe) && goto :found
 
 @if NOT exist "%_Py_EXTERNALS_DIR%" mkdir "%_Py_EXTERNALS_DIR%"
 @set _Py_NUGET=%NUGET%



More information about the Python-checkins mailing list