Type checking inside a C extension... doesn't throw exception on failure

Stephen Horne steve at ninereeds.fsnet.co.uk
Wed Apr 7 17:35:05 EDT 2004


On Wed, 07 Apr 2004 11:50:05 +0800, Jon Perez <jbperez808 at yahoo.com>
wrote:

>Andrew MacIntyre wrote:
>
>> First rule of using the Python C API: _always_ check the return value of
>> API functions.
>
>This only applies to API functions which return PyObject*, right?

It isn't that simple. There are API functions returning an int, IIRC,
where that int can indicate an error condition.

However, if you can ensure that an error cannot exist OR if you can
arrange things such that the error is naturally handled, THEN you
don't need an explicit check.

For instance, sometimes you can just return the result of an API
function. If the API function returns a valid result, so does your
function. If the API function returns NULL, your function does so too
so the exception is propogated. If you don't need any further
processing or cleanup, why make a fuss?

"_always_ check the return value" is a very good principle, however,
even if not quite literally correct.


-- 
Steve Horne

steve at ninereeds dot fsnet dot co dot uk



More information about the Python-list mailing list