Skip to content

Preserve co_filename for code pickled by value from a source file - #629

Open
lyn1dev wants to merge 1 commit into
cloudpipe:masterfrom
lyn1dev:preserve-co-filename-for-file-backed-code
Open

lyn1dev wants to merge 1 commit into
cloudpipe:masterfrom
lyn1dev:preserve-co-filename-for-file-backed-code

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 22, 2026

Copy link
Copy Markdown

What was wrong

_code_reduce replaced the co_filename of 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.py reports no data for the restored function, inspect.getsource() raises OSError, 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_reduce now keeps co_filename when 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 single os.path.isfile call 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, and inspect.getsource() returns the function source.
  • test_dynamic_func_gets_placeholder_co_filename: a function compiled from <string> still restores with co_filename == "<dynamic-code>".

Results on Windows, CPython 3.13.18:

  • Without the fix: test_file_backed_func_preserves_co_filename fails for both protocol classes ('<dynamic-code>' != '...\tests\cloudpickle_test.py'), the placeholder test passes.
  • With the fix: 256 passed, 24 skipped, 2 xfailed for the full suite (pytest tests/), including tests/cloudpickle_ipykernel_test.py::test_deterministic_payload_for_dynamic_func_in_notebook (notebook payloads are still identical across two kernels).
  • End-to-end check with coverage.py 7.17.4 on a function pickled by value from an importable module: without the fix get_data().lines(path) is None and the run warns "No data was collected"; with the fix the body line is recorded.
  • ruff check --select E4,E7,E9,F and black --check are clean on the changed files.

A CHANGES.md entry is included.

Fixes #628

AI was used to write this code.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3.1.3 co_filename normalization breaks coverage of serialized file-backed functions

1 participant