sbinet/go-python

passing in NULL to go-python function?

reiinakano opened this issue · 5 comments

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.

hi @reiinakano

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

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)

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.

it's fixed with 2665c25.

thanks for your patience.

Thank you!