Skip to content

BaseException.__setstate__ #5821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 23, 2025
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
3 changes: 1 addition & 2 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1933,8 +1933,7 @@ def _check_tracemalloc():
"if tracemalloc module is tracing "
"memory allocations")


# TODO: RUSTPYTHON (comment out before)
# TODO: RUSTPYTHON; GC is not supported yet
# def check_free_after_iterating(test, iter, cls, args=()):
# class A(cls):
# def __del__(self):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def test_numbers(self):
self.assertEqual(a, b,
msg="{0!r} != {1!r}; testcase={2!r}".format(a, b, testcase))

# TODO: RUSTPYTHON
# TODO: RUSTPYTHON - requires UTF-32 encoding support in codecs and proper array reconstructor implementation
@unittest.expectedFailure
def test_unicode(self):
teststr = "Bonne Journ\xe9e \U0002030a\U00020347"
Expand Down
4 changes: 1 addition & 3 deletions Lib/test/test_baseexception.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_inheritance(self):
exc_set = set(e for e in exc_set if not e.startswith('_'))
# RUSTPYTHON specific
exc_set.discard("JitError")
# TODO: RUSTPYTHON; this will be officially introduced in Python 3.15
# XXX: RUSTPYTHON; IncompleteInputError will be officially introduced in Python 3.15
exc_set.discard("IncompleteInputError")
self.assertEqual(len(exc_set), 0, "%s not accounted for" % exc_set)

Expand Down Expand Up @@ -121,8 +121,6 @@ def test_interface_no_arg(self):
[repr(exc), exc.__class__.__name__ + '()'])
self.interface_test_driver(results)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_setstate_refcount_no_crash(self):
# gh-97591: Acquire strong reference before calling tp_hash slot
# in PyObject_SetAttr.
Expand Down
18 changes: 18 additions & 0 deletions vm/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down