Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
In development
==============

- Preserve the `co_filename` of code objects that are pickled by value when
their source file exists, so that source based tools (`coverage.py`,
`inspect`, `linecache`) can keep mapping the restored code to its source.
The `<dynamic-code>` placeholder is now only used for code that has no
source file. ([issue #628](https://github.com/cloudpipe/cloudpickle/issues/628))

3.1.3
=====

Expand Down
9 changes: 8 additions & 1 deletion cloudpickle/cloudpickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import itertools
import logging
import opcode
import os
import pickle
from pickle import _getattribute as _pickle_getattribute
import platform
Expand Down Expand Up @@ -847,7 +848,13 @@ def _code_reduce(obj):
# the payload deterministic, independent of where the function is defined
# which is especially useful when defining classes in jupyter/ipython
# cells which do not have a deterministic filename.
co_filename = "".join("<dynamic-code>")
# Code that comes from a source file that still exists on disk keeps its
# original filename so that source based tools (coverage.py, inspect,
# linecache) can keep mapping it back to its source.
if os.path.isfile(obj.co_filename):
co_filename = obj.co_filename
else:
co_filename = "".join("<dynamic-code>")

# Create shallow copies of these tuple to make cloudpickle payload deterministic.
# When creating a code object during load, copies of these four tuples are
Expand Down
35 changes: 35 additions & 0 deletions tests/cloudpickle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import base64
import dataclasses
import functools
import inspect
import io
import itertools
import logging
Expand Down Expand Up @@ -2048,6 +2049,40 @@ class A:
)
assert_run_python_script(code)

def test_file_backed_func_preserves_co_filename(self):
# A nested function of this test module is pickled by value because it
# cannot be imported by reference. Its code object must keep the
# filename of the source file it was compiled from so that source based
# tools (coverage.py, tracebacks, inspect) can resolve it.
def make_func():
def func():
return 1

return func

func = make_func()
assert os.path.abspath(func.__code__.co_filename) == os.path.abspath(__file__)

restored = cloudpickle.loads(cloudpickle.dumps(func))
assert restored() == 1
assert restored.__code__.co_filename == func.__code__.co_filename
assert os.path.isfile(restored.__code__.co_filename)
# The source of the restored function is still reachable.
assert "def func():" in inspect.getsource(restored)

def test_dynamic_func_gets_placeholder_co_filename(self):
# A function whose code object has no backing source file keeps the
# placeholder filename that makes its payload deterministic.
namespace = {"__name__": "not_importable"}
exec("def func():\n return 1\n", namespace)

func = namespace["func"]
assert not os.path.isfile(func.__code__.co_filename)

restored = cloudpickle.loads(cloudpickle.dumps(func))
assert restored() == 1
assert restored.__code__.co_filename == "<dynamic-code>"

def test_dynamic_func_deterministic_roundtrip(self):
# Check that the pickle serialization for a dynamic func is the same
# in two processes.
Expand Down