Repository: ipython/ipython

Issue:
Improper path comparison of virtualenv directories
Minor, non-breaking issue found during review of #13094.

If path of the active virtualenv is a substring of another virtualenv, IPython started from the second one will not fire up any warning.

Example:

```
virtualenv aaa
virtualenv aaaa
. aaaa/bin/activate
python -m pip install ipython
. aaa/bin/activate
aaaa/bin/ipython
```

Expected behavior after executing `aaaa/bin/ipython`: the following warning message is printed:

```
/home/mrmino/r/ipython/AAA/lib/python3.9/site-packages/IPython/core/interactiveshell.py:958: UserWarning: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv
.
```

It is not shown on `master` nor `7.x`. The root cause is the following condition in `IPython.core.interactiveshell`:

https://github.com/ipython/ipython/blob/3e2c48a0aaf251b06acbf717b462bc4e7fec8cd6/IPython/core/interactiveshell.py#L936-L938

Prefix-wise check of strings with `in` is of course not adequate here. It also covers up the logic from `pathlib.Path` related to case sensitivity. Removing `fspath` from this code and using proper parent relationships from `pathlib.Path` to perform these checks would alleviate the need to use `sys.platform` branches in this method.

Improper path comparison of virtualenv directories
Minor, non-breaking issue found during review of #13094.

If path of the active virtualenv is a substring of another virtualenv, IPython started from the second one will not fire up any warning.

Example:

```
virtualenv aaa
virtualenv aaaa
. aaaa/bin/activate
python -m pip install ipython
. aaa/bin/activate
aaaa/bin/ipython
```

Expected behavior after executing `aaaa/bin/ipython`: the following warning message is printed:

```
/home/mrmino/r/ipython/AAA/lib/python3.9/site-packages/IPython/core/interactiveshell.py:958: UserWarning: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv
.
```

It is not shown on `master` nor `7.x`. The root cause is the following condition in `IPython.core.interactiveshell`:

https://github.com/ipython/ipython/blob/3e2c48a0aaf251b06acbf717b462bc4e7fec8cd6/IPython/core/interactiveshell.py#L936-L938

Prefix-wise check of strings with `in` is of course not adequate here. It also covers up the logic from `pathlib.Path` related to case sensitivity. Removing `fspath` from this code and using proper parent relationships from `pathlib.Path` to perform these checks would alleviate the need to use `sys.platform` branches in this method.



Discussion:
I'll love to try and fix this issue! I am looking into it.
I'll love to try and fix this issue! I am looking into it.

Resolving patch:
diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py
--- a/IPython/core/interactiveshell.py
+++ b/IPython/core/interactiveshell.py
@@ -921,19 +921,12 @@ def init_virtualenv(self):
         while p.is_symlink():
             p = Path(os.readlink(p))
             paths.append(p.resolve())
-
+        
         # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible
         if str(p_venv).startswith("\\cygdrive"):
-            p_venv = Path(str(p_venv)[11:])
-        elif len(str(p_venv)) >= 2 and str(p_venv)[1] == ":":
-            p_venv = Path(str(p_venv)[2:])
-
-        if sys.platform == "win32":
-            # On Windows there might be a mixture of lower-case and mixed-case paths
-            p_venv = Path(str(p_venv).lower())
-            paths = [Path(str(p).lower()) for p in paths]
+            p_venv = "C:" / Path(str(p_venv)[11:])
 
-        if any(os.fspath(p_venv) in os.fspath(p) for p in paths):
+        if any(p_venv == p.parents[1] for p in paths):
             # Our exe is inside or has access to the virtualenv, don't need to do anything.
             return
 


Test patch: