forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpickler.py
More file actions
131 lines (98 loc) · 4.22 KB
/
Copy pathpickler.py
File metadata and controls
131 lines (98 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#
# 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.
#
"""Pickler for values, functions, and classes.
For internal use only. No backwards compatibility guarantees.
Pickles created by the pickling library contain non-ASCII characters, so
we base64-encode the results so that we can put them in a JSON objects.
The pickler is used to embed FlatMap callable objects into the workflow JSON
description.
The pickler module should be used to pickle functions and modules; for values,
the coders.*PickleCoder classes should be used instead.
"""
from apache_beam.internal import cloudpickle_pickler
try:
from apache_beam.internal import dill_pickler
except ImportError:
dill_pickler = None # type: ignore[assignment]
USE_CLOUDPICKLE = 'cloudpickle'
USE_DILL = 'dill'
USE_DILL_UNSAFE = 'dill_unsafe'
DEFAULT_PICKLE_LIB = USE_CLOUDPICKLE
desired_pickle_lib = cloudpickle_pickler
def dumps(
o,
enable_trace=True,
use_zlib=False,
enable_best_effort_determinism=False,
enable_stable_code_identifier_pickling=False) -> bytes:
if (desired_pickle_lib == cloudpickle_pickler):
return cloudpickle_pickler.dumps(
o,
enable_trace=enable_trace,
use_zlib=use_zlib,
enable_best_effort_determinism=enable_best_effort_determinism,
enable_stable_code_identifier_pickling=
enable_stable_code_identifier_pickling,
)
return desired_pickle_lib.dumps(
o,
enable_trace=enable_trace,
use_zlib=use_zlib,
enable_best_effort_determinism=enable_best_effort_determinism)
def loads(encoded, enable_trace=True, use_zlib=False):
"""For internal use only; no backwards-compatibility guarantees."""
return desired_pickle_lib.loads(
encoded, enable_trace=enable_trace, use_zlib=use_zlib)
def roundtrip(o):
"""Internal utility for testing round-trip pickle serialization."""
return desired_pickle_lib.roundtrip(o)
def dump_session(file_path):
"""For internal use only; no backwards-compatibility guarantees.
Pickle the current python session to be used in the worker.
"""
return desired_pickle_lib.dump_session(file_path)
def load_session(file_path):
return desired_pickle_lib.load_session(file_path)
def is_currently_dill():
return desired_pickle_lib == dill_pickler
def is_currently_cloudpickle():
return desired_pickle_lib == cloudpickle_pickler
def set_library(selected_library=DEFAULT_PICKLE_LIB):
""" Sets pickle library that will be used. """
global desired_pickle_lib
if selected_library == 'default':
selected_library = DEFAULT_PICKLE_LIB
if selected_library == USE_DILL and not dill_pickler:
raise ImportError(
"Pipeline option pickle_library=dill is set, but dill is not "
"installed. Install apache-beam with the dill extras package "
"e.g. apache-beam[dill].")
if selected_library == USE_DILL_UNSAFE and not dill_pickler:
raise ImportError(
"Pipeline option pickle_library=dill_unsafe is set, but dill is not "
"installed. Install dill in job submission and runtime environments.")
dill_is_requested = (
selected_library == USE_DILL or selected_library == USE_DILL_UNSAFE)
# If switching to or from dill, update the pickler hook overrides.
if is_currently_dill() != dill_is_requested:
dill_pickler.override_pickler_hooks(selected_library == USE_DILL)
if dill_is_requested:
desired_pickle_lib = dill_pickler
elif selected_library == USE_CLOUDPICKLE:
desired_pickle_lib = cloudpickle_pickler
else:
raise ValueError(f'Unknown pickler library: {selected_library}')