BaseException.__setstate__

This commit is contained in:
Jeong YunWon committed 2025-06-23 09:41:21 +09:00
1 parent 0a6e1e80d7
commit a288b77c4f
2 files changed
+18 -2

No files matched your search

-2
View File
@@ -121,8 +121,6 @@ class ExceptionClassTests(unittest.TestCase):
[repr(exc), exc.__class__.__name__ + '()'])
self.interface_test_driver(results)
# TODO: RUSTPYTHON - BaseException.__setstate__ method not implemented
@unittest.expectedFailure
def test_setstate_refcount_no_crash(self):
# gh-97591: Acquire strong reference before calling tp_hash slot
# in PyObject_SetAttr.
+18
View File
@@ -647,6 +647,24 @@ impl PyRef<PyBaseException> {
vm.new_tuple((self.class().to_owned(), self.args()))
}
}
#[pymethod(magic)]
fn setstate(self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
if !vm.is_none(&state) {
let dict = state
.downcast::<crate::builtins::PyDict>()
.map_err(|_| vm.new_type_error("state is not a dictionary".to_owned()))?;
for (key, value) in &dict {
let key_str = key.str(vm)?;
if key_str.as_str().starts_with("__") {
continue;
}
self.as_object().set_attr(&key_str, value.clone(), vm)?;
}
}
Ok(vm.ctx.none())
}
}
impl Constructor for PyBaseException {