rustpython_vm/builtins/
traceback.rs1use rustpython_common::lock::PyMutex;
2
3use super::PyType;
4use crate::{
5 class::PyClassImpl, frame::FrameRef, source_code::LineNumber, Context, Py, PyPayload, PyRef,
6};
7
8#[pyclass(module = false, name = "traceback", traverse)]
9#[derive(Debug)]
10pub struct PyTraceback {
11 pub next: PyMutex<Option<PyTracebackRef>>,
12 pub frame: FrameRef,
13 #[pytraverse(skip)]
14 pub lasti: u32,
15 #[pytraverse(skip)]
16 pub lineno: LineNumber,
17}
18
19pub type PyTracebackRef = PyRef<PyTraceback>;
20
21impl PyPayload for PyTraceback {
22 fn class(ctx: &Context) -> &'static Py<PyType> {
23 ctx.types.traceback_type
24 }
25}
26
27#[pyclass]
28impl PyTraceback {
29 pub fn new(next: Option<PyRef<Self>>, frame: FrameRef, lasti: u32, lineno: LineNumber) -> Self {
30 PyTraceback {
31 next: PyMutex::new(next),
32 frame,
33 lasti,
34 lineno,
35 }
36 }
37
38 #[pygetset]
39 fn tb_frame(&self) -> FrameRef {
40 self.frame.clone()
41 }
42
43 #[pygetset]
44 fn tb_lasti(&self) -> u32 {
45 self.lasti
46 }
47
48 #[pygetset]
49 fn tb_lineno(&self) -> usize {
50 self.lineno.to_usize()
51 }
52
53 #[pygetset]
54 fn tb_next(&self) -> Option<PyRef<Self>> {
55 self.next.lock().as_ref().cloned()
56 }
57
58 #[pygetset(setter)]
59 fn set_tb_next(&self, value: Option<PyRef<Self>>) {
60 *self.next.lock() = value;
61 }
62}
63
64impl PyTracebackRef {
65 pub fn iter(&self) -> impl Iterator<Item = PyTracebackRef> {
66 std::iter::successors(Some(self.clone()), |tb| tb.next.lock().clone())
67 }
68}
69
70pub fn init(context: &Context) {
71 PyTraceback::extend_class(context, context.types.traceback_type);
72}
73
74#[cfg(feature = "serde")]
75impl serde::Serialize for PyTraceback {
76 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
77 use serde::ser::SerializeStruct;
78
79 let mut struc = s.serialize_struct("PyTraceback", 3)?;
80 struc.serialize_field("name", self.frame.code.obj_name.as_str())?;
81 struc.serialize_field("lineno", &self.lineno.get())?;
82 struc.serialize_field("filename", self.frame.code.source_path.as_str())?;
83 struc.end()
84 }
85}