Skip to content
Merged
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
23 changes: 16 additions & 7 deletions crates/stdlib/src/elementtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) mod _elementtree {
IterNext, Iterable, Representable, SelfIter,
},
};
use rustpython_common::lock::PyRwLock;
use rustpython_common::{lock::PyRwLock, wtf8::Wtf8Buf};

// The per-module bookkeeping `_elementtree.c` keeps in its
// `elementtreestate`: the `Comment`/`ProcessingInstruction` factories
Expand Down Expand Up @@ -883,12 +883,21 @@ pub(crate) mod _elementtree {

#[pymethod]
fn __setstate__(zelf: &Py<Self>, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
let dict = state.downcast_ref::<PyDict>().ok_or_else(|| {
vm.new_type_error(format!(
"Don't know how to unpickle \"{}\" as an Element",
state.repr(vm).map(|s| s.to_string()).unwrap_or_default()
))
})?;
let Some(dict) = state
.downcast_ref::<PyDict>()
.filter(|_| state.class().is(vm.ctx.types.dict_type))
else {
let repr = state.repr(vm)?;
let end = repr
.as_wtf8()
.code_point_indices()
.nth(200)
.map_or_else(|| repr.byte_len(), |(offset, _)| offset);
let mut message = Wtf8Buf::from("Don't know how to unpickle \"");
message.push_wtf8(&repr.as_wtf8()[..end]);
message.push_str("\" as an Element");
return Err(vm.new_type_error(message));
};
let tag = dict.get_item_opt("tag", vm)?;
let attrib = dict.get_item_opt("attrib", vm)?;
let text = dict.get_item_opt("text", vm)?;
Expand Down
Loading