Skip to content
Myr

Merge pull request #1733 from ionrock/pickleable-responses

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

Response and Request objects are pickleable.

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 248c9ec, 512beb8 · 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
492 unmodified lines
493 """The :class:`Response <Response>` object, which contains a494 server's response to an HTTP request.495 """496497 __attrs__ = [498 '_content',499 'status_code',500 'headers',501 'url',502 'history',503 'encoding',504 'reason',505 'cookies',506 'elapsed',507 'request',508 ]509510 def __init__(self):511 super(Response, self).__init__()512513 self._content = False
31 unmodified lines
545 #: The amount of time elapsed between sending the request546 #: and the arrival of the response (as a timedelta)547 self.elapsed = datetime.timedelta(0)548549 def __getstate__(self):550 # Consume everything; accessing the content attribute makes551 # sure the content has been fully read.552 if not self._content_consumed:553 self.content554555 return dict(556 (attr, getattr(self, attr, None))557 for attr in self.__attrs__558 )559560 def __setstate__(self, state):561 for name, value in state.items():562 setattr(self, name, value)563564 # pickled objects do not have .raw565 setattr(self, '_content_consumed', True)566567 def __repr__(self):568 return '<Response [%s]>' % (self.status_code)569570 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
349 unmodified lines