You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Closure captures a caller’s argument after #924 #925
On CPython 3.14 and pyscript 933e177, the assertion passes. On 148e2d3, read_label() returns "caller" and the assertion fails. Calling make()() directly still returns "global" on all three.
read is defined inside make, so its free name label should resolve to the module variable regardless of who calls make.
Thanks @dmamelin for the clear report and the small script. I can reproduce it: on 148e2d3read_label() returns "caller", and on 933e177 it returns "global", as CPython does.
The cause. When pyscript creates an inner function, EvalFunc.resolve_nonlocals() in eval.py decides which outside variables the inner function shares. It searches the symbol tables of the functions that are currently running (sym_table_stack), and takes the first one that keeps the name as a shared variable. Python only looks in the functions the inner function is written inside. In your script, read is written inside make, so the search should never reach caller, but it does, because caller is the function that called make.
Why #924 exposed it. Before #924, only a function with an inner function or a lambda kept its variables in the shared form that this search picks up, so most callers were skipped. The bug was already there for those functions. On 933e177, your script also gives "caller" if caller contains a lambda:
#924 made every function keep its variables in the shared form, so now it happens with any caller.
The fix. I'll change the search so an inner function only shares variables from the functions it is written inside, and add both scripts to the tests that compare pyscript with CPython, as written and with an inner function. I'll open a PR for it soon.
Thanks @dmamelin again for the report. #935 fixes it.
When pyscript evaluates a def, it now looks for the outside variables the new function reads only in the function the def is written in, as Python does, instead of in every function on the call stack. For a method, it looks through the class body to the function around the class. So your script now gives "global", and so does the variant with a lambda in caller.
Two kinds of code only worked before because of that call stack search, so I fixed them in the same PR so they keep working:
a method of a class inside an inner function, when the class body assigns or declares global the same name the method reads. In Python, methods don't see the class body's names, so the method reads the variable of the function around the class;
nonlocal in a function two or more levels deep, through a function in the middle.
It also fixes a related, older difference: a nested function's parameters and a comprehension's loop variables made the function around them share an outer variable with the same name, which eval() and locals() there showed.
The PR adds 66 scripts that are compared with CPython, each as written and with an inner function: your script and its lambda variant, the other ways a caller's variable leaked (a function two levels deep, methods, a decorator, recursion, eval(), and lambdas, comprehensions and generator expressions inside a middle function), and controls for the class and nonlocal cases above. It doesn't fix the default value case, where a default of a function inside another inner function reads the outer function's variable. That one was already wrong before #924, and I've listed it in the PR with a few other older differences.
Activity
HoneyBearTech commentedon Nov 24, 2026
Thanks @dmamelin for the clear report and the small script. I can reproduce it: on 148e2d3
read_label()returns"caller", and on 933e177 it returns"global", as CPython does.The cause. When pyscript creates an inner function,
EvalFunc.resolve_nonlocals()ineval.pydecides which outside variables the inner function shares. It searches the symbol tables of the functions that are currently running (sym_table_stack), and takes the first one that keeps the name as a shared variable. Python only looks in the functions the inner function is written inside. In your script,readis written insidemake, so the search should never reachcaller, but it does, becausecalleris the function that calledmake.Why #924 exposed it. Before #924, only a function with an inner function or a lambda kept its variables in the shared form that this search picks up, so most callers were skipped. The bug was already there for those functions. On 933e177, your script also gives
"caller"ifcallercontains a lambda:#924 made every function keep its variables in the shared form, so now it happens with any caller.
The fix. I'll change the search so an inner function only shares variables from the functions it is written inside, and add both scripts to the tests that compare pyscript with CPython, as written and with an inner function. I'll open a PR for it soon.
HoneyBearTech commentedon Dec 1, 2026
Thanks @dmamelin again for the report. #935 fixes it.
When pyscript evaluates a
def, it now looks for the outside variables the new function reads only in the function thedefis written in, as Python does, instead of in every function on the call stack. For a method, it looks through the class body to the function around the class. So your script now gives"global", and so does the variant with a lambda incaller.Two kinds of code only worked before because of that call stack search, so I fixed them in the same PR so they keep working:
globalthe same name the method reads. In Python, methods don't see the class body's names, so the method reads the variable of the function around the class;nonlocalin a function two or more levels deep, through a function in the middle.It also fixes a related, older difference: a nested function's parameters and a comprehension's loop variables made the function around them share an outer variable with the same name, which
eval()andlocals()there showed.The PR adds 66 scripts that are compared with CPython, each as written and with an inner function: your script and its lambda variant, the other ways a caller's variable leaked (a function two levels deep, methods, a decorator, recursion,
eval(), and lambdas, comprehensions and generator expressions inside a middle function), and controls for the class andnonlocalcases above. It doesn't fix the default value case, where a default of a function inside another inner function reads the outer function's variable. That one was already wrong before #924, and I've listed it in the PR with a few other older differences.Don't let an inner function read its caller's variables (custom-compo…