I'm wrapping a Go library for Python. I need to be able to return None, but it's not finding it at compile time:
/*
#cgo pkg-config: python3
#define Py_LIMITED_API
#include <Python.h>
*/
import "C"
//export Nothing
func Nothing(self, args *C.PyObject) (status *C.PyObject) {
C.Py_IncRef(C.Py_None)
return C.Py_None
}
Here's the output of go build
go build -buildmode=c-shared -o mymodule.so
# example.com/mywrapper
/tmp/go-build293667616/example.com/mywrapper/_obj/_cgo_main.o:(.data.rel+0x0): undefined reference to `Py_None'
collect2: error: ld returned 1 exit status
I'm not understanding how it can be finding all of the other Py* functions and types (PyArgs_ParseTuple
and PyLong_FromLong
work just fine), but can't find Py_None
. The Python library is obviously being loaded. What's going on here?
Thanks to a comment from Ismail Badawi, the answer is to write a function in C that returns None. This is required because Py_None
is a macro, which Go can't see.
none.c
#define Py_LIMITED_API
#include <Python.h>
PyObject *IncrNone() {
Py_RETURN_NONE;
}
mymodule.go
/*
#cgo pkg-config: python3
#define Py_LIMITED_API
#include <Python.h>
PyObject *IncrNone();
*/
import "C"
//export Nothing
func Nothing(self, args *C.PyObject) (status *C.PyObject) {
return C.IncrNone()
}