Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensures activate.bat can handle Unicode contents #2416

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog/2416.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensures activate.bat can handle Unicode contents - by :user:`OmerFI`.
13 changes: 13 additions & 0 deletions src/virtualenv/activation/batch/activate.bat
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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=
)
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
26 changes: 24 additions & 2 deletions tests/unit/activation/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down