File tree 2 files changed +50
-0
lines changed
2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change @@ -63,6 +63,9 @@ class PipelineContext(dict):
63
63
:param transport: The HTTP transport type.
64
64
:param kwargs: Developer-defined keyword arguments.
65
65
"""
66
+ _PICKLE_CONTEXT = {
67
+ 'deserialized_data'
68
+ }
66
69
67
70
def __init__ (self , transport , ** kwargs ): # pylint: disable=super-init-not-called
68
71
self .transport = transport
@@ -75,6 +78,21 @@ def __getstate__(self):
75
78
del state ['transport' ]
76
79
return state
77
80
81
+ def __reduce__ (self ):
82
+ reduced = super (PipelineContext , self ).__reduce__ ()
83
+ saved_context = {}
84
+ for key , value in self .items ():
85
+ if key in self ._PICKLE_CONTEXT :
86
+ saved_context [key ] = value
87
+ # 1 is for from __reduce__ spec of pickle (generic args for recreation)
88
+ # 2 is how dict is implementing __reduce__ (dict specific)
89
+ # tuple are read-only, we use a list in the meantime
90
+ reduced = list (reduced )
91
+ dict_reduced_result = list (reduced [1 ])
92
+ dict_reduced_result [2 ] = saved_context
93
+ reduced [1 ] = tuple (dict_reduced_result )
94
+ return tuple (reduced )
95
+
78
96
def __setstate__ (self , state ):
79
97
self .__dict__ .update (state )
80
98
# Re-create the unpickable entries
Original file line number Diff line number Diff line change 25
25
#
26
26
#--------------------------------------------------------------------------
27
27
import logging
28
+ import pickle
28
29
try :
29
30
from unittest import mock
30
31
except ImportError :
56
57
HTTPPolicy ,
57
58
)
58
59
60
+ def test_pipeline_context ():
61
+ kwargs = {
62
+ 'stream' :True ,
63
+ 'cont_token' :"bla"
64
+ }
65
+ context = PipelineContext ('transport' , ** kwargs )
66
+ context ['foo' ] = 'bar'
67
+ context ['xyz' ] = '123'
68
+ context ['deserialized_data' ] = 'marvelous'
69
+
70
+ assert context ['foo' ] == 'bar'
71
+ assert context .options == kwargs
72
+
73
+ with pytest .raises (TypeError ):
74
+ context .clear ()
75
+
76
+ with pytest .raises (TypeError ):
77
+ context .update ({})
78
+
79
+ assert context .pop ('foo' ) == 'bar'
80
+ assert 'foo' not in context
81
+
82
+ serialized = pickle .dumps (context )
83
+
84
+ revived_context = pickle .loads (serialized )
85
+ assert revived_context .options == kwargs
86
+ assert revived_context .transport is None
87
+ assert 'deserialized_data' in revived_context
88
+ assert len (revived_context ) == 1
89
+
90
+
59
91
def test_request_history ():
60
92
class Non_deep_copiable (object ):
61
93
def __deepcopy__ (self , memodict = {}):
You can’t perform that action at this time.
0 commit comments