Message 68617 - Python tracker (original) (raw)

The problem is in PyFrame_LocalsToFast.

(As I understand it: "Locals" refers to the locals() dictionary of the frame; "Fast" refers to an optimization where local variables are stored in an array. Each call to locals() or the trace function normalizes data by copying the Fast array into the Locals dictionary; with sys.settrace, the user may modify the locals, so the inverse transformation is done after each call to the trace function)

When defining a class, PyFrame_FastToLocals does not copy the free variables from enclosing scopes. This is the goal of r53954 "Do not copy free variables to locals in class namespaces". When executing code in the class body, the sys.settrace function is called with this reduced set of locals(). The problem is that PyFrame_LocalsToFastdoes does not have the same test, and uses the locals() to update (and clear) the free variables as well.

I attach a patch that makes PyFrame_LocalsToFast symmetric to PyFrame_FastToLocals: in the case of a class statement, do not update the free variables.