forked from gefarion/python2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha.go
226 lines (196 loc) · 5.74 KB
/
a.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
/*
#cgo pkg-config: python2
#define Py_LIMITED_API
#include <Python.h>
static inline int pyParseTuple1(PyObject *a, PyObject **o) {return PyArg_ParseTuple(a, "O", o);}
static inline int pyParseTuple2(PyObject *a, PyObject **o1, PyObject **o2) {return PyArg_ParseTuple(a, "OO", o1, o2);}
static inline int pyParseTuple2Opt(PyObject *a, PyObject **o1, PyObject **o2) {return PyArg_ParseTuple(a, "O|O", o1, o2);}
static inline PyObject* pyInitModule(const char *n, PyMethodDef *m) { return Py_InitModule(n, m); }
static inline void pyDecref(PyObject *o) { Py_DECREF(o); }
// go protos
extern PyObject* pySum(PyObject*, PyObject*);
extern PyObject* pyNumTimesString(PyObject*, PyObject*);
extern PyObject* pyClone(PyObject*, PyObject*);
extern PyObject* pyMD5(PyObject*, PyObject*);
extern PyObject* pyFibonacci(PyObject*, PyObject*);
extern PyObject* pyCatchExplode(PyObject*, PyObject*);
extern PyObject* pyNoCatchExplode(PyObject*, PyObject*);
extern PyObject* pyCreateObject(PyObject*, PyObject*);
extern PyObject* pyCallback(PyObject*, PyObject*);
extern PyObject* pyListOfClones(PyObject*, PyObject*);
*/
import "C"
import "fmt"
var testObj *C.PyObject
var pyMethods = []C.PyMethodDef{
{C.CString("sum"), C.PyCFunction(C.pySum), C.METH_VARARGS, nil},
{C.CString("num_times_string"), C.PyCFunction(C.pyNumTimesString), C.METH_VARARGS, nil},
{C.CString("sum"), C.PyCFunction(C.pySum), C.METH_VARARGS, nil},
{C.CString("clone"), C.PyCFunction(C.pyClone), C.METH_VARARGS, nil},
{C.CString("md5"), C.PyCFunction(C.pyMD5), C.METH_VARARGS, nil},
{C.CString("fibonacci"), C.PyCFunction(C.pyFibonacci), C.METH_VARARGS, nil},
{C.CString("catch_explode"), C.PyCFunction(C.pyCatchExplode), C.METH_VARARGS, nil},
{C.CString("no_catch_explode"), C.PyCFunction(C.pyNoCatchExplode), C.METH_VARARGS, nil},
{C.CString("create_object"), C.PyCFunction(C.pyCreateObject), C.METH_VARARGS, nil},
{C.CString("callback"), C.PyCFunction(C.pyCallback), C.METH_VARARGS, nil},
{C.CString("list_of_clones"), C.PyCFunction(C.pyListOfClones), C.METH_VARARGS, nil},
{nil, nil, 0, nil},
}
func pyCall1(f func(a *C.PyObject) *C.PyObject, args *C.PyObject) *C.PyObject {
var a *C.PyObject
if C.pyParseTuple1(args, &a) == 0 {
return nil
}
if a == nil {
fmt.Println("pyCall1: a = nil")
return nil
}
return f(a)
}
func pyCall2(f func(a, b *C.PyObject) *C.PyObject, args *C.PyObject) *C.PyObject {
var a, b *C.PyObject
if C.pyParseTuple2(args, &a, &b) == 0 {
return nil
}
return f(a, b)
}
func pyCall2Opt(f func(a, b *C.PyObject) *C.PyObject, args *C.PyObject) *C.PyObject {
var a, b *C.PyObject
if C.pyParseTuple2Opt(args, &a, &b) == 0 {
return nil
}
return f(a, b)
}
func pyCallInstanceMethod(o *C.PyObject, s string) *C.PyObject {
if o == nil {
fmt.Println("pyCallInstanceMethod: o == nil")
return nil
}
m := C.PyObject_GetAttrString(o, C.CString(s))
if m == nil {
return nil
}
//C.pyDecref(o);
a := C.PyTuple_New(0)
r := C.PyObject_CallObject(m, a)
C.pyDecref(m)
C.pyDecref(a)
return r
}
func sum(a, b *C.PyObject) *C.PyObject {
al := C.PyInt_AsLong(a)
bl := C.PyInt_AsLong(b)
return C.PyInt_FromLong(al + bl)
}
//export pySum
func pySum(self, args *C.PyObject) *C.PyObject {
return pyCall2(sum, args)
}
func numTimesString(o *C.PyObject) *C.PyObject {
return C.PyObject_GetAttrString(o, C.CString("num_times_string"))
}
//export pyNumTimesString
func pyNumTimesString(self, args *C.PyObject) *C.PyObject {
return pyCall1(numTimesString, args)
}
//export pyClone
func pyClone(self, args *C.PyObject) *C.PyObject {
return pyCall2Opt(func(a, b *C.PyObject) *C.PyObject {
return pyCallInstanceMethod(a, "clone")
}, args)
}
//export pyMD5
func pyMD5(self, args *C.PyObject) *C.PyObject {
return pyCall1(func(o *C.PyObject) *C.PyObject {
return pyCallInstanceMethod(o, "md5")
}, args)
}
//export pyFibonacci
func pyFibonacci(self, args *C.PyObject) *C.PyObject {
return pyCall1(func(o *C.PyObject) *C.PyObject {
return pyCallInstanceMethod(o, "fibonacci")
}, args)
}
//export pyNoCatchExplode
func pyNoCatchExplode(self, args *C.PyObject) *C.PyObject {
return pyCall1(func(o *C.PyObject) *C.PyObject {
return pyCallInstanceMethod(o, "explode")
}, args)
}
func catchExplode(o *C.PyObject) *C.PyObject {
r := pyCallInstanceMethod(o, "explode")
if r == nil {
return C.PyInt_FromLong(C.long(1))
}
return r
}
//export pyCatchExplode
func pyCatchExplode(self, args *C.PyObject) *C.PyObject {
return pyCall1(catchExplode, args)
}
func createObject(n, s *C.PyObject) *C.PyObject {
t := C.PyTuple_New(2)
C.PyTuple_SetItem(t, 0, n)
C.PyTuple_SetItem(t, 1, s)
r := C.PyObject_CallObject(testObj, t)
C.pyDecref(t)
return r
}
//export pyCreateObject
func pyCreateObject(self, args *C.PyObject) *C.PyObject {
return pyCall2(createObject, args)
}
func callback(a, b *C.PyObject) *C.PyObject {
t := C.PyTuple_New(1)
if t == nil {
return nil
}
C.PyTuple_SetItem(t, 0, b)
r := C.PyObject_CallObject(a, t)
C.pyDecref(t)
return r
}
//export pyCallback
func pyCallback(self, args *C.PyObject) *C.PyObject {
return pyCall2(callback, args)
}
func listOfClones(a, b *C.PyObject) *C.PyObject {
n := C.PyInt_AsLong(b)
l := C.PyList_New(n)
if l == nil {
return nil
}
for i := 0; i < int(n); i++ {
c := pyCallInstanceMethod(a, "clone")
if c == nil {
return nil
}
if C.PyList_SetItem(l, C.long(i), c) != 0 {
return nil
}
}
return l
}
//export pyListOfClones
func pyListOfClones(self, args *C.PyObject) *C.PyObject {
return pyCall2(listOfClones, args)
}
//export inita
func inita() {
m := C.pyInitModule(C.CString("a"), &pyMethods[0])
if m == nil {
return
}
// import object
e := C.PyImport_ImportModule(C.CString("entities"))
if e == nil {
return
}
c := C.PyObject_GetAttrString(e, C.CString("TestObj"))
if c == nil {
return
}
testObj = c
}
func main() {}