jupytercalpoly / reactivepy

A reactive Python kernel
BSD 3-Clause "New" or "Revised" License
86 stars 11 forks source link

Not All Single Assignments Captured #28

Open declanvk opened 6 years ago

declanvk commented 6 years ago

The current process for executing code involves a few stages.

  1. The code is converted into an AST
  2. If the last node in the top level is a single assignment, then insert another node after that that evaluates to the assigned variable.
  3. 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.
  4. Wrap everything that remains in an ast.Module.
  5. Execute the ast.Module using exec, and the ast.Interactive using eval.

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