-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgo2py_converter.go
70 lines (62 loc) · 1.27 KB
/
go2py_converter.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
package py
/*
#include "Python.h"
PyObject* getPyNone() {
return Py_BuildValue("");
}
*/
import "C"
import (
"unsafe"
"gopkg.in/sensorbee/sensorbee.v0/data"
)
func getPyNone() *C.PyObject {
return C.getPyNone()
}
func getNewPyDic(m map[string]interface{}) Object {
return Object{}
}
func newPyString(s string) *C.PyObject {
cs := C.CString(s)
defer C.free(unsafe.Pointer(cs))
return C.PyUnicode_FromString(cs)
}
func newPyArray(a data.Array) (*C.PyObject, error) {
pylist := C.PyList_New(C.Py_ssize_t(len(a)))
if pylist == nil {
return nil, getPyErr()
}
for i, v := range a {
value, err := newPyObj(v)
if err != nil {
return nil, err
}
// PyList object takes over the value's reference, and not need to
// decrease reference counter.
C.PyList_SetItem(pylist, C.Py_ssize_t(i), value.p)
}
return pylist, nil
}
func newPyMap(m data.Map) (*C.PyObject, error) {
pydict := C.PyDict_New()
if pydict == nil {
return nil, getPyErr()
}
for k, v := range m {
err := func() error {
ck := C.CString(k)
defer C.free(unsafe.Pointer(ck))
value, err := newPyObj(v)
if err != nil {
return err
}
defer value.decRef()
C.PyDict_SetItemString(pydict, ck, value.p)
return nil
}()
if err != nil {
return nil, err
}
}
return pydict, nil
}