From a288b77c4f775e10c7dc89f1dca9060858e2728f Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sun, 22 Jun 2025 21:51:06 +0900 Subject: [PATCH] BaseException.__setstate__ --- Lib/test/test_baseexception.py | 2 -- vm/src/exceptions.rs | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_baseexception.py b/Lib/test/test_baseexception.py index c2446e549..63bf538aa 100644 --- a/Lib/test/test_baseexception.py +++ b/Lib/test/test_baseexception.py @@ -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. diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index 77829608b..885f82b71 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -647,6 +647,24 @@ impl PyRef { vm.new_tuple((self.class().to_owned(), self.args())) } } + + #[pymethod(magic)] + fn setstate(self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult { + if !vm.is_none(&state) { + let dict = state + .downcast::() + .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 {