Skip to content

Commit

Permalink
fix: Ignore .pth files that are not utf-8 encoded
Browse files Browse the repository at this point in the history
Issue-300: #300
PR-301: #301
  • Loading branch information
qthequartermasterman authored Jul 3, 2024
1 parent c9b2e09 commit ea299dc
Showing 1 changed file with 9 additions and 2 deletions.
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

0 comments on commit ea299dc

Please sign in to comment.