當前位置: 首頁>>代碼示例>>Python>>正文


Python codecs.replace_errors方法代碼示例

本文整理匯總了Python中codecs.replace_errors方法的典型用法代碼示例。如果您正苦於以下問題:Python codecs.replace_errors方法的具體用法?Python codecs.replace_errors怎麽用?Python codecs.replace_errors使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在codecs的用法示例。


在下文中一共展示了codecs.replace_errors方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_fake_error_class

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import replace_errors [as 別名]
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()) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:20,代碼來源:test_codeccallbacks.py

示例2: test_fake_error_class

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import replace_errors [as 別名]
def test_fake_error_class(self):
        handlers = [
            codecs.strict_errors,
            codecs.ignore_errors,
            codecs.replace_errors,
            codecs.backslashreplace_errors,
            codecs.namereplace_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()) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:25,代碼來源:test_codeccallbacks.py

示例3: test_fake_error_class

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import replace_errors [as 別名]
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()) 
開發者ID:IronLanguages,項目名稱:ironpython3,代碼行數:24,代碼來源:test_codeccallbacks.py

示例4: test_badandgoodreplaceexceptions

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import replace_errors [as 別名]
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)
        ) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:41,代碼來源:test_codeccallbacks.py

示例5: test_badandgoodreplaceexceptions

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import replace_errors [as 別名]
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)
        ) 
開發者ID:dxwu,項目名稱:BinderFilter,代碼行數:38,代碼來源:test_codeccallbacks.py

示例6: test_badandgoodreplaceexceptions

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import replace_errors [as 別名]
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)
        ) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:41,代碼來源:test_codeccallbacks.py

示例7: test_badandgoodreplaceexceptions

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import replace_errors [as 別名]
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.assertEquals(
            codecs.replace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
            (u"?", 1)
        )
        self.assertEquals(
            codecs.replace_errors(UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")),
            (u"\ufffd", 1)
        )
        self.assertEquals(
            codecs.replace_errors(UnicodeTranslateError(u"\u3042", 0, 1, "ouch")),
            (u"\ufffd", 1)
        ) 
開發者ID:ofermend,項目名稱:medicare-demo,代碼行數:38,代碼來源:test_codeccallbacks.py

示例8: test_error_handlers

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import replace_errors [as 別名]
def test_error_handlers(self):
        ude = UnicodeDecodeError('dummy', b"abcdefgh", 3, 5, "decoding testing purposes")
        uee = UnicodeEncodeError('dummy', "abcdefgh", 2, 6, "encoding testing purposes")
        ute = UnicodeTranslateError("abcdefgh", 2, 6, "translating testing purposes")
        unicode_data = "ab\xff\u20ac\U0001f40d\0\t\r\nz"
        uee_unicode = UnicodeEncodeError('dummy', unicode_data, 2, len(unicode_data), "encoding testing purposes")

        strict = codecs.lookup_error('strict')
        self.assertEqual(strict, codecs.strict_errors)
        with self.assertRaises(UnicodeDecodeError) as cm:
            strict(ude)
        self.assertEqual(cm.exception, ude)
        with self.assertRaises(UnicodeEncodeError) as cm:
            strict(uee)
        self.assertEqual(cm.exception, uee)
        with self.assertRaises(UnicodeTranslateError) as cm:
            strict(ute)
        self.assertEqual(cm.exception, ute)
        self.assertRaisesRegex(TypeError, "codec must pass exception instance", strict, None)
        self.assertRaisesRegex(TypeError, "\w+\(\) takes exactly (one|1) argument \(0 given\)", strict)
        self.assertRaisesRegex(TypeError, "\w+\(\) takes exactly (one|1) argument \(2 given\)", strict, ude, uee)
        self.assertRaises(LookupError, codecs.lookup_error, "STRICT")

        ignore = codecs.lookup_error('ignore')
        self.assertEqual(ignore, codecs.ignore_errors)
        self.assertEqual(ignore(ude), ("", 5))
        self.assertEqual(ignore(uee), ("", 6))
        self.assertEqual(ignore(ute), ("", 6))
        self.assertEqual(ignore(uee_unicode), ("", uee_unicode.end))

        replace = codecs.lookup_error('replace')
        self.assertEqual(replace, codecs.replace_errors)
        self.assertEqual(replace(ude), ("�", 5))
        self.assertEqual(replace(uee), ("????", 6))
        self.assertEqual(replace(ute), ("����", 6))
        self.assertEqual(replace(uee_unicode), ("?" * (uee_unicode.end - uee_unicode.start), uee_unicode.end))

        backslashreplace = codecs.lookup_error('backslashreplace')
        self.assertEqual(backslashreplace, codecs.backslashreplace_errors)
        self.assertRaisesRegex(TypeError, "don't know how to handle UnicodeDecodeError in error callback", backslashreplace, ude)
        self.assertEqual(backslashreplace(uee), (r"\x63\x64\x65\x66", 6))
        self.assertRaisesRegex(TypeError, "don't know how to handle UnicodeTranslateError in error callback", backslashreplace, ute)
        self.assertEqual(backslashreplace(uee_unicode), (r"\xff\u20ac\U0001f40d\x00\x09\x0d\x0a\x7a", uee_unicode.end))

        xmlcharrefreplace = codecs.lookup_error('xmlcharrefreplace')
        self.assertEqual(xmlcharrefreplace, codecs.xmlcharrefreplace_errors)
        self.assertRaisesRegex(TypeError, "don't know how to handle UnicodeDecodeError in error callback", xmlcharrefreplace, ude)
        self.assertEqual(xmlcharrefreplace(uee), ("cdef", 6))
        self.assertRaisesRegex(TypeError, "don't know how to handle UnicodeTranslateError in error callback", xmlcharrefreplace, ute)
        self.assertEqual(xmlcharrefreplace(uee_unicode), ("ÿ€🐍�	
z", uee_unicode.end)) 
開發者ID:IronLanguages,項目名稱:ironpython3,代碼行數:52,代碼來源:test_codecs.py


注:本文中的codecs.replace_errors方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。