Skip to content

Commit e1744b4

Browse files
committed
FIX: Gracefully handle empty files for imgmath depth extraction
Closes sphinx-doc#14466. While empty files are likely an error `read_svg_depth()` is not the place to flag empty files. It is designed to handle files without depth comment, and that should also be true for the edge case of an empty file.
1 parent cc7c6f4 commit e1744b4

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

sphinx/ext/imgmath.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,17 @@ class InvokeError(SphinxError):
6767

6868

6969
def read_svg_depth(filename: str | os.PathLike[str]) -> int | None:
70-
"""Read the depth from comment at last line of SVG file"""
70+
"""Read the depth from comment at last line of SVG file.
71+
72+
Returns None if the file does not contain a depth comment.
73+
"""
7174
with open(filename, encoding='utf-8') as f:
72-
for line in f: # NoQA: B007
73-
pass
74-
# Only last line is checked
75-
matched = depthsvgcomment_re.match(line)
76-
if matched:
75+
last_line = next(reversed(list(f)), None)
76+
77+
if last_line is not None:
78+
if matched := depthsvgcomment_re.match(last_line):
7779
return int(matched.group(1))
78-
return None
80+
return None
7981

8082

8183
def write_svg_depth(filename: Path, depth: int) -> None:

0 commit comments

Comments
 (0)