我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用codecs.code_page_decode()。
def check_decode(self, cp, tests): for raw, errors, expected in tests: if expected is not None: try: decoded = codecs.code_page_decode(cp, raw, errors) except UnicodeDecodeError as err: self.fail('Unable to decode %a from "cp%s" with ' 'errors=%r: %s' % (raw, cp, errors, err)) self.assertEqual(decoded[0], expected, '%a.decode("cp%s", %r)=%a != %a' % (raw, cp, errors, decoded[0], expected)) # assert 0 <= decoded[1] <= len(raw) self.assertGreaterEqual(decoded[1], 0) self.assertLessEqual(decoded[1], len(raw)) else: self.assertRaises(UnicodeDecodeError, codecs.code_page_decode, cp, raw, errors)
def test_incremental(self): decoded = codecs.code_page_decode(932, b'\x82', 'strict', False) self.assertEqual(decoded, ('', 0)) decoded = codecs.code_page_decode(932, b'\xe9\x80\xe9', 'strict', False) self.assertEqual(decoded, ('\u9a3e', 2)) decoded = codecs.code_page_decode(932, b'\xe9\x80\xe9\x80', 'strict', False) self.assertEqual(decoded, ('\u9a3e\u9a3e', 4)) decoded = codecs.code_page_decode(932, b'abc', 'strict', False) self.assertEqual(decoded, ('abc', 3))
def decode(input, errors='strict'): return codecs.code_page_decode(65001, input, errors, True)
def test_invalid_code_page(self): self.assertRaises(ValueError, codecs.code_page_encode, -1, 'a') self.assertRaises(ValueError, codecs.code_page_decode, -1, b'a') self.assertRaises(WindowsError, codecs.code_page_encode, 123, 'a') self.assertRaises(WindowsError, codecs.code_page_decode, 123, b'a')
def test_code_page_name(self): self.assertRaisesRegex(UnicodeEncodeError, 'cp932', codecs.code_page_encode, 932, '\xff') self.assertRaisesRegex(UnicodeDecodeError, 'cp932', codecs.code_page_decode, 932, b'\x81\x00') self.assertRaisesRegex(UnicodeDecodeError, 'CP_UTF8', codecs.code_page_decode, self.CP_UTF8, b'\xff')
def test_invalid_code_page(self): self.assertRaises(ValueError, codecs.code_page_encode, -1, 'a') self.assertRaises(ValueError, codecs.code_page_decode, -1, b'a') self.assertRaises(OSError, codecs.code_page_encode, 123, 'a') self.assertRaises(OSError, codecs.code_page_decode, 123, b'a')