[Pyrex] Cannot cdef __init__?

Greg Ewing greg at cosc.canterbury.ac.nz
Thu Jan 22 03:20:07 CET 2004


Andrew Bennetts <andrew-pyrex at puzzling.org>:

> Or to put it differently: writing raw C, it is easy enough to write a type
> that is not callable from Python.  Pyrex should be easier than writing C :)

The problem is that Pyrex currently instantiates extension types the
same way Python does, i.e. by calling the type object. So if you made
the type object uncallable, you wouldn't be able to instantiate it at
all!

I can see two ways to approach this using Pyrex as it stands:

1) Leave the C pointer uninitialised in the constructor, and
set it from outside in the factory functions. The danger here
is that Python code will try to instantiate the type directly
and end up with an invalid instance. You can make this a little
harder by removing the type object from the module namespace,
but it's always accessible throught the __class__ attribute
of any instance. So for safety you'll need to include checks
in every method which uses the C pointer to make sure it's
valid.

2) Pass the C pointer into the constructor inside a PyCObject.
This effectively prevents Python code from instantiating
the type directly, since it won't be able to supply an
argument of the correct type. It also ensures that the
instance is valid once instantiated, so no further checks
are needed.

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg at cosc.canterbury.ac.nz	   +--------------------------------------+





More information about the Pyrex mailing list