Accept bytes-like input in pyexpat.Parse (#9187)

Assisted-by: Codex:GPT-6
This commit is contained in:
1ndahous3 authored and GitHub committed 2026-10-18 22:36:39 +09:00
1 parent 94ec89a687
commit e792ec8fb3
2 files changed
+31 -7

No files matched your search

+27
View File
@@ -21,3 +21,30 @@ for separator, expected_name, expected_attribute in (
else {expected_attribute: "v", "a": "u"}
)
assert starts[1][1] == expected_attributes
# Parse accepts contiguous buffer objects, including views of mutable input.
for xml in (bytearray(b"<r>ok</r>"), memoryview(b"<r>ok</r>")):
parser = expat.ParserCreate()
text = []
parser.CharacterDataHandler = text.append
assert parser.Parse(xml, True) == 1
assert "".join(text) == "ok"
try:
expat.ParserCreate().Parse(memoryview(b"<r/>")[::2], True)
except BufferError:
pass
else:
raise AssertionError("Parse accepted a non-contiguous buffer")
# Keep the export alive through callbacks without retaining its read lock.
xml = bytearray(b"<r/>")
parser = expat.ParserCreate()
parser.StartElementHandler = lambda *_: xml.extend(b" ")
try:
parser.Parse(xml, True)
except BufferError:
pass
else:
raise AssertionError("Parse released its input buffer before the callback")
xml.extend(b" ")