Re: accessing __*__ functions from C

Guido.van.Rossum@cwi.nl
Tue, 20 Sep 1994 14:03:07 +0200

> I am writing some C code which uses a number of user-defined classes.
> Some of these classes implement smart lists, that is, they provide
> __getslice__(), __getitem__() and __len__(). Is it possible, from
> within C, to make use of these functions without calling them directly?
> I would like to simply use getlistitem() and getlistsize() without writing
> special wrappers for each method, but looking at the code it doesn't look
> like this is possible.

Technically, in Python, your class implements a sequence, not a list.
(A list is a particular kind of sequence whose implementation is done
entirely in C.)

To call e.g. the getitem function of an arbitrary sequence, you have
to call x->ob_type->tp_as_sequence->sq_item(x, i), checking each
pointer (except ob_type) for being non-NULL before dereferencing it.
I agree that there should be a standard C interface to each of the
sequence operations (and to each of the mapping operations). Donald
Beaudry has written such code, all it needs is conversion to the
standard naming scheme (it's in Donald's todo list :-).

--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>