-
Notifications
You must be signed in to change notification settings - Fork 8
/
python_globals.go
172 lines (139 loc) Β· 3.22 KB
/
python_globals.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
/*
#include "starlark.h"
*/
import "C"
import (
"unsafe"
)
//export Starlark_global_names
func Starlark_global_names(self *C.Starlark, _ *C.PyObject) *C.PyObject {
state := rlockSelf(self)
if state == nil {
return nil
}
defer state.Mutex.RUnlock()
list := C.PyList_New(0)
for _, key := range state.Globals.Keys() {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))
pykey := C.cgoPy_BuildString(ckey)
if pykey == nil {
C.Py_DecRef(list)
return nil
}
if C.PyList_Append(list, pykey) != 0 {
C.Py_DecRef(pykey)
C.Py_DecRef(list)
return nil
}
}
return list
}
//export Starlark_get_global
func Starlark_get_global(self *C.Starlark, args *C.PyObject, kwargs *C.PyObject) *C.PyObject {
var name *C.char = nil
var default_value *C.PyObject = nil
if C.parseGetGlobalArgs(args, kwargs, &name, &default_value) == 0 {
return nil
}
goName := C.GoString(name)
state := rlockSelf(self)
if state == nil {
return nil
}
defer state.Mutex.RUnlock()
value, ok := state.Globals[goName]
if !ok {
if default_value != nil {
return default_value
}
C.PyErr_SetString(C.PyExc_KeyError, name)
return nil
}
retval, err := starlarkValueToPython(value)
if err != nil {
return nil
}
return retval
}
//export Starlark_set_globals
func Starlark_set_globals(self *C.Starlark, args *C.PyObject, kwargs *C.PyObject) *C.PyObject {
posargs := C.PyObject_Length(args)
if posargs > 0 {
errmsg := C.CString("set_globals takes no positional arguments")
defer C.free(unsafe.Pointer(errmsg))
C.PyErr_SetString(C.PyExc_TypeError, errmsg)
return nil
}
if kwargs == nil {
return C.cgoPy_NewRef(C.Py_None)
}
pyiter := C.PyObject_GetIter(kwargs)
if pyiter == nil {
return nil
}
defer C.Py_DecRef(pyiter)
state := rlockSelf(self)
if state == nil {
return nil
}
defer state.Mutex.RUnlock()
for pykey := C.PyIter_Next(pyiter); pykey != nil; pykey = C.PyIter_Next(pyiter) {
defer C.Py_DecRef(pykey)
var size C.Py_ssize_t
ckey := C.PyUnicode_AsUTF8AndSize(pykey, &size)
if ckey == nil {
return nil
}
key := C.GoString(ckey)
pyvalue := C.PyObject_GetItem(kwargs, pykey)
if pyvalue == nil {
return nil
}
defer C.Py_DecRef(pyvalue)
value, err := pythonToStarlarkValue(pyvalue, state)
if err != nil {
return nil
}
value.Freeze()
state.Globals[key] = value
}
return C.cgoPy_NewRef(C.Py_None)
}
//export Starlark_pop_global
func Starlark_pop_global(self *C.Starlark, args *C.PyObject, kwargs *C.PyObject) *C.PyObject {
var name *C.char = nil
var default_value *C.PyObject = nil
if C.parsePopGlobalArgs(args, kwargs, &name, &default_value) == 0 {
return nil
}
goName := C.GoString(name)
state := rlockSelf(self)
if state == nil {
return nil
}
defer state.Mutex.RUnlock()
value, ok := state.Globals[goName]
if !ok {
if default_value != nil {
return default_value
}
C.PyErr_SetString(C.PyExc_KeyError, name)
return nil
}
delete(state.Globals, goName)
retval, err := starlarkValueToPython(value)
if err != nil {
return nil
}
return retval
}
//export Starlark_tp_iter
func Starlark_tp_iter(self *C.Starlark) *C.PyObject {
keys := Starlark_global_names(self, nil)
if keys == nil {
return nil
}
return C.PyObject_GetIter(keys)
}