diff --git a/Languages/IronPython/IronPython.Modules/_codecs.cs b/Languages/IronPython/IronPython.Modules/_codecs.cs index 5985a417ad..2c5d037942 100644 --- a/Languages/IronPython/IronPython.Modules/_codecs.cs +++ b/Languages/IronPython/IronPython.Modules/_codecs.cs @@ -639,12 +639,12 @@ private static PythonTuple DoDecode(Encoding encoding, object input, string erro } #if FEATURE_ENCODING // DecoderFallback - var utf8Workaround = encoding == Encoding.UTF8 && DotNet; encoding = (Encoding)encoding.Clone(); ExceptionFallBack fallback = null; if (fAlwaysThrow) { encoding.DecoderFallback = DecoderFallback.ExceptionFallback; } else { + var utf8Workaround = encoding == Encoding.UTF8 && DotNet; fallback = new ExceptionFallBack(bytes, utf8Workaround); encoding.DecoderFallback = fallback; } diff --git a/Languages/IronPython/Tests/modules/io_related/codecs_test.py b/Languages/IronPython/Tests/modules/io_related/codecs_test.py index 990ce01679..07f41e70af 100644 --- a/Languages/IronPython/Tests/modules/io_related/codecs_test.py +++ b/Languages/IronPython/Tests/modules/io_related/codecs_test.py @@ -347,6 +347,26 @@ def test_utf_8_decode(): AreEqual(new_str, u'abc') AreEqual(size, 3) + +def test_cp34951(): + def internal_cp34951(sample1): + AreEqual(codecs.utf_8_decode(sample1), (u'12\u20ac\x0a', 6)) + sample1 = sample1[:-1] # 12 + AreEqual(codecs.utf_8_decode(sample1), (u'12\u20ac', 5)) + sample1 = sample1[:-1] # 12 + AreEqual(codecs.utf_8_decode(sample1), (u'12', 2)) + + sample1 = sample1 + 'x7f' # makes it invalid + try: + r = codecs.utf_8_decode(sample1) + Assert(False, "expected UncodeDecodeError not raised") + except Exception as e: + AreEqual(type(e), UnicodeDecodeError) + + internal_cp34951(b'\x31\x32\xe2\x82\xac\x0a') # 12 + internal_cp34951(b'\xef\xbb\xbf\x31\x32\xe2\x82\xac\x0a') # 12 + + def test_utf_8_encode(): ''' '''