forked from goplus/py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python.go
88 lines (70 loc) · 1.79 KB
/
python.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
package py
/*
#cgo CFLAGS: -Werror -I/usr/local/include/python2.7
#cgo LDFLAGS: -lpython2.7
#include <Python.h>
static inline int enterRecursive(char *w) {
return Py_EnterRecursiveCall(w);
}
static inline void leaveRecursive() {
Py_LeaveRecursiveCall();
}
*/
import "C"
import "unsafe"
func init() {
C.Py_Initialize()
}
func Initialize() {
C.Py_Initialize()
}
func InitializeEx(initsigs bool) {
if initsigs {
C.Py_InitializeEx(1)
} else {
C.Py_InitializeEx(0)
}
}
func Finalize() {
C.Py_Finalize()
}
func AddToPath(dir string) {
p := C.CString("path")
defer C.free(unsafe.Pointer(p))
sys_path := C.PySys_GetObject(p)
if sys_path == nil {
return
}
s := C.CString(dir)
defer C.free(unsafe.Pointer(s))
pDir := C.PyString_FromString(s)
if pDir == nil {
return
}
C.PyList_Append(sys_path, pDir)
}
func Main(args []string) int {
argv := make([]*C.char, len(args))
for i, arg := range args {
argv[i] = C.CString(arg)
defer C.free(unsafe.Pointer(argv[i]))
}
return int(C.Py_Main(C.int(len(argv)), &argv[0]))
}
// EnterRecusiveCall marks a point where a recursive Go-level call is about to
// be performed. It returns true if the recursive call is permitted, otherwise
// a Python exception is set and false is returned. where is a string that will
// be appended to the RuntimeError set if the recursion limit has been exceeded
// (e.g. " in instance check"). This function needs to be called if the
// recursive function may not invoke Python code (which automatically tracks
// recursion depth).
func EnterRecursiveCall(where string) bool {
s := C.CString(where)
defer C.free(unsafe.Pointer(s))
return C.enterRecursive(s) == 0
}
// LeaveRecursiveCall must be called after a recursive call that was indicated
// by EnterRecursiveCall.
func LeaveRecursiveCall() {
C.leaveRecursive()
}