Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create new None object in embedded python

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);
like image 452
ewok Avatar asked Nov 26 '12 17:11

ewok


1 Answers

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

PyObject* Py_None The Python None 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 returning Py_None from within a C function.

like image 118
NPE Avatar answered Nov 05 '22 13:11

NPE