From: Arthur Darcet Date: Fri, 12 Dec 2014 15:11:32 +0000 (+0100) Subject: utils.guess_filename fails if the given parameter looks like a file object but has... X-Git-Tag: v2.5.1~7^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e8d02ea0bbc05042e618a7ca115f4fca7b2deeb9;p=services%2Fpython-requests.git utils.guess_filename fails if the given parameter looks like a file object but has a non-string name attribute 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 --- diff --git a/AUTHORS.rst b/AUTHORS.rst index 71171d0..3f2a4d3 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -158,3 +158,4 @@ Patches and Suggestions - Joe Alcorn (`@buttscicles `_) - Syed Suhail Ahmed (`@syedsuhail `_) - Scott Sadler (`@ssadler `_) +- Arthur Darcet (`@arthurdarcet `_) diff --git a/requests/utils.py b/requests/utils.py index aa5c140..7467941 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -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) diff --git a/test_requests.py b/test_requests.py index 2d3ee62..68ee08c 100755 --- a/test_requests.py +++ b/test_requests.py @@ -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'