Skip to content

Commit

Permalink
fix windows powershell activate encoding problem
Browse files Browse the repository at this point in the history
  • Loading branch information
PzaThief committed Feb 5, 2024
1 parent cfb9e78 commit 7d56d99
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
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_powershell.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ 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"""
return quote(s).replace('"', '""')

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 7d56d99

Please sign in to comment.