Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,18 @@ def test_fish_activate_shadowed_builtins(self):
self.assertNotIn('LEAK', text)
self.assertIn('OLDSTATUS=7', text)

def test_fish_quoting(self):
"""
Test that the template strings are quoted properly (fish)
"""
builder = venv.EnvBuilder()
context = builder.ensure_directories(self.env_dir)
context.script_path = 'activate.fish'
context.prompt = r"x\'$(echo PWNED)'\'"
self.assertEqual(
builder.replace_variables('__VENV_PROMPT__', context),
r"'x\\\'$(echo PWNED)\'\\\''")

# gh-124651: test quoted strings on Windows
@unittest.skipUnless(os.name == 'nt', 'only relevant on Windows')
def test_special_chars_windows(self):
Expand Down
7 changes: 7 additions & 0 deletions Lib/venv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,13 +521,20 @@ def quote_ps1(s):
def quote_bat(s):
return s

def quote_fish(s):
# gh-160782: fish reads \\ and \' as escapes inside single quotes
s = s.replace('\\', '\\\\').replace("'", "\\'")
return f"'{s}'"

# gh-124651: need to quote the template strings properly
quote = shlex.quote
script_path = context.script_path
if script_path.endswith('.ps1'):
quote = quote_ps1
elif script_path.endswith('.bat'):
quote = quote_bat
elif script_path.endswith('.fish'):
quote = quote_fish
else:
# fallbacks to POSIX shell compliant quote
quote = shlex.quote
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`venv`: a backslash before a quote in the prompt or the path no longer
breaks ``activate.fish``.
Loading