Skip to content

Commit b4660a8

Browse files
changes to uses path_normalization with file_interceptor in CONFIG
1 parent 1a4298f commit b4660a8

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

sdks/python/apache_beam/internal/cloudpickle_pickler.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@
3737

3838
from apache_beam.internal import code_object_pickler
3939
from apache_beam.internal.cloudpickle import cloudpickle
40+
from apache_beam.internal.code_object_pickler import get_normalized_path
4041

4142
DEFAULT_CONFIG = cloudpickle.CloudPickleConfig(
42-
skip_reset_dynamic_type_state=True)
43+
skip_reset_dynamic_type_state=True, filepath_interceptor=get_normalized_path)
4344
NO_DYNAMIC_CLASS_TRACKING_CONFIG = cloudpickle.CloudPickleConfig(
44-
id_generator=None, skip_reset_dynamic_type_state=True)
45+
id_generator=None, skip_reset_dynamic_type_state=True, filepath_interceptor=get_normalized_path)
4546
STABLE_CODE_IDENTIFIER_CONFIG = cloudpickle.CloudPickleConfig(
46-
skip_reset_dynamic_type_state=True,
47+
skip_reset_dynamic_type_state=True, filepath_interceptor=get_normalized_path,
4748
get_code_object_params=cloudpickle.GetCodeObjectParams(
4849
get_code_object_identifier=code_object_pickler.
4950
get_code_object_identifier,

sdks/python/apache_beam/internal/cloudpickle_pickler_test.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919

2020
# pytype: skip-file
2121

22+
import os
2223
import threading
2324
import types
2425
import unittest
2526

2627
from apache_beam.coders import proto2_coder_test_messages_pb2
28+
from apache_beam.internal import cloudpickle_pickler as beam_cloudpickle
29+
from apache_beam.internal import code_object_pickler
2730
from apache_beam.internal import module_test
2831
from apache_beam.internal.cloudpickle_pickler import dumps
2932
from apache_beam.internal.cloudpickle_pickler import loads
@@ -220,6 +223,36 @@ def test_best_effort_determinism_not_implemented(self):
220223
'Ignoring unsupported option: enable_best_effort_determinism',
221224
'\n'.join(l.output))
222225

226+
@unittest.mock.patch.object(
227+
code_object_pickler,
228+
'get_normalized_path',
229+
wraps=code_object_pickler.get_normalized_path)
230+
def test_default_config_interceptor(self, mock_get_normalized_path):
231+
"""Tests config.filepath_interceptor is called for CodeType pickling."""
232+
233+
def sample_func():
234+
return "Beam"
235+
236+
code_obj = sample_func.__code__
237+
original_filename = os.path.abspath(code_obj.co_filename)
238+
239+
try:
240+
pickled_code = beam_cloudpickle.dumps(code_obj)
241+
unpickled_code = beam_cloudpickle.loads(pickled_code)
242+
243+
mock_get_normalized_path.assert_called()
244+
245+
unpickled_filename = os.path.abspath(unpickled_code.co_filename)
246+
self.assertEqual(unpickled_filename, original_filename)
247+
248+
except AttributeError as e:
249+
if 'get_code_object_params' in str(e):
250+
self.fail(
251+
"Vendored cloudpickle BUG: AttributeError 'get_code_object_params' "
252+
f"raised during CodeType pickling. Error: {e}")
253+
else:
254+
raise
255+
223256

224257
if __name__ == '__main__':
225258
unittest.main()

0 commit comments

Comments
 (0)