From 506ca3c48b0c0177dc780144f7e68dbdc6d81794 Mon Sep 17 00:00:00 2001 From: Nathan Buckner Date: Sat, 26 Feb 2022 08:23:48 -0800 Subject: [PATCH 1/2] Fix for the path look that's compatible with python 3.8 --- IPython/core/interactiveshell.py | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 6b30a22e3f0..dd200e9bf0a 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -758,6 +758,33 @@ def init_displayhook(self): # the appropriate time. self.display_trap = DisplayTrap(hook=self.displayhook) + @staticmethod + def get_python_link_paths(p: Path): + """Gets python paths including symlinks + + Examples + -------- + In [1]: from IPython.core.interactiveshell import InteractiveShell + + In [2]: import sys, pathlib + + In [3]: paths = InteractiveShell.get_python_link_paths(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 @@ -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_python_link_paths(p) # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible if p_venv.parts[1] == "cygdrive": From 3e572a4edd6cf36331fa626f4c7d65ed70dbf38a Mon Sep 17 00:00:00 2001 From: Nathan Buckner Date: Sun, 27 Feb 2022 16:39:20 -0800 Subject: [PATCH 2/2] Name of function didn't make sense because it doesn't call sys.executable and takes in any path --- IPython/core/interactiveshell.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index dd200e9bf0a..93c234fcf91 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -759,8 +759,8 @@ def init_displayhook(self): self.display_trap = DisplayTrap(hook=self.displayhook) @staticmethod - def get_python_link_paths(p: Path): - """Gets python paths including symlinks + def get_path_links(p: Path): + """Gets path links including all symlinks Examples -------- @@ -768,7 +768,7 @@ def get_python_link_paths(p: Path): In [2]: import sys, pathlib - In [3]: paths = InteractiveShell.get_python_link_paths(pathlib.Path(sys.executable)) + In [3]: paths = InteractiveShell.get_path_links(pathlib.Path(sys.executable)) In [4]: len(paths) == len(set(paths)) Out[4]: True @@ -810,7 +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 = self.get_python_link_paths(p) + paths = self.get_path_links(p) # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible if p_venv.parts[1] == "cygdrive":