Jon Dufresne
unread,Sep 18, 2014, 4:01:21 PM9/18/14Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django-d...@googlegroups.com
Hi,
In my Django application, I'm making a strong attempt to always deal
with Unicode strings at the application layer. Byte strings are only
handled at the protocol layer -- sending data out on the "wire". If my
application tests if an object is a string, I'll use isinstance(obj,
unicode) (Python2).
One gotcha that I noticed is that reverse() will always return a byte
string. Tracing this through the code, I see this happens during the
call to iri_to_uri(), as this function creates a string consisting
only of ASCII characters, other characters are escaped.
Now, reverse() is often used to grab a URL and handle it at the
application layer. It is not reserved only for the protocol layer. An
example would be presenting a URL inside a HTML template, (as an href
or as text), mail, or JSON.
In my opinion, reverse() should return a Unicode string, even if that
string consists only of ASCII characters. It is not until the string
hits the wire that it ought to be forced to bytes.
To verify this, I have created a unit test that I placed in
"urlpatterns_reverse.tests.URLPatternReverse" to demonstrate this is
at the Django layer.
def test_reverse_unicode(self):
name, expected, args, kwargs = test_data[0]
self.assertIsInstance(
reverse(name, args=args, kwargs=kwargs),
six.text_type)
What do you think? If others agree, I can file a bug and create a pull
request to fix this.
Thanks,
Jon