Often a function written in C for Python needs to return nothing in particular -- a "return None" in Python terms; but _don't_ just "return Py_None" from C, as that will mess up reference counts!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | /* suppose we need an empty C-coded function, equivalent to Python:
def empty1(*args):
pass
or identically:
def empty2(*args):
return None
There is still a right and a wrong way to do it...!
*/
/* WRONG! will mess up reference counts...: */
static PyObject*
empty3(PyObject* self, PyObject* args)
{
return Py_None;
}
/* Fine! the simplest way to do it...: */
static PyObject*
empty4(PyObject* self, PyObject* args)
{
return Py_BuildValue("");
}
/* Fine! the canonical approach...: */
static PyObject*
empty5(PyObject* self, PyObject* args)
{
Py_INCREF(Py_None);
return Py_None;
}
|
None, the Python object we must explicitly return from a C-coded function that is Python-callable, IS a Python object, still subject to all normal reference count rules. The returned object must be incref'd by the returning function.
So, a bare "return Py_None;" is a nasty lurking bug; either explicitly incref the None object you're returning, or (simpler, but costs a few machine cycles) delegate the work to handy function Py_BuildValue (the latter can be used for just about all return-value-from-C-to-Python needs, which offers potential advantages of uniformity) by just calling it with an empty formatstring.
why I only use Py_BuildValue("") to return None.
Eh? This comment about Py_BuildValue() doesn't make sense to me.
Many things ordinarily done in extension modules depend on the compiler's alignment rules matching those of the Python executable. Just off the top of my head:
using Py_INCREF and Py_DECREF at all
using any other macros defined in Python.h, such as PyString_AS_STRING()
declaring a static PyTypeObject
If your compiler settings aren't fully link-compatible with the ones used to build Python, then it seems to me that Py_None is the least of your problems.
Ooops. Thanks for this correction. I really made a hash of this posting. For me, replacing Py_None by Py_BuildValue("") got everything working. As you point out, this was really just by accident.
My original thought was that it would be easier to write extensions if macros like Py_INCREF, Py_DECREF, PyTypeObject, etc. were functions. Replacing macros with functions in the C API would reduce (but not eliminate!) the compiler settings that could cause problems at link time. In particular, compiler settings affect function calls as well. I am acutely embarrassed not to have realized this. Thanks again for setting me straight.
Edward