Message234329
The following code:
import traceback
import sys
from PyQt5.QtCore import Qt
class MetaA(type):
pass
class A(metaclass=MetaA):
pass
class MetaB(type):
pass
class B(metaclass=MetaB):
pass
for ClassB in B, Qt:
print("Trying class %s" % (ClassB.__name__, ))
class MyMeta(type(A), type(ClassB)):
def __setattr__(cls, key, value):
print(cls)
super(type, cls).__setattr__(key, value)
class MyClass(A, ClassB, metaclass=MyMeta):
pass
try:
setattr(MyClass, 'abc', 123)
except:
traceback.print_exc(file=sys.stdout)
try:
type.__setattr__(MyClass, 'test', 42)
except:
traceback.print_exc(file=sys.stdout)
Fails with the following output:
Trying class B
<class '__main__.MyClass'>
Traceback (most recent call last):
File "test3.py", line 31, in <module>
setattr(MyClass, 'abc', 123)
File "test3.py", line 25, in __setattr__
super(type, cls).__setattr__(key, value)
TypeError: can't apply this __setattr__ to type object
Trying class Qt
<class '__main__.MyClass'>
Traceback (most recent call last):
File "test3.py", line 31, in <module>
setattr(MyClass, 'abc', 123)
File "test3.py", line 25, in __setattr__
super(type, cls).__setattr__(key, value)
TypeError: can't apply this __setattr__ to sip.wrappertype object
Traceback (most recent call last):
File "test3.py", line 36, in <module>
type.__setattr__(MyClass, 'test', 42)
TypeError: can't apply this __setattr__ to sip.wrappertype object
The metaclass of a class should be able to update its class' __dict__ my calling super(type, cls).__setattr__ (there is no other way known to me to do this). Furthermore, if subclassing an external class, like Qt, it should be possible to use type.__setattr__(MyClass, ...) externally to change the class' attributes.
The error is caused by the hackcheck function in objects/typeobject.c. |
|
Date |
User |
Action |
Args |
2015-01-19 22:22:19 | devkid | set | recipients:
+ devkid |
2015-01-19 22:22:19 | devkid | set | messageid: <1421706139.14.0.239687578391.issue23276@psf.upfronthosting.co.za> |
2015-01-19 22:22:19 | devkid | link | issue23276 messages |
2015-01-19 22:22:18 | devkid | create | |
|