sbinet / go-python

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

passing in NULL to go-python function? #61

Closed reiinakano closed 6 years ago

reiinakano commented 6 years ago

I'm trying to use PyList_SetSlice and according to the documentation, we can set itemlist to NULL. How would I do this? I tried C.NULL, nil, and even 0 but nothing seems to work.

sbinet commented 6 years ago

hi @reiinakano

could you please give a small compilable example of what you've tried?

reiinakano commented 6 years ago

Only this works

// origList is my original list (*python.PyObject) from some other function
// empty original list
emptyList := python.PyList_New(0)
err := python.PyList_SetSlice(origList, 0, length, emptyList)

These others don't

// origList is my original list (*python.PyObject) from some other function
// empty original list
err := python.PyList_SetSlice(origList, 0, length, nil)
// origList is my original list (*python.PyObject) from some other function
// empty original list
err := python.PyList_SetSlice(origList, 0, length, 0)
import "C"
// origList is my original list (*python.PyObject) from some other function
// empty original list
err := python.PyList_SetSlice(origList, 0, length, C.NULL)
reiinakano commented 6 years ago

Sorry, you asked for compilable. Here

package main

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

import "C"

func main() {
    python.Initialize()

    origList := python.PyList_New(0)
    python.PyList_Append(origList, python.PyString_FromString("i want this gone"))
    fmt.Println(python.PyString_AsString(origList.Str()))

    emptyList := python.PyList_New(0)
    python.PyList_SetSlice(origList, 0, 1, emptyList)
    fmt.Println(python.PyString_AsString(origList.Str()))

    origList = python.PyList_New(0)
    python.PyList_Append(origList, python.PyString_FromString("i want this gone"))
    fmt.Println(python.PyString_AsString(origList.Str()))

// None of the below work

    python.PyList_SetSlice(origList, 0, 1, nil)
    fmt.Println(python.PyString_AsString(origList.Str()))

       python.PyList_SetSlice(origList, 0, 1,  python.PyObject_FromVoidPtr(unsafe.Pointer(C.NULL)))
       fmt.Println(python.PyString_AsString(origList.Str()))

       python.PyList_SetSlice(origList, 0, 1,  python.PyObject_FromVoidPtr(nil))
       fmt.Println(python.PyString_AsString(origList.Str()))

       zero := 0
       python.PyList_SetSlice(origList, 0, 1,  python.PyObject_FromVoidPtr(
unsafe.Pointer(&zero)))
       fmt.Println(python.PyString_AsString(origList.Str()))

How do I pass in NULL?

Passing C.NULL does not compile.

sbinet commented 6 years ago

it's fixed with 2665c25.

thanks for your patience.

reiinakano commented 6 years ago

Thank you!