python / python/cpython

venv: '!' in --prompt breaks activate.csh (csh history expansion)

Open
#152,686 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-venv type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

venv quotes substituted strings in its activation scripts with POSIX
shlex.quote() for every script except .ps1/.bat (gh-124651). That is
wrong for activate.csh: (t)csh performs history expansion on ! even
inside single quotes
, so a --prompt (or path) containing ! followed by a
word character produces an activate.csh that fails to source.

Reproducer

$ python -m venv --prompt 'py!env' /tmp/env
$ csh -c 'source /tmp/env/bin/activate.csh'
env: Event not found.

activate.csh runs setenv VIRTUAL_ENV_PROMPT 'py!env'; csh history-expands
!env, the line errors, and activation aborts (VIRTUAL_ENV/prompt unset).
Both csh and tcsh are affected; bash/zsh activate and activate.fish
are not (POSIX-compatible single quotes).

Cause

Lib/venv/__init__.py:replace_variables() dispatches the quoting function by
script suffix (.ps1 -> quote_ps1, .bat -> quote_bat, else -> shlex.quote).
activate.csh falls into the POSIX branch, which does not account for csh
history expansion.

Scope

--prompt is the cleanest case: the venv path is clean, so source works and
only the substituted prompt breaks -- fully fixable in venv. A ! in the venv
directory path is also mis-quoted by venv internally (same root cause), but
note that activating such a venv additionally requires the user to escape the
! in their own source <path> command line, which is outside venv's control.

Fix

Add a quote_csh that escapes !, and dispatch on .csh -- mirroring the
existing quote_ps1/quote_bat handling. Verified under csh and tcsh:
!, quotes, $, backtick, ;, &&, spaces, parens round-trip. (A newline
cannot be represented in a csh single-quoted string; that remains out of scope
and is unchanged from current behavior.)

Not a security issue: csh errors out rather than executing anything.

Linked PRs
  • gh-152687

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Lib/venv/init.py at replace_variables() and compare its existing quote_ps1 and quote_bat dispatch. Reproduce the failure with a --prompt containing !, then source the generated activate.csh under csh and tcsh. Done means the prompt and other listed special characters round-trip without activation failing.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, shell
Domain
cli, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.