Skip to content
Myr

Use BytesIO for bytes.

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

This fixes a TypeError on Python 3 that ocurred when passing bytes as the values for files.

authored by Jakub Roztocil · committed by Jakub Roztocil · parent cfa627a · Browse files · 2 files+11 added−3 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+11 added−3 removed

  • tests:+7 added−2 removed
  • other:+4 added−1 removed

2 files+11 added−3 removed

modified:requests/models.py+4 added-1 removed
8 unmodified lines
910import os11import socket12from datetime import datetime13from io import BytesIO1415from .hooks import dispatch_hook, HOOKS16from .structures import CaseInsensitiveDict17from .status_codes import codes
359 unmodified lines
377 fn, fp = v378 else:379 fn = guess_filename(v) or k380 fp = v380 if isinstance(fp, (bytes, str)):381 if isinstance(fp, str):382 fp = StringIO(fp)383 if isinstance(fp, bytes):384 fp = BytesIO(fp)385 fields.append((k, (fn, fp.read())))386387 for k, vs in tuples(self.data):388 if isinstance(vs, list):
494 unmodified lines
modified:tests/test_requests.py+7 added-2 removed
5 unmodified lines
6# Path hack.7import sys8import os9sys.path.insert(0, os.path.abspath('..'))1010import json11import os12import unittest13import pickle
1014 unmodified lines
1028 self.assertEqual(r.status_code, 200)10291030 def test_post_fields_with_multiple_values_and_files_as_tuples(self):1031 """Test that it is possible to POST multiple data and file fields1033 with the same name."""1032 with the same name.1033 https://github.com/kennethreitz/requests/pull/7461034 """10351036 data = [1037 ('__field__', '__value__'),1038 ('__field__', '__value__'),
22 unmodified lines
1061 ' name="__field__"; filename="__field__"')1062 self.assertEqual(body.count('__value__'), 4)1063 self.assertEqual(body.count(file_field), 2)10641065 def test_bytes_files(self):1066 """Test that `bytes` can be used as the values of `files`."""1067 post(httpbin('post'), files={'test': b'test'})106810691070if __name__ == '__main__':1071 unittest.main()