tuple creation in C extensions

Bernhard Herzog herzog at online.de
Wed Jun 14 13:38:01 EDT 2000


"Louis M. Pecora" <pecora at anvil.nrl.navy.mil> writes:

> In article <etdk8ftm55x.fsf at w20-575-109.mit.edu>, Alex
> <cut_me_out at hotmail.com> wrote:
> 
> > Wow, thanks for looking that code over, Bernhard.  What a horrible bunch
> > of holes.

You're welcome.

> And who could have seen all that so clearly without an expert on hand. 
> Even then I couldn't understand all the reasoning.  In Python we are
> all happy, but the ugly side is when we return to C. 

Well, in the vast majority of cases you can get by with a few simple
rules (using the terminology of the extension docs):

 1. Functions that return references, return new references.

 2. If you pass a reference to a function, that function borrows the
    reference.

Unfortunately there are some exceptions, such as PyDict_GetItem. The
Extension and Embedding document does a pretty good job if explaining
things, IMHO. The exceptions could be better documented, though, but
then again, my local copy of the API reference is a bit old now.

In my experience, refcounting only becomes tedious and error prone if
you try to do too complex things in C. Try to do as much as possible in
Python and keep your C-extensions simple as far as constructing and
manipulating python objects is concerned.

-- 
Bernhard Herzog   | Sketch, a drawing program for Unix
herzog at online.de  | http://sketch.sourceforge.net/



More information about the Python-list mailing list