Skip to content
Merged
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
16 changes: 15 additions & 1 deletion crates/stdlib/src/elementtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions extra_tests/snippets/stdlib_xml.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -66,3 +68,18 @@ def endElement(self, name):
("characters", "text"),
("end-cdata",),
]

root = Element("root")
for value, type_name in (

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Remove the newly added test logic

This hunk adds test data, control flow, and an assertion to extra_tests/snippets/stdlib_xml.py, but the repository's critical test-file policy permits only adding or removing expectedFailure decorators; this regression coverage therefore needs to be removed rather than extending the snippet.

AGENTS.md reference: AGENTS.md:L275-L279

Useful? React with 👍 / 👎.

(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
)
Loading