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: Ignore .pth files that are not utf-8 encoded, for example PyTorch files #301

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/griffe/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,15 @@ def _handle_pth_file(path: Path) -> list[_SP]:
# No item is added to sys.path more than once.
# Blank lines and lines beginning with # are skipped.
# Lines starting with import (followed by space or tab) are executed.
directories = []
for line in path.read_text(encoding="utf8").strip().replace(";", "\n").splitlines(keepends=False):
directories: list[_SP] = []
try:
# It turns out PyTorch recommends its users to use `.pth` as the extension
# when saving models on the disk. These model files are not encoded in UTF8.
# If UTF8 decoding fails, we skip the .pth file.
text = path.read_text(encoding="utf8")
except UnicodeDecodeError:
return directories
for line in text.strip().replace(";", "\n").splitlines(keepends=False):
line = line.strip() # noqa: PLW2901
if _re_import_line.match(line):
editable_module = path.parent / f"{line[len('import'):].lstrip()}.py"
Expand Down
Loading