[issue15099] exec of function doesn't call __getitem__ or __missing__ on undefined global

John Firestone report at bugs.python.org
Mon Jun 18 12:59:59 CEST 2012


New submission from John Firestone <johnf at freenet.de>:

exec(source, Dict()) doesn't call Dict().__getitem__ or Dict().__missing__ if the source string contains a function and the function references an undefined global.

class Dict1(dict):
    def __getitem__(self, key):
        print '    __getitem__', repr(key)
        if key == 's':
            return None
        return dict.__getitem__(self, key)

class Dict2(dict):
    def __missing__(self, key):
        print '    __missing__', repr(key)
        return None

source = """if 1:
    print '  1'
    s
    def f():
        print '  2'
        s
        print '  3'
    f()"""

print 'Dict1.__getitem__'
try:
    exec(source, Dict1())
except NameError as exc_value:
    print '  %s: %s' % (exc_value.__class__.__name__, exc_value)

print 'Dict2.__missing__'
try:
    exec(source, Dict2())
except NameError as exc_value:
    print '    %s: %s' % (exc_value.__class__.__name__, exc_value)


Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:32:06) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
>>> import curiosity
Dict1.__getitem__
  1
    __getitem__ 's'
    __getitem__ 'f'
  2
    NameError: global name 's' is not defined
Dict2.__missing__
  1
    __missing__ 's'
  2
    NameError: global name 's' is not defined
>>>

----------
components: Interpreter Core
files: curiosity.py
messages: 163095
nosy: johnf
priority: normal
severity: normal
status: open
title: exec of function doesn't call __getitem__ or __missing__ on undefined global
type: behavior
versions: Python 2.7
Added file: http://bugs.python.org/file26041/curiosity.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15099>
_______________________________________


More information about the Python-bugs-list mailing list