Skip to content

Commit 9e220bb

Browse files
[3.13] gh-149701: Fully silence potential hash -r error (GH-149702) (GH-149759)
(cherry picked from commit cd60968)
1 parent a76d9f4 commit 9e220bb

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

Lib/test/test_venv.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,26 @@ def test_deactivate_with_strict_bash_opts(self):
658658
self.assertEqual(out, "".encode())
659659
self.assertEqual(err, "".encode())
660660

661+
# gh-149701: Test exit code is zero even when hashing is disabled
662+
@unittest.skipIf(os.name == 'nt', 'not relevant on Windows')
663+
def test_deactivate_with_strict_bash_opts_and_hashing_disabled(self):
664+
bash = shutil.which("bash")
665+
if bash is None:
666+
self.skipTest("bash required for this test")
667+
rmtree(self.env_dir)
668+
builder = venv.EnvBuilder(clear=True)
669+
builder.create(self.env_dir)
670+
activate = os.path.join(self.env_dir, self.bindir, "activate")
671+
test_script = os.path.join(self.env_dir, "test_hash_disabled.sh")
672+
with open(test_script, "w") as f:
673+
f.write("set -euo pipefail\n"
674+
"set +h\n" # disable hashing
675+
f"source {activate}\n"
676+
"deactivate")
677+
out, err = check_output([bash, test_script])
678+
self.assertEqual(out, "".encode())
679+
self.assertEqual(err, "".encode())
680+
661681

662682
@unittest.skipUnless(sys.platform == 'darwin', 'only relevant on macOS')
663683
def test_macos_env(self):

Lib/venv/scripts/common/activate

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ deactivate () {
1717
# Call hash to forget past locations. Without forgetting
1818
# past locations the $PATH changes we made may not be respected.
1919
# See "man bash" for more details. hash is usually a builtin of your shell
20-
hash -r 2> /dev/null
20+
hash -r 2> /dev/null || true
2121

2222
if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
2323
PS1="${_OLD_VIRTUAL_PS1:-}"
@@ -73,4 +73,4 @@ fi
7373

7474
# Call hash to forget past commands. Without forgetting
7575
# past commands the $PATH changes we made may not be respected
76-
hash -r 2> /dev/null
76+
hash -r 2> /dev/null || true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bad return code from Lib/venv/bin/activate if hashing is disabled

0 commit comments

Comments
 (0)