Skip to content

Commit ca74c57

Browse files
samstagernMartin Bijl
authored andcommitted
bpo-32409 Fix regression in activate.bat on international Windows (GH-10295)
Handle Unicode contents on localized Windows systems when activating a virtualenv. activate.bat currently breaks on German Windows systems as chcp does not return a plain number as on English systems, but appends a dot at the end (for example "Aktive Codepage: 850." instead of "Active Codepage: 850). The dependency to chcp.com is removed and ctypes is used to get, set and restore the console ouput codepage. The codepage for console input is not changed. We can't use __VENV_PYTHON__ to find python.exe since it's UTF-8. CMD decodes the script using the console output codepage.
1 parent 488c0a6 commit ca74c57

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

Lib/venv/scripts/nt/activate.bat

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
@echo off
22

3-
rem This file is UTF-8 encoded, so we need to update the current code page while executing it
4-
for /f "tokens=2 delims=:" %%a in ('"%SystemRoot%\System32\chcp.com"') do (
5-
set "_OLD_CODEPAGE=%%a"
6-
)
3+
rem This file is UTF-8 encoded, so we need to update the current code page while executing it.
4+
for /f %%a in ('%~dp0python.exe -Ic "import ctypes; print(ctypes.windll.kernel32.GetConsoleOutputCP())"') do (set "_OLD_CODEPAGE=%%a")
5+
76
if defined _OLD_CODEPAGE (
8-
"%SystemRoot%\System32\chcp.com" 65001 > nul
7+
%~dp0python.exe -Ic "import ctypes; ctypes.windll.kernel32.SetConsoleOutputCP(65001)"
98
)
109

1110
set "VIRTUAL_ENV=__VENV_DIR__"
@@ -40,6 +39,6 @@ set "PATH=%VIRTUAL_ENV%\__VENV_BIN_NAME__;%PATH%"
4039

4140
:END
4241
if defined _OLD_CODEPAGE (
43-
"%SystemRoot%\System32\chcp.com" %_OLD_CODEPAGE% > nul
42+
%~dp0python.exe -Ic "import ctypes; ctypes.windll.kernel32.SetConsoleOutputCP(%_OLD_CODEPAGE%)"
4443
set "_OLD_CODEPAGE="
4544
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed implementation of :file:`activate.bat` to handle Unicode contents on
2+
localized Windows systems (eg. German). Patch by Martin Bijl.

0 commit comments

Comments
 (0)