I am writing a simple C wrapper for a Python module. What I need to do is create a new PyObject*
that represents None
. I can't figure out how to do this. Is there a simple function that will return a PyObject*
pointing to None
, similar to how PyTuple_New
returns a PyObject*
pointing to a new tuple or PyString_FromString` returns one pointing to a python string?
Note Is it possible that, when calling a function as below, passing a C NULL
will work? example:
//pFunc points to a function with 2 arguments
PyObject *pArgs = PyTuple_New(2);
PyTuple_SetItem(pArgs, 0, PyString_FromString("hello"));
PyTuple_SetItem(pArgs, 1, NULL); // <--- will this create a python object representing None?
pValue = PyObject_CallObject(pFunc, pArgs);
According to the documentation, None
is a singleton, and is accessible from C code as Py_None
.
PyObject* Py_None
The PythonNone
object, denoting lack of value. This object has no methods. It needs to be treated just like any other object with respect to reference counts.
Py_RETURN_NONE
Properly handle returningPy_None
from within a C function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With