From ebd5fa714a6f5d044e516873c2778722fea496fc Mon Sep 17 00:00:00 2001 From: ThanhNguyxn Date: Sat, 7 Feb 2026 16:35:02 +0700 Subject: [PATCH] gh-140006: Preserve fish prompt status in venv activation --- Lib/test/test_venv.py | 10 ++++++++++ Lib/venv/scripts/common/activate.fish | 2 +- .../2026-02-07-16-36-00.gh-issue-140006.H3z8kW.rst | 3 +++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-02-07-16-36-00.gh-issue-140006.H3z8kW.rst diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py index 68bcf535eada10..72f80aa2edfa6b 100644 --- a/Lib/test/test_venv.py +++ b/Lib/test/test_venv.py @@ -772,6 +772,16 @@ def test_activate_shell_script_has_no_dos_newlines(self): error_message = f"CR LF found in line {i}" self.assertNotEndsWith(line, b'\r\n', error_message) + @requireVenvCreate + def test_activate_fish_script_preserves_status(self): + venv_dir = pathlib.Path(self.env_dir) + rmtree(venv_dir) + script_path = venv_dir / self.bindir / 'activate.fish' + venv.create(venv_dir) + script = script_path.read_text(encoding='utf-8') + self.assertIn('return $old_status', script) + self.assertNotIn('echo "exit $old_status" | .', script) + @requireVenvCreate def test_scm_ignore_files_git(self): """ diff --git a/Lib/venv/scripts/common/activate.fish b/Lib/venv/scripts/common/activate.fish index 284a7469c99b57..306a84a2e3f549 100644 --- a/Lib/venv/scripts/common/activate.fish +++ b/Lib/venv/scripts/common/activate.fish @@ -60,7 +60,7 @@ if test -z "$VIRTUAL_ENV_DISABLE_PROMPT" printf "%s(%s)%s " (set_color 4B8BBE) __VENV_PROMPT__ (set_color normal) # Restore the return status of the previous command. - echo "exit $old_status" | . + return $old_status # Output the original/"old" prompt. _old_fish_prompt end diff --git a/Misc/NEWS.d/next/Library/2026-02-07-16-36-00.gh-issue-140006.H3z8kW.rst b/Misc/NEWS.d/next/Library/2026-02-07-16-36-00.gh-issue-140006.H3z8kW.rst new file mode 100644 index 00000000000000..3ff656bf08a4f1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-02-07-16-36-00.gh-issue-140006.H3z8kW.rst @@ -0,0 +1,3 @@ +Fix ``venv``'s ``activate.fish`` prompt hook to preserve the previous command +status without sourcing ``exit`` from stdin, avoiding prompt corruption in +interactive fish sessions.