Robert Bradshaw | 1 Nov 21:14 2010

Re: [Cython] metaclasses

On Mon, Nov 1, 2010 at 3:38 AM, Stefan Behnel <stefan_ml@...> wrote:
> Vitja Makarov, 01.11.2010 08:46:
>> I want to see metaclasses in cython.
>
> +1, could you open an enhancement ticket in the bug tracker? I'm surprised
> there isn't one yet.

+1 from me too, this'd be great to see. It's probably the largest
missing piece after generators.

>> So I look into the code and I see that classes are generated like this:
>>
>> class Foo:
>>     xxx = 111
>>
>> is transformed in something like:
>>
>> Foo = Pyx_CreateClass('Foo', bases=(), attrs={})
>> # and then it does setattr()
>> Foo.xxx = 111
>>
>> So nothing to do with metaclasses in Pyx_CreateClass() as attributes
>> are set after class is actually created ;(
>> Btw if class dict will be filled before class creation it's easy to
>> handle metaclass stuff in Pyx_CreateClass.
>
> Your description seems to indicate that you want this only for Python
> classes, which is fine. I think building the dict before building the class
> is the right way to implement this, as the metaclass is allowed to do
> whatever it likes with its input, including changes to the class dict. So
> it really needs to know the complete dict.

I agree, we should create the dict first. It'll take some work, but
don't think this'll be that big of a change.

> Also see this post:
>
> http://thread.gmane.org/gmane.comp.python.cython.devel/10388
>
> I don't know if the method binding problem has gone away, but it's worth a
> try. If it hasn't, it's worth fixing the way methods work, IMHO, but that
> may not be low hanging fruit.

It has been fixed, in the sense that it has been implemented. I'm it's
enabled by default yet, but that would be a question of just flipping
a (context-triggered for now) switch.
http://trac.cython.org/cython_trac/ticket/494

> The post is from this originating thread, where Ryan Kelly proposed ways to
> implement metaclasses:
>
> http://thread.gmane.org/gmane.comp.python.cython.user/1652
>
> I don't think metaclasses for cdef classes are ready to be implemented yet,
> but it should be easier for Python classes.
>
> Also note that there is a new syntax for metaclasses in Py3, which I prefer
> over the Py2 way. So we will eventually have to support both, although the
> Py2 way is easy to emulate for Py3 code.
>
> Stefan
> _______________________________________________
> Cython-dev mailing list
> Cython-dev@...
> http://codespeak.net/mailman/listinfo/cython-dev
>

Gmane