Vitja Makarov, 01.11.2010 12:58:
> 2010/11/1 Stefan Behnel:
>> 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.
>>
> It seems that I don't have write access to trac.
It's restricted to authorised users to prevent spam. Please send a htpasswd
line and a user name to Robert Bradshaw.
>>> 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.
>
> Yes, I'm mostly interested in metaclasses for "pure" python. As I
> understand current PyClassDef implementation is mostly based on Pyrex
> one.
Correct.
>> 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.
>>
>> 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.
>
> This should be done in _Pyx_CreateClass() wrapper?
Partly, I think. If the metaclass is defined outside of the class, e.g.
because it was passed as a keyword argument to the class, or because there
is a globally defined name __metaclass__, I think it can safely be passed
as an argument to that function from the calling code. The rest, i.e.
checking for a locally defined name in the class dict, can be done inside
of __Pyx_CreateClass(). I don't know how Py3 handles duplicate metaclass
definitions through keyword *and* __metaclass__, but Cython should do it in
the same way.
> So is there a way to turn (set|get)attr into (set|get)item to write
> dict instead of class object?
The class body (see PyClassDefNode in Nodes.py) is evaluated in its own
scope (PyClassScope in Symtab.py). It might (!) work to build a DictNode at
the end of the class body that contains all names defined in the scope with
their current value, so that it can be passed into __Pyx_CreateClass(). Not
sure how to handle "del" here, but I doubt that that currently works anyway.
Does that help? Please ask back on this list when you have any questions.
Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev