File tree Expand file tree Collapse file tree 2 files changed +37
-5
lines changed Expand file tree Collapse file tree 2 files changed +37
-5
lines changed Original file line number Diff line number Diff line change 4
4
class HTTPError (Exception ):
5
5
""" Base of all other errors"""
6
6
7
- def __init__ (self , error ):
8
- self .status_code = error .code
9
- self .reason = error .reason
10
- self .body = error .read ()
11
- self .headers = error .hdrs
7
+ def __init__ (self , * args ):
8
+ if len (args ) == 4 :
9
+ self .status_code = args [0 ]
10
+ self .reason = args [1 ]
11
+ self .body = args [2 ]
12
+ self .headers = args [3 ]
13
+ else :
14
+ self .status_code = args [0 ].code
15
+ self .reason = args [0 ].reason
16
+ self .body = args [0 ].read ()
17
+ self .headers = args [0 ].hdrs
18
+
19
+ def __reduce__ (self ):
20
+ return (
21
+ HTTPError ,
22
+ (self .status_code , self .reason , self .body , self .headers )
23
+ )
12
24
13
25
@property
14
26
def to_dict (self ):
Original file line number Diff line number Diff line change 7
7
BadRequestsError ,
8
8
NotFoundError ,
9
9
ServiceUnavailableError ,
10
+ UnauthorizedError ,
10
11
UnsupportedMediaTypeError ,
11
12
)
12
13
@@ -208,6 +209,25 @@ def test_client_pickle_unpickle(self):
208
209
"original client and unpickled client must have the same state"
209
210
)
210
211
212
+ @mock .patch ('python_http_client.client.urllib' )
213
+ def test_pickle_error (self , mock_lib ):
214
+ mock_opener = MockOpener ()
215
+ mock_lib .build_opener .return_value = mock_opener
216
+
217
+ client = self .client .__getattr__ ('hello' )
218
+
219
+ mock_opener .response_code = 401
220
+ try :
221
+ client .get ()
222
+ except UnauthorizedError as e :
223
+ pickled_error = pickle .dumps (e )
224
+ unpickled_error = pickle .loads (pickled_error )
225
+ self .assertDictEqual (
226
+ e .__dict__ ,
227
+ unpickled_error .__dict__ ,
228
+ "original error and unpickled error must have the same state"
229
+ )
230
+
211
231
212
232
if __name__ == '__main__' :
213
233
unittest .main ()
You can’t perform that action at this time.
0 commit comments