-
Notifications
You must be signed in to change notification settings - Fork 8
/
python_eval.go
144 lines (115 loc) Β· 2.67 KB
/
python_eval.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
package main
/*
#include "starlark.h"
*/
import "C"
import (
"unsafe"
"go.starlark.net/starlark"
)
// Manage the Python global interpreter lock (GIL)
type PythonEnv interface {
// Detach the GIL and save the thread state
DetachGIL()
// Re-attach the GIL with the saved thread state
ReattachGIL()
}
//export Starlark_eval
func Starlark_eval(self *C.Starlark, args *C.PyObject, kwargs *C.PyObject) *C.PyObject {
var (
expr *C.char
filename *C.char = nil
convert C.uint = 1
print *C.PyObject = nil
goFilename string = "<expr>"
)
if C.parseEvalArgs(args, kwargs, &expr, &filename, &convert, &print) == 0 {
return nil
}
print = pythonPrint(self, print)
if print == nil {
return nil
}
goExpr := C.GoString(expr)
if filename != nil {
goFilename = C.GoString(filename)
}
state := rlockSelf(self)
if state == nil {
return nil
}
defer state.Mutex.RUnlock()
state.DetachGIL()
starlarkPrint := func(_ *starlark.Thread, msg string) {
state.ReattachGIL()
defer state.DetachGIL()
callPythonPrint(print, msg)
}
thread := &starlark.Thread{Print: starlarkPrint}
result, err := starlark.Eval(thread, goFilename, goExpr, state.Globals)
state.ReattachGIL()
if err != nil {
raisePythonException(err)
return nil
}
if convert == 0 {
cstr := C.CString(result.String())
defer C.free(unsafe.Pointer(cstr))
return C.cgoPy_BuildString(cstr)
} else {
retval, err := starlarkValueToPython(result)
if err != nil {
return nil
}
return retval
}
}
//export Starlark_exec
func Starlark_exec(self *C.Starlark, args *C.PyObject, kwargs *C.PyObject) *C.PyObject {
var (
defs *C.char
filename *C.char = nil
print *C.PyObject = nil
goFilename string = "<expr>"
)
if C.parseExecArgs(args, kwargs, &defs, &filename, &print) == 0 {
return nil
}
print = pythonPrint(self, print)
if print == nil {
return nil
}
goDefs := C.GoString(defs)
if filename != nil {
goFilename = C.GoString(filename)
}
state := lockSelf(self)
if state == nil {
return nil
}
defer state.Mutex.Unlock()
state.DetachGIL()
starlarkPrint := func(_ *starlark.Thread, msg string) {
state.ReattachGIL()
defer state.DetachGIL()
callPythonPrint(print, msg)
}
_, program, err := starlark.SourceProgram(goFilename, goDefs, state.Globals.Has)
if err != nil {
state.ReattachGIL()
raisePythonException(err)
return nil
}
thread := &starlark.Thread{Print: starlarkPrint}
newGlobals, err := program.Init(thread, state.Globals)
state.ReattachGIL()
if err != nil {
raisePythonException(err)
return nil
}
for k, v := range newGlobals {
v.Freeze()
state.Globals[k] = v
}
return C.cgoPy_NewRef(C.Py_None)
}