python / python/cpython

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

オープン
#152,686 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib topic-venv type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Lib/venv/init.py の replace_variables() から始め、既存の quote_ps1 と quote_bat のディスパッチを比較します。! を含む --prompt で失敗を再現し、その後、生成された activate.csh を csh と tcsh で source します。プロンプトとその他の一覧にある特殊文字が、アクティベーションを失敗させることなくラウンドトリップすれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python, shell
領域
cli, tooling
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。