From 73ac9e3f04c4a1a6b1add05c207d11c8ff461b7d Mon Sep 17 00:00:00 2001 From: swatantra Date: Fri, 9 Aug 2019 13:49:18 +0530 Subject: [PATCH] Fixed #30677 -- Improved error message for urlencode() and Client when None is passed as data. --- django/test/client.py | 4 ++-- django/utils/http.py | 9 +++++---- tests/test_client/tests.py | 8 ++++---- tests/utils_tests/test_http.py | 4 ++-- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/django/test/client.py b/django/test/client.py index e8e4e0c6f80..9b3fdf59364 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -193,8 +193,8 @@ def encode_multipart(boundary, data): for (key, value) in data.items(): if value is None: raise TypeError( - 'Cannot encode None as POST data. Did you mean to pass an ' - 'empty string or omit the value?' + "Cannot encode None for key '%s' as POST data. Did you mean " + "to pass an empty string or omit the value?" % key ) elif is_file(value): lines.extend(encode_file(boundary, key, value)) diff --git a/django/utils/http.py b/django/utils/http.py index b6a78184a69..050375832cf 100644 --- a/django/utils/http.py +++ b/django/utils/http.py @@ -113,8 +113,8 @@ def urlencode(query, doseq=False): for key, value in query: if value is None: raise TypeError( - 'Cannot encode None in a query string. Did you mean to pass ' - 'an empty string or omit the value?' + "Cannot encode None for key '%s' in a query string. Did you " + "mean to pass an empty string or omit the value?" % key ) elif not doseq or isinstance(value, (str, bytes)): query_val = value @@ -130,8 +130,9 @@ def urlencode(query, doseq=False): for item in itr: if item is None: raise TypeError( - 'Cannot encode None in a query string. Did you ' - 'mean to pass an empty string or omit the value?' + "Cannot encode None for key '%s' in a query " + "string. Did you mean to pass an empty string or " + "omit the value?" % key ) elif not isinstance(item, bytes): item = str(item) diff --git a/tests/test_client/tests.py b/tests/test_client/tests.py index 84d245c05e4..ce9ce40de51 100644 --- a/tests/test_client/tests.py +++ b/tests/test_client/tests.py @@ -61,8 +61,8 @@ class ClientTest(TestCase): def test_get_data_none(self): msg = ( - 'Cannot encode None in a query string. Did you mean to pass an ' - 'empty string or omit the value?' + "Cannot encode None for key 'value' in a query string. Did you " + "mean to pass an empty string or omit the value?" ) with self.assertRaisesMessage(TypeError, msg): self.client.get('/get_view/', {'value': None}) @@ -102,8 +102,8 @@ class ClientTest(TestCase): def test_post_data_none(self): msg = ( - 'Cannot encode None as POST data. Did you mean to pass an empty ' - 'string or omit the value?' + "Cannot encode None for key 'value' as POST data. Did you mean " + "to pass an empty string or omit the value?" ) with self.assertRaisesMessage(TypeError, msg): self.client.post('/post_view/', {'value': None}) diff --git a/tests/utils_tests/test_http.py b/tests/utils_tests/test_http.py index c843bc90625..d1a1243ace9 100644 --- a/tests/utils_tests/test_http.py +++ b/tests/utils_tests/test_http.py @@ -14,8 +14,8 @@ from django.utils.http import ( class URLEncodeTests(SimpleTestCase): cannot_encode_none_msg = ( - 'Cannot encode None in a query string. Did you mean to pass an ' - 'empty string or omit the value?' + "Cannot encode None for key 'a' in a query string. Did you mean to " + "pass an empty string or omit the value?" ) def test_tuples(self):