Skip to content

Add warning for scope of exec#47

Open
Eddy114514 wants to merge 2 commits intosoftdevteam:migrationfrom
Eddy114514:execscope_warn
Open

Add warning for scope of exec#47
Eddy114514 wants to merge 2 commits intosoftdevteam:migrationfrom
Eddy114514:execscope_warn

Conversation

@Eddy114514
Copy link
Copy Markdown
Collaborator

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 b
return 99

In Python 3
def f(): b = 42 exec("b = 99") return b
return 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

  1. Before exec runs, snapshot the current fast locals.
  2. After exec, compare fast locals and record which local slots changed.
  3. If one of those locals is later read (LOAD_FAST), emit the warning.
  4. If it is overwritten or deleted before being read, clear the pending state and do not warn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant