Skip to content
Myr

Response and Request objects are pickleable.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Includes a basic test. More could be add to confirm known attributes that could cause problems.

authored by Eric Larson · committed by Eric Larson · parent a123f83 · Browse files · 2 files+44 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+44 added−1 removed

  • other:+44 added−1 removed

2 files+44 added−1 removed

modified:requests/models.py+31 added
487 unmodified lines
488 """The :class:`Response <Response>` object, which contains a489 server's response to an HTTP request.490 """491492 __attrs__ = [493 '_content',494 'status_code',495 'headers',496 'url',497 'history',498 'encoding',499 'reason',500 'cookies',501 'elapsed',502 'request',503 ]504505 def __init__(self):506 super(Response, self).__init__()507508 self._content = False
31 unmodified lines
540 #: The amount of time elapsed between sending the request541 #: and the arrival of the response (as a timedelta)542 self.elapsed = datetime.timedelta(0)543544 def __getstate__(self):545 # Consume everything; accessing the content attribute makes546 # sure the content has been fully read.547 if not self._content_consumed:548 self.content549550 return dict(551 (attr, getattr(self, attr, None))552 for attr in self.__attrs__553 )554555 def __setstate__(self, state):556 for name, value in state.items():557 setattr(self, name, value)558559 # pickled objects do not have .raw560 setattr(self, '_content_consumed', True)561562 def __repr__(self):563 return '<Response [%s]>' % (self.status_code)564565 def __bool__(self):
200 unmodified lines
modified:test_requests.py+13 added-1 removed
431 unmodified lines
432 open(filename, 'rb'))})433 prep = r.prepare()434 assert b'name="stuff"' in prep.body435 assert b'name="b\'stuff\'"' not in prep.body436 436437 def test_unicode_method_name(self):438 files = {'file': open('test_requests.py', 'rb')}439 r = requests.request(method=u'POST', url=httpbin('post'), files=files)440 assert r.status_code == 200
105 unmodified lines
546 r.raw = io547 assert next(iter(r))548 io.close()549550 def test_request_and_response_are_pickleable(self):551 r = requests.get(httpbin('get'))552553 # verify we can pickle the original request554 assert pickle.loads(pickle.dumps(r.request))555556 # verify we can pickle the response and that we have access to557 # the original request.558 pr = pickle.loads(pickle.dumps(r))559 assert r.request.url == pr.request.url560 assert r.request.headers == pr.request.headers561562 def test_get_auth_from_url(self):563 url = 'http://user:pass@complex.url.com/path?query=yes'564 assert ('user', 'pass') == requests.utils.get_auth_from_url(url)565
337 unmodified lines