Author Wombatz
Recipients Wombatz
Date 2016-11-25.02:48:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1480042109.13.0.341330811366.issue28797@psf.upfronthosting.co.za>
In-reply-to
Content
This behavior occurs at least in python-3.6.0b3 and python-3.6.0b4.

Modifying the class __dict__ inside the __set_name__ method of a descriptor that is used inside that class can lead to a bug that possibly prevents other descriptors from being initialized (the call to their __set_name__ is prevented).

That happens because internally the cpython interpreter iterates the class __dict__ when calling all the __set_name__ methods. Changing the class __dict__ while iterating it leads to undefined behavior. This is the line in the source code https://github.com/python/cpython/blob/master/Objects/typeobject.c#L7010

See the attached file for an example where the bug can be observed. It defines a "desc" class that implements the __set_name__ method where it prints the name and modifies the class __dict__ of the containing class. Later a class is defined that has multiple instances of the "desc" class as class attributes. Depending on the number of attributes not all of them are initialized.

When you see the underlying C-Code the reason for this behavior is obvious but in python itself the problem might not be apparent.

To fix this bug the class __dict__ could be cashed and then the __set_name__ methods could be called while iterating over that copy.
History
Date User Action Args
2016-11-25 02:48:29Wombatzsetrecipients: + Wombatz
2016-11-25 02:48:29Wombatzsetmessageid: <1480042109.13.0.341330811366.issue28797@psf.upfronthosting.co.za>
2016-11-25 02:48:28Wombatzlinkissue28797 messages
2016-11-25 02:48:27Wombatzcreate