Fix empty Markdown document parsing (#18503)

## Summary
- treat empty Markdown binaries as in-memory content instead of local
file paths
- add a regression test ensuring empty content does not access the
filesystem

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Jiang, Guomin
2026-08-19 16:33:51 +08:00
committed by GitHub
parent 3791f27b38
commit 9e913c3fb0
2 changed files with 16 additions and 1 deletions

View File

@@ -894,7 +894,7 @@ class Markdown(MarkdownParser):
def __call__(self, filename, binary=None, separate_tables=True, delimiter=None, return_section_images=False):
"""Parse markdown into text sections and optional standalone table chunks."""
if binary:
if binary is not None:
encoding = find_codec(binary)
txt = binary.decode(encoding, errors="ignore")
else:

View File

@@ -91,6 +91,21 @@ def parser(naive_module):
return naive_module.Markdown(128)
@pytest.mark.p1
def test_parses_empty_binary_without_opening_filename(parser):
with patch("builtins.open") as open_file:
sections, tables, section_images = parser(
"empty-document.md",
binary=b"",
return_section_images=True,
)
open_file.assert_not_called()
assert sections == []
assert tables == []
assert section_images == []
@pytest.mark.p1
def test_blocks_internal_url_without_fetching(parser):
"""A markdown image pointing at an internal host must never be requested."""