[Tutor] Python Extensions in C

Alan Gauld alan.gauld at btinternet.com
Fri May 27 01:50:39 CEST 2011


"James Reynolds" <eire1130 at gmail.com> wrote


> As far as the null point goes, it shouldn't be null at all once it 
> gets to
> the point Alan pointed out. The pointer is set in (for example) 
> stat_avg
>
>    seq = PySequence_Fast(obj, "Expected a Sequence");

Can the function fail? If so what does it return? That was
my point. Can seq ever still be NULL after the function call?
For example if the function is allocating memory it could fail
to grab enough and then return a NULL....

But it depends on how reliable the Python function is in
its return value...

> But maybe I am missing something that you have seen?

Don't rely on functio  returns being valid values.
It is common practice in industrial strength C to return a NULL
and expect the user to check. Manyb of the standard library
functions work that way too.

So you often see code like

if (foo = someFunction() ){   // notice it is an assignment not 
equality test
      process(foo);
}

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list