This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Kevin Shweh
Recipients Kevin Shweh
Date 2019-03-07.03:41:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1551930086.95.0.70586200648.issue36220@roundup.psfhosted.org>
In-reply-to
Content
LOAD_NAME and LOAD_GLOBAL don't treat dict subclasses for globals() the same way. If globals() is a dict subclass, LOAD_GLOBAL will respect overridden __getitem__, but LOAD_NAME will use PyDict_GetItem. This causes global lookup to behave differently in class statements; for example, in the following code, the final exec is the only one where print(y) causes a NameError:

class Foo(dict):
    def __getitem__(self, index):
        return 5 if index == 'y' else super().__getitem__(index)
 
exec('print(y)', Foo())
exec('global y; print(y)', Foo())
exec('''
class UsesLOAD_NAME:
    global y
    print(y)''', Foo())
exec('''
class UsesLOAD_NAME:
    print(y)''', Foo())


STORE_GLOBAL and DELETE_GLOBAL also go straight for PyDict_SetItem and PyDict_DelItem; I don't know whether those should be considered bugs as well, but the inconsistency between LOAD_NAME and LOAD_GLOBAL definitely seems wrong.

(For reference, the change that introduced the inconsistency was made for issue #14385, which was intended to support non-dict __builtins__.)
History
Date User Action Args
2019-03-07 03:41:26Kevin Shwehsetrecipients: + Kevin Shweh
2019-03-07 03:41:26Kevin Shwehsetmessageid: <1551930086.95.0.70586200648.issue36220@roundup.psfhosted.org>
2019-03-07 03:41:26Kevin Shwehlinkissue36220 messages
2019-03-07 03:41:26Kevin Shwehcreate