From fc9599d1638e6302a57c40c085894b3d22677f6d Mon Sep 17 00:00:00 2001 From: Markus Leuthold Date: Fri, 17 Nov 2017 00:08:18 +0100 Subject: [PATCH] allow binary data in OAuth1Session.request In Python 2, binary data was handled just fine, because type bytes was just a wrapper around str >>> data = bytes(1) >>> 'a' in data False In Python 3, this gives an error >>> data = bytes(1) >>> 'a' in data Traceback (most recent call last): File "", line 1, in TypeError: 'str' does not support the buffer interface Example client code: Python 3 port of https://github.com/marekrei/smuploader --- AUTHORS.md | 1 + rauth/session.py | 3 ++- tests/test_session.py | 7 +++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/AUTHORS.md b/AUTHORS.md index 61365d3..7cc6685 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -34,4 +34,5 @@ Add yourself as a contributor! * [Tatsuji Tsuchiya](https://github.com/ta2xeo) * [Chris McGraw](https://github.com/mitgr81) * [Ken Koontz](https://github.com/kennethkoontz) +* [Markus Leuthold](https://github.com/githubkusi) * (your name here) diff --git a/rauth/session.py b/rauth/session.py index b46dd1e..2bd1f00 100644 --- a/rauth/session.py +++ b/rauth/session.py @@ -225,7 +225,8 @@ def _parse_optional_params(self, oauth_params, req_kwargs): for oauth_param in OPTIONAL_OAUTH_PARAMS: if oauth_param in params: oauth_params[oauth_param] = params.pop(oauth_param) - if oauth_param in data: + + if type(data) is not bytes and oauth_param in data: oauth_params[oauth_param] = data.pop(oauth_param) if params: diff --git a/tests/test_session.py b/tests/test_session.py index 4d69722..ba9012d 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -55,6 +55,13 @@ def test_request_with_optional_data_as_string(self, mock_request): r = self.session.request('POST', 'http://example.com/', data=data) self.assert_ok(r) + @patch.object(requests.Session, 'request') + def test_request_with_optional_data_as_binary(self, mock_request): + mock_request.return_value = self.response + data = bytes(1) + r = self.session.request('POST', 'http://example.com/', data=data) + self.assert_ok(r) + @patch.object(requests.Session, 'request') def test_request_with_optional_params_with_data(self, mock_request): mock_request.return_value = self.response