Repository navigation
Conversation
vibhor-aggr
left a comment
There was a problem hiding this comment.
The ordinary source-file case works on 61e4de8: a restored nested function points to its original file, inspect.getsource finds its body, and line tracing records the source line. There is still one notebook case from #538 that the new isfile check does not distinguish: the notebook debugger writes real, process-specific cell files.
I checked the same function in two independent processes per case using ipykernel 6.30.1 and CPython 3.13.5. Both executed the cell via ZMQInteractiveShell.run_cell with its normal compiler. In the second case I first called ipykernel's actual Debugger.dumpCell handler with that cell's source (no frontend/network transport). All restored functions returned the same result.
| Across two processes | Base b92b61d |
PR 61e4de8 |
|---|---|---|
| Cell not dumped to disk | Identical payloads | Identical payloads |
Same cell written by dumpCell |
Identical payloads | Different payloads |
On the head, the restored filenames were /tmp/ipykernel_7/2290290463.py and /tmp/ipykernel_9/2290290463.py; the payload SHA-256 prefixes were 976bfbfa and a9639414. The base normalized both to <dynamic-code>. This explains why the undumped notebook test can pass while a debugged cell invalidates a downstream cache after a kernel restart.
Could the cross-kernel test cover a debug-dumped cell as well as the ordinary-file coverage case? File existence alone is not enough to identify a persistent source file. If this tradeoff is intentional, it is worth narrowing the changelog's determinism claim.
Verified with OpenAI Codex.
What was wrong
_code_reducereplaced theco_filenameof every pickled code object with<dynamic-code>. That makes payloads deterministic for code without a source file (the notebook case of #538), but it also strips the filename of code that is pickled by value from a regular source file, for example a nested function returned by another function.Tools that map executed code back to its source rely on
co_filename. After such a round trip,coverage.pyreports no data for the restored function,inspect.getsource()raisesOSError, and tracebacks point at<dynamic-code>. Since 3.1.3, downstream projects that serialize functions and then measure their execution (e.g. Dask wrappers) lose that coverage; #628 has the details and the downstream CI links.The fix
_code_reducenow keepsco_filenamewhen it names a source file that exists on disk, and only falls back to<dynamic-code>when it does not. Code without a real file behind it (<string>,<ipython-input-...>, removed files) keeps the deterministic placeholder, so the behaviour that #560 introduced for notebooks is unchanged. The check is a singleos.path.isfilecall per pickled code object.Testing
New tests in
tests/cloudpickle_test.py:test_file_backed_func_preserves_co_filename: a nested function from the test module is pickled by value; the restored code object keeps the filename, the file exists, andinspect.getsource()returns the function source.test_dynamic_func_gets_placeholder_co_filename: a function compiled from<string>still restores withco_filename == "<dynamic-code>".Results on Windows, CPython 3.13.18:
test_file_backed_func_preserves_co_filenamefails for both protocol classes ('<dynamic-code>' != '...\tests\cloudpickle_test.py'), the placeholder test passes.256 passed, 24 skipped, 2 xfailedfor the full suite (pytest tests/), includingtests/cloudpickle_ipykernel_test.py::test_deterministic_payload_for_dynamic_func_in_notebook(notebook payloads are still identical across two kernels).coverage.py7.17.4 on a function pickled by value from an importable module: without the fixget_data().lines(path)isNoneand the run warns "No data was collected"; with the fix the body line is recorded.ruff check --select E4,E7,E9,Fandblack --checkare clean on the changed files.A CHANGES.md entry is included.
Fixes #628
AI was used to write this code.