diff --git a/docs/changelog/2416.feature.rst b/docs/changelog/2416.feature.rst new file mode 100644 index 000000000..61e433beb --- /dev/null +++ b/docs/changelog/2416.feature.rst @@ -0,0 +1 @@ +Ensures activate.bat can handle Unicode contents - by :user:`OmerFI`. diff --git a/src/virtualenv/activation/batch/activate.bat b/src/virtualenv/activation/batch/activate.bat index 816853c6b..103d4e855 100644 --- a/src/virtualenv/activation/batch/activate.bat +++ b/src/virtualenv/activation/batch/activate.bat @@ -1,3 +1,11 @@ +@REM This file is UTF-8 encoded, so we need to update the current code page while executing it +@for /f "tokens=2 delims=:." %%a in ('"%SystemRoot%\System32\chcp.com"') do @( + @set _OLD_CODEPAGE=%%a +) +@if defined _OLD_CODEPAGE ( + "%SystemRoot%\System32\chcp.com" 65001 > nul +) + @set "VIRTUAL_ENV=__VIRTUAL_ENV__" @if defined _OLD_VIRTUAL_PROMPT ( @@ -35,3 +43,8 @@ :ENDIFVPATH2 @set "PATH=%VIRTUAL_ENV%\__BIN_NAME__;%PATH%" + +@if defined _OLD_CODEPAGE ( + "%SystemRoot%\System32\chcp.com" %_OLD_CODEPAGE% > nul + @set _OLD_CODEPAGE= +) diff --git a/tests/conftest.py b/tests/conftest.py index 68924dc35..1bce07e4e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -11,7 +11,7 @@ from virtualenv.app_data import AppDataDiskFolder from virtualenv.discovery.builtin import get_interpreter from virtualenv.discovery.py_info import PythonInfo -from virtualenv.info import IS_WIN, fs_supports_symlink +from virtualenv.info import fs_supports_symlink from virtualenv.report import LOGGER @@ -294,7 +294,7 @@ def is_inside_ci(): def special_char_name(): base = "e-$ Γ¨Ρ€Ρ‚πŸš’β™žδΈ­η‰‡-j" # workaround for pypy3 https://bitbucket.org/pypy/pypy/issues/3147/venv-non-ascii-support-windows - encoding = "ascii" if IS_WIN else sys.getfilesystemencoding() + encoding = sys.getfilesystemencoding() # let's not include characters that the file system cannot encode) result = "" for char in base: diff --git a/tests/unit/activation/test_batch.py b/tests/unit/activation/test_batch.py index 1d951b227..856ce4dcd 100644 --- a/tests/unit/activation/test_batch.py +++ b/tests/unit/activation/test_batch.py @@ -20,9 +20,31 @@ def __init__(self, session): self.pydoc_call = f"call {self.pydoc_call}" self.unix_line_ending = False + def set_code_page_utf8(self): + return """ + for /f "tokens=2 delims=:." %%a in ('"%SystemRoot%\\System32\\chcp.com"') do ( + set _OLD_CODEPAGE=%%a + ) + if defined _OLD_CODEPAGE ( + "%SystemRoot%\\System32\\chcp.com" 65001 > nul + ) + """.strip().splitlines() + + def unset_code_page_utf8(self): + return """ + if defined _OLD_CODEPAGE ( + "%SystemRoot%\\System32\\chcp.com" %_OLD_CODEPAGE% > nul + set _OLD_CODEPAGE= + ) + """.strip().splitlines() + def _get_test_lines(self, activate_script): - # for BATCH utf-8 support need change the character code page to 650001 - return ["@echo off", "", "chcp 65001 1>NUL"] + super()._get_test_lines(activate_script) + return ( + ["@echo off", ""] + + self.set_code_page_utf8() + + super()._get_test_lines(activate_script) + + self.unset_code_page_utf8() + ) def quote(self, s): """double quotes needs to be single, and single need to be double"""