sbinet / go-python

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

Issues about calling A python's method which return a wrong result #67

Closed hesilong closed 6 years ago

hesilong commented 6 years ago

hello: Recently,I came across an issue. when I try to call a python's method which was supposed to return a list whose elements were also list,like this [[1.23, 4.56]], but it return []. My code shows below: Golang:

var TensorCall *python.PyObject
func test() {
    sysModule := python.PyImport_ImportModule("sys")
    path := sysModule.GetAttrString("path")
    python.PyList_Insert(path, 0, PyStr(dir)) // python file's location
    m := python.PyImport_ImportModule(name)   //python file's name
    TensorCall = m.GetAttrString("tensorgo")  // method name in python file
    bArgs := python.PyTuple_New(2)
    python.PyTuple_SetItem(bArgs, 0, PyStr("arg1")) // arg, you can ignore
    python.PyTuple_SetItem(bArgs, 1, PyStr("arg2")) // arg, you can ignore
    res := TensorCall.Call(bArgs, python.Py_None)
    size := python.PyList_Size(res) - 1
    fmt.Println(python.PyList_Check(res))
    fmt.Println("size:", size)
}

python:

def tensorgo(s,sgf):
    return [[1.23, 4.56]]

Run the Golang Server, and call the func test(), output is:

true
size:0

I'm not sure whether my procedure is wrong or not. If you have any idea, please tell me. Thanks!

sbinet commented 6 years ago

by my book, it's all working as expected :)

what the tensorgo function is returning is a 1-element list (that element happens to be a 2-element list...) you subtract 1 to that length, so, 0 is the expected answer.

Feel free to re-open if something isn't quite clear.

PS: I took the liberty to slightly edit your original post for better code formating

hesilong commented 6 years ago

Thank you very much, I'll try again!