-
Notifications
You must be signed in to change notification settings - Fork 11
Open
Description
The current process for executing code involves a few stages.
- The code is converted into an AST
- If the last node in the top level is a single assignment, then insert another node after that that evaluates to the assigned variable.
- If the last node in the updated, or not updated, top level is an expression of any kind, pull the node off and wrap it in an
ast.Interactive. - Wrap everything that remains in an
ast.Module. - Execute the
ast.Moduleusingexec, and theast.Interactiveusingeval.
The Issue
This process will not be able to extract the value of a single assignment if it does not occur last in a code block. For example:
a = 10
print(a)
Will not be able to extract the value of a.
The Solution
Instead of manipulating the AST, it would be better to simply provide the name of the output variable, and then read the variable from the namespace dictionary.
This will eliminate all of the different AST manipulation and the splitting of code into exec and eval, ast.Module and ast.Interactive. All of those steps can be skipped, going straight to execing the block of code.
Sent with GitHawk
Metadata
Metadata
Assignees
Labels
No labels