Skip to content
Myr

Flip conditional in session.send()

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

Previously we checked that the `request` being sent was an instance of a PreparedRequest. If a user somehow created a PreparedRequest using a different Requests library instance, this check makes the request un-sendable. (This happened recently - unbeknownst to me, my server was running an outdated version of pip, vulnerable to this issue - pypa/pip#1489, which creates multiple subdirectories (src/requests, src/requests/requests) when you rerun pip install --target. So the PreparedRequest was being created in one version of the library and compared against the other version of the library, and throwing this exception, even though they were both PreparedRequest instances!) It would probably be preferable to check the object's behavior (instead of its type), but a PreparedRequest has a lot of behavior, and it wouldn't be really feasible or allow us to provide a helpful error message to check all of it here. Instead flip the conditional to guard against the user sending an unprepared Request, which should still give us most of the benefits of the better error message. Fixes #3102

authored by Kevin Burke · committed by Kevin Burke · parent aa1c3ad · Browse files · 2 files+9 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+9 added−1 removed

  • tests:+8 added−0 removed
  • other:+1 added−1 removed

2 files+9 added−1 removed

modified:requests/sessions.py+1 added-1 removed
555 unmodified lines
556 kwargs.setdefault('proxies', self.proxies)557558 # It's possible that users might accidentally send a Request object.559 # Guard against that specific failure case.560 if not isinstance(request, PreparedRequest):560 if isinstance(request, Request):561 raise ValueError('You can only send PreparedRequests.')562563 # Set up variables needed for resolve_redirects and dispatching of hooks564 allow_redirects = kwargs.pop('allow_redirects', True)
125 unmodified lines
modified:tests/test_requests.py+8 added
636 unmodified lines
637638 resp = s.send(prep)639 assert resp.status_code == 200640641 def test_non_prepared_request_error(self):642 s = requests.Session()643 req = requests.Request(u('POST'), '/')644645 with pytest.raises(ValueError) as e:646 s.send(req)647 assert str(e.value) == 'You can only send PreparedRequests.'648649 def test_custom_content_type(self, httpbin):650 r = requests.post(651 httpbin('post'),652 data={'stuff': json.dumps({'a': 123})},
944 unmodified lines