Don't understand syntax error: unqualified exec is not allowed ..

Terry Reedy tjreedy at udel.edu
Tue Oct 21 16:47:34 EDT 2008


Stef Mientki wrote:

>>>    import sys
>>>    p_locals  = sys._getframe(1).f_locals
>>
>> Which locals does this get you? __init__'s? (locals()?)
> AFAIK locals() == sys._getframe(0).f_locals

Then it is not clear to me which locals your are getting.

>>>    p_globals = sys._getframe(1).f_globals
>>
>> Isn't this just the same as globals()?
> AFAIK, again one level up
>>
>>>    try :
>>>      exec ( code, p_globals, p_locals )
>>
>> This is 3.0 exec function syntax.
> weird, I use it in 2.5 and if I remember well it already worked in 2.4.
> but exchanging the function with the statement yields exactly the same 
> results

2.5 manual says
"6.14 The exec statement

exec_stmt ::= "exec" or_expr ["in" expression ["," expression]]

This statement supports dynamic execution of Python code. The first 
expression should evaluate to either a string, an open file object, or a 
code object."

Your expression evaluates to a tuple (triple), which is not documented.
 From the error message, I wondoer if p_globals, p_locals is ignored. Try
exec code in p_globals, p_locals




More information about the Python-list mailing list