Ejemplo n.º 1
0
 def test_badandgoodnamereplaceexceptions(self):
     # "namereplace" complains about a non-exception passed in
     self.assertRaises(TypeError, codecs.namereplace_errors, 42)
     # "namereplace" complains about the wrong exception types
     self.assertRaises(TypeError, codecs.namereplace_errors,
                       UnicodeError("ouch"))
     # "namereplace" can only be used for encoding
     self.assertRaises(
         TypeError, codecs.namereplace_errors,
         UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch"))
     self.assertRaises(TypeError, codecs.namereplace_errors,
                       UnicodeTranslateError("\u3042", 0, 1, "ouch"))
     # Use the correct exception
     tests = [
         ("\u3042", "\\N{HIRAGANA LETTER A}"),
         ("\x00", "\\x00"),
         ("\ufbf9", "\\N{ARABIC LIGATURE UIGHUR KIRGHIZ YEH WITH "
          "HAMZA ABOVE WITH ALEF MAKSURA ISOLATED FORM}"),
         ("\U000e007f", "\\N{CANCEL TAG}"),
         ("\U0010ffff", "\\U0010ffff"),
         # Lone surrogates
         ("\ud800", "\\ud800"),
         ("\udfff", "\\udfff"),
         ("\ud800\udfff", "\\ud800\\udfff"),
     ]
     for s, r in tests:
         with self.subTest(str=s):
             self.assertEqual(
                 codecs.namereplace_errors(
                     UnicodeEncodeError("ascii", "a" + s + "b", 1,
                                        1 + len(s), "ouch")),
                 (r, 1 + len(s)))
Ejemplo n.º 2
0
def copyerrors(err):
    (escaped, end) = codecs.namereplace_errors(err)

    badData = b''
    while escaped:
        if not is_escape_sequence(escaped[0:6]):
            raise err

        badData += bytes.fromhex(escaped[2:6])
        escaped = escaped[6:]

    return (badData, end)
Ejemplo n.º 3
0
 def test_badandgoodnamereplaceexceptions(self):
     # "namereplace" complains about a non-exception passed in
     self.assertRaises(
        TypeError,
        codecs.namereplace_errors,
        42
     )
     # "namereplace" complains about the wrong exception types
     self.assertRaises(
        TypeError,
        codecs.namereplace_errors,
        UnicodeError("ouch")
     )
     # "namereplace" can only be used for encoding
     self.assertRaises(
         TypeError,
         codecs.namereplace_errors,
         UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch")
     )
     self.assertRaises(
         TypeError,
         codecs.namereplace_errors,
         UnicodeTranslateError("\u3042", 0, 1, "ouch")
     )
     # Use the correct exception
     tests = [
         ("\u3042", "\\N{HIRAGANA LETTER A}"),
         ("\x00", "\\x00"),
         ("\ufbf9", "\\N{ARABIC LIGATURE UIGHUR KIRGHIZ YEH WITH "
                    "HAMZA ABOVE WITH ALEF MAKSURA ISOLATED FORM}"),
         ("\U000e007f", "\\N{CANCEL TAG}"),
         ("\U0010ffff", "\\U0010ffff"),
         # Lone surrogates
         ("\ud800", "\\ud800"),
         ("\udfff", "\\udfff"),
         ("\ud800\udfff", "\\ud800\\udfff"),
     ]
     for s, r in tests:
         with self.subTest(str=s):
             self.assertEqual(
                 codecs.namereplace_errors(
                     UnicodeEncodeError("ascii", "a" + s + "b",
                                        1, 1 + len(s), "ouch")),
                 (r, 1 + len(s))
             )
 def test_badandgoodnamereplaceexceptions(self):
     # "namereplace" complains about a non-exception passed in
     self.assertRaises(
        TypeError,
        codecs.namereplace_errors,
        42
     )
     # "namereplace" complains about the wrong exception types
     self.assertRaises(
        TypeError,
        codecs.namereplace_errors,
        UnicodeError("ouch")
     )
     # "namereplace" can only be used for encoding
     self.assertRaises(
         TypeError,
         codecs.namereplace_errors,
         UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch")
     )
     self.assertRaises(
         TypeError,
         codecs.namereplace_errors,
         UnicodeTranslateError("\u3042", 0, 1, "ouch")
     )
     # Use the correct exception
     self.assertEqual(
         codecs.namereplace_errors(
             UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")),
         ("\\N{HIRAGANA LETTER A}", 1)
     )
     self.assertEqual(
         codecs.namereplace_errors(
             UnicodeEncodeError("ascii", "\x00", 0, 1, "ouch")),
         ("\\x00", 1)
     )
     self.assertEqual(
         codecs.namereplace_errors(
             UnicodeEncodeError("ascii", "\xff", 0, 1, "ouch")),
         ("\\N{LATIN SMALL LETTER Y WITH DIAERESIS}", 1)
     )
     self.assertEqual(
         codecs.namereplace_errors(
             UnicodeEncodeError("ascii", "\u0100", 0, 1, "ouch")),
         ("\\N{LATIN CAPITAL LETTER A WITH MACRON}", 1)
     )
     self.assertEqual(
         codecs.namereplace_errors(
             UnicodeEncodeError("ascii", "\uffff", 0, 1, "ouch")),
         ("\\uffff", 1)
     )
     if SIZEOF_WCHAR_T > 0:
         self.assertEqual(
             codecs.namereplace_errors(
                 UnicodeEncodeError("ascii", "\U00010000",
                                    0, 1, "ouch")),
             ("\\N{LINEAR B SYLLABLE B008 A}", 1)
         )
         self.assertEqual(
             codecs.namereplace_errors(
                 UnicodeEncodeError("ascii", "\U0010ffff",
                                    0, 1, "ouch")),
             ("\\U0010ffff", 1)
         )
         # Lone surrogates (regardless of unicode width)
         self.assertEqual(
             codecs.namereplace_errors(
                 UnicodeEncodeError("ascii", "\ud800", 0, 1, "ouch")),
             ("\\ud800", 1)
         )
         self.assertEqual(
             codecs.namereplace_errors(
                 UnicodeEncodeError("ascii", "\udfff", 0, 1, "ouch")),
             ("\\udfff", 1)
         )