Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions crates/stdlib/src/pyexpat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ mod _pyexpat {
use crate::vm::{
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
VirtualMachine,
builtins::{PyBytesRef, PyException, PyModule, PyStr, PyStrRef, PyType, PyUtf8StrRef},
builtins::{PyException, PyModule, PyStr, PyStrRef, PyType, PyUtf8StrRef},
extend_module,
function::{ArgBytesLike, Either, IntoFuncArgs, OptionalArg, OptionalOption},
function::{ArgBytesLike, ArgStrOrBytesLike, IntoFuncArgs, OptionalArg, OptionalOption},
types::Constructor,
};
use alloc::collections::VecDeque;
Expand Down Expand Up @@ -1403,14 +1403,11 @@ mod _pyexpat {
#[pymethod(name = "Parse")]
fn parse(
zelf: &Py<Self>,
data: Either<PyStrRef, PyBytesRef>,
data: ArgStrOrBytesLike,
isfinal: OptionalArg<bool>,
vm: &VirtualMachine,
) -> PyResult<i32> {
let bytes = match data {
Either::A(s) => s.as_bytes().to_vec(),
Either::B(b) => b.as_bytes().to_vec(),
};
let bytes = data.borrow_bytes().to_vec();
zelf.feed(vm, &bytes, isfinal.unwrap_or(false))?;
Ok(1)
}
Expand Down
27 changes: 27 additions & 0 deletions extra_tests/snippets/stdlib_pyexpat.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,30 @@
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" ")
Loading