Add warning for scope of exec#47
Conversation
|
This mostly looks OK at a high-level (except the code around |
|
@ltratt Thank you for your comments, I have fixed the low levels issues. For the For the case of |
|
Thanks! Please squash. |
|
@ltratt Sorry I had 2 exam this week and I was preparing for it. I have squashed the commits. |
exec behaves differently in Python 2 and Python 3 inside function scope. In Python 2, a plain exec statement can write back into the enclosing function's local variables. In Python 3, exec() does not reliably write back to function locals unless an explicit locals mapping is provided.
Example:
In Python 2
def f(): b = 42 exec "b = 99" return breturn 99
In Python 3
def f(): b = 42 exec("b = 99") return breturn 42 as exec didn't update local b.
In order to allow exec to modify local b. A safer version will be
def g(): b = 42 ns = {"b": b} exec("b = 99", globals(), ns) return ns["b"]Thus, in order to warn about such scope issue. I mainly modified cevel.c that