Repository: pypa/pipenv

Issue:
Parentheses escaping bug when running `pipenv shell`
### Summary:

When running `pipenv shell` in POSIX (zsh), running `/bin/activate` will fail if there are parentheses in the virtualenv path due to the regex in `_get_activate_script()` (Line 53, `pipenv/shells.py`) not escaping parentheses properly.

### How to reproduce the issue:

1. Create a new directory `a ( )`
2. Create `.venv` folder inside the directory
3. Run `pipenv install` in `a ( )`
4. Run `pipenv shell`. The issue should now be reproduced.
5. To verify, run `pipenv shell` again to confirm the improper activation.

### What is expected to happen:

Step 4 successfully activates virtual environment without error return code (1).

### What actually happens:

Step 4 output:

``` zsh
$ pipenv shell
Launching subshell in virtual environment...
 . /Users/leohku/a\ (\ )/.venv/bin/activate
zsh: no matches found: /Users/leohku/a ( )/.venv/bin/activate
```

Step 5 output:

``` zsh
$ pipenv shell
Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated.
No action taken to avoid nested environments.
```

### Environment:

pipenv, version 2020.11.15
running zsh on macOS Big Sur v11.1 x86
Parentheses escaping bug when running `pipenv shell`
### Summary:

When running `pipenv shell` in POSIX (zsh), running `/bin/activate` will fail if there are parentheses in the virtualenv path due to the regex in `_get_activate_script()` (Line 53, `pipenv/shells.py`) not escaping parentheses properly.

### How to reproduce the issue:

1. Create a new directory `a ( )`
2. Create `.venv` folder inside the directory
3. Run `pipenv install` in `a ( )`
4. Run `pipenv shell`. The issue should now be reproduced.
5. To verify, run `pipenv shell` again to confirm the improper activation.

### What is expected to happen:

Step 4 successfully activates virtual environment without error return code (1).

### What actually happens:

Step 4 output:

``` zsh
$ pipenv shell
Launching subshell in virtual environment...
 . /Users/leohku/a\ (\ )/.venv/bin/activate
zsh: no matches found: /Users/leohku/a ( )/.venv/bin/activate
```

Step 5 output:

``` zsh
$ pipenv shell
Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated.
No action taken to avoid nested environments.
```

### Environment:

pipenv, version 2020.11.15
running zsh on macOS Big Sur v11.1 x86


Discussion:

Resolving patch:
diff --git a/pipenv/project.py b/pipenv/project.py
--- a/pipenv/project.py
+++ b/pipenv/project.py
@@ -395,7 +395,7 @@ def _sanitize(cls, name):
         #   https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
         #   http://www.tldp.org/LDP/abs/html/special-chars.html#FIELDREF
         #   https://github.com/torvalds/linux/blob/2bfe01ef/include/uapi/linux/binfmts.h#L18
-        return re.sub(r'[ &$`!*@"\\\r\n\t]', "_", name)[0:42]
+        return re.sub(r'[ &$`!*@"()\[\]\\\r\n\t]', "_", name)[0:42]
 
     def _get_virtualenv_hash(self, name):
         # type: (str) -> str
diff --git a/pipenv/shells.py b/pipenv/shells.py
--- a/pipenv/shells.py
+++ b/pipenv/shells.py
@@ -50,7 +50,7 @@ def _get_activate_script(cmd, venv):
         command = "."
     # Escape any special characters located within the virtualenv path to allow
     # for proper activation.
-    venv_location = re.sub(r'([ &$])', r"\\\1", str(venv))
+    venv_location = re.sub(r'([ &$()\[\]])', r"\\\1", str(venv))
     # The leading space can make history cleaner in some shells.
     return " {2} {0}/bin/activate{1}".format(venv_location, suffix, command)
 


Test patch: