2010/11/4 Stefan Behnel <[email protected]>:
> Vitja Makarov, 03.11.2010 22:13:
>> 2010/11/3 Stefan Behnel:
>>> I actually don't think that "python3+ handle __metaclass__ itself". Why
>>> should it? It has syntax support for this very feature.
>>
>> Yes, I didnt knew much about python3.
>>
>> Now it seems to me that class declaration is more like function call
>> with special case for metaclass keyword and positional arguments:
>>
>> class Base(type):
>>      def __new__(cls, name, bases, attrs, **kwargs):
>>          print(cls, name, bases, attrs, kwargs)
>>
>> class Foo(1, 2, 3, metaclass=Base, foo='bar'):
>>      def hello(self):
>>          pass
>
> Yes, I think that's exactly how this should work. Want to give it another try?
>

Yes, I want to finish this part ;)

> It seems that the number of positional arguments is fixed, but you can pass
> any number of keyword arguments, out of which only "metaclass" is special
> cased and removed (from a copy of the dict, BTW). There is also an
> additional protocol if the metaclass has a "__prepare__" class method. See
> "__build_class__".
>

Yes, all positional arguments are turned into bases. So we can make it this way:

Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name,
PyObject *modname, PyObject *kwargs);

And then

args = (metaclass, bases, name, dict)
PyEval_CallObjectWithKeywords(metaclass, args, kwargs)

> I think the Python 2 protocol in Cython should work as in Py3. Metaclasses
> in Py2 won't generally support kwargs, but if a user explicitly provides
> them, it's best to assume that that's intentional. In the worst case, there
> will be a runtime TypeError.
>

Do you mean that new syntax should be used for Py2?

what about this case:

bases = [Foo, Bar]

class MyClass(*bases):
    pass

Don't see any problem to support this for Py2

> So, kwargs should pass through, except for __metaclass__. If both the
> __metaclass__ class field and the metaclass kwarg are passed, I'd lean
> towards ignoring the field and prefer the kwarg, just like Py3 does. It can
> be argued that this is worth a warning, though.
>

Py3 doesn't support __metaclass__ attribute, so if metaclass is set
that seems to be mostly Py3 so __metaclass__ should be ignored.

> I also think that the parser should extract the metaclass keyword, not the
> runtime code. So, if provided, it should be passed into __Pyx_CreateClass()
> as an argument and not in the kwargs.
>

I think parser can't extract metaclass keyword, try this code:

class Base(type):
    def __new__(cls, name, bases, attrs, **kwargs):
        print(cls, name, bases, attrs, kwargs)

myargs={'metaclass': Base}
class Foo(1, 2, 3, **myargs):
    def hello(self):
        pass
.
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to