我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用codecs.code_page_encode()。
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 check_encode(self, cp, tests): for text, errors, expected in tests: if expected is not None: try: encoded = codecs.code_page_encode(cp, text, errors) except UnicodeEncodeError as err: self.fail('Unable to encode %a to "cp%s" with ' 'errors=%r: %s' % (text, cp, errors, err)) self.assertEqual(encoded[0], expected, '%a.encode("cp%s", %r)=%a != %a' % (text, cp, errors, encoded[0], expected)) self.assertEqual(encoded[1], len(text)) else: self.assertRaises(UnicodeEncodeError, codecs.code_page_encode, cp, text, errors)
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')