From e215484c75e3205cb7af27a5e5b58bc4552efd5d Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Fri, 30 Mar 2018 11:25:13 +0200 Subject: [PATCH] encoding: Never crash if unknown bytes in OS errors Fixes #1599 --- docs/changelog.rst | 4 ++++ mopidy/internal/encoding.py | 2 +- tests/internal/test_encoding.py | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index af4f9b04..782b6281 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -22,6 +22,10 @@ Feature release. - MPD: Added Unix domain sockets for binding MPD to. (Fixes: :issue:`1531`, PR: :issue:`1629`) +- Ensure that decoding of OS errors with unknown encoding never crashes, but + instead replaces unknown bytes with a replacement marker. (Fixes: + :issue:`1599`) + v2.1.0 (2017-01-02) =================== diff --git a/mopidy/internal/encoding.py b/mopidy/internal/encoding.py index 27506816..d7d0dbdf 100644 --- a/mopidy/internal/encoding.py +++ b/mopidy/internal/encoding.py @@ -9,4 +9,4 @@ def locale_decode(bytestr): try: return compat.text_type(bytestr) except UnicodeError: - return bytes(bytestr).decode(locale.getpreferredencoding()) + return bytes(bytestr).decode(locale.getpreferredencoding(), 'replace') diff --git a/tests/internal/test_encoding.py b/tests/internal/test_encoding.py index 98e982b8..91f17047 100644 --- a/tests/internal/test_encoding.py +++ b/tests/internal/test_encoding.py @@ -43,3 +43,10 @@ class LocaleDecodeTest(unittest.TestCase): encoding.locale_decode(b'abc') self.assertFalse(mock.called) + + def test_replaces_unknown_bytes_instead_of_crashing(self, mock): + mock.return_value = 'US-ASCII' + + result = encoding.locale_decode(b'abc\xc3def') + + assert result == 'abc\ufffddef'