From 26619ad7b069b9113d154c11c586935ee9aaa4c4 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Fri, 3 Feb 2017 19:36:53 -0500 Subject: [PATCH] Removed an untested and broken branch in force_bytes() (refs #6353). The new test crashed in the removed branch. It's unclear if the branch has value since c6a2bd9b962af1cdf46f964589e6023046cfa8ec didn't include tests. --- django/utils/encoding.py | 13 +------------ tests/utils_tests/test_encoding.py | 4 ++-- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/django/utils/encoding.py b/django/utils/encoding.py index 7d33f58f1a..65bc1ad728 100644 --- a/django/utils/encoding.py +++ b/django/utils/encoding.py @@ -108,19 +108,8 @@ def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'): return s if isinstance(s, memoryview): return bytes(s) - if isinstance(s, Promise): + if isinstance(s, Promise) or not isinstance(s, str): return str(s).encode(encoding, errors) - if not isinstance(s, str): - try: - return str(s).encode(encoding) - except UnicodeEncodeError: - if isinstance(s, Exception): - # An Exception subclass containing non-ASCII data that doesn't - # know how to print itself properly. We shouldn't raise a - # further exception. - return b' '.join(force_bytes(arg, encoding, strings_only, errors) - for arg in s) - return str(s).encode(encoding, errors) else: return s.encode(encoding, errors) diff --git a/tests/utils_tests/test_encoding.py b/tests/utils_tests/test_encoding.py index b7ffd0afe7..c1e3323f15 100644 --- a/tests/utils_tests/test_encoding.py +++ b/tests/utils_tests/test_encoding.py @@ -42,8 +42,8 @@ class TestEncodingUtils(SimpleTestCase): """ error_msg = "This is an exception, voilĂ " exc = ValueError(error_msg) - result = force_bytes(exc) - self.assertEqual(result, error_msg.encode('utf-8')) + self.assertEqual(force_bytes(exc), error_msg.encode('utf-8')) + self.assertEqual(force_bytes(exc, encoding='ascii', errors='ignore'), b'This is an exception, voil') def test_force_bytes_strings_only(self): today = datetime.date.today()