New submission from Alfred Krohmer:

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.

----------
components: Interpreter Core, Library (Lib)
messages: 234329
nosy: devkid
priority: normal
severity: normal
status: open
title: hackcheck is broken in association with __setattr__
type: behavior
versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4

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

Reply via email to