MCPcopy Index your code
hub / github.com/RustPython/RustPython / assertCorrectUTF8Decoding

Method assertCorrectUTF8Decoding

Lib/test/test_str.py:2026–2043  ·  view source on GitHub ↗

Check that an invalid UTF-8 sequence raises a UnicodeDecodeError when 'strict' is used, returns res when 'replace' is used, and that doesn't return anything when 'ignore' is used.

(self, seq, res, err)

Source from the content-addressed store, hash-verified

2024 res.replace('\uFFFD', ''))
2025
2026 def assertCorrectUTF8Decoding(self, seq, res, err):
2027 """
2028 Check that an invalid UTF-8 sequence raises a UnicodeDecodeError when
2029 'strict' is used, returns res when 'replace' is used, and that doesn't
2030 return anything when 'ignore' is used.
2031 """
2032 with self.assertRaises(UnicodeDecodeError) as cm:
2033 seq.decode('utf-8')
2034 exc = cm.exception
2035
2036 self.assertIn(err, str(exc))
2037 self.assertEqual(seq.decode('utf-8', 'replace'), res)
2038 self.assertEqual((b'aaaa' + seq + b'bbbb').decode('utf-8', 'replace'),
2039 'aaaa' + res + 'bbbb')
2040 res = res.replace('\ufffd', '')
2041 self.assertEqual(seq.decode('utf-8', 'ignore'), res)
2042 self.assertEqual((b'aaaa' + seq + b'bbbb').decode('utf-8', 'ignore'),
2043 'aaaa' + res + 'bbbb')
2044
2045 def test_invalid_start_byte(self):
2046 """

Calls 6

strFunction · 0.85
assertInMethod · 0.80
assertRaisesMethod · 0.45
decodeMethod · 0.45
assertEqualMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected