[New-bugs-announce] [issue32615] Inconsistent behavior if globals is a dict subclass

ppperry report at bugs.python.org
Sun Jan 21 18:46:26 EST 2018


New submission from ppperry <mapreader at olum.org>:

Take the following code:

    import builtins
    class K(dict):
	def __getitem__(self, k):
		if k not in builtins.__dict__:
			print("get %s" % k)
		return dict.__getitem__(self, k)
	def __setitem__(self, k, v):
		print("set %s" % k)
		dict.__setitem__(self, k, v)
    exec("""
        foo = "bar"
        foo
        try:
            qyz
        except NameError:
	    pass
        class K:
	    baz = foo
	    def f(ggg=foo): pass
        def g(ggg=foo):
	    global f
	    f = 87
            f
        g()
    """,K())

This should consitently either call or not call the overridden methods on the dictionary, producing either no output or:

    set foo
    get foo
    get qyz
    get foo
    get foo
    set K
    get foo
    set g
    get g
    set f
    get f
    
Instead, only sometime the override gets called, producing

    set foo
    get foo
    get qyz
    set K
    get foo
    set g
    get g
    get f

meaning that 
    (a) modifications of global variables via global statements
    (b) code at class-level

ignore overridden methods, whereas everything else follows them

----------
components: Interpreter Core
messages: 310388
nosy: ppperry
priority: normal
severity: normal
status: open
title: Inconsistent behavior if globals is a dict subclass
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32615>
_______________________________________


More information about the New-bugs-announce mailing list