[Python-Dev] Python's C interface for types

Nick Maclaren nmm1 at cus.cam.ac.uk
Fri Jan 26 14:51:04 CET 2007


I have a fair amount of my binary floating-point model written,
though even of what I have done only some is debugged (and none
has been rigorously tested).  But I have hit some things that I
can't work out, and one query reduced comp.lang.python to a
stunned silence :-)

Note that I am not intending to do all the following, at least for
now, but I have had to restructure half a dozen times to match my
implementation requirements to the C interface (as I have learnt
more about Python!) and designing to avoid that is always good.

Any pointers appreciated.

I can't find any detailed description of the methods that I need
to provide.  Specifically:

Does Python use classic division (nb_divide) and inversion (nb_invert)
or are they entirely historical?  Note that I can very easily provide
the latter.

Is there any documentation on the coercion function (nb_coerce)?  It
seems to have unusual properties.

How critical is the 'numeric' property of the nb_hash function?  I
can certainly honour it, but is it worth it?

I assume that Python will call nb_richcompare if defined and 
nb_compare if not.  Is that right?

Are the inplace methods used and, if so, what is their specification?

I assume that I can ignore all of the allocation, deallocation and
attribute handling functions, as the default for a VAR object is
fine.  That seems to work.

Except for one thing!  My base type is static, but I create some
space for every derivation (and it can ONLY be used in derived form).
The space creation is donein C but the derivation in Python.  I
assume that I need a class (not instance) destructor, but what
should it do to free the space?  Call C to Py_DECREF it?

I assume that a class structure will never go away until after all
instances have gone away (unless I use Py_DECREF), so a C pointer
from an instance to something owned by the class is OK.

Is there any documentation on how to support marshalling/pickling
and the converse from C types?

I would quite like to provide some attributes.  They are 'simple'
but need code executing to return them.  I assume that means that
they aren't simple enough, and have to be provided as methods
(like conjugate).  That's what I have done, anyway.

Is there any obvious place for a reduction method to be hooked in?
That is a method that takes a sequence, all members of which must
be convertible to a single class, and returns a member of that
class.  Note that it specifically does NOT make sense on a single
value of that class.

Sorry about the length of this!


Regards,
Nick Maclaren,
University of Cambridge Computing Service,
New Museums Site, Pembroke Street, Cambridge CB2 3QH, England.
Email:  nmm1 at cam.ac.uk
Tel.:  +44 1223 334761    Fax:  +44 1223 334679


More information about the Python-Dev mailing list