mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 10:44:36 +08:00
Match Element.remove argument type diagnostics (#9068)
Match native type names, None spelling, and UTF-8 byte limits while preserving removal behavior. Keep all six regression cases in the existing XML snippet. Assisted-by: OpenAI Codex:model identifier unavailable
This commit is contained in:
1 parent
4e3b2f4039
commit
1fa883b9b5
2 files changed
+32
-1
No files matched your search
@@ -578,7 +578,21 @@ pub(crate) mod _elementtree {
|
||||
|
||||
#[pymethod]
|
||||
fn remove(zelf: &Py<Self>, subelement: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
|
||||
check_element(&subelement, vm)?;
|
||||
if !subelement.downcastable::<Self>() {
|
||||
let name = if vm.is_none(&subelement) {
|
||||
"None".into()
|
||||
} else {
|
||||
subelement.class().slot_name()
|
||||
};
|
||||
let mut end = name.len().min(50);
|
||||
while !name.is_char_boundary(end) {
|
||||
end -= 1;
|
||||
}
|
||||
return Err(vm.new_type_error(format!(
|
||||
"remove() argument must be xml.etree.ElementTree.Element, not {}",
|
||||
&name[..end]
|
||||
)));
|
||||
}
|
||||
// Identity first, then equality, the way `element_remove` does;
|
||||
// the equality test can run arbitrary code, so the lock is
|
||||
// dropped for it and the list re-read afterwards.
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import array
|
||||
import xml.sax
|
||||
from xml.etree.ElementTree import Element
|
||||
from xml.parsers import expat
|
||||
|
||||
from testutils import assert_raises
|
||||
@@ -66,3 +68,18 @@ assert events == [
|
||||
("characters", "text"),
|
||||
("end-cdata",),
|
||||
]
|
||||
|
||||
root = Element("root")
|
||||
for value, type_name in (
|
||||
(None, "None"),
|
||||
(1, "int"),
|
||||
("child", "str"),
|
||||
(array.array("b"), "array.array"),
|
||||
(type("A" * 60, (), {})(), "A" * 50),
|
||||
(type("€" * 20, (), {})(), "€" * 16),
|
||||
):
|
||||
with assert_raises(TypeError) as caught:
|
||||
root.remove(value)
|
||||
assert str(caught.exception) == (
|
||||
"remove() argument must be xml.etree.ElementTree.Element, not " + type_name
|
||||
)
|
||||
Reference in new issue
Block a user