Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(8)

Side by Side Diff: Lib/test/test_codecs.py

Issue 25270: codecs.escape_encode systemerror on empty byte string
Patch Set: Created 3 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Doc/c-api/bytes.rst ('k') | Objects/bytesobject.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import codecs 1 import codecs
2 import contextlib 2 import contextlib
3 import io 3 import io
4 import locale 4 import locale
5 import sys 5 import sys
6 import unittest 6 import unittest
7 import encodings 7 import encodings
8 8
9 from test import support 9 from test import support
10 10
(...skipping 2522 matching lines...) Expand 10 before | Expand all | Expand 10 after
2533 b"\\" + c + b"0"*i) 2533 b"\\" + c + b"0"*i)
2534 self.assertRaises(UnicodeDecodeError, decode, 2534 self.assertRaises(UnicodeDecodeError, decode,
2535 b"[\\" + c + b"0"*i + b"]") 2535 b"[\\" + c + b"0"*i + b"]")
2536 data = b"[\\" + c + b"0"*i + b"]\\" + c + b"0"*i 2536 data = b"[\\" + c + b"0"*i + b"]\\" + c + b"0"*i
2537 self.assertEqual(decode(data, "ignore"), ("[]", len(data))) 2537 self.assertEqual(decode(data, "ignore"), ("[]", len(data)))
2538 self.assertEqual(decode(data, "replace"), 2538 self.assertEqual(decode(data, "replace"),
2539 ("[\ufffd]\ufffd", len(data))) 2539 ("[\ufffd]\ufffd", len(data)))
2540 self.assertRaises(UnicodeDecodeError, decode, br"\U00110000") 2540 self.assertRaises(UnicodeDecodeError, decode, br"\U00110000")
2541 self.assertEqual(decode(br"\U00110000", "ignore"), ("", 10)) 2541 self.assertEqual(decode(br"\U00110000", "ignore"), ("", 10))
2542 self.assertEqual(decode(br"\U00110000", "replace"), ("\ufffd", 10)) 2542 self.assertEqual(decode(br"\U00110000", "replace"), ("\ufffd", 10))
2543
2544
2545 class EscapeEncodeTest(unittest.TestCase):
2546
2547 def test_escape_encode(self):
2548 tests = [
2549 (b'', (b'', 0)),
2550 (b'foobar', (b'foobar', 6)),
2551 (b'spam\0eggs', (b'spam\\x00eggs', 9)),
2552 (b'a\'b', (b"a\\'b", 3)),
2553 (b'b\\c', (b'b\\\\c', 3)),
2554 (b'c\nd', (b'c\\nd', 3)),
2555 (b'd\re', (b'd\\re', 3)),
2556 (b'f\x7fg', (b'f\\x7fg', 3)),
2557 (b'h\ri', (b'h\\ri', 3)),
storchaka 2016/09/16 13:52:41 Duplicates (b'd\re', (b'd\\re', 3))
2558 ]
2559 for data, output in tests:
2560 with self.subTest(data=data):
2561 self.assertEqual(codecs.escape_encode(data), output)
2562 self.assertRaises(TypeError, codecs.escape_encode, 'spam')
2563 self.assertRaises(TypeError, codecs.escape_encode, bytearray(b'spam'))
2543 2564
2544 2565
2545 class SurrogateEscapeTest(unittest.TestCase): 2566 class SurrogateEscapeTest(unittest.TestCase):
2546 2567
2547 def test_utf8(self): 2568 def test_utf8(self):
2548 # Bad byte 2569 # Bad byte
2549 self.assertEqual(b"foo\x80bar".decode("utf-8", "surrogateescape"), 2570 self.assertEqual(b"foo\x80bar".decode("utf-8", "surrogateescape"),
2550 "foo\udc80bar") 2571 "foo\udc80bar")
2551 self.assertEqual("foo\udc80bar".encode("utf-8", "surrogateescape"), 2572 self.assertEqual("foo\udc80bar".encode("utf-8", "surrogateescape"),
2552 b"foo\x80bar") 2573 b"foo\x80bar")
(...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after
3225 for data, expected in ( 3246 for data, expected in (
3226 (b'abc', 'abc'), 3247 (b'abc', 'abc'),
3227 (b'[\x80\xff]', '[\x80\xff]'), 3248 (b'[\x80\xff]', '[\x80\xff]'),
3228 ): 3249 ):
3229 with self.subTest(data=data, expected=expected): 3250 with self.subTest(data=data, expected=expected):
3230 self.assertEqual(data.decode('latin1'), expected) 3251 self.assertEqual(data.decode('latin1'), expected)
3231 3252
3232 3253
3233 if __name__ == "__main__": 3254 if __name__ == "__main__":
3234 unittest.main() 3255 unittest.main()
OLDNEW
« no previous file with comments | « Doc/c-api/bytes.rst ('k') | Objects/bytesobject.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+