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


Python codecs.xmlcharrefreplace_errors方法代碼示例

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


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

示例1: test_fake_error_class

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import xmlcharrefreplace_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_lookup

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import xmlcharrefreplace_errors [as 別名]
def test_lookup(self):
        self.assertEqual(codecs.strict_errors, codecs.lookup_error("strict"))
        self.assertEqual(codecs.ignore_errors, codecs.lookup_error("ignore"))
        self.assertEqual(codecs.strict_errors, codecs.lookup_error("strict"))
        self.assertEqual(
            codecs.xmlcharrefreplace_errors,
            codecs.lookup_error("xmlcharrefreplace")
        )
        self.assertEqual(
            codecs.backslashreplace_errors,
            codecs.lookup_error("backslashreplace")
        )
        self.assertEqual(
            codecs.namereplace_errors,
            codecs.lookup_error("namereplace")
        ) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:18,代碼來源:test_codeccallbacks.py

示例3: test_fake_error_class

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import xmlcharrefreplace_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

示例4: test_fake_error_class

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import xmlcharrefreplace_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

示例5: htmlentityreplace_errors

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import xmlcharrefreplace_errors [as 別名]
def htmlentityreplace_errors(exc):
    if isinstance(exc, (UnicodeEncodeError, UnicodeTranslateError)):
        res = []
        codepoints = []
        skip = False
        for i, c in enumerate(exc.object[exc.start:exc.end]):
            if skip:
                skip = False
                continue
            index = i + exc.start
            if _utils.isSurrogatePair(exc.object[index:min([exc.end, index + 2])]):
                codepoint = _utils.surrogatePairToCodepoint(exc.object[index:index + 2])
                skip = True
            else:
                codepoint = ord(c)
            codepoints.append(codepoint)
        for cp in codepoints:
            e = _encode_entity_map.get(cp)
            if e:
                res.append("&")
                res.append(e)
                if not e.endswith(";"):
                    res.append(";")
            else:
                res.append("&#x%s;" % (hex(cp)[2:]))
        return ("".join(res), exc.end)
    else:
        return xmlcharrefreplace_errors(exc) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:30,代碼來源:serializer.py

示例6: htmlentityreplace_errors

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import xmlcharrefreplace_errors [as 別名]
def htmlentityreplace_errors(exc):
        if isinstance(exc, (UnicodeEncodeError, UnicodeTranslateError)):
            res = []
            codepoints = []
            skip = False
            for i, c in enumerate(exc.object[exc.start:exc.end]):
                if skip:
                    skip = False
                    continue
                index = i + exc.start
                if utils.isSurrogatePair(exc.object[index:min([exc.end, index + 2])]):
                    codepoint = utils.surrogatePairToCodepoint(exc.object[index:index + 2])
                    skip = True
                else:
                    codepoint = ord(c)
                codepoints.append(codepoint)
            for cp in codepoints:
                e = encode_entity_map.get(cp)
                if e:
                    res.append("&")
                    res.append(e)
                    if not e.endswith(";"):
                        res.append(";")
                else:
                    res.append("&#x%s;" % (hex(cp)[2:]))
            return ("".join(res), exc.end)
        else:
            return xmlcharrefreplace_errors(exc) 
開發者ID:jpush,項目名稱:jbox,代碼行數:30,代碼來源:htmlserializer.py

