-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathtraceback.go
104 lines (92 loc) · 2.38 KB
/
traceback.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2018 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Traceback objects
package py
import (
"fmt"
"io"
"os"
)
// A python Traceback object
type Traceback struct {
Next *Traceback
Frame *Frame
Lasti int32
Lineno int32
}
var TracebackType = NewType("traceback", "A python traceback")
// Type of this object
func (o *Traceback) Type() *Type {
return TracebackType
}
// Make a new traceback
func NewTraceback(next *Traceback, frame *Frame, lasti, lineno int32) *Traceback {
return &Traceback{
Next: next,
Frame: frame,
Lasti: lasti,
Lineno: lineno,
}
}
/*
Traceback (most recent call last):
File "throws.py", line 8, in <module>
main()
File "throws.py", line 5, in main
throws()
File "throws.py", line 2, in throws
raise RuntimeError('this is the error message')
RuntimeError: this is the error message
*/
// Dump a traceback for tb to w
func (tb *Traceback) TracebackDump(w io.Writer) {
for ; tb != nil; tb = tb.Next {
fmt.Fprintf(w, " File %q, line %d, in %s\n", tb.Frame.Code.Filename, tb.Lineno, tb.Frame.Code.Name)
//fmt.Fprintf(w, " %s\n", "FIXME line of source goes here")
}
}
// Dumps a traceback to stderr
func TracebackDump(err interface{}) {
switch e := err.(type) {
case ExceptionInfo:
e.TracebackDump(os.Stderr)
case *ExceptionInfo:
e.TracebackDump(os.Stderr)
case *Exception:
fmt.Fprintf(os.Stderr, "Exception %v\n", e)
fmt.Fprintf(os.Stderr, "-- No traceback available --\n")
default:
fmt.Fprintf(os.Stderr, "Error %v\n", err)
fmt.Fprintf(os.Stderr, "-- No traceback available --\n")
}
}
// Properties
func init() {
TracebackType.Dict["__tb_next__"] = &Property{
Fget: func(self Object) (Object, error) {
next := self.(*Traceback).Next
if next == nil {
return None, nil
}
return next, nil
},
}
TracebackType.Dict["__tb_frame__"] = &Property{
Fget: func(self Object) (Object, error) {
return self.(*Traceback).Frame, nil
},
}
TracebackType.Dict["__tb_lasti__"] = &Property{
Fget: func(self Object) (Object, error) {
return Int(self.(*Traceback).Lasti), nil
},
}
TracebackType.Dict["__tb_lineno__"] = &Property{
Fget: func(self Object) (Object, error) {
return Int(self.(*Traceback).Lineno), nil
},
}
}
// Make sure it satisfies the interface
var _ Object = (*Traceback)(nil)