mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Accept bytes-like input in pyexpat.Parse (#9187)
Assisted-by: Codex:GPT-6
This commit is contained in:
1 parent
94ec89a687
commit
e792ec8fb3
2 files changed
+31
-7
No files matched your search
@@ -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;
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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" ")
|
||||
Reference in new issue
Block a user