示例7: test_badandgoodxmlcharrefreplaceexceptions

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import xmlcharrefreplace_errors [as 別名]
def test_badandgoodxmlcharrefreplaceexceptions(self):
        # "xmlcharrefreplace" complains about a non-exception passed in
        self.assertRaises(
           TypeError,
           codecs.xmlcharrefreplace_errors,
           42
        )
        # "xmlcharrefreplace" complains about the wrong exception types
        self.assertRaises(
           TypeError,
           codecs.xmlcharrefreplace_errors,
           UnicodeError("ouch")
        )
        # "xmlcharrefreplace" can only be used for encoding
        self.assertRaises(
            TypeError,
            codecs.xmlcharrefreplace_errors,
            UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")
        )
        self.assertRaises(
            TypeError,
            codecs.xmlcharrefreplace_errors,
            UnicodeTranslateError(u"\u3042", 0, 1, "ouch")
        )
        # Use the correct exception
        cs = (0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000)
        cs += (0xdfff, 0xd800)
        s = u"".join(unichr(c) for c in cs)
        s += u"\U0001869f\U000186a0\U000f423f\U000f4240"
        cs += (99999, 100000, 999999, 1000000)
        self.assertEqual(
            codecs.xmlcharrefreplace_errors(
                UnicodeEncodeError("ascii", u"a" + s + u"b",
                                   1, 1 + len(s), "ouch")
            ),
            (u"".join(u"&#%d;" % c for c in cs), 1 + len(s))
        ) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:39,代碼來源:test_codeccallbacks.py

示例8: test_lookup

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import xmlcharrefreplace_errors [as 別名]
def test_lookup(self):
        self.assertEqual(codecs.strict_errors, codecs.lookup_error("strict"))
        self.assertEqual(codecs.ignore_errors, codecs.lookup_error("ignore"))
        self.assertEqual(codecs.strict_errors, codecs.lookup_error("strict"))
        self.assertEqual(
            codecs.xmlcharrefreplace_errors,
            codecs.lookup_error("xmlcharrefreplace")
        )
        self.assertEqual(
            codecs.backslashreplace_errors,
            codecs.lookup_error("backslashreplace")
        ) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:14,代碼來源:test_codeccallbacks.py

示例9: test_xmlcharrefvalues

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import xmlcharrefreplace_errors [as 別名]
def test_xmlcharrefvalues(self):
        # enhance coverage of:
        # Python/codecs.c::PyCodec_XMLCharRefReplaceErrors()
        # and inline implementations
        v = (1, 5, 10, 50, 100, 500, 1000, 5000, 10000, 50000)
        if sys.maxunicode>=100000:
            v += (100000, 500000, 1000000)
        s = u"".join([unichr(x) for x in v])
        codecs.register_error("test.xmlcharrefreplace", codecs.xmlcharrefreplace_errors)
        for enc in ("ascii", "iso-8859-15"):
            for err in ("xmlcharrefreplace", "test.xmlcharrefreplace"):
                s.encode(enc, err) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:14,代碼來源:test_codeccallbacks.py

示例10: test_badandgoodxmlcharrefreplaceexceptions

# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import xmlcharrefreplace_errors [as 別名]
def test_badandgoodxmlcharrefreplaceexceptions(self):
        # "xmlcharrefreplace" complains about a non-exception passed in
        self.assertRaises(
           TypeError,
           codecs.xmlcharrefreplace_errors,
           42
        )
        # "xmlcharrefreplace" complains about the wrong exception types
        self.assertRaises(
           TypeError,
           codecs.xmlcharrefreplace_errors,
           UnicodeError("ouch")
        )
        # "xmlcharrefreplace" can only be used for encoding
        self.assertRaises(
            TypeError,
            codecs.xmlcharrefreplace_errors,
            UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")
        )
        self.assertRaises(
            TypeError,
            codecs.xmlcharrefreplace_errors,
            UnicodeTranslateError(u"\u3042", 0, 1, "ouch")
        )
        # Use the correct exception
        cs = (0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000, 0x3042)
        s = "".join(unichr(c) for c in cs)
        self.assertEqual(
            codecs.xmlcharrefreplace_errors(
                UnicodeEncodeError("ascii", s, 0, len(s), "ouch")
            ),
            (u"".join(u"&#%d;" % ord(c) for c in s), len(s))
        ) 
開發者ID:dxwu,項目名稱:BinderFilter,代碼行數:35,代碼來源:test_codeccallbacks.py


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