[Python-checkins] bpo-37885: venv: Don't produce unbound variable warning on deactivate (GH-15973)

T. Wouters webhook-mailer at python.org
Wed Sep 11 11:56:01 EDT 2019


https://github.com/python/cpython/commit/63eefc35674ec12ab4d00af4feaf21de4cb1c91c
commit: 63eefc35674ec12ab4d00af4feaf21de4cb1c91c
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: T. Wouters <thomas at python.org>
date: 2019-09-11T08:55:57-07:00
summary:

bpo-37885: venv: Don't produce unbound variable warning on deactivate (GH-15973)

Before, running deactivate from a bash shell configured to treat undefined variables as errors (`set -u`) would produce a warning:

```
$ python3 -m venv test
$ source test/bin/activate
(test) $ deactivate
-bash: $1: unbound variable
```
(cherry picked from commit 5209e586b7cac9a43b2c44349a26b1b0af06ead3)

Co-authored-by: Daniel Abrahamsson <hamsson at gmail.com>

files:
A Misc/NEWS.d/next/Library/2019-08-19-10-31-41.bpo-37885.4Nc9sp.rst
M Lib/test/test_venv.py
M Lib/venv/scripts/common/activate

diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py
index 7130dc6f8309..ede65935b650 100644
--- a/Lib/test/test_venv.py
+++ b/Lib/test/test_venv.py
@@ -9,6 +9,7 @@
 import os
 import os.path
 import re
+import shutil
 import struct
 import subprocess
 import sys
@@ -338,6 +339,25 @@ def test_multiprocessing(self):
             'pool.terminate()'])
         self.assertEqual(out.strip(), "python".encode())
 
+    @unittest.skipIf(os.name == 'nt', 'not relevant on Windows')
+    def test_deactivate_with_strict_bash_opts(self):
+        bash = shutil.which("bash")
+        if bash is None:
+            self.skipTest("bash required for this test")
+        rmtree(self.env_dir)
+        builder = venv.EnvBuilder(clear=True)
+        builder.create(self.env_dir)
+        activate = os.path.join(self.env_dir, self.bindir, "activate")
+        test_script = os.path.join(self.env_dir, "test_strict.sh")
+        with open(test_script, "w") as f:
+            f.write("set -euo pipefail\n"
+                    f"source {activate}\n"
+                    "deactivate\n")
+        out, err = check_output([bash, test_script])
+        self.assertEqual(out, "".encode())
+        self.assertEqual(err, "".encode())
+
+
 @requireVenvCreate
 class EnsurePipTest(BaseTest):
     """Test venv module installation of pip."""
diff --git a/Lib/venv/scripts/common/activate b/Lib/venv/scripts/common/activate
index fff0765af53b..b9d498fb2ef0 100644
--- a/Lib/venv/scripts/common/activate
+++ b/Lib/venv/scripts/common/activate
@@ -28,7 +28,7 @@ deactivate () {
     fi
 
     unset VIRTUAL_ENV
-    if [ ! "$1" = "nondestructive" ] ; then
+    if [ ! "${1:-}" = "nondestructive" ] ; then
     # Self destruct!
         unset -f deactivate
     fi
diff --git a/Misc/NEWS.d/next/Library/2019-08-19-10-31-41.bpo-37885.4Nc9sp.rst b/Misc/NEWS.d/next/Library/2019-08-19-10-31-41.bpo-37885.4Nc9sp.rst
new file mode 100644
index 000000000000..055d02970146
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-08-19-10-31-41.bpo-37885.4Nc9sp.rst
@@ -0,0 +1 @@
+venv: Don't generate unset variable warning on deactivate.
\ No newline at end of file



More information about the Python-checkins mailing list