IBM / pyflowgraph

Flow graphs for Python
Apache License 2.0
25 stars 8 forks source link

AST rewriting breaks functions that inspect the call stack #14

Closed epatters closed 5 years ago

epatters commented 5 years ago

In rare cases, the current AST rewriting strategy can break functions which violate referential transparency. For example, patsy's formula evaluation magic breaks because it inspects the local namespace of the previous frame in the call stack.

The reason is that rewriting the function call f(x,y) to

trace(f, x, y)

(see #13) changes the observed call stack. Instead, we should rewrite f(x,y) to

trace_return((trace_fun(f))(trace_arg(x), trace_arg(y)))

which, in view of Python's left-to-right evaluation order, will evaluate as

trace_fun(f)
trace_arg(x)
trace_arg(y)
trace_return(...)

This will slightly complicate the tracing machinery but will better preserve the call stack. In particular, it should fix the problem with patsy.