Issue 16271: weird dual behavior with changing qualname; different values observed through attribute and descriptor (original) (raw)

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.._TokenClass'> cls.name '_TokenClass' cls.qualname '_maketokensnodes.._TokenClass' type.dict["qualname"].get(cls) '_maketokensnodes.._TokenClass'

cls.name = 'KEYWORD' cls.qualname = 'KEYWORD'

cls.name 'KEYWORD' cls.qualname 'KEYWORD' type.dict["qualname"].get(cls) '_maketokensnodes.._TokenClass' cls <class '__main__._maketokensnodes.._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.