Skip to content

Commit 627e06c

Browse files
Fixed a few code generation error for methods.
1 parent 684f3de commit 627e06c

File tree

1 file changed

+63
-54
lines changed

1 file changed

+63
-54
lines changed

bind/gen_func.go

Lines changed: 63 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,67 @@ func isPointer(pyfmt string) bool {
251251
return false
252252
}
253253

254+
func (g *pyGen)generateReturn(buildPyTuple bool, npyres int, results []*Var, retvals *[]string) {
255+
g.gofile.Printf("\n")
256+
valueCalls := make([]string, npyres, npyres)
257+
for i := 0; i < npyres; i++ {
258+
result := results[i]
259+
sret := current.symtype(result.GoType())
260+
if sret == nil {
261+
panic(fmt.Errorf(
262+
"gopy: could not find symbol for %q",
263+
result.Name(),
264+
))
265+
}
266+
formatStr := sret.pyfmt
267+
if sret.pyfmt == "" {
268+
formatStr = "?"
269+
}
270+
271+
retval := ""
272+
if retvals != nil {
273+
retval = (*retvals)[i]
274+
} else if result.sym.zval != "" {
275+
retval = result.sym.zval
276+
} else {
277+
fmt.Printf("gopy: programmer error: empty zval zero value in symbol: %v\n", result.sym)
278+
}
279+
280+
if result.sym.go2py != "" {
281+
retval = result.sym.go2py + "(" + retval + ")" + result.sym.go2pyParenEx
282+
}
283+
284+
if (buildPyTuple) {
285+
buildValueFunc := "C.Py_BuildValue1"
286+
typeCast := "unsafe.Pointer"
287+
if !isPointer(formatStr) {
288+
buildValueFunc = "C.Py_BuildValue2"
289+
typeCast = "C.longlong"
290+
formatStr = "L"
291+
}
292+
valueCalls[i] = fmt.Sprintf("%s(C.CString(\"%s\"), %s(%s))",
293+
buildValueFunc,
294+
formatStr,
295+
typeCast,
296+
retval)
297+
} else {
298+
valueCalls[i] = retval
299+
}
300+
}
301+
302+
if npyres == 0 {
303+
g.gofile.Printf("return\n")
304+
} else if (buildPyTuple) {
305+
g.gofile.Printf("retTuple := C.PyTuple_New(%d);\n", npyres)
306+
for i := 0; i < npyres; i++ {
307+
g.gofile.Printf("C.PyTuple_SetItem(retTuple, %d, %s);\n", i, valueCalls[i])
308+
}
309+
g.gofile.Printf("return unsafe.Pointer(retTuple);")
310+
} else {
311+
g.gofile.Printf("return %s\n", strings.Join(valueCalls, ", "))
312+
}
313+
}
314+
254315
func (g *pyGen) genFuncBody(sym *symbol, fsym *Func) {
255316
isMethod := (sym != nil)
256317
isIface := false
@@ -303,23 +364,7 @@ func (g *pyGen) genFuncBody(sym *symbol, fsym *Func) {
303364
if __err != nil {
304365
`, symNm)
305366
g.gofile.Indent()
306-
if npyres > 0 {
307-
retvals := make([]string, npyres, npyres)
308-
for i := 0; i < npyres; i++ {
309-
ret := res[i]
310-
if ret.sym.zval == "" {
311-
fmt.Printf("gopy: programmer error: empty zval zero value in symbol: %v\n", ret.sym)
312-
}
313-
if ret.sym.go2py != "" {
314-
retvals[i] = ret.sym.go2py + "(" + ret.sym.zval + ")" + ret.sym.go2pyParenEx
315-
} else {
316-
retvals[i] = ret.sym.zval
317-
}
318-
}
319-
g.gofile.Printf("return %s\n", strings.Join(retvals, ", "))
320-
} else {
321-
g.gofile.Printf("return\n")
322-
}
367+
g.generateReturn(buildPyTuple(fsym), npyres, res, nil)
323368
g.gofile.Outdent()
324369
g.gofile.Printf("}\n")
325370
} else if rvHasErr {
@@ -459,9 +504,6 @@ if __err != nil {
459504
if res[i].sym.hasHandle() && !res[i].sym.isPtrOrIface(){
460505
retvals[i] = "&" + retvals[i]
461506
}
462-
if res[i].sym.go2py != "" {
463-
retvals[i] = fmt.Sprintf("%s(%s)%s", res[i].sym.go2py, retvals[i], res[i].sym.go2pyParenEx)
464-
}
465507
}
466508

467509
if fsym.haserr {
@@ -490,40 +532,7 @@ if __err != nil {
490532
}
491533
}
492534

493-
if buildPyTuple(fsym) {
494-
g.gofile.Printf("\n")
495-
g.gofile.Printf("retTuple := C.PyTuple_New(%d);\n", npyres)
496-
for i := 0; i < npyres; i++ {
497-
sret := current.symtype(res[i].GoType())
498-
if sret == nil {
499-
panic(fmt.Errorf(
500-
"gopy: could not find symbol for %q",
501-
res[i].Name(),
502-
))
503-
}
504-
formatStr := sret.pyfmt
505-
if sret.pyfmt == "" {
506-
formatStr = "?"
507-
}
508-
509-
buildValueFunc := "C.Py_BuildValue1"
510-
typeCast := "unsafe.Pointer"
511-
if !isPointer(formatStr) {
512-
buildValueFunc = "C.Py_BuildValue2"
513-
typeCast = "C.longlong"
514-
formatStr = "L"
515-
}
516-
valueCall := fmt.Sprintf("%s(C.CString(\"%s\"), %s(%s))",
517-
buildValueFunc,
518-
formatStr,
519-
typeCast,
520-
retvals[i])
521-
g.gofile.Printf("C.PyTuple_SetItem(retTuple, %d, %s);\n", i, valueCall)
522-
}
523-
g.gofile.Printf("return unsafe.Pointer(retTuple);")
524-
} else {
525-
g.gofile.Printf("return %s\n", strings.Join(retvals[0:npyres], ", "))
526-
}
535+
g.generateReturn(buildPyTuple(fsym), npyres, res, &retvals);
527536
} else {
528537
g.gofile.Printf("if boolPyToGo(goRun) {\n")
529538
g.gofile.Indent()

0 commit comments

Comments
 (0)