-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patherror_py2.go
78 lines (67 loc) · 1.56 KB
/
error_py2.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
// +build !py3.4
// +build !py3.5
// +build !py3.6
package py
/*
#include "Python.h"
PyObject* idOrNone(PyObject* o)
{
return o ? o : Py_BuildValue("");
}
void fetchPythonError(PyObject* excInfo)
{
PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
PyTuple_SetItem(excInfo, 0, idOrNone(type));
PyTuple_SetItem(excInfo, 1, idOrNone(value));
PyTuple_SetItem(excInfo, 2, idOrNone(traceback));
}
*/
import "C"
import (
"errors"
"unsafe"
"gopkg.in/sensorbee/py.v0/mainthread"
)
func init() {
ch := make(chan error)
mainthread.Exec(func() {
traceback, err := loadModule("traceback")
if err != nil {
ch <- err
return
}
defer traceback.decRef()
formatException, err := getPyFunc(traceback.p, "format_exception")
if err != nil {
ch <- err
return
}
tracebackFormatExceptionFunc = formatException
exceptions, err := loadModule("exceptions")
if err != nil {
ch <- err
return
}
defer exceptions.decRef()
syntaxErrorCString := C.CString("SyntaxError")
defer C.free(unsafe.Pointer(syntaxErrorCString))
syntaxError := C.PyObject_GetAttrString(exceptions.p, syntaxErrorCString)
if syntaxError == nil {
ch <- errors.New("cannot load exceptions.SyntaxError")
return
}
syntaxErrorType.p = syntaxError
ch <- nil
})
if err := <-ch; err != nil {
panic(err)
}
}
func fetchPythonError(o Object) {
C.fetchPythonError(o.p)
}
func extractLineFromFormattedErrorMessage(formatted Object, n C.Py_ssize_t) string {
line := C.PyList_GetItem(formatted.p, n)
return C.GoString(C.PyString_AsString(line))
}