From: Jakub Roztocil Date: Mon, 30 Jul 2012 08:35:47 +0000 (+0200) Subject: Use BytesIO for bytes. X-Git-Tag: v0.13.7~9^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=dee3693ea004e2f859e4fba4cfedb376e4d0bb2b;p=services%2Fpython-requests.git Use BytesIO for bytes. This fixes a TypeError on Python 3 that ocurred when passing bytes as the values for files. --- diff --git a/requests/models.py b/requests/models.py index 1a74dc2..136427f 100644 --- a/requests/models.py +++ b/requests/models.py @@ -10,6 +10,7 @@ This module contains the primary objects that power Requests. import os import socket from datetime import datetime +from io import BytesIO from .hooks import dispatch_hook, HOOKS from .structures import CaseInsensitiveDict @@ -377,8 +378,10 @@ class Request(object): else: fn = guess_filename(v) or k fp = v - if isinstance(fp, (bytes, str)): + if isinstance(fp, str): fp = StringIO(fp) + if isinstance(fp, bytes): + fp = BytesIO(fp) fields.append((k, (fn, fp.read()))) for k, vs in tuples(self.data): diff --git a/tests/test_requests.py b/tests/test_requests.py index abc57e1..e196af4 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -7,7 +7,6 @@ import sys import os sys.path.insert(0, os.path.abspath('..')) - import json import os import unittest @@ -1030,7 +1029,9 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): def test_post_fields_with_multiple_values_and_files_as_tuples(self): """Test that it is possible to POST multiple data and file fields - with the same name.""" + with the same name. + https://github.com/kennethreitz/requests/pull/746 + """ data = [ ('__field__', '__value__'), @@ -1061,6 +1062,10 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): self.assertEqual(body.count('__value__'), 4) self.assertEqual(body.count(file_field), 2) + def test_bytes_files(self): + """Test that `bytes` can be used as the values of `files`.""" + post(httpbin('post'), files={'test': b'test'}) + if __name__ == '__main__': unittest.main()