Skip to content

Commit a288b77

Browse files
committed
BaseException.__setstate__
1 parent 0a6e1e8 commit a288b77

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Lib/test/test_baseexception.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ def test_interface_no_arg(self):
121121
[repr(exc), exc.__class__.__name__ + '()'])
122122
self.interface_test_driver(results)
123123

124-
# TODO: RUSTPYTHON - BaseException.__setstate__ method not implemented
125-
@unittest.expectedFailure
126124
def test_setstate_refcount_no_crash(self):
127125
# gh-97591: Acquire strong reference before calling tp_hash slot
128126
# in PyObject_SetAttr.

vm/src/exceptions.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,24 @@ impl PyRef<PyBaseException> {
647647
vm.new_tuple((self.class().to_owned(), self.args()))
648648
}
649649
}
650+
651+
#[pymethod(magic)]
652+
fn setstate(self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
653+
if !vm.is_none(&state) {
654+
let dict = state
655+
.downcast::<crate::builtins::PyDict>()
656+
.map_err(|_| vm.new_type_error("state is not a dictionary".to_owned()))?;
657+
658+
for (key, value) in &dict {
659+
let key_str = key.str(vm)?;
660+
if key_str.as_str().starts_with("__") {
661+
continue;
662+
}
663+
self.as_object().set_attr(&key_str, value.clone(), vm)?;
664+
}
665+
}
666+
Ok(vm.ctx.none())
667+
}
650668
}
651669

652670
impl Constructor for PyBaseException {

0 commit comments

Comments
 (0)