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 johnf
Recipients johnf
Date 2012-06-18.10:59:56
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1340017199.64.0.733628658868.issue15099@psf.upfronthosting.co.za>
In-reply-to
Content
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
>>>
History
Date User Action Args
2012-06-18 10:59:59johnfsetrecipients: + johnf
2012-06-18 10:59:59johnfsetmessageid: <1340017199.64.0.733628658868.issue15099@psf.upfronthosting.co.za>
2012-06-18 10:59:58johnflinkissue15099 messages
2012-06-18 10:59:57johnfcreate