Author Martin.Teichmann
Recipients Martin.Teichmann, eric.snow, ncoghlan
Date 2016-07-17.14:27:18
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1468765639.68.0.88932700909.issue23722@psf.upfronthosting.co.za>
In-reply-to
Content
Currently, a class is created as follows: the compiler turns the class statement into a call to __build_class__. This runs the class body. If __class__ or super() is used within a method of the class, an empty PyCell is created, to be filled later with the class once its done.

The class body returns this cell. Then the metaclass is called to create the actual class, and finally the cell is set to whatever the metaclass returns.

This has the disadvantage that in the metaclasses __new__ and __init__, __class__ and super() are not set. This is a pity, especially because the two parameter version of super() doesn't work either, as the class is not yet bound to a name.

The attached patch lets the compiler add said cell as __classcell__ to the classes namespace, where it will later be taken out by type.__new__ in order to be properly filled.

This resembles the approach used for __qualname__, with the difference that __qualname__ is already added at the beginning of the classes body, such that it is visible to the user.

This way __class__ will be properly set immediately after it is created, thus all methods are immediately usable, already in a metaclasses __new__ or __init__.

This changes the behavior if a metaclass returns another class. currently, __build_class__ will try to set the __class__ in the methods of the class body to whatever __new__ returns, which might be completely unrelated to the classes body.
History
Date User Action Args
2016-07-17 14:27:19Martin.Teichmannsetrecipients: + Martin.Teichmann, ncoghlan, eric.snow
2016-07-17 14:27:19Martin.Teichmannsetmessageid: <1468765639.68.0.88932700909.issue23722@psf.upfronthosting.co.za>
2016-07-17 14:27:19Martin.Teichmannlinkissue23722 messages
2016-07-17 14:27:19Martin.Teichmanncreate