From 5ff165a1e2b30d5568c058b11139038963b4f979 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Wed, 1 Aug 2012 10:36:45 -0400 Subject: [PATCH] Better handling of invalid files. I stole the idea from _encode_params in all candor. --- requests/models.py | 7 +++++++ tests/test_requests.py | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/requests/models.py b/requests/models.py index 379c26d..9bda749 100644 --- a/requests/models.py +++ b/requests/models.py @@ -342,8 +342,15 @@ class Request(object): try: fields = list(self.data.items()) except AttributeError: + dict(self.data) fields = list(self.data) + try: + dict(files) + except ValueError: + raise ValueError('Unable to encode lists with elements that ' + 'are not 2-tuples.') + if isinstance(files, dict): files = files.items() diff --git a/tests/test_requests.py b/tests/test_requests.py index 5521557..4e0d034 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -320,6 +320,11 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): post4 = post(url, data='[{"some": "json"}]') self.assertEqual(post4.status_code, 200) + try: + post(url, files=['bad file data']) + except ValueError: + pass + def test_POSTBIN_GET_POST_FILES_WITH_PARAMS(self): for service in SERVICES: -- 2.7.4