-
Notifications
You must be signed in to change notification settings - Fork 6
/
error_py3.go
65 lines (54 loc) · 1.27 KB
/
error_py3.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
// +build py3.4 py3.5 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);
PyErr_NormalizeException(&type, &value, &traceback);
if (traceback != NULL) {
PyException_SetTraceback(value, traceback);
}
PyTuple_SetItem(excInfo, 0, idOrNone(type));
PyTuple_SetItem(excInfo, 1, idOrNone(value));
PyTuple_SetItem(excInfo, 2, idOrNone(traceback));
}
*/
import "C"
import (
"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
syntaxErrorType.p = C.PyExc_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.PyUnicode_AsUTF8(line))
}