Opened 12 years ago

Closed 12 years ago

#18902 closed Bug (fixed)

`force_bytes` doesn't return bytes when it's passed an exception

Reported by: Aymeric Augustin Owned by: nobody
Component: Python 3 Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I hit the following error — it's a 404 than turns into a 500 because smart_bytes raises an exception:

Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/Users/aaugustin/Documents/dev/django/django/contrib/staticfiles/handlers.py", line 71, in __call__
    return self.application(environ, start_response)
  File "/Users/aaugustin/Documents/dev/django/django/core/handlers/wsgi.py", line 237, in __call__
    response = self.get_response(request)
  File "/Users/aaugustin/Documents/dev/django/django/core/handlers/base.py", line 152, in get_response
    response = debug.technical_404_response(request, e)
  File "/Users/aaugustin/Documents/dev/django/django/views/debug.py", line 443, in technical_404_response
    'reason': smart_bytes(exception, errors='replace'),
  File "/Users/aaugustin/Documents/dev/django/django/utils/encoding.py", line 147, in smart_bytes
    return force_bytes(s, encoding, strings_only, errors)
  File "/Users/aaugustin/Documents/dev/django/django/utils/encoding.py", line 178, in force_bytes
    errors) for arg in s])
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 23: ordinal not in range(128)

The following change fixes the problem. I believe it was missing from the unicode_literals patch:

--- a/django/utils/encoding.py
+++ b/django/utils/encoding.py
@@ -174,7 +174,7 @@ def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
                 # An Exception subclass containing non-ASCII data that doesn't
                 # know how to print itself properly. We shouldn't raise a
                 # further exception.
-                return ' '.join([force_bytes(arg, encoding, strings_only,
+                return b' '.join([force_bytes(arg, encoding, strings_only,
                         errors) for arg in s])
             return six.text_type(s).encode(encoding, errors)
     else:

Change History (1)

comment:1 by Claude Paroz <claude@…>, 12 years ago

Resolution: fixed
Status: newclosed

In [cc9b767fc60a8b0455a67f00ca23077b55e709bb]:

Fixed #18902 -- Made force_bytes properly handle exception input

Thanks Aymeric Augustin for the report and the initial patch.

Note: See TracTickets for help on using tickets.
Back to Top