[Pyrex] Cannot cdef __init__?

Andrew Bennetts andrew-pyrex at puzzling.org
Thu Jan 22 03:38:21 CET 2004


On Thu, Jan 22, 2004 at 03:20:07PM +1300, Greg Ewing wrote:
> 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!

Ah, I see!  Thanks.

> 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.

Yeah, this had occured to me, but remembering to put a call to
"self.assertInitialised()" in every member function is both too annoying and
too error-prone for my liking.

> 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.

CObjects look like they might be the simplest way to achieve the sort of
behaviour I'm after.

Thanks,

-Andrew.





More information about the Pyrex mailing list