Skip to content
Merged
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
46 changes: 46 additions & 0 deletions sdks/python/apache_beam/internal/code_object_pickler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os
import sys


def get_relative_path(path):
"""Returns the path of filename relative to the first directory in sys.path
contained in filename. Returns the unchanged filename if it is not in any
sys.path directory.

Args:
path: The path to the file.
"""
for dir_path in sys.path:
# The path for /aaa/bbb/c.py is relative to /aaa/bbb and not /aaa/bb.
if not dir_path.endswith(os.path.sep):
dir_path += os.path.sep
if path.startswith(dir_path):
return os.path.relpath(path, dir_path)
return path


def get_normalized_path(path):
"""Returns a normalized path. This function is intended to be overridden."""
# Use relative paths to make pickling lambdas deterministic for google3
# This is needed only for code running inside Google on borg.
if '/borglet/' in path:
return get_relative_path(path)

return path
7 changes: 1 addition & 6 deletions sdks/python/apache_beam/internal/dill_pickler.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,14 @@

import dill

from apache_beam.internal.code_object_pickler import get_normalized_path
from apache_beam.internal.set_pickler import save_frozenset
from apache_beam.internal.set_pickler import save_set

settings = {'dill_byref': None}

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


def get_normalized_path(path):
"""Returns a normalized path. This function is intended to be overridden."""
return path


if patch_save_code:
# The following function is based on 'save_code' from 'dill'
# Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
Expand Down
Loading