Skip to content

Commit

Permalink
Merge pull request #13559 from bucknerns/fix_path_loop_with_tests
Browse files Browse the repository at this point in the history
Fix for the path loop that's compatible with python 3.8
  • Loading branch information
Carreau committed Mar 3, 2022
2 parents d9b5e55 + 3e572a4 commit da607ae
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions IPython/core/interactiveshell.py
Expand Up @@ -758,6 +758,33 @@ def init_displayhook(self):
# the appropriate time.
self.display_trap = DisplayTrap(hook=self.displayhook)

@staticmethod
def get_path_links(p: Path):
"""Gets path links including all symlinks
Examples
--------
In [1]: from IPython.core.interactiveshell import InteractiveShell
In [2]: import sys, pathlib
In [3]: paths = InteractiveShell.get_path_links(pathlib.Path(sys.executable))
In [4]: len(paths) == len(set(paths))
Out[4]: True
In [5]: bool(paths)
Out[5]: True
"""
paths = [p]
while p.is_symlink():
new_path = Path(os.readlink(p))
if not new_path.is_absolute():
new_path = p.parent / new_path
p = new_path
paths.append(p)
return paths

def init_virtualenv(self):
"""Add the current virtualenv to sys.path so the user can import modules from it.
This isn't perfect: it doesn't use the Python interpreter with which the
Expand All @@ -783,10 +810,7 @@ def init_virtualenv(self):
# stdlib venv may symlink sys.executable, so we can't use realpath.
# but others can symlink *to* the venv Python, so we can't just use sys.executable.
# So we just check every item in the symlink tree (generally <= 3)
paths = [p]
while p.is_symlink():
p = Path(os.readlink(p))
paths.append(p.resolve())
paths = self.get_path_links(p)

# In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible
if p_venv.parts[1] == "cygdrive":
Expand Down

0 comments on commit da607ae

Please sign in to comment.