Emanuel Barry added the comment:

In Objects/typeobject.c#L2290, the code checks that the (meta?)class is exactly 
`type` and that there's one argument, then returns `Py_TYPE(x)`. Subclasses of 
`type` allowing a single argument is a side-effect of not overriding __new__, 
not a documented feature.

Changing the call from `PyType_CheckExact` to `PyType_Check` makes it work, but 
I'm assuming there may be something I didn't think of. Or maybe there isn't, 
but either way, I don't consider that this is worth fixing -- if you want to 
call your subclass with only one argument, override __new__ and do the logic in 
there. And if you want the type of an object, use type directly.

Also, there may be performance concerns here. `type` is heavily optimized in 
many places; I thought that `PyType_Check` called Python code, but after 
checking the macro definitions and testing a bit, it turns out I'm wrong. But 
if there *is* a negative performance impact, this probably can't go in -- this 
check runs everytime that type() is called, no matter how many arguments, and 
including in class creation; that's also probably why `PyType_CheckExact` was 
chosen to begin with.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27157>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to