Skip to content

Commit 1fa883b

Browse files
authored
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
1 parent 4e3b2f4 commit 1fa883b

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

‎crates/stdlib/src/elementtree.rs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,21 @@ pub(crate) mod _elementtree {
578578

579579
#[pymethod]
580580
fn remove(zelf: &Py<Self>, subelement: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
581-
check_element(&subelement, vm)?;
581+
if !subelement.downcastable::<Self>() {
582+
let name = if vm.is_none(&subelement) {
583+
"None".into()
584+
} else {
585+
subelement.class().slot_name()
586+
};
587+
let mut end = name.len().min(50);
588+
while !name.is_char_boundary(end) {
589+
end -= 1;
590+
}
591+
return Err(vm.new_type_error(format!(
592+
"remove() argument must be xml.etree.ElementTree.Element, not {}",
593+
&name[..end]
594+
)));
595+
}
582596
// Identity first, then equality, the way `element_remove` does;
583597
// the equality test can run arbitrary code, so the lock is
584598
// dropped for it and the list re-read afterwards.

‎extra_tests/snippets/stdlib_xml.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import array
12
import xml.sax
3+
from xml.etree.ElementTree import Element
24
from xml.parsers import expat
35

46
from testutils import assert_raises
@@ -66,3 +68,18 @@ def endElement(self, name):
6668
("characters", "text"),
6769
("end-cdata",),
6870
]
71+
72+
root = Element("root")
73+
for value, type_name in (
74+
(None, "None"),
75+
(1, "int"),
76+
("child", "str"),
77+
(array.array("b"), "array.array"),
78+
(type("A" * 60, (), {})(), "A" * 50),
79+
(type("€" * 20, (), {})(), "€" * 16),
80+
):
81+
with assert_raises(TypeError) as caught:
82+
root.remove(value)
83+
assert str(caught.exception) == (
84+
"remove() argument must be xml.etree.ElementTree.Element, not " + type_name
85+
)

0 commit comments

Comments
 (0)