Skip to content

Commit

Permalink
Fix windows utf8 encoding issue (#2687)
Browse files Browse the repository at this point in the history
  • Loading branch information
PzaThief authored Mar 22, 2024
1 parent f7d58be commit e62efdb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
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__"

@set "VIRTUAL_ENV_PROMPT=__VIRTUAL_PROMPT__"
Expand Down Expand Up @@ -36,3 +44,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: 3 additions & 1 deletion src/virtualenv/activation/via_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ def _generate(self, replacements, templates, to_folder, creator):
# errors when the dest is not writable
if dest.exists():
dest.unlink()
# Powershell assumes Windows 1252 encoding when reading files without BOM
encoding = "utf-8-sig" if template.endswith(".ps1") else "utf-8"
# use write_bytes to avoid platform specific line normalization (\n -> \r\n)
dest.write_bytes(text.encode("utf-8"))
dest.write_bytes(text.encode(encoding))
generated.append(dest)
return generated

Expand Down
3 changes: 1 addition & 2 deletions tests/unit/activation/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def __init__(self, session) -> None:
self.unix_line_ending = False

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", *super()._get_test_lines(activate_script)]

def quote(self, s):
"""double quotes needs to be single, and single need to be double"""
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/activation/test_powershell.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ def __init__(self, session) -> None:
self._version_cmd = [cmd, "-c", "$PSVersionTable"]
self._invoke_script = [cmd, "-ExecutionPolicy", "ByPass", "-File"]
self.activate_cmd = "."
self.script_encoding = "utf-16"
self.script_encoding = "utf-8-sig"

def quote(self, s):
"""powershell double quote needed for quotes within single quotes"""
text = quote(s)
return text.replace('"', '""') if sys.platform == "win32" else text

def _get_test_lines(self, activate_script):
# for BATCH utf-8 support need change the character code page to 650001
return super()._get_test_lines(activate_script)

def invoke_script(self):
Expand Down

0 comments on commit e62efdb

Please sign in to comment.