File tree 3 files changed +16
-2
lines changed
3 files changed +16
-2
lines changed Original file line number Diff line number Diff line change 4
4
dev
5
5
===
6
6
7
+ - Fix a side effect altering dynamic modules at pickling time.
8
+ ([ PR #426 ] ( https://github.com/cloudpipe/cloudpickle/pull/426 ) )
9
+
7
10
- Support for pickling type annotations on Python 3.10 as per [ PEP 563] (
8
11
https://www.python.org/dev/peps/pep-0563/ )
9
12
([ PR #400 ] ( https://github.com/cloudpipe/cloudpickle/pull/400 ) )
Original file line number Diff line number Diff line change @@ -342,8 +342,13 @@ def _module_reduce(obj):
342
342
if _is_importable (obj ):
343
343
return subimport , (obj .__name__ ,)
344
344
else :
345
- obj .__dict__ .pop ('__builtins__' , None )
346
- return dynamic_subimport , (obj .__name__ , vars (obj ))
345
+ # Some external libraries can populate the "__builtins__" entry of a
346
+ # module's `__dict__` with unpicklable objects (see #316). For that
347
+ # reason, we do not attempt to pickle the "__builtins__" entry, and
348
+ # restore a default value for it at unpickling time.
349
+ state = obj .__dict__ .copy ()
350
+ state .pop ('__builtins__' , None )
351
+ return dynamic_subimport , (obj .__name__ , state )
347
352
348
353
349
354
def _method_reduce (obj ):
Original file line number Diff line number Diff line change @@ -604,6 +604,12 @@ def __reduce__(self):
604
604
assert hasattr (depickled_mod .__builtins__ , "abs" )
605
605
assert depickled_mod .f (- 1 ) == 1
606
606
607
+ # Additional check testing that the issue #425 is fixed: without the
608
+ # fix for #425, `mod.f` would not have access to `__builtins__`, and
609
+ # thus calling `mod.f(-1)` (which relies on the `abs` builtin) would
610
+ # fail.
611
+ assert mod .f (- 1 ) == 1
612
+
607
613
def test_load_dynamic_module_in_grandchild_process (self ):
608
614
# Make sure that when loaded, a dynamic module preserves its dynamic
609
615
# property. Otherwise, this will lead to an ImportError if pickled in
You can’t perform that action at this time.
0 commit comments