File tree 3 files changed +33
-0
lines changed
3 files changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ 0.9.0
2
+ =====
3
+
4
+ - Fix a bug making functions with keyword-only arguments forget the default
5
+ values of these arguments after being pickled.
6
+ ([ issue #264 ] ( https://github.com/cloudpipe/cloudpickle/pull/264 ) )
7
+
1
8
0.8.1
2
9
=====
3
10
Original file line number Diff line number Diff line change @@ -591,6 +591,8 @@ def save_function_tuple(self, func):
591
591
state ['annotations' ] = func .__annotations__
592
592
if hasattr (func , '__qualname__' ):
593
593
state ['qualname' ] = func .__qualname__
594
+ if hasattr (func , '__kwdefaults__' ):
595
+ state ['kwdefaults' ] = func .__kwdefaults__
594
596
save (state )
595
597
write (pickle .TUPLE )
596
598
write (pickle .REDUCE ) # applies _fill_function on the tuple
@@ -1075,6 +1077,8 @@ def _fill_function(*args):
1075
1077
func .__module__ = state ['module' ]
1076
1078
if 'qualname' in state :
1077
1079
func .__qualname__ = state ['qualname' ]
1080
+ if 'kwdefaults' in state :
1081
+ func .__kwdefaults__ = state ['kwdefaults' ]
1078
1082
1079
1083
cells = func .__closure__
1080
1084
if cells is not None :
Original file line number Diff line number Diff line change @@ -1392,6 +1392,28 @@ def g():
1392
1392
cloned_func = pickle_depickle (func , protocol = self .protocol )
1393
1393
assert cloned_func () == "hello from a {}!" .format (source )
1394
1394
1395
+ @pytest .mark .skipif (sys .version_info [0 ] < 3 ,
1396
+ reason = "keyword only arguments were introduced in "
1397
+ "python 3" )
1398
+ def test_interactively_defined_func_with_keyword_only_argument (self ):
1399
+ # fixes https://github.com/cloudpipe/cloudpickle/issues/263
1400
+ # The source code of this test is bundled in a string and is ran from
1401
+ # the __main__ module of a subprocess in order to avoid a SyntaxError
1402
+ # in python2 when pytest imports this file, as the keyword-only syntax
1403
+ # is python3-only.
1404
+ code = """
1405
+ from cloudpickle import loads, dumps
1406
+
1407
+ def f(a, *, b=1):
1408
+ return a + b
1409
+
1410
+ depickled_f = loads(dumps(f, protocol={protocol}))
1411
+
1412
+ for func in (f, depickled_f):
1413
+ assert func(2) == 3
1414
+ assert func.__kwdefaults__ == {{'b': 1}}
1415
+ """ .format (protocol = self .protocol )
1416
+ assert_run_python_script (textwrap .dedent (code ))
1395
1417
1396
1418
class Protocol2CloudPickleTest (CloudPickleTest ):
1397
1419
You can’t perform that action at this time.
0 commit comments