sbinet/go-python

panic when register callback from go into python as module

xiyanxiyan10 opened this issue · 1 comments

code

package pythonbind

import "C"
import (
	"fmt"
	"github.com/sbinet/go-python"
	"time"
)

type Go2Python struct {
	fileName string
}

func Hello(self, args *python.PyObject) *python.PyObject {
	fmt.Printf("Hello world")
	return python.Py_None
}

// Run run the scripts
func (g Go2Python) Run() error {
	if err := python.Initialize(); err != nil {
		return err
	}
	methods := make([]python.PyMethodDef,1)
	method := &methods[0]
	method.Name = "hello"
	method.Meth = Hello
	method.Doc = "hello world"
	method.Flags = python.MethNoArgs

	_, err := python.Py_InitModule("snack", methods)
	if err != nil {
		panic(err)
	}
	if err := python.PyRun_SimpleFile(g.fileName); err != nil {
		return err
	}
	if err := python.Finalize(); err != nil {
		return err
	}
	time.Sleep(time.Minute * 5)
	return nil
}

when I run function run, panic happen in Py_InitModule

API server listening at: 127.0.0.1:60703
=== RUN   TestSum
--- FAIL: TestSum (7.60s)
panic: runtime error: cgo argument has Go pointer to Go pointer [recovered]
	panic: runtime error: cgo argument has Go pointer to Go pointer

goroutine 19 [running]:
testing.tRunner.func1(0xc0000e0100)
	/Users/xiyanxiyan10/go1.13/src/testing/testing.go:874 +0x60f
panic(0x4181560, 0xc0000984e0)
	/Users/xiyanxiyan10/go1.13/src/runtime/panic.go:679 +0x1e0
github.com/sbinet/go-python.cpyMethodDefs.func1(0x5003380, 0xc000049b78, 0xc0000ba080)
	/Users/xiyanxiyan10/project/goquant/back/pkg/mod/github.com/sbinet/go-python@v0.0.0-20190615090516-46d882be3991/heap.go:31 +0x12c
github.com/sbinet/go-python.cpyMethodDefs(0x41b0201, 0x5, 0xc000049e38, 0x1, 0x1, 0x0)
	/Users/xiyanxiyan10/project/goquant/back/pkg/mod/github.com/sbinet/go-python@v0.0.0-20190615090516-46d882be3991/heap.go:31 +0x2eb
github.com/sbinet/go-python.Py_InitModule(0x41b0201, 0x5, 0xc000049e38, 0x1, 0x1, 0x0, 0x0, 0x0)
	/Users/xiyanxiyan10/project/goquant/back/pkg/mod/github.com/sbinet/go-python@v0.0.0-20190615090516-46d882be3991/heap.go:42 +

go version 1.13

I am afraid this can't be done correctly right now.
(and I don't actually think that code ever worked either)

I'll remove the part of the API that suggested one could directly wrap a Go function as a C.PyCFuntion.