Fix bundle() on Python 3.13 by snapshotting the caller's locals - #54
Open
Max Freedom Pollard (MaxFreedomPollard) wants to merge 1 commit into
Open
Conversation
Fixes microsoft#43. bundle() captures the calling frame's locals so that a trainable operator can be re-executed with them. Python 3.13 changed frame.f_locals to return a live FrameLocalsProxy rather than a dict (PEP 667), so the captured value is no longer a dict and FunModule's isinstance check rejects it: AssertionError: _ldict must be a dictionary. or None Every bundle() call fails on 3.13 as a result, which is what microsoft#43 reports. Taking dict(...) of the proxy at the capture site restores the pre-3.13 behaviour exactly -- a plain dict snapshot of the caller's locals -- on every supported version, and needs no change to the isinstance check. tests/unit_tests/test_bundle.py already exercises the failing path, since it applies @Bundle inside a function; it simply never ran on 3.13. This adds an explicit assertion that the captured locals are a dict holding the caller's variables, and extends the CI matrix to 3.9 and 3.13 so the regression is covered from now on.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #43.
Root cause
bundle()captures the calling frame's locals so a trainable operator can be re-executed with them:Python 3.13 implemented PEP 667, which changed
frame.f_localsto return a liveFrameLocalsProxyinstead of adict.FunModule.__init__then rejects it:Every
bundle()call fails on 3.13 as a result, so the package is effectively unusable there — which is what #43 reports. Ching-An Cheng (@chinganc) identified this cause in the issue thread; this PR is the fix for it.The fix
I went with a snapshot at the capture site rather than relaxing the
isinstance(_ldict, dict)check, for two reasons:f_localshanded back a point-in-time dict copy.FrameLocalsProxyis a live view of the frame, so merely accepting it would leave 3.13 with subtly different semantics from every other version.FunModulestill receives a realdict, so the existing assertion and the_ldict.copy()on the next line keep working untouched.FrameLocalsProxyis aMapping, sodict(...)is the documented way to materialise it, and on ≤3.12 this is just a copy of a dict that was about to be copied anyway.I also checked the other frame manipulation in
bundle.pyfor 3.13 breakage —update_local()callsctypes.pythonapi.PyFrame_LocalsToFast, which I expected to be gone in 3.13. It is still exported and still works, and theoverwrite_python_recursionpath it serves passes on 3.13, so nothing further is needed.Coverage
tests/unit_tests/test_bundle.pyalready exercised the broken path — it applies@bundle()inside a function, which is exactly what triggers the capture — it just never ran on 3.13. So this PR:dictcontaining the caller's variables, which pins the behaviour rather than testing it incidentally["3.9", "3.13"]withfail-fast: false, so 3.13 is actually covered from now onWithout the fix,
test_bundle.pyfails on 3.13 with theAssertionErrorabove and passes on 3.9. With it, both pass.Verification
python tests/unit_tests/run.pyThe matrixed workflow, run on this exact commit in my fork: https://github.com/MaxFreedomPollard/Trace/actions/runs/33941552555 — both legs green, 22/22 each.
ruff==0.0.261reports the same 60 pre-existing findings before and after this change, andblack==23.3.0proposes no changes to any added line (both files already differ from black onmain, so I left the rest alone). No API, signature, or dependency changes.