Author tkhyn
Recipients tkhyn
Date 2018-04-12.09:55:09
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1523526909.26.0.682650639539.issue33268@psf.upfronthosting.co.za>
In-reply-to
Content
The following script, run with python 3.6.5, highlights an issue with the class' __name__ attribute being set incorrectly just because of a loop in the metaclass' __new__ method:

class MC(type):
    def __new__(mcs, name, bases, attrs):
        for name, attr in attrs.items():
            pass
        return super(MC, mcs).__new__(mcs, name, bases, attrs)

class C(metaclass=MC):
    a = None

print(C.__name__)


Expected output: "C"
Actual output: "a"

Comment the for loop and you get the expected output!

On Python 2.7.13, the amended code exposes the same bug:

class MC(type):
    def __new__(mcs, name, bases, attrs):
        for name, attr in attrs.items():
            pass
        return super(MC, mcs).__new__(mcs, name, bases, attrs)

class C(object):
    __metaclass__ = MC
    a = None

print C.__name__

output is "__metaclass__" and should be "C"
History
Date User Action Args
2018-04-12 09:55:09tkhynsetrecipients: + tkhyn
2018-04-12 09:55:09tkhynsetmessageid: <1523526909.26.0.682650639539.issue33268@psf.upfronthosting.co.za>
2018-04-12 09:55:09tkhynlinkissue33268 messages
2018-04-12 09:55:09tkhyncreate