[issue8113] PyUnicode_AsUnicode doesn't check for NULL pointer

STINNER Victor report at bugs.python.org
Thu Mar 11 02:09:35 CET 2010


STINNER Victor <victor.stinner at haypocalc.com> added the comment:

> I just looked at the code for PyFloat_AsDouble and it checks for NULL pointers

It's just a little help for developers: it will raise the ugly TypeError("bad argument type for built-in operation") and returns -1.

It's never a good idea to pass NULL to a function of the Python C API. I think that most (or all?) functions will fail.

> IMHO, the C-API documentation lacks information on how it behaves 
> in error conditions (this might be a bit over generalized).

I didn't read the documentation, but you HAVE TO always check the result of a Python function.

If the function returns a PyObject*, you have to check that the result is not NULL. The same rule applies for Pyxxx_AsXxx(): you also have to check the result (the error value depends on the function). Eg. PyInt_AsLong() returns -1 on error: use (result == -1) && PyErr_Occured()) in the caller to detect an error.

----------
nosy: +haypo

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8113>
_______________________________________


More information about the Python-bugs-list mailing list