Skip to content

Commit

Permalink
Return False if os.path.commonpath raises ValueError for different dr…
Browse files Browse the repository at this point in the history
…ives

Signed-off-by: Xiaochao Dong (@damnever) <the.xcdong@gmail.com>
  • Loading branch information
damnever committed Dec 15, 2022
1 parent 34e1e6b commit 66cceda
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
6 changes: 3 additions & 3 deletions pigar/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .log import logger
from .helpers import (
Color, parse_requirements, PraseRequirementError, trim_prefix, trim_suffix,
determine_python_sys_lib_paths, is_site_packages_path
is_commonpath, determine_python_sys_lib_paths, is_site_packages_path
)
from .parser import parse_imports, Module
from .dist import (
Expand Down Expand Up @@ -476,7 +476,7 @@ def is_user_module(module: Module, project_root: str):
return False
return (
spec.origin != module.file
and os.path.commonpath([spec.origin, project_root]) == project_root
and is_commonpath([spec.origin, project_root], project_root)
) or root_module_name == os.path.basename(project_root)
except Exception:
return False
Expand Down Expand Up @@ -518,7 +518,7 @@ def check_stdlib(name: str, _sys_lib_paths=determine_python_sys_lib_paths()):
return False, module_path

for sys_path in _sys_lib_paths:
if os.path.commonpath([sys_path, module_path]) == sys_path:
if is_commonpath([sys_path, module_path], sys_path):
return True, None

return False, module_path
Expand Down
14 changes: 7 additions & 7 deletions pigar/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import asyncio

from .log import logger
from .helpers import cmp_to_key, trim_prefix, trim_suffix, InMemoryOrDiskFile
from .helpers import cmp_to_key, trim_prefix, trim_suffix, InMemoryOrDiskFile, is_commonpath
from .unpack import parse_top_levels
from .db import database
from .version import version
Expand Down Expand Up @@ -139,11 +139,11 @@ def from_dist(cls, dist):
continue
if not any(
[
os.path.commonpath([code_file_dir, file]) ==
code_file_dir, # Fast path to skip the same path prefix.
os.path.commonpath([dist_info_dir, file]) == dist_info_dir,
os.path.commonpath([os.pardir, file]) == os.pardir,
os.path.commonpath([os.curdir, file]) == os.curdir,
is_commonpath([code_file_dir, file], code_file_dir
), # Fast path to skip the same path prefix.
is_commonpath([dist_info_dir, file], dist_info_dir),
is_commonpath([os.pardir, file], os.pardir),
is_commonpath([os.curdir, file], os.curdir),
file.startswith('__')
]
):
Expand All @@ -169,7 +169,7 @@ def contains_file(self, file):
if not self.code_paths or not file:
return False
for code_path in self.code_paths:
if code_path == os.path.commonpath([code_path, file]):
if is_commonpath([code_path, file], code_path):
return True
return False

Expand Down
13 changes: 10 additions & 3 deletions pigar/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,20 @@ def determine_python_sys_lib_paths() -> List[str]:
parts = pathlib.PurePath(path).parts
if 'site-packages' in parts or 'dist-packages' in parts:
continue
if os.path.commonpath(
[path, py_sys_path_prefix]
) == py_sys_path_prefix:
if is_commonpath([path, py_sys_path_prefix], py_sys_path_prefix):
lib_paths.append(path)
return lib_paths


def is_commonpath(paths: List[str], target: str) -> bool:
try:
return os.path.commonpath(paths) == target
except ValueError:
# Raise ValueError if paths contain both absolute and relative pathnames,
# the paths are on the different drives or if *paths* is empty.
return False


def is_site_packages_path(path: str) -> bool:
parts = pathlib.PurePath(path).parts
return 'site-packages' in parts or 'dist-packages' in parts

0 comments on commit 66cceda

Please sign in to comment.