Skip to content

Exception.set_traceback_typed #5832

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 1 commit into from
Jun 24, 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
2 changes: 0 additions & 2 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,8 +607,6 @@ class MyException(Exception):
self.assertIsInstance(e, MyException)
self.assertEqual(e.__traceback__, tb)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def testInvalidTraceback(self):
try:
Exception().__traceback__ = 5
Expand Down
25 changes: 22 additions & 3 deletions vm/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::object::{Traverse, TraverseFn};
use crate::{
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
builtins::{
PyNone, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef, traceback::PyTracebackRef,
PyNone, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef,
traceback::{PyTraceback, PyTracebackRef},
},
class::{PyClassImpl, StaticType},
convert::{ToPyException, ToPyObject},
Expand Down Expand Up @@ -324,7 +325,7 @@ impl VirtualMachine {
let ctor = ExceptionCtor::try_from_object(self, exc_type)?;
let exc = ctor.instantiate_value(exc_val, self)?;
if let Some(tb) = Option::<PyTracebackRef>::try_from_object(self, exc_tb)? {
exc.set_traceback(Some(tb));
exc.set_traceback_typed(Some(tb));
}
Ok(exc)
}
Expand Down Expand Up @@ -584,7 +585,25 @@ impl PyBaseException {
}

#[pygetset(magic, setter)]
pub fn set_traceback(&self, traceback: Option<PyTracebackRef>) {
pub fn set_traceback(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
let traceback = if vm.is_none(&value) {
None
} else {
match value.downcast::<PyTraceback>() {
Ok(tb) => Some(tb),
Err(_) => {
return Err(
vm.new_type_error("__traceback__ must be a traceback or None".to_owned())
);
}
}
};
self.set_traceback_typed(traceback);
Ok(())
}

// Helper method for internal use that doesn't require PyObjectRef
pub(crate) fn set_traceback_typed(&self, traceback: Option<PyTracebackRef>) {
*self.traceback.write() = traceback;
}

Expand Down
2 changes: 1 addition & 1 deletion vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ impl ExecutingFrame<'_> {
let new_traceback =
PyTraceback::new(next, frame.object.to_owned(), frame.lasti(), loc.row);
vm_trace!("Adding to traceback: {:?} {:?}", new_traceback, loc.row);
exception.set_traceback(Some(new_traceback.into_ref(&vm.ctx)));
exception.set_traceback_typed(Some(new_traceback.into_ref(&vm.ctx)));

vm.contextualize_exception(&exception);

Expand Down
2 changes: 1 addition & 1 deletion vm/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,6 @@ pub fn remove_importlib_frames(vm: &VirtualMachine, exc: &PyBaseExceptionRef) {

if let Some(tb) = exc.traceback() {
let trimmed_tb = remove_importlib_frames_inner(vm, Some(tb), always_trim).0;
exc.set_traceback(trimmed_tb);
exc.set_traceback_typed(trimmed_tb);
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy