[issue31401] Dynamic compilation that uses function in comprehension fails when compiled inside function

Vedran Čačić report at bugs.python.org
Sat Sep 9 19:14:34 EDT 2017


Vedran Čačić added the comment:

List comprehension is irrelevant here, any nested function has the same problem. And the inner name doesn't have to refer to a function. Also, compilation as a separate step is not necessary.

So the minimal example is

    src = '''\
    a = 5
    def g(x): return a + x
    print(g(3))
    '''
    def comp(): exec(src)
    comp()

But I'm not sure if it is a bug at all. If you write explicitly

    exec(src, {})

you get a result 8 without problem. The docs say

    ...if the optional parts are omitted, the code is executed in the current scope.

And the current scope inside comp is "fast locals", where you can't really make new names. Maybe exec should raise an exception then, but it surely cannot just "work" inside fast locals scope.

----------
nosy: +veky

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


More information about the Python-bugs-list mailing list