Skip to content

Commit 2468175

Browse files
committed
Rewriting python finder to eventually fix a number of issue reports.
1 parent 19d3eec commit 2468175

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

pipenv/vendor/pythonfinder/utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
ensure_path,
55
filter_pythons,
66
is_executable,
7+
is_in_path,
78
looks_like_python,
89
path_is_python,
910
resolve_path,
@@ -22,6 +23,7 @@
2223
"get_python_version",
2324
"guess_company",
2425
"is_executable",
26+
"is_in_path",
2527
"looks_like_python",
2628
"parse_asdf_version_order",
2729
"parse_pyenv_version_order",

pipenv/vendor/pythonfinder/utils/path_utils.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,45 @@ def exists_and_is_accessible(path: Path) -> bool:
213213
return False
214214
else:
215215
raise
216+
217+
218+
def is_in_path(path: Union[str, Path], parent_path: Union[str, Path]) -> bool:
219+
"""
220+
Check if a path is inside another path.
221+
222+
Args:
223+
path: The path to check.
224+
parent_path: The potential parent path.
225+
226+
Returns:
227+
Whether the path is inside the parent path.
228+
"""
229+
if not isinstance(path, Path):
230+
path = Path(str(path))
231+
if not isinstance(parent_path, Path):
232+
parent_path = Path(str(parent_path))
233+
234+
# Resolve both paths to absolute paths
235+
path = path.absolute()
236+
parent_path = parent_path.absolute()
237+
238+
# Check if path is a subpath of parent_path
239+
try:
240+
# In Python 3.9+, we could use is_relative_to
241+
# return path.is_relative_to(parent_path)
242+
243+
# For compatibility with Python 3.8 and earlier
244+
path_str = str(path)
245+
parent_path_str = str(parent_path)
246+
247+
# Check if paths are the same
248+
if path_str == parent_path_str:
249+
return True
250+
251+
# Ensure parent_path ends with a separator to avoid partial matches
252+
if not parent_path_str.endswith(os.sep):
253+
parent_path_str += os.sep
254+
255+
return path_str.startswith(parent_path_str)
256+
except (ValueError, OSError):
257+
return False

0 commit comments

Comments
 (0)