-
Notifications
You must be signed in to change notification settings - Fork 9
/
python.go
86 lines (70 loc) · 2.01 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
// Copyright 2011 Julian Phillips. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package py
// #include "utils.h"
//
// static inline int enterRecursive(char *w) {
// return Py_EnterRecursiveCall(w);
// }
// static inline void leaveRecursive(void) {
// Py_LeaveRecursiveCall();
// }
import "C"
import (
"unsafe"
)
func Initialize() {
InitializeEx(false)
}
func Finalize() {
C.Py_Finalize()
}
func InitializeEx(initsigs bool) {
if initsigs {
panic("Python signal handlers can not be enabled. See https://code.google.com/p/go/issues/detail?id=5287 for details")
} else {
C.Py_InitializeEx(0)
}
}
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.PyUnicode_FromString(s)
if pDir == nil {
return
}
C.PyList_Append(sys_path, pDir)
}
func Main(args []string) int {
panic("Main not ported")
// 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()
}