This repository was archived by the owner on Oct 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_func.go
More file actions
454 lines (405 loc) · 11 KB
/
gen_func.go
File metadata and controls
454 lines (405 loc) · 11 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// Copyright 2019 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bind
import (
"fmt"
"go/types"
"strings"
)
func (g *pyGen) recurse(gotype types.Type, prefix, name string) {
switch t := gotype.(type) {
case *types.Basic:
// pass
case *types.Map:
g.recurse(t.Elem(), prefix, "Map_"+t.Key().String()+"_string_elem")
case *types.Named:
o := t.Obj()
if o == nil || o.Pkg() == nil {
return
}
g.recurse(t.Underlying(), prefix, o.Pkg().Name()+"_"+o.Name())
case *types.Struct:
for i := 0; i < t.NumFields(); i++ {
f := t.Field(i)
g.recurse(f.Type(), prefix, name+"_"+f.Name()+"_Get")
}
case *types.Slice:
g.recurse(t.Elem(), prefix, "Slice_string_elem")
}
}
// genFuncSig generates just the signature for binding
// returns false if function is not suitable for python
// binding (e.g., multiple return values)
func (g *pyGen) genFuncSig(sym *symbol, fsym *Func) bool {
isMethod := (sym != nil)
if fsym.sig == nil {
return false
}
gname := fsym.GoName()
if g.cfg.RenameCase {
gname = toSnakeCase(gname)
}
gname, gdoc, err := extractPythonName(gname, fsym.Doc())
if err != nil {
return false
}
ifchandle, gdoc := isIfaceHandle(gdoc)
sig := fsym.sig
args := sig.Params()
res := sig.Results()
nargs := 0
nres := len(res)
// note: this is enforced in creation of Func, in newFuncFrom
if nres > 2 {
return false
}
if nres == 2 && !fsym.err {
return false
}
var (
goArgs []string
pyArgs []string
wpArgs []string
)
if isMethod {
goArgs = append(goArgs, "_handle CGoHandle")
pyArgs = append(pyArgs, fmt.Sprintf("param('%s', '_handle')", PyHandle))
wpArgs = append(wpArgs, "self")
}
nargs = len(args)
for i := 0; i < nargs; i++ {
arg := args[i]
sarg := current.symtype(arg.GoType())
if sarg == nil {
return false
}
anm := pySafeArg(arg.Name(), i)
if ifchandle && arg.sym.goname == "interface{}" {
goArgs = append(goArgs, fmt.Sprintf("%s %s", anm, CGoHandle))
pyArgs = append(pyArgs, fmt.Sprintf("param('%s', '%s')", PyHandle, anm))
} else {
goArgs = append(goArgs, fmt.Sprintf("%s %s", anm, sarg.cgoname))
if sarg.cpyname == "PyObject*" {
pyArgs = append(pyArgs, fmt.Sprintf("param('%s', '%s', transfer_ownership=False)", sarg.cpyname, anm))
} else {
pyArgs = append(pyArgs, fmt.Sprintf("param('%s', '%s')", sarg.cpyname, anm))
}
}
if i!=nargs-1 || !fsym.isVariadic {
wpArgs = append(wpArgs, anm)
}
}
// support for optional arg to run in a separate go routine -- only if no return val
if nres == 0 {
goArgs = append(goArgs, "goRun C.char")
pyArgs = append(pyArgs, "param('bool', 'goRun')")
wpArgs = append(wpArgs, "goRun=False")
}
// To support variadic args, we add *args at the end.
if fsym.isVariadic {
wpArgs = append(wpArgs, "*args")
}
// When building the pybindgen builder code, we start with
// a function that adds function calls with exception checking.
// But given specific return types, we may want to add more
// behavior to the wrapped function code gen.
addFuncName := "add_checked_function"
if len(res) > 0 {
ret := res[0]
switch t := ret.GoType().(type) {
case *types.Basic:
// string return types need special memory leak patches
// to free the allocated char*
if t.Kind() == types.String {
addFuncName = "add_checked_string_function"
}
}
}
switch {
case isMethod:
mnm := sym.id + "_" + fsym.GoName()
g.gofile.Printf("\n//export %s\n", mnm)
g.gofile.Printf("func %s(", mnm)
g.pybuild.Printf("%s(mod, '%s', ", addFuncName, mnm)
g.pywrap.Printf("def %s(", gname)
default:
g.gofile.Printf("\n//export %s\n", fsym.ID())
g.gofile.Printf("func %s(", fsym.ID())
g.pybuild.Printf("%s(mod, '%s', ", addFuncName, fsym.ID())
g.pywrap.Printf("def %s(", gname)
}
goRet := ""
nres = len(res)
if nres > 0 {
ret := res[0]
sret := current.symtype(ret.GoType())
if sret == nil {
panic(fmt.Errorf(
"gopy: could not find symbol for %q",
ret.Name(),
))
}
if sret.cpyname == "PyObject*" {
g.pybuild.Printf("retval('%s', caller_owns_return=True)", sret.cpyname)
} else {
g.pybuild.Printf("retval('%s')", sret.cpyname)
}
goRet = fmt.Sprintf("%s", sret.cgoname)
} else {
g.pybuild.Printf("None")
}
if len(goArgs) > 0 {
gstr := strings.Join(goArgs, ", ")
g.gofile.Printf("%v) %v", gstr, goRet)
pstr := strings.Join(pyArgs, ", ")
g.pybuild.Printf(", [%v])\n", pstr)
wstr := strings.Join(wpArgs, ", ")
g.pywrap.Printf("%v)", wstr)
} else {
g.gofile.Printf(") %v", goRet)
g.pybuild.Printf(", [])\n")
g.pywrap.Printf(")")
}
return true
}
func (g *pyGen) genFunc(o *Func) {
if g.genFuncSig(nil, o) {
g.genFuncBody(nil, o)
}
}
func (g *pyGen) genMethod(s *symbol, o *Func) {
if g.genFuncSig(s, o) {
g.genFuncBody(s, o)
}
}
func isIfaceHandle(gdoc string) (bool, string) {
const PythonIface = "gopy:interface=handle"
if idx := strings.Index(gdoc, PythonIface); idx >= 0 {
gdoc = gdoc[:idx] + gdoc[idx+len(PythonIface)+1:]
return true, gdoc
}
return false, gdoc
}
func (g *pyGen) genFuncBody(sym *symbol, fsym *Func) {
isMethod := (sym != nil)
isIface := false
symNm := ""
if isMethod {
symNm = sym.goname
isIface = sym.isInterface()
if !isIface {
symNm = "*" + symNm
}
}
pkgname := g.cfg.Name
_, gdoc, _ := extractPythonName(fsym.GoName(), fsym.Doc())
ifchandle, gdoc := isIfaceHandle(gdoc)
sig := fsym.Signature()
res := sig.Results()
args := sig.Params()
nres := len(res)
rvIsErr := false // set to true if the main return is an error
if nres == 1 {
ret := res[0]
if isErrorType(ret.GoType()) {
rvIsErr = true
}
}
g.pywrap.Printf(":\n")
g.pywrap.Indent()
g.pywrap.Printf(`"""%s"""`, gdoc)
g.pywrap.Printf("\n")
g.gofile.Printf(" {\n")
g.gofile.Indent()
if fsym.hasfun {
for i, arg := range args {
if arg.sym.isSignature() {
g.gofile.Printf("_fun_arg := %s\n", pySafeArg(arg.Name(), i))
}
}
}
if isMethod {
g.gofile.Printf(
`vifc, __err := gopyh.VarFromHandleTry((gopyh.CGoHandle)(_handle), "%s")
if __err != nil {
`, symNm)
g.gofile.Indent()
if nres > 0 {
ret := res[0]
if ret.sym.zval == "" {
fmt.Printf("gopy: programmer error: empty zval zero value in symbol: %v\n", ret.sym)
}
if ret.sym.go2py != "" {
g.gofile.Printf("return %s(%s)%s\n", ret.sym.go2py, ret.sym.zval, ret.sym.go2pyParenEx)
} else {
g.gofile.Printf("return %s\n", ret.sym.zval)
}
} else {
g.gofile.Printf("return\n")
}
g.gofile.Outdent()
g.gofile.Printf("}\n")
} else if rvIsErr {
g.gofile.Printf("var __err error\n")
}
callArgs := []string{}
wrapArgs := []string{}
if isMethod {
wrapArgs = append(wrapArgs, "self.handle")
}
for i, arg := range args {
na := ""
anm := pySafeArg(arg.Name(), i)
switch {
case ifchandle && arg.sym.goname == "interface{}":
na = fmt.Sprintf(`gopyh.VarFromHandle((gopyh.CGoHandle)(%s), "interface{}")`, anm)
case arg.sym.isSignature():
na = fmt.Sprintf("%s", arg.sym.py2go)
case arg.sym.py2go != "":
na = fmt.Sprintf("%s(%s)%s", arg.sym.py2go, anm, arg.sym.py2goParenEx)
default:
na = anm
}
if i == len(args) - 1 && fsym.isVariadic {
na = na + "..."
}
callArgs = append(callArgs, na)
switch {
case arg.sym.goname == "interface{}":
if ifchandle {
wrapArgs = append(wrapArgs, fmt.Sprintf("%s.handle", anm))
} else {
wrapArgs = append(wrapArgs, anm)
}
case arg.sym.hasHandle():
wrapArgs = append(wrapArgs, fmt.Sprintf("%s.handle", anm))
default:
wrapArgs = append(wrapArgs, anm)
}
// To support variadic args, we add *args at the end.
if fsym.isVariadic && i == len(args)-1 {
packagePrefix := ""
if arg.sym.gopkg.Name() != fsym.pkg.Name() {
packagePrefix = arg.sym.gopkg.Name() + "."
}
g.pywrap.Printf("%s = %s%s(args)\n", anm, packagePrefix, arg.sym.id)
}
}
// pywrap output
mnm := fsym.ID()
if isMethod {
mnm = sym.id + "_" + fsym.GoName()
}
rvHasHandle := false
if nres > 0 {
ret := res[0]
if !rvIsErr && ret.sym.hasHandle() {
rvHasHandle = true
cvnm := ret.sym.pyPkgId(g.pkg.pkg)
g.pywrap.Printf("return %s(handle=_%s.%s(", cvnm, pkgname, mnm)
} else {
g.pywrap.Printf("return _%s.%s(", pkgname, mnm)
}
} else {
g.pywrap.Printf("_%s.%s(", pkgname, mnm)
}
hasRetCvt := false
hasAddrOfTmp := false
if nres > 0 {
ret := res[0]
switch {
case rvIsErr:
g.gofile.Printf("__err = ")
case nres == 2:
g.gofile.Printf("cret, __err := ")
case ret.sym.hasHandle() && !ret.sym.isPtrOrIface():
hasAddrOfTmp = true
g.gofile.Printf("cret := ")
case ret.sym.go2py != "":
hasRetCvt = true
g.gofile.Printf("return %s(", ret.sym.go2py)
default:
g.gofile.Printf("return ")
}
}
if nres == 0 {
wrapArgs = append(wrapArgs, "goRun")
}
g.pywrap.Printf("%s)", strings.Join(wrapArgs, ", "))
if rvHasHandle {
g.pywrap.Printf(")")
}
funCall := ""
if isMethod {
if sym.isStruct() {
funCall = fmt.Sprintf("gopyh.Embed(vifc, reflect.TypeOf(%s{})).(%s).%s(%s)", nonPtrName(symNm), symNm, fsym.GoName(), strings.Join(callArgs, ", "))
} else {
funCall = fmt.Sprintf("vifc.(%s).%s(%s)", symNm, fsym.GoName(), strings.Join(callArgs, ", "))
}
} else {
funCall = fmt.Sprintf("%s(%s)", fsym.GoFmt(), strings.Join(callArgs, ", "))
}
if hasRetCvt {
ret := res[0]
funCall += fmt.Sprintf(")%s", ret.sym.go2pyParenEx)
}
if nres == 0 {
g.gofile.Printf("if boolPyToGo(goRun) {\n")
g.gofile.Indent()
g.gofile.Printf("go %s\n", funCall)
g.gofile.Outdent()
g.gofile.Printf("} else {\n")
g.gofile.Indent()
g.gofile.Printf("%s\n", funCall)
g.gofile.Outdent()
g.gofile.Printf("}")
} else {
g.gofile.Printf("%s\n", funCall)
}
if rvIsErr || nres == 2 {
g.gofile.Printf("\n")
g.gofile.Printf("if __err != nil {\n")
g.gofile.Indent()
g.gofile.Printf("estr := C.CString(__err.Error())\n")
g.gofile.Printf("C.PyErr_SetString(C.PyExc_RuntimeError, estr)\n")
if rvIsErr {
g.gofile.Printf("return estr\n") // NOTE: leaked string
} else {
g.gofile.Printf("C.free(unsafe.Pointer(estr))\n") // python should have converted, safe
ret := res[0]
if ret.sym.zval == "" {
fmt.Printf("gopy: programmer error: empty zval zero value in symbol: %v\n", ret.sym)
}
if ret.sym.go2py != "" {
g.gofile.Printf("return %s(%s)%s\n", ret.sym.go2py, ret.sym.zval, ret.sym.go2pyParenEx)
} else {
g.gofile.Printf("return %s\n", ret.sym.zval)
}
}
g.gofile.Outdent()
g.gofile.Printf("}\n")
if rvIsErr {
g.gofile.Printf("return C.CString(\"\")") // NOTE: leaked string
} else {
ret := res[0]
if ret.sym.go2py != "" {
if ret.sym.hasHandle() && !ret.sym.isPtrOrIface() {
g.gofile.Printf("return %s(&cret)%s", ret.sym.go2py, ret.sym.go2pyParenEx)
} else {
g.gofile.Printf("return %s(cret)%s", ret.sym.go2py, ret.sym.go2pyParenEx)
}
} else {
g.gofile.Printf("return cret")
}
}
} else if hasAddrOfTmp {
ret := res[0]
g.gofile.Printf("\nreturn %s(&cret)%s", ret.sym.go2py, ret.sym.go2pyParenEx)
}
g.gofile.Printf("\n")
g.gofile.Outdent()
g.gofile.Printf("}\n")
g.pywrap.Printf("\n")
g.pywrap.Outdent()
}