]> granicus.if.org Git - python/commitdiff
bpo-37885: venv: Don't produce unbound variable warning on deactivate (GH-15330)
authorDaniel Abrahamsson <hamsson@gmail.com>
Wed, 11 Sep 2019 14:58:56 +0000 (16:58 +0200)
committerT. Wouters <thomas@python.org>
Wed, 11 Sep 2019 14:58:56 +0000 (07:58 -0700)
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
```

Lib/test/test_venv.py
Lib/venv/scripts/common/activate
Misc/NEWS.d/next/Library/2019-08-19-10-31-41.bpo-37885.4Nc9sp.rst [new file with mode: 0644]

index de93d9539c2cd619962b0dbc0b73162e2ac4e993..0103de8828dd1b6a14b8aac465b5b5cdc524af23 100644 (file)
@@ -9,6 +9,7 @@ import ensurepip
 import os
 import os.path
 import re
+import shutil
 import struct
 import subprocess
 import sys
@@ -360,6 +361,25 @@ class BasicTest(BaseTest):
             '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."""
index 6a2320a369cb5c359b6fb220593d087ff7a03b06..b1b4625fdddc3a7e14f06fbcc3afe9e9eff0d476 100644 (file)
@@ -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 (file)
index 0000000..055d029
--- /dev/null
@@ -0,0 +1 @@
+venv: Don't generate unset variable warning on deactivate.
\ No newline at end of file