我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用codecs.replace_errors()。
def test_fake_error_class(self): handlers = [ codecs.strict_errors, codecs.ignore_errors, codecs.replace_errors, codecs.backslashreplace_errors, codecs.xmlcharrefreplace_errors, ] for cls in UnicodeEncodeError, UnicodeDecodeError, UnicodeTranslateError: class FakeUnicodeError(str): __class__ = cls for handler in handlers: self.assertRaises(TypeError, handler, FakeUnicodeError()) class FakeUnicodeError(Exception): __class__ = cls for handler in handlers: with self.assertRaises((TypeError, FakeUnicodeError)): handler(FakeUnicodeError())
def test_fake_error_class(self): handlers = [ codecs.strict_errors, codecs.ignore_errors, codecs.replace_errors, codecs.backslashreplace_errors, codecs.xmlcharrefreplace_errors, codecs.lookup_error('surrogateescape'), codecs.lookup_error('surrogatepass'), ] for cls in UnicodeEncodeError, UnicodeDecodeError, UnicodeTranslateError: class FakeUnicodeError(str): __class__ = cls for handler in handlers: with self.subTest(handler=handler, error_class=cls): self.assertRaises(TypeError, handler, FakeUnicodeError()) class FakeUnicodeError(Exception): __class__ = cls for handler in handlers: with self.subTest(handler=handler, error_class=cls): with self.assertRaises((TypeError, FakeUnicodeError)): handler(FakeUnicodeError())
def test_badandgoodreplaceexceptions(self): # "replace" complains about a non-exception passed in self.assertRaises( TypeError, codecs.replace_errors, 42 ) # "replace" complains about the wrong exception type self.assertRaises( TypeError, codecs.replace_errors, UnicodeError("ouch") ) self.assertRaises( TypeError, codecs.replace_errors, BadObjectUnicodeEncodeError() ) self.assertRaises( TypeError, codecs.replace_errors, BadObjectUnicodeDecodeError() ) # With the correct exception, "replace" returns an "?" or "\ufffd" replacement self.assertEqual( codecs.replace_errors( UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")), ("?", 1) ) self.assertEqual( codecs.replace_errors( UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch")), ("\ufffd", 1) ) self.assertEqual( codecs.replace_errors( UnicodeTranslateError("\u3042", 0, 1, "ouch")), ("\ufffd", 1) )
def test_badandgoodreplaceexceptions(self): # "replace" complains about a non-exception passed in self.assertRaises( TypeError, codecs.replace_errors, 42 ) # "replace" complains about the wrong exception type self.assertRaises( TypeError, codecs.replace_errors, UnicodeError("ouch") ) self.assertRaises( TypeError, codecs.replace_errors, BadObjectUnicodeEncodeError() ) self.assertRaises( TypeError, codecs.replace_errors, BadObjectUnicodeDecodeError() ) # With the correct exception, "replace" returns an "?" or u"\ufffd" replacement self.assertEqual( codecs.replace_errors( UnicodeEncodeError("ascii", u"a\u3042b", 1, 2, "ouch")), (u"?", 2) ) self.assertEqual( codecs.replace_errors( UnicodeDecodeError("ascii", "a\xffb", 1, 2, "ouch")), (u"\ufffd", 2) ) self.assertEqual( codecs.replace_errors( UnicodeTranslateError(u"a\u3042b", 1, 2, "ouch")), (u"\ufffd", 2) )
def test_badandgoodreplaceexceptions(self): # "replace" complains about a non-exception passed in self.assertRaises( TypeError, codecs.replace_errors, 42 ) # "replace" complains about the wrong exception type self.assertRaises( TypeError, codecs.replace_errors, UnicodeError("ouch") ) self.assertRaises( TypeError, codecs.replace_errors, BadObjectUnicodeEncodeError() ) self.assertRaises( TypeError, codecs.replace_errors, BadObjectUnicodeDecodeError() ) # With the correct exception, "replace" returns an "?" or u"\ufffd" replacement self.assertEqual( codecs.replace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")), (u"?", 1) ) self.assertEqual( codecs.replace_errors(UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")), (u"\ufffd", 1) ) self.assertEqual( codecs.replace_errors(UnicodeTranslateError(u"\u3042", 0, 1, "ouch")), (u"\ufffd", 1) )
def test_badandgoodreplaceexceptions(self): # "replace" complains about a non-exception passed in self.assertRaises( TypeError, codecs.replace_errors, 42 ) # "replace" complains about the wrong exception type self.assertRaises( TypeError, codecs.replace_errors, UnicodeError("ouch") ) self.assertRaises( TypeError, codecs.replace_errors, BadObjectUnicodeEncodeError() ) self.assertRaises( TypeError, codecs.replace_errors, BadObjectUnicodeDecodeError() ) # With the correct exception, "replace" returns an "?" or "\ufffd" replacement self.assertEqual( codecs.replace_errors( UnicodeEncodeError("ascii", "a\u3042b", 1, 2, "ouch")), ("?", 2) ) self.assertEqual( codecs.replace_errors( UnicodeDecodeError("ascii", bytearray(b"a\xffb"), 1, 2, "ouch")), ("\ufffd", 2) ) self.assertEqual( codecs.replace_errors( UnicodeTranslateError("a\u3042b", 1, 2, "ouch")), ("\ufffd", 2) )