forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraceback.rs
More file actions
80 lines (66 loc) · 1.98 KB
/
traceback.rs
File metadata and controls
80 lines (66 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use rustpython_common::lock::PyMutex;
use super::PyType;
use crate::{class::PyClassImpl, frame::FrameRef, Context, Py, PyPayload, PyRef, VirtualMachine};
#[pyclass(module = false, name = "traceback")]
#[derive(Debug)]
pub struct PyTraceback {
pub next: PyMutex<Option<PyTracebackRef>>,
pub frame: FrameRef,
pub lasti: u32,
pub lineno: usize,
}
pub type PyTracebackRef = PyRef<PyTraceback>;
impl PyPayload for PyTraceback {
fn class(vm: &VirtualMachine) -> &'static Py<PyType> {
vm.ctx.types.traceback_type
}
}
#[pyclass]
impl PyTraceback {
pub fn new(next: Option<PyRef<Self>>, frame: FrameRef, lasti: u32, lineno: usize) -> Self {
PyTraceback {
next: PyMutex::new(next),
frame,
lasti,
lineno,
}
}
#[pygetset]
fn tb_frame(&self) -> FrameRef {
self.frame.clone()
}
#[pygetset]
fn tb_lasti(&self) -> u32 {
self.lasti
}
#[pygetset]
fn tb_lineno(&self) -> usize {
self.lineno
}
#[pygetset]
fn tb_next(&self) -> Option<PyRef<Self>> {
self.next.lock().as_ref().cloned()
}
#[pygetset(setter)]
fn set_tb_next(&self, value: Option<PyRef<Self>>) {
*self.next.lock() = value;
}
}
impl PyTracebackRef {
pub fn iter(&self) -> impl Iterator<Item = PyTracebackRef> {
std::iter::successors(Some(self.clone()), |tb| tb.next.lock().clone())
}
}
pub fn init(context: &Context) {
PyTraceback::extend_class(context, context.types.traceback_type);
}
impl serde::Serialize for PyTraceback {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
use serde::ser::SerializeStruct;
let mut struc = s.serialize_struct("PyTraceback", 3)?;
struc.serialize_field("name", self.frame.code.obj_name.as_str())?;
struc.serialize_field("lineno", &self.lineno)?;
struc.serialize_field("filename", self.frame.code.source_path.as_str())?;
struc.end()
}
}