Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for the path loop that's compatible with python 3.8 #13559

Merged
merged 2 commits into from Mar 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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