Skip to content

Commit ad7c151

Browse files
authored
Move non-dill specific code out of dill_pickler.py (#35139)
* Create consistent_pickle.py * Update dill_pickler.py * Update consistent_pickle.py * Add Apache license to consistent_pickle.py * Update dill_pickler.py * Update and rename consistent_pickle.py to code_object_pickler.py * Update dill_pickler.py to change consistent_pickle to code_object_pickler * Fix formatting issue in code_object_pickler.py * Fix formatting issue in dill_pickler.py * Remove apache license from code_object_pickler.py * Add apache license to code_object_pickler.py
1 parent 17d9823 commit ad7c151

2 files changed

Lines changed: 47 additions & 6 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
import os
19+
import sys
20+
21+
22+
def get_relative_path(path):
23+
"""Returns the path of filename relative to the first directory in sys.path
24+
contained in filename. Returns the unchanged filename if it is not in any
25+
sys.path directory.
26+
27+
Args:
28+
path: The path to the file.
29+
"""
30+
for dir_path in sys.path:
31+
# The path for /aaa/bbb/c.py is relative to /aaa/bbb and not /aaa/bb.
32+
if not dir_path.endswith(os.path.sep):
33+
dir_path += os.path.sep
34+
if path.startswith(dir_path):
35+
return os.path.relpath(path, dir_path)
36+
return path
37+
38+
39+
def get_normalized_path(path):
40+
"""Returns a normalized path. This function is intended to be overridden."""
41+
# Use relative paths to make pickling lambdas deterministic for google3
42+
# This is needed only for code running inside Google on borg.
43+
if '/borglet/' in path:
44+
return get_relative_path(path)
45+
46+
return path

sdks/python/apache_beam/internal/dill_pickler.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,14 @@
4444

4545
import dill
4646

47+
from apache_beam.internal.code_object_pickler import get_normalized_path
4748
from apache_beam.internal.set_pickler import save_frozenset
4849
from apache_beam.internal.set_pickler import save_set
4950

5051
settings = {'dill_byref': None}
5152

5253
patch_save_code = sys.version_info >= (3, 10) and dill.__version__ == "0.3.1.1"
5354

54-
55-
def get_normalized_path(path):
56-
"""Returns a normalized path. This function is intended to be overridden."""
57-
return path
58-
59-
6055
if patch_save_code:
6156
# The following function is based on 'save_code' from 'dill'
6257
# Author: Mike McKerns (mmckerns @caltech and @uqfoundation)

0 commit comments

Comments
 (0)