Exemplo n.º 1
0
 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.assertEquals(
         codecs.xmlcharrefreplace_errors(
             UnicodeEncodeError("ascii", s, 0, len(s), "ouch")
         ),
         (u"".join(u"&#%d;" % ord(c) for c in s), len(s))
     )
 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)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
 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
     self.assertEquals(
         codecs.xmlcharrefreplace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
         (u"&#%d;" % 0x3042, 1)
     )
Exemplo n.º 5
0
 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)
     if test_support.due_to_ironpython_bug("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=303935"):
         return
     self.assertEquals(
         codecs.xmlcharrefreplace_errors(
             UnicodeEncodeError("ascii", s, 0, len(s), "ouch")
         ),
         (u"".join(u"&#%d;" % ord(c) for c in s), len(s))
     )
Exemplo n.º 6
0
 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)
     if test_support.due_to_ironpython_bug("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=303935"):
         return
     self.assertEquals(
         codecs.xmlcharrefreplace_errors(
             UnicodeEncodeError("ascii", s, 0, len(s), "ouch")
         ),
         (u"".join(u"&#%d;" % ord(c) for c in s), len(s))
     )
Exemplo n.º 7
0
 def htmlentityreplace_errors(exc):
     if isinstance(exc, (UnicodeEncodeError, UnicodeTranslateError)):
         res = []
         for c in exc.object[exc.start:exc.end]:
             e = encode_entity_map.get(c)
             if e:
                 res.append("&")
                 res.append(e)
                 if not e.endswith(";"):
                     res.append(";")
             else:
                 res.append(c.encode(exc.encoding, "xmlcharrefreplace"))
         return ("".join(res), exc.end)
     else:
         return xmlcharrefreplace_errors(exc)
Exemplo n.º 8
0
 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, 99999, 100000,
           999999, 1000000)
     cs += (0xd800, 0xdfff)
     s = "".join(chr(c) for c in cs)
     self.assertEqual(
         codecs.xmlcharrefreplace_errors(
             UnicodeEncodeError("ascii", "a" + s + "b", 1, 1 + len(s),
                                "ouch")),
         ("".join("&#%d;" % c for c in cs), 1 + len(s)))
Exemplo n.º 9
0
 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)))
 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))
     )
Exemplo n.º 11
0
 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, 99999, 100000,
           999999, 1000000)
     cs += (0xd800, 0xdfff)
     s = "".join(chr(c) for c in cs)
     self.assertEqual(
         codecs.xmlcharrefreplace_errors(
             UnicodeEncodeError("ascii", "a" + s + "b",
                                1, 1 + len(s), "ouch")
         ),
         ("".join("&#%d;" % c for c in cs), 1 + len(s))
     )