[issue3672] Ill-formed surrogates not treated as errors during encoding/decoding - Code Review (original) (raw)
OLD
NEW
1 from test import support
1 from test import support
2 import unittest
2 import unittest
3 import codecs
3 import codecs
4 import sys, _testcapi, io
4 import sys, _testcapi, io
5
5
6 class Queue(object):
6 class Queue(object):
7 """
7 """
8 queue: write bytes at one end, read bytes from the other end
8 queue: write bytes at one end, read bytes from the other end
9 """
9 """
10 def __init__(self, buffer):
10 def __init__(self, buffer):
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
534 "\x00\xff\u07ff\u0800",
534 "\x00\xff\u07ff\u0800",
535 "\x00\xff\u07ff\u0800\uffff",
535 "\x00\xff\u07ff\u0800\uffff",
536 ]
536 ]
537 )
537 )
538
538
539 def test_decoder_state(self):
539 def test_decoder_state(self):
540 u = "\x00\x7f\x80\xff\u0100\u07ff\u0800\uffff\U0010ffff"
540 u = "\x00\x7f\x80\xff\u0100\u07ff\u0800\uffff\U0010ffff"
541 self.check_state_handling_decode(self.encoding,
541 self.check_state_handling_decode(self.encoding,
542 u, u.encode(self.encoding))
542 u, u.encode(self.encoding))
543
543
544 def test_surrogates(self):
545 self.assertRaises(UnicodeEncodeError, "\ud800".encode, "utf-8")
546 self.assertRaises(UnicodeDecodeError, b"\xed\xa0\x80".decode, "utf-8")
547 self.assertEquals("\ud800".encode("utf-8", "surrogates"), b"\xed\xa0\x80 ")
548 self.assertEquals(b"\xed\xa0\x80".decode("utf-8", "surrogates"), "\ud800 ")
549 self.assertTrue(codecs.lookup_error("surrogates"))
550
544 class UTF7Test(ReadTest):
551 class UTF7Test(ReadTest):
545 encoding = "utf-7"
552 encoding = "utf-7"
546
553
547 def test_partial(self):
554 def test_partial(self):
548 self.check_partial(
555 self.check_partial(
549 "a+-b",
556 "a+-b",
550 [
557 [
551 "a",
558 "a",
552 "a",
559 "a",
553 "a+",
560 "a+",
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
1016
1023
1017
1024
1018 class NameprepTest(unittest.TestCase):
1025 class NameprepTest(unittest.TestCase):
1019 def test_nameprep(self):
1026 def test_nameprep(self):
1020 from encodings.idna import nameprep
1027 from encodings.idna import nameprep
1021 for pos, (orig, prepped) in enumerate(nameprep_tests):
1028 for pos, (orig, prepped) in enumerate(nameprep_tests):
1022 if orig is None:
1029 if orig is None:
1023 # Skipped
1030 # Skipped
1024 continue
1031 continue
1025 # The Unicode strings are given in UTF-8
1032 # The Unicode strings are given in UTF-8
1026 orig = str(orig, "utf-8")
1033 orig = str(orig, "utf-8", "surrogates")
1027 if prepped is None:
1034 if prepped is None:
1028 # Input contains prohibited characters
1035 # Input contains prohibited characters
1029 self.assertRaises(UnicodeError, nameprep, orig)
1036 self.assertRaises(UnicodeError, nameprep, orig)
1030 else:
1037 else:
1031 prepped = str(prepped, "utf-8")
1038 prepped = str(prepped, "utf-8", "surrogates")
1032 try:
1039 try:
1033 self.assertEquals(nameprep(orig), prepped)
1040 self.assertEquals(nameprep(orig), prepped)
1034 except Exception as e:
1041 except Exception as e:
1035 raise support.TestFailed("Test 3.%d: %s" % (pos+1, str(e)))
1042 raise support.TestFailed("Test 3.%d: %s" % (pos+1, str(e)))
1036
1043
1037 class IDNACodecTest(unittest.TestCase):
1044 class IDNACodecTest(unittest.TestCase):
1038 def test_builtin_decode(self):
1045 def test_builtin_decode(self):
1039 self.assertEquals(str(b"python.org", "idna"), "python.org")
1046 self.assertEquals(str(b"python.org", "idna"), "python.org")
1040 self.assertEquals(str(b"python.org.", "idna"), "python.org.")
1047 self.assertEquals(str(b"python.org.", "idna"), "python.org.")
1041 self.assertEquals(str(b"xn--pythn-mua.org", "idna"), "pyth\xf6n.org")
1048 self.assertEquals(str(b"xn--pythn-mua.org", "idna"), "pyth\xf6n.org")
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
1530 EncodedFileTest,
1537 EncodedFileTest,
1531 BasicUnicodeTest,
1538 BasicUnicodeTest,
1532 CharmapTest,
1539 CharmapTest,
1533 WithStmtTest,
1540 WithStmtTest,
1534 TypesTest,
1541 TypesTest,
1535 )
1542 )
1536
1543
1537
1544
1538 if __name__ == "__main__":
1545 if __name__ == "__main__":
1539 test_main()
1546 test_main()
OLD
NEW