@@ -381,6 +381,25 @@ def dumps(
381381 use_zlib = False ,
382382 enable_best_effort_determinism = False ) -> bytes :
383383 """For internal use only; no backwards-compatibility guarantees."""
384+ s = _dumps (o , enable_trace , enable_best_effort_determinism )
385+
386+ # Compress as compactly as possible (compresslevel=9) to decrease peak memory
387+ # usage (of multiple in-memory copies) and to avoid hitting protocol buffer
388+ # limits.
389+ # WARNING: Be cautious about compressor change since it can lead to pipeline
390+ # representation change, and can break streaming job update compatibility on
391+ # runners such as Dataflow.
392+ if use_zlib :
393+ c = zlib .compress (s , 9 )
394+ else :
395+ c = bz2 .compress (s , compresslevel = 9 )
396+ del s # Free up some possibly large and no-longer-needed memory.
397+
398+ return base64 .b64encode (c )
399+
400+
401+ def _dumps (o , enable_trace = True , enable_best_effort_determinism = False ) -> bytes :
402+ """For internal use only; no backwards-compatibility guarantees."""
384403 with _pickle_lock :
385404 if enable_best_effort_determinism :
386405 old_save_set = dill .dill .Pickler .dispatch [set ]
@@ -400,20 +419,7 @@ def dumps(
400419 if enable_best_effort_determinism :
401420 dill .dill .pickle (set , old_save_set )
402421 dill .dill .pickle (frozenset , old_save_frozenset )
403-
404- # Compress as compactly as possible (compresslevel=9) to decrease peak memory
405- # usage (of multiple in-memory copies) and to avoid hitting protocol buffer
406- # limits.
407- # WARNING: Be cautious about compressor change since it can lead to pipeline
408- # representation change, and can break streaming job update compatibility on
409- # runners such as Dataflow.
410- if use_zlib :
411- c = zlib .compress (s , 9 )
412- else :
413- c = bz2 .compress (s , compresslevel = 9 )
414- del s # Free up some possibly large and no-longer-needed memory.
415-
416- return base64 .b64encode (c )
422+ return s
417423
418424
419425def loads (encoded , enable_trace = True , use_zlib = False ):
@@ -427,7 +433,10 @@ def loads(encoded, enable_trace=True, use_zlib=False):
427433 s = bz2 .decompress (c )
428434
429435 del c # Free up some possibly large and no-longer-needed memory.
436+ return _loads (s , enable_trace )
437+
430438
439+ def _loads (s , enable_trace = True ):
431440 with _pickle_lock :
432441 try :
433442 return dill .loads (s )
@@ -441,6 +450,11 @@ def loads(encoded, enable_trace=True, use_zlib=False):
441450 dill .dill ._trace (False ) # pylint: disable=protected-access
442451
443452
453+ def roundtrip (o ):
454+ """Internal utility for testing round-trip pickle serialization."""
455+ return _loads (_dumps (o ))
456+
457+
444458def dump_session (file_path ):
445459 """For internal use only; no backwards-compatibility guarantees.
446460
0 commit comments