Skip to content

Commit bbb5781

Browse files
committed
DOP-2613: Handle unicode decode errors in literalinclude
1 parent 9ab33ab commit bbb5781

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

snooty/parser.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,14 +720,15 @@ def create_page() -> Tuple[Page, EmbeddedRstParser]:
720720

721721
# Attempt to read the literally included file
722722
try:
723-
with open(filepath) as file:
724-
text = file.read()
725-
723+
text = filepath.read_text(encoding="utf-8")
726724
except OSError as err:
727725
self.diagnostics.append(
728726
CannotOpenFile(argument_text, err.strerror, line)
729727
)
730728
return doc
729+
except UnicodeDecodeError as err:
730+
self.diagnostics.append(CannotOpenFile(argument_text, str(err), line))
731+
return doc
731732

732733
lines = text.split("\n")
733734

snooty/test_parser.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,20 @@ def test_literalinclude() -> None:
703703
assert len(diagnostics) == 1
704704
assert isinstance(diagnostics[0], DocUtilsParseError)
705705

706+
# Test non-textual
707+
page, diagnostics = parse_rst(
708+
parser,
709+
path,
710+
"""
711+
.. literalinclude:: /compass-explain-plan-with-index-raw-json.png
712+
""",
713+
)
714+
page.finish(diagnostics)
715+
assert len(diagnostics) == 1
716+
assert [(type(d), "utf-8" in d.message) for d in diagnostics] == [
717+
(CannotOpenFile, True)
718+
]
719+
706720

707721
def test_include() -> None:
708722
path = ROOT_PATH.joinpath(Path("test.rst"))

0 commit comments

Comments
 (0)