utils.guess_filename fails if the given parameter looks like a file object but has...
authorArthur Darcet <arthur.darcet@m4x.org>
Fri, 12 Dec 2014 15:11:32 +0000 (16:11 +0100)
committerArthur Darcet <arthur.darcet@m4x.org>
Fri, 12 Dec 2014 16:32:43 +0000 (17:32 +0100)
e.g. a cherrypy uploaded file behave like a regular file, except that its name attribute is an int and passing it directly to requests fails because of that

AUTHORS.rst
requests/utils.py
test_requests.py

index 71171d0821b124280fbba6d68ae37633d545c78f..3f2a4d308a171330480728c5dd72e366ee1e2241 100644 (file)
@@ -158,3 +158,4 @@ Patches and Suggestions
 - Joe Alcorn (`@buttscicles <https://github.com/buttscicles>`_)
 - Syed Suhail Ahmed <ssuhail.ahmed93@gmail.com> (`@syedsuhail <https://github.com/syedsuhail>`_)
 - Scott Sadler (`@ssadler <https://github.com/ssadler>`_)
+- Arthur Darcet (`@arthurdarcet <https://github.com/arthurdarcet>`_)
index aa5c140e58e4c24287ef3099ed6c78b02fe2a73a..74679414471b02a8bd6e806dc2a6e47f4a9c18d3 100644 (file)
@@ -115,7 +115,7 @@ def get_netrc_auth(url):
 def guess_filename(obj):
     """Tries to guess the filename of the given object."""
     name = getattr(obj, 'name', None)
-    if name and name[0] != '<' and name[-1] != '>':
+    if name and isinstance(name, builtin_str) and name[0] != '<' and name[-1] != '>':
         return os.path.basename(name)
 
 
index 2d3ee628c5dfe94de1e1fbcc8ba0622591230bea..68ee08c56f2382da2817ac81ed6c7de936b7a98d 100755 (executable)
@@ -928,6 +928,14 @@ class RequestsTestCase(unittest.TestCase):
 
         assert 'multipart/form-data' in p.headers['Content-Type']
 
+    def test_can_send_file_object_with_non_string_filename(self):
+        f = io.BytesIO()
+        f.name = 2
+        r = requests.Request('POST', httpbin('post'), files={'f': f})
+        p = r.prepare()
+
+        assert 'multipart/form-data' in p.headers['Content-Type']
+
     def test_autoset_header_values_are_native(self):
         data = 'this is a string'
         length = '16'