Skip to content

Commit ea299dc

Browse files
fix: Ignore .pth files that are not utf-8 encoded
Issue-300: #300 PR-301: #301
1 parent c9b2e09 commit ea299dc

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/griffe/finder.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,15 @@ def _handle_pth_file(path: Path) -> list[_SP]:
439439
# No item is added to sys.path more than once.
440440
# Blank lines and lines beginning with # are skipped.
441441
# Lines starting with import (followed by space or tab) are executed.
442-
directories = []
443-
for line in path.read_text(encoding="utf8").strip().replace(";", "\n").splitlines(keepends=False):
442+
directories: list[_SP] = []
443+
try:
444+
# It turns out PyTorch recommends its users to use `.pth` as the extension
445+
# when saving models on the disk. These model files are not encoded in UTF8.
446+
# If UTF8 decoding fails, we skip the .pth file.
447+
text = path.read_text(encoding="utf8")
448+
except UnicodeDecodeError:
449+
return directories
450+
for line in text.strip().replace(";", "\n").splitlines(keepends=False):
444451
line = line.strip() # noqa: PLW2901
445452
if _re_import_line.match(line):
446453
editable_module = path.parent / f"{line[len('import'):].lstrip()}.py"

0 commit comments

Comments
 (0)