﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
18902	`force_bytes` doesn't return bytes when it's passed an exception	Aymeric Augustin	nobody	"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:
}}}"	Bug	closed	Python 3	dev	Normal	fixed			Unreviewed	0	0	0	0	0	0
