-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
159 lines (137 loc) · 3.64 KB
/
error.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package py
// #include <Python.h>
// static inline void incref(PyObject *obj) { Py_INCREF(obj); }
// static inline void decref(PyObject *obj) { Py_DECREF(obj); }
// static inline void xdecref(PyObject *obj) { Py_XDECREF(obj); }
import "C"
import "fmt"
import "syscall"
import "strings"
import "runtime"
import "github.com/qiniu/errors"
// Error represents a Python exception as a Go struct that implements the
// error interface. It allows Go code to handle Python exceptions in an
// idiomatic Go fashion.
type Error struct {
Kind *Base
Value *Base
tb *C.PyObject
}
func newError(kind, val *Base, tb *C.PyObject) *Error {
e := &Error{kind, val, tb}
runtime.SetFinalizer(e, (*Error).release)
return e
}
func (e *Error) release() error {
if e.Kind != nil {
e.Kind.Decref()
e.Value.Decref()
e.Kind = nil
e.Value = nil
if e.tb != nil {
C.decref(e.tb)
e.tb = nil
}
}
return nil
}
// Error() returns a string representation of the Python exception represented
// by the Error e. This is the same as the final line of the Python output from
// an uncaught exception.
func (e *Error) Error() string {
kind := e.Kind.String()
if strings.HasPrefix(kind, "<type 'exceptions.") {
kind = kind[18:len(kind)-2]
}
return kind + ": " + e.Value.String()
}
/*
// Matches returns true if e.Kind matches the exception in exc. If exc is a
// Class, then true is returned if e.Kind is an instance. If exc is a Tuple,
// then all elements (and recursively for sub elements) are searched for a
// match.
func (e *Error) Matches(exc Object) bool {
return C.PyErr_GivenExceptionMatches(c(e.Kind), c(exc)) != 0
}
*/
// Normalize adjusts e.Kind/e.Value in the case that the values aren't
// normalized to start with. It's possible that an Error returned from Python
// might have e.Kind be a Class, with e.Value not being an instance of that
// class, Normalize will fix this. The separate normalization is implemented in
// Python to improve performance.
func (e *Error) Normalize() {
exc := e.Kind.c()
val := e.Value.c()
tb := e.tb
C.PyErr_NormalizeException(&exc, &val, &tb)
e.Kind = newObject(exc)
e.Value = newObject(val)
e.tb = tb
}
// NewErrorV returns a new Error of the specified kind, and with the given
// value.
func NewErrorV(kind *Base, value *Base) *Error {
kind.Incref()
value.Incref()
return newError(kind, value, nil)
}
// NewError returns a new Error of the specified kind, and with the value
// being a new String containing the string created the given format and args.
func NewError(kind *Base, format string, args ...interface{}) *Error {
msg := fmt.Sprintf(format, args...)
kind.Incref()
val := NewString(msg)
return newError(kind, &val.Base, nil)
}
func Raise(err error) {
var val *C.PyObject
var exc = C.PyExc_Exception
e, ok := err.(*Error)
if ok {
exc = e.Kind.c()
val = e.Value.c()
} else {
v := NewString(errors.Detail(err))
val = v.c()
defer C.decref(val)
}
C.PyErr_SetObject(exc, val)
}
func GetException() error {
return exception()
}
func exceptionRaised() bool {
return C.PyErr_Occurred() != nil
}
func exception() error {
if C.PyErr_Occurred() == nil {
return syscall.EFAULT
}
var t, v, tb *C.PyObject
C.PyErr_Fetch(&t, &v, &tb)
return newError(newObject(t), newObject(v), tb)
}
func ssize_t2Int64Err(s C.Py_ssize_t) (int64, error) {
if s < 0 {
return 0, exception()
}
return int64(s), nil
}
func int2BoolErr(i C.int) (bool, error) {
if i < 0 {
return false, exception()
}
return i > 0, nil
}
func int2Err(i C.int) error {
if i < 0 {
return exception()
}
return nil
}
func obj2ObjErr(obj *C.PyObject) (*Base, error) {
if obj == nil {
return nil, exception()
}
return newObject(obj), nil
}