From 41be85862d9067a809ccf3707d2a22dfef23d99a Mon Sep 17 00:00:00 2001 From: François Freitag Date: Mon, 9 Oct 2017 13:20:01 -0700 Subject: Fixed #28679 -- Fixed urlencode()'s handling of bytes. Regression in fee42fd99ee470528858c2ccb3621135c30ec262. Thanks Claude Paroz, Jon Dufresne, and Tim Graham for the guidance. --- django/utils/http.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'django') diff --git a/django/utils/http.py b/django/utils/http.py index c13f44602b..4fbb786d89 100644 --- a/django/utils/http.py +++ b/django/utils/http.py @@ -88,11 +88,24 @@ def urlencode(query, doseq=False): query = query.lists() elif hasattr(query, 'items'): query = query.items() - return original_urlencode( - [(k, [str(i) for i in v] if isinstance(v, (list, tuple)) else str(v)) - for k, v in query], - doseq - ) + query_params = [] + for key, value in query: + if isinstance(value, (str, bytes)): + query_val = value + else: + try: + iter(value) + except TypeError: + query_val = value + else: + # Consume generators and iterators, even when doseq=True, to + # work around https://bugs.python.org/issue31706. + query_val = [ + item if isinstance(item, bytes) else str(item) + for item in value + ] + query_params.append((key, query_val)) + return original_urlencode(query_params, doseq) def cookie_date(epoch_seconds=None): -- cgit v1.3