sbinet / go-python

naive go bindings to the CPython2 C-API
Other
1.53k stars 139 forks source link

Call PyInstance_New Failed #82

Closed longerlsx closed 5 years ago

longerlsx commented 5 years ago

I was trying to get a new instance from a python class Test. However, it returns nil when I called PyInstance_New() function. Here is my testing code.

// hello.py
class Test(object):
    def __init__(self, name):
        print(name)
    def hello(self, line):
        return line

// test.go
func init() {
    err := python.Initialize()
    if err != nil {
        panic(err.Error())
    }
}

func main() {
    hello := python.PyImport_ImportModule("hello")
    if hello == nil {
        log.Fatalf("could not import 'hello'\n")
    }

    testGetter := hello.GetAttrString("Test")
    if testGetter == nil {
        log.Fatalf("could not retrieve 'hello.Test'")
    }

    gostr := "here is a test"
    pystr := python.PyString_FromString(gostr)
    test := python.PyInstance_New(testGetter, pystr, nil)
    if test == nil {
                 // log fail at here
        log.Fatalf("could not retrieve 'test'")
    }
}

Is the way I called function is wrong? How could I get a new instance from python? Thanks.

dfelici87 commented 3 years ago

Hi, how did you resolve this issue? thanks.