diff options
| author | Claude Paroz <claude@2xlibre.net> | 2017-01-23 17:44:25 +0100 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2017-01-23 19:59:33 +0100 |
| commit | d2e7d15b4c594f64ee9d37bf40e61920cea41487 (patch) | |
| tree | c0cf7b1d036690d23e84ecb01d9b7e79ac27e828 /django | |
| parent | f0573aad4befcea969c73fa5f9a624ac22603164 (diff) | |
Assumed iri_to_uri always returns a string
Thanks Tim Graham for the review.
Diffstat (limited to 'django')
| -rw-r--r-- | django/template/defaultfilters.py | 2 | ||||
| -rw-r--r-- | django/urls/base.py | 4 | ||||
| -rw-r--r-- | django/utils/encoding.py | 16 |
3 files changed, 12 insertions, 10 deletions
diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index a85e545242..c6472bce16 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -164,7 +164,7 @@ def floatformat(text, arg=-1): @stringfilter def iriencode(value): """Escapes an IRI value for use in a URL.""" - return force_text(iri_to_uri(value)) + return iri_to_uri(value) @register.filter(is_safe=True, needs_autoescape=True) diff --git a/django/urls/base.py b/django/urls/base.py index 408bc36ead..6dccdd2e7d 100644 --- a/django/urls/base.py +++ b/django/urls/base.py @@ -1,7 +1,7 @@ from threading import local from urllib.parse import urlsplit, urlunsplit -from django.utils.encoding import force_text, iri_to_uri +from django.utils.encoding import iri_to_uri from django.utils.functional import lazy from django.utils.translation import override @@ -85,7 +85,7 @@ def reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None): if ns_pattern: resolver = get_ns_resolver(ns_pattern, resolver) - return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))) + return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)) reverse_lazy = lazy(reverse, str) diff --git a/django/utils/encoding.py b/django/utils/encoding.py index 71c2985e27..d0fd2f19e7 100644 --- a/django/utils/encoding.py +++ b/django/utils/encoding.py @@ -151,13 +151,13 @@ def iri_to_uri(iri): Convert an Internationalized Resource Identifier (IRI) portion to a URI portion that is suitable for inclusion in a URL. - This is the algorithm from section 3.1 of RFC 3987. However, since we are - assuming input is either UTF-8 or unicode already, we can simplify things a - little from the full method. + This is the algorithm from section 3.1 of RFC 3987, slightly simplified + since the input is assumed to be a string rather than an arbitrary byte + stream. - Takes an IRI in UTF-8 bytes (e.g. '/I \xe2\x99\xa5 Django/') or unicode - (e.g. '/I ♥ Django/') and returns ASCII bytes containing the encoded result - (e.g. '/I%20%E2%99%A5%20Django/'). + Take an IRI (string or UTF-8 bytes, e.g. '/I ♥ Django/' or + b'/I \xe2\x99\xa5 Django/') and return a string containing the encoded + result with ASCII chars only (e.g. '/I%20%E2%99%A5%20Django/'). """ # The list of safe characters here is constructed from the "reserved" and # "unreserved" characters specified in sections 2.2 and 2.3 of RFC 3986: @@ -173,7 +173,9 @@ def iri_to_uri(iri): # converted. if iri is None: return iri - return quote(force_bytes(iri), safe=b"/#%[]=:;$&()+,!?*@'~") + elif isinstance(iri, Promise): + iri = str(iri) + return quote(iri, safe="/#%[]=:;$&()+,!?*@'~") def uri_to_iri(uri): |
