Create a new None object in embedded python

I am writing a simple C wrapper for a Python module. I need to create a new PyObject* that represents None . I can’t figure out how to do this. Is there a simple function that will return PyObject* pointing to None , similar to how PyTuple_New returns 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 shown below, C NULL will be executed? 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); 
+4
source share
1 answer

According to the documentation, None is singleton and is accessible from C code as Py_None .

PyObject* Py_None A Python None object indicating no value. This object has no methods. It must be treated in the same way as any other object regarding reference counts.

Py_RETURN_NONE Properly handle returning Py_None from within the C function.

+6
source

All Articles