diff --git a/CHANGES.md b/CHANGES.md index 15cc4996..e4f1fc9a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 `` placeholder is now only used for code that has no + source file. ([issue #628](https://github.com/cloudpipe/cloudpickle/issues/628)) + 3.1.3 ===== diff --git a/cloudpickle/cloudpickle.py b/cloudpickle/cloudpickle.py index 08882306..796fcb68 100644 --- a/cloudpickle/cloudpickle.py +++ b/cloudpickle/cloudpickle.py @@ -62,6 +62,7 @@ import itertools import logging import opcode +import os import pickle from pickle import _getattribute as _pickle_getattribute import platform @@ -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("") + # 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("") # Create shallow copies of these tuple to make cloudpickle payload deterministic. # When creating a code object during load, copies of these four tuples are diff --git a/tests/cloudpickle_test.py b/tests/cloudpickle_test.py index e2097d1c..a9d86421 100644 --- a/tests/cloudpickle_test.py +++ b/tests/cloudpickle_test.py @@ -4,6 +4,7 @@ import base64 import dataclasses import functools +import inspect import io import itertools import logging @@ -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 == "" + def test_dynamic_func_deterministic_roundtrip(self): # Check that the pickle serialization for a dynamic func is the same # in two processes.