我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用codecs.xmlcharrefreplace_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 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)
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)
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 (u"".join(res), exc.end) else: return xmlcharrefreplace_errors(exc)
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", bytearray(b"\xff"), 0, 1, "ouch") ) self.assertRaises( TypeError, codecs.xmlcharrefreplace_errors, UnicodeTranslateError("\u3042", 0, 1, "ouch") ) # Use the correct exception cs = (0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000, 0x3042) s = "".join(chr(c) for c in cs) self.assertEqual( codecs.xmlcharrefreplace_errors( UnicodeEncodeError("ascii", s, 0, len(s), "ouch") ), ("".join("&#%d;" % ord(c) for c in s), len(s)) )
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") )
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 = "".join([chr(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)