Repository navigation
Conversation
This branch has not been deployed
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.
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.