Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #18902 -- Made force_bytes properly handle exception input

Thanks Aymeric Augustin for the report and the initial patch.
  • Loading branch information...
commit cc9b767fc60a8b0455a67f00ca23077b55e709bb 1 parent 03ad7d3
Claude Paroz authored September 04, 2012
2  django/utils/encoding.py
@@ -174,7 +174,7 @@ def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
174 174
                 # An Exception subclass containing non-ASCII data that doesn't
175 175
                 # know how to print itself properly. We shouldn't raise a
176 176
                 # further exception.
177  
-                return ' '.join([force_bytes(arg, encoding, strings_only,
  177
+                return b' '.join([force_bytes(arg, encoding, strings_only,
178 178
                         errors) for arg in s])
179 179
             return six.text_type(s).encode(encoding, errors)
180 180
     else:
17  tests/regressiontests/utils/encoding.py
... ...
@@ -0,0 +1,17 @@
  1
+# -*- encoding: utf-8 -*-
  2
+from __future__ import unicode_literals
  3
+
  4
+from django.utils import unittest
  5
+from django.utils.encoding import force_bytes
  6
+
  7
+
  8
+class TestEncodingUtils(unittest.TestCase):
  9
+    def test_force_bytes_exception(self):
  10
+        """
  11
+        Test that force_bytes knows how to convert to bytes an exception
  12
+        containing non-ASCII characters in its args.
  13
+        """
  14
+        error_msg = "This is an exception, voilà"
  15
+        exc = ValueError(error_msg)
  16
+        result = force_bytes(exc)
  17
+        self.assertEqual(result, error_msg.encode('utf-8'))
1  tests/regressiontests/utils/tests.py
@@ -13,6 +13,7 @@
13 13
 from .dateparse import DateParseTests
14 14
 from .datetime_safe import DatetimeTests
15 15
 from .decorators import DecoratorFromMiddlewareTests
  16
+from .encoding import TestEncodingUtils
16 17
 from .feedgenerator import FeedgeneratorTest
17 18
 from .functional import FunctionalTestCase
18 19
 from .html import TestUtilsHtml

0 notes on commit cc9b767

Please sign in to comment.
Something went wrong with that request. Please try again.