I have some functions where I would like to return NULL on failure, and on success they do not need to return anything. I have been using void * function_name(...), but it still requires me to return something.
The reason I am doing this is to detect failure as part of a python C extension and they recommend that the innermost function set any exception, return NULL and then do that up through the stack.
What is the best practice in this situation? Return 0, -1 or something similar?
In pure C terms, the easiest is to return a Boolean flag indicating success/failure.
As far as integration with Python is concerned, another approach is:
PyObject* func() {
...
if (error) {
/* set the exception */
PyErr_Format(PyExc_..., ...); /* TODO: fill in as appropriate */
return NULL;
}
/* return None */
Py_RETURN_NONE;
}
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