encoding: Never crash if unknown bytes in OS errors

Fixes #1599
This commit is contained in:
Stein Magnus Jodal 2018-03-30 11:25:13 +02:00
parent f3c9387219
commit e215484c75
3 changed files with 12 additions and 1 deletions

View File

@ -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)
===================

View File

@ -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')

View File

@ -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'