Skip to content

Commit 29b6bae

Browse files
committed
feat: keep track of the local variables inside Python eval/exec to a dict
Python 3.13 dramatically changed how the namespace in `exec`/`eval` works. See https://docs.python.org/3.13/whatsnew/3.13.html#defined-mutation-semantics-for-locals
1 parent a5fbef8 commit 29b6bae

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

python/pythonmonkey/require.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@
8282
globalThis.python.stderr.write = lambda s: sys.stderr.write(s)
8383
globalThis.python.stdout.read = lambda n: sys.stdout.read(n)
8484
globalThis.python.stderr.read = lambda n: sys.stderr.read(n)
85-
globalThis.python.eval = eval
86-
globalThis.python.exec = exec
85+
# Python 3.13 dramatically changed how the namespace in `exec`/`eval` works
86+
# See https://docs.python.org/3.13/whatsnew/3.13.html#defined-mutation-semantics-for-locals
87+
_locals = {} # keep the local variables inside `eval`/`exec` to a dict
88+
globalThis.python.eval = lambda x: eval(x, None, _locals)
89+
globalThis.python.exec = lambda x: exec(x, None, _locals)
8790
globalThis.python.getenv = os.getenv
8891
globalThis.python.paths = sys.path
8992

0 commit comments

Comments
 (0)