[issue16271] weird dual behavior with changing __qualname__; different values observed through attribute and descriptor

Christopher the Magnificent report at bugs.python.org
Thu Oct 18 01:43:06 CEST 2012


New submission from Christopher the Magnificent:

The output below is NOT typed at the Python interactive interpeter.  The ">>> " shows what is being evaluated and the line below it shows what the result it.

The output gets generated here:  (lines 418-449 of the attached file)

    def name_globalize_class(name, second=None):
        def decorator(cls):
            
            def printval(source, value=None):
                print(">>> " + source)
                if value is not None:
                    print(repr(value))
            print("in decorator: new name is:", repr(name))
            print()
            printval("cls", cls)
            printval("cls.__name__", cls.__name__)
            printval("cls.__qualname__", cls.__qualname__)
            printval('type.__dict__["__qualname__"].__get__(cls)',
                     type.__dict__["__qualname__"].__get__(cls))
            print()
            
            cls.__name__ = name
            cls.__qualname__ = name
            
            stuff = ">>> cls.__name__ = {0}\n>>> cls.__qualname__ = {0}\n"
            print(stuff.format(repr(name)))
            
            printval("cls.__name__", cls.__name__)
            printval("cls.__qualname__", cls.__qualname__)
            printval('type.__dict__["__qualname__"].__get__(cls)',
                     type.__dict__["__qualname__"].__get__(cls))
            printval("cls", cls)
            print()
            globals()[name] = cls
            pdb.set_trace()
            return cls
        return decorator
    
HERE IS THE OUTPUT:

>>> cls
<class '__main__._maketokensnodes.<locals>._TokenClass'>
>>> cls.__name__
'_TokenClass'
>>> cls.__qualname__
'_maketokensnodes.<locals>._TokenClass'
>>> type.__dict__["__qualname__"].__get__(cls)
'_maketokensnodes.<locals>._TokenClass'

>>> cls.__name__ = 'KEYWORD'
>>> cls.__qualname__ = 'KEYWORD'

>>> cls.__name__
'KEYWORD'
>>> cls.__qualname__
'KEYWORD'
>>> type.__dict__["__qualname__"].__get__(cls)
'_maketokensnodes.<locals>._TokenClass'
>>> cls
<class '__main__._maketokensnodes.<locals>._TokenClass'>

END OF OUTPUT

Note how after assigning to cls.__qualname__ it looks like the class's dictionary object has been assigned into, masking the class's C-level type attribute-level ht_qualname!

My gut feeling is that this has to be some kind of a bug.  Let me know if it is.

----------
components: Interpreter Core
files: parser_BUGGY2.py
messages: 173217
nosy: christopherthemagnificent
priority: normal
severity: normal
status: open
title: weird dual behavior with changing __qualname__; different values observed through attribute and descriptor
versions: Python 3.3
Added file: http://bugs.python.org/file27604/parser_BUGGY2.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16271>
_______________________________________


More information about the Python-bugs-list mailing list