Python codecs.backslashreplace_errors() Examples
The following are 28
code examples of codecs.backslashreplace_errors().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
codecs
, or try the search function
.
Example #1
Source File: test_codeccallbacks.py From ironpython2 with Apache License 2.0 | 6 votes |
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())
Example #2
Source File: test_codeccallbacks.py From android_universal with MIT License | 6 votes |
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())
Example #3
Source File: test_codeccallbacks.py From android_universal with MIT License | 6 votes |
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") )
Example #4
Source File: test_codeccallbacks.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
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())
Example #5
Source File: test_codeccallbacks.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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") )
Example #6
Source File: test_codeccallbacks.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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())
Example #7
Source File: test_codeccallbacks.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
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") )
Example #8
Source File: test_codeccallbacks.py From ironpython3 with Apache License 2.0 | 6 votes |
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())
Example #9
Source File: test_codeccallbacks.py From ironpython2 with Apache License 2.0 | 5 votes |
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") )
Example #10
Source File: test_codeccallbacks.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
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") )
Example #11
Source File: test_codeccallbacks.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
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") )
Example #12
Source File: test_codeccallbacks.py From medicare-demo with Apache License 2.0 | 5 votes |
def test_lookup(self): self.assertEquals(codecs.strict_errors, codecs.lookup_error("strict")) self.assertEquals(codecs.ignore_errors, codecs.lookup_error("ignore")) self.assertEquals(codecs.strict_errors, codecs.lookup_error("strict")) self.assertEquals( codecs.xmlcharrefreplace_errors, codecs.lookup_error("xmlcharrefreplace") ) self.assertEquals( codecs.backslashreplace_errors, codecs.lookup_error("backslashreplace") )
Example #13
Source File: test_codeccallbacks.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
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") )
Example #14
Source File: test_codeccallbacks.py From ironpython3 with Apache License 2.0 | 5 votes |
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") )
Example #15
Source File: test_codeccallbacks.py From oss-ftp with MIT License | 5 votes |
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") )
Example #16
Source File: test_codeccallbacks.py From BinderFilter with MIT License | 5 votes |
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") )
Example #17
Source File: test_codecs.py From ironpython3 with Apache License 2.0 | 4 votes |
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))
Example #18
Source File: test_codeccallbacks.py From gcblue with BSD 3-Clause "New" or "Revised" License | 4 votes |
def test_badandgoodbackslashreplaceexceptions(self): # "backslashreplace" complains about a non-exception passed in self.assertRaises( TypeError, codecs.backslashreplace_errors, 42 ) # "backslashreplace" complains about the wrong exception types self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeError("ouch") ) # "backslashreplace" can only be used for encoding self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch") ) self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeTranslateError(u"\u3042", 0, 1, "ouch") ) # Use the correct exception self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")), (u"\\u3042", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\x00", 0, 1, "ouch")), (u"\\x00", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\xff", 0, 1, "ouch")), (u"\\xff", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u0100", 0, 1, "ouch")), (u"\\u0100", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\uffff", 0, 1, "ouch")), (u"\\uffff", 1) ) if sys.maxunicode>0xffff: self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U00010000", 0, 1, "ouch")), (u"\\U00010000", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U0010ffff", 0, 1, "ouch")), (u"\\U0010ffff", 1) )
Example #19
Source File: test_codeccallbacks.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 4 votes |
def test_badandgoodbackslashreplaceexceptions(self): # "backslashreplace" complains about a non-exception passed in self.assertRaises( TypeError, codecs.backslashreplace_errors, 42 ) # "backslashreplace" complains about the wrong exception types self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeError("ouch") ) # Use the correct exception tests = [ ("\u3042", "\\u3042"), ("\n", "\\x0a"), ("a", "\\x61"), ("\x00", "\\x00"), ("\xff", "\\xff"), ("\u0100", "\\u0100"), ("\uffff", "\\uffff"), ("\U00010000", "\\U00010000"), ("\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.backslashreplace_errors( UnicodeEncodeError("ascii", "a" + s + "b", 1, 1 + len(s), "ouch")), (r, 1 + len(s)) ) self.assertEqual( codecs.backslashreplace_errors( UnicodeTranslateError("a" + s + "b", 1, 1 + len(s), "ouch")), (r, 1 + len(s)) ) tests = [ (b"a", "\\x61"), (b"\n", "\\x0a"), (b"\x00", "\\x00"), (b"\xff", "\\xff"), ] for b, r in tests: with self.subTest(bytes=b): self.assertEqual( codecs.backslashreplace_errors( UnicodeDecodeError("ascii", bytearray(b"a" + b + b"b"), 1, 2, "ouch")), (r, 2) )
Example #20
Source File: test_codeccallbacks.py From ironpython3 with Apache License 2.0 | 4 votes |
def test_badandgoodbackslashreplaceexceptions(self): # "backslashreplace" complains about a non-exception passed in self.assertRaises( TypeError, codecs.backslashreplace_errors, 42 ) # "backslashreplace" complains about the wrong exception types self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeError("ouch") ) # "backslashreplace" can only be used for encoding self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch") ) self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeTranslateError("\u3042", 0, 1, "ouch") ) # Use the correct exception tests = [ ("\u3042", "\\u3042"), ("\n", "\\x0a"), ("a", "\\x61"), ("\x00", "\\x00"), ("\xff", "\\xff"), ("\u0100", "\\u0100"), ("\uffff", "\\uffff"), ("\U00010000", "\\U00010000"), ("\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.backslashreplace_errors( UnicodeEncodeError("ascii", "a" + s + "b", 1, 1 + len(s), "ouch")), (r, 1 + len(s)) )
Example #21
Source File: test_codeccallbacks.py From Fluid-Designer with GNU General Public License v3.0 | 4 votes |
def test_badandgoodbackslashreplaceexceptions(self): # "backslashreplace" complains about a non-exception passed in self.assertRaises( TypeError, codecs.backslashreplace_errors, 42 ) # "backslashreplace" complains about the wrong exception types self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeError("ouch") ) # Use the correct exception tests = [ ("\u3042", "\\u3042"), ("\n", "\\x0a"), ("a", "\\x61"), ("\x00", "\\x00"), ("\xff", "\\xff"), ("\u0100", "\\u0100"), ("\uffff", "\\uffff"), ("\U00010000", "\\U00010000"), ("\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.backslashreplace_errors( UnicodeEncodeError("ascii", "a" + s + "b", 1, 1 + len(s), "ouch")), (r, 1 + len(s)) ) self.assertEqual( codecs.backslashreplace_errors( UnicodeTranslateError("a" + s + "b", 1, 1 + len(s), "ouch")), (r, 1 + len(s)) ) tests = [ (b"a", "\\x61"), (b"\n", "\\x0a"), (b"\x00", "\\x00"), (b"\xff", "\\xff"), ] for b, r in tests: with self.subTest(bytes=b): self.assertEqual( codecs.backslashreplace_errors( UnicodeDecodeError("ascii", bytearray(b"a" + b + b"b"), 1, 2, "ouch")), (r, 2) )
Example #22
Source File: test_codeccallbacks.py From medicare-demo with Apache License 2.0 | 4 votes |
def test_badandgoodbackslashreplaceexceptions(self): # "backslashreplace" complains about a non-exception passed in self.assertRaises( TypeError, codecs.backslashreplace_errors, 42 ) # "backslashreplace" complains about the wrong exception types self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeError("ouch") ) # "backslashreplace" can only be used for encoding self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch") ) self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeTranslateError(u"\u3042", 0, 1, "ouch") ) # Use the correct exception self.assertEquals( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")), (u"\\u3042", 1) ) self.assertEquals( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\x00", 0, 1, "ouch")), (u"\\x00", 1) ) self.assertEquals( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\xff", 0, 1, "ouch")), (u"\\xff", 1) ) self.assertEquals( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u0100", 0, 1, "ouch")), (u"\\u0100", 1) ) self.assertEquals( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\uffff", 0, 1, "ouch")), (u"\\uffff", 1) ) if sys.maxunicode>0xffff: self.assertEquals( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U00010000", 0, 1, "ouch")), (u"\\U00010000", 1) ) self.assertEquals( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U0010ffff", 0, 1, "ouch")), (u"\\U0010ffff", 1) )
Example #23
Source File: test_codeccallbacks.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 4 votes |
def test_badandgoodbackslashreplaceexceptions(self): # "backslashreplace" complains about a non-exception passed in self.assertRaises( TypeError, codecs.backslashreplace_errors, 42 ) # "backslashreplace" complains about the wrong exception types self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeError("ouch") ) # "backslashreplace" can only be used for encoding self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch") ) self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeTranslateError(u"\u3042", 0, 1, "ouch") ) # Use the correct exception self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")), (u"\\u3042", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\x00", 0, 1, "ouch")), (u"\\x00", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\xff", 0, 1, "ouch")), (u"\\xff", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u0100", 0, 1, "ouch")), (u"\\u0100", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\uffff", 0, 1, "ouch")), (u"\\uffff", 1) ) if sys.maxunicode>0xffff: self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U00010000", 0, 1, "ouch")), (u"\\U00010000", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U0010ffff", 0, 1, "ouch")), (u"\\U0010ffff", 1) )
Example #24
Source File: test_codeccallbacks.py From oss-ftp with MIT License | 4 votes |
def test_badandgoodbackslashreplaceexceptions(self): # "backslashreplace" complains about a non-exception passed in self.assertRaises( TypeError, codecs.backslashreplace_errors, 42 ) # "backslashreplace" complains about the wrong exception types self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeError("ouch") ) # "backslashreplace" can only be used for encoding self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch") ) self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeTranslateError(u"\u3042", 0, 1, "ouch") ) # Use the correct exception self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")), (u"\\u3042", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\x00", 0, 1, "ouch")), (u"\\x00", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\xff", 0, 1, "ouch")), (u"\\xff", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u0100", 0, 1, "ouch")), (u"\\u0100", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\uffff", 0, 1, "ouch")), (u"\\uffff", 1) ) if sys.maxunicode>0xffff: self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U00010000", 0, 1, "ouch")), (u"\\U00010000", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U0010ffff", 0, 1, "ouch")), (u"\\U0010ffff", 1) )
Example #25
Source File: test_codeccallbacks.py From android_universal with MIT License | 4 votes |
def test_badandgoodbackslashreplaceexceptions(self): # "backslashreplace" complains about a non-exception passed in self.assertRaises( TypeError, codecs.backslashreplace_errors, 42 ) # "backslashreplace" complains about the wrong exception types self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeError("ouch") ) # Use the correct exception tests = [ ("\u3042", "\\u3042"), ("\n", "\\x0a"), ("a", "\\x61"), ("\x00", "\\x00"), ("\xff", "\\xff"), ("\u0100", "\\u0100"), ("\uffff", "\\uffff"), ("\U00010000", "\\U00010000"), ("\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.backslashreplace_errors( UnicodeEncodeError("ascii", "a" + s + "b", 1, 1 + len(s), "ouch")), (r, 1 + len(s)) ) self.assertEqual( codecs.backslashreplace_errors( UnicodeTranslateError("a" + s + "b", 1, 1 + len(s), "ouch")), (r, 1 + len(s)) ) tests = [ (b"a", "\\x61"), (b"\n", "\\x0a"), (b"\x00", "\\x00"), (b"\xff", "\\xff"), ] for b, r in tests: with self.subTest(bytes=b): self.assertEqual( codecs.backslashreplace_errors( UnicodeDecodeError("ascii", bytearray(b"a" + b + b"b"), 1, 2, "ouch")), (r, 2) )
Example #26
Source File: test_codeccallbacks.py From BinderFilter with MIT License | 4 votes |
def test_badandgoodbackslashreplaceexceptions(self): # "backslashreplace" complains about a non-exception passed in self.assertRaises( TypeError, codecs.backslashreplace_errors, 42 ) # "backslashreplace" complains about the wrong exception types self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeError("ouch") ) # "backslashreplace" can only be used for encoding self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch") ) self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeTranslateError(u"\u3042", 0, 1, "ouch") ) # Use the correct exception self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")), (u"\\u3042", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\x00", 0, 1, "ouch")), (u"\\x00", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\xff", 0, 1, "ouch")), (u"\\xff", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u0100", 0, 1, "ouch")), (u"\\u0100", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\uffff", 0, 1, "ouch")), (u"\\uffff", 1) ) if sys.maxunicode>0xffff: self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U00010000", 0, 1, "ouch")), (u"\\U00010000", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U0010ffff", 0, 1, "ouch")), (u"\\U0010ffff", 1) )
Example #27
Source File: test_codeccallbacks.py From CTFCrackTools with GNU General Public License v3.0 | 4 votes |
def test_badandgoodbackslashreplaceexceptions(self): # "backslashreplace" complains about a non-exception passed in self.assertRaises( TypeError, codecs.backslashreplace_errors, 42 ) # "backslashreplace" complains about the wrong exception types self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeError("ouch") ) # "backslashreplace" can only be used for encoding self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch") ) self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeTranslateError(u"\u3042", 0, 1, "ouch") ) # Use the correct exception self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")), (u"\\u3042", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\x00", 0, 1, "ouch")), (u"\\x00", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\xff", 0, 1, "ouch")), (u"\\xff", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u0100", 0, 1, "ouch")), (u"\\u0100", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\uffff", 0, 1, "ouch")), (u"\\uffff", 1) ) if sys.maxunicode>0xffff: self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U00010000", 0, 1, "ouch")), (u"\\U00010000", 1) ) self.assertEqual( codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U0010ffff", 0, 1, "ouch")), (u"\\U0010ffff", 1) )
Example #28
Source File: test_codeccallbacks.py From ironpython2 with Apache License 2.0 | 4 votes |
def test_badandgoodbackslashreplaceexceptions(self): # "backslashreplace" complains about a non-exception passed in self.assertRaises( TypeError, codecs.backslashreplace_errors, 42 ) # "backslashreplace" complains about the wrong exception types self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeError("ouch") ) # "backslashreplace" can only be used for encoding self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch") ) self.assertRaises( TypeError, codecs.backslashreplace_errors, UnicodeTranslateError(u"\u3042", 0, 1, "ouch") ) # Use the correct exception tests = [ (u"\u3042", u"\\u3042"), (u"\n", u"\\x0a"), (u"a", u"\\x61"), (u"\x00", u"\\x00"), (u"\xff", u"\\xff"), (u"\u0100", u"\\u0100"), (u"\uffff", u"\\uffff"), # Lone surrogates (u"\ud800", u"\\ud800"), (u"\udfff", u"\\udfff"), ] if sys.maxunicode > 0xffff: tests += [ (u"\U00010000", u"\\U00010000"), (u"\U0010ffff", u"\\U0010ffff"), ] else: tests += [ (u"\U00010000", u"\\ud800\\udc00"), (u"\U0010ffff", u"\\udbff\\udfff"), ] for s, r in tests: self.assertEqual( codecs.backslashreplace_errors( UnicodeEncodeError("ascii", u"a" + s + u"b", 1, 1 + len(s), "ouch")), (r, 1 + len(s)) )