[Pyrex] metaclasses for c extension types
Simon Burton
simon at arrowtheory.com
Tue Aug 9 00:47:50 CEST 2005
On Mon, 08 Aug 2005 15:25:07 +1200
Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>
> Simon Burton wrote:
>
> > I see the __metaclass__ attribute is not supported for cdef classes.
>
> > I'm wondering if this is even theoretically possible (eg. by hacking the c code).
>
> This would be difficult, since extension types are created
> a completely different way from Python classes. Instead of
> calling a metaclass, the type object is statically allocated
> and initialised by calling PyType_Ready.
>
> What are you trying to accomplish with a metaclass? There
> may be another way of getting the same effect.
I'm building a type system similar to ctypes: classes to represent c-level objects (int, void* etc).
These classes need (among other things) a __getattr__ so i can write CInt.pointer to mean "int*".
It seems that today i'm getting some mileage out of inheriting from the extension types with a metaclass:
class Meta(type):
def __getattr__( cls, name ):
if name == "pointer":
tp = type( "CPointer"+cls.__name__, (CPointer,), {} )
tp.target = cls
return tp
CInt = Meta( "CInt", (CInt,), {} )
This only works at the python level, of course.
cheers,
Simon.
--
Simon Burton, B.Sc.
Licensed PO Box 8066
ANU Canberra 2601
Australia
Ph. 61 02 6249 6940
http://arrowtheory.com
More information about the Pyrex
mailing list