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

Delta Between Two Patch Sets: Lib/test/test_bz2.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 5 years, 12 months ago
Right Patch Set: Created 5 years, 6 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_builtin.py ('k') | Lib/test/test_capi.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #!/usr/bin/env python3
2 from test import support 1 from test import support
3 from test.support import bigmemtest, _4G 2 from test.support import bigmemtest, _4G
4 3
5 import unittest 4 import unittest
6 from io import BytesIO 5 from io import BytesIO
7 import os 6 import os
7 import pickle
8 import random 8 import random
9 import subprocess 9 import subprocess
10 import sys 10 import sys
11 from test.support import unlink
11 12
12 try: 13 try:
13 import threading 14 import threading
14 except ImportError: 15 except ImportError:
15 threading = None 16 threading = None
16 17
17 # Skip tests if the bz2 module doesn't exist. 18 # Skip tests if the bz2 module doesn't exist.
18 bz2 = support.import_module('bz2') 19 bz2 = support.import_module('bz2')
19 from bz2 import BZ2File, BZ2Compressor, BZ2Decompressor 20 from bz2 import BZ2File, BZ2Compressor, BZ2Decompressor
20 21
(...skipping 20 matching lines...) Expand all
41 b'nobody:x:65534:65534:Nobody:/home:\n', 42 b'nobody:x:65534:65534:Nobody:/home:\n',
42 b'postfix:x:100:101:postfix:/var/spool/postfix:\n', 43 b'postfix:x:100:101:postfix:/var/spool/postfix:\n',
43 b'niemeyer:x:500:500::/home/niemeyer:/bin/bash\n', 44 b'niemeyer:x:500:500::/home/niemeyer:/bin/bash\n',
44 b'postgres:x:101:102:PostgreSQL Server:/var/lib/pgsql:/bin/bash\n', 45 b'postgres:x:101:102:PostgreSQL Server:/var/lib/pgsql:/bin/bash\n',
45 b'mysql:x:102:103:MySQL server:/var/lib/mysql:/bin/bash\n', 46 b'mysql:x:102:103:MySQL server:/var/lib/mysql:/bin/bash\n',
46 b'www:x:103:104::/var/www:/bin/false\n', 47 b'www:x:103:104::/var/www:/bin/false\n',
47 ] 48 ]
48 TEXT = b''.join(TEXT_LINES) 49 TEXT = b''.join(TEXT_LINES)
49 DATA = b'BZh91AY&SY.\xc8N\x18\x00\x01>_\x80\x00\x10@\x02\xff\xf0\x01\x07n\x0 0?\xe7\xff\xe00\x01\x99\xaa\x00\xc0\x03F\x86\x8c#&\x83F\x9a\x03\x06\xa6\xd0\xa6\ x93M\x0fQ\xa7\xa8\x06\x804hh\x12$\x11\xa4i4\xf14S\xd2<Q\xb5\x0fH\xd3\xd4\xdd\xd5 \x87\xbb\xf8\x94\r\x8f\xafI\x12\xe1\xc9\xf8/E\x00pu\x89\x12]\xc9\xbbDL\nQ\x0e\t1 \x12\xdf\xa0\xc0\x97\xac2O9\x89\x13\x94\x0e\x1c7\x0ed\x95I\x0c\xaaJ\xa4\x18L\x10 \x05#\x9c\xaf\xba\xbc/\x97\x8a#C\xc8\xe1\x8cW\xf9\xe2\xd0\xd6M\xa7\x8bXa<e\x84t\ xcbL\xb3\xa7\xd9\xcd\xd1\xcb\x84.\xaf\xb3\xab\xab\xad`n}\xa0lh\tE,\x8eZ\x15\x17V H>\x88\xe5\xcd9gd6\x0b\n\xe9\x9b\xd5\x8a\x99\xf7\x08.K\x8ev\xfb\xf7xw\xbb\xdf\xa 1\x92\xf1\xdd|/";\xa2\xba\x9f\xd5\xb1#A\xb6\xf6\xb3o\xc9\xc5y\\\xebO\xe7\x85\x9a \xbc\xb6f8\x952\xd5\xd7"%\x89>V,\xf7\xa6z\xe2\x9f\xa3\xdf\x11\x11"\xd6E)I\xa9\x1 3^\xca\xf3r\xd0\x03U\x922\xf26\xec\xb6\xed\x8b\xc3U\x13\x9d\xc5\x170\xa4\xfa^\x9 2\xacDF\x8a\x97\xd6\x19\xfe\xdd\xb8\xbd\x1a\x9a\x19\xa3\x80ankR\x8b\xe5\xd83]\xa 9\xc6\x08\x82f\xf6\xb9"6l$\xb8j@\xc0\x8a\xb0l1..\xbak\x83ls\x15\xbc\xf4\xc1\x13\ xbe\xf8E\xb8\x9d\r\xa8\x9dk\x84\xd3n\xfa\xacQ\x07\xb1%y\xaav\xb4\x08\xe0z\x1b\x1 6\xf5\x04\xe9\xcc\xb9\x08z\x1en7.G\xfc]\xc9\x14\xe1B@\xbb!8`' 50 DATA = b'BZh91AY&SY.\xc8N\x18\x00\x01>_\x80\x00\x10@\x02\xff\xf0\x01\x07n\x0 0?\xe7\xff\xe00\x01\x99\xaa\x00\xc0\x03F\x86\x8c#&\x83F\x9a\x03\x06\xa6\xd0\xa6\ x93M\x0fQ\xa7\xa8\x06\x804hh\x12$\x11\xa4i4\xf14S\xd2<Q\xb5\x0fH\xd3\xd4\xdd\xd5 \x87\xbb\xf8\x94\r\x8f\xafI\x12\xe1\xc9\xf8/E\x00pu\x89\x12]\xc9\xbbDL\nQ\x0e\t1 \x12\xdf\xa0\xc0\x97\xac2O9\x89\x13\x94\x0e\x1c7\x0ed\x95I\x0c\xaaJ\xa4\x18L\x10 \x05#\x9c\xaf\xba\xbc/\x97\x8a#C\xc8\xe1\x8cW\xf9\xe2\xd0\xd6M\xa7\x8bXa<e\x84t\ xcbL\xb3\xa7\xd9\xcd\xd1\xcb\x84.\xaf\xb3\xab\xab\xad`n}\xa0lh\tE,\x8eZ\x15\x17V H>\x88\xe5\xcd9gd6\x0b\n\xe9\x9b\xd5\x8a\x99\xf7\x08.K\x8ev\xfb\xf7xw\xbb\xdf\xa 1\x92\xf1\xdd|/";\xa2\xba\x9f\xd5\xb1#A\xb6\xf6\xb3o\xc9\xc5y\\\xebO\xe7\x85\x9a \xbc\xb6f8\x952\xd5\xd7"%\x89>V,\xf7\xa6z\xe2\x9f\xa3\xdf\x11\x11"\xd6E)I\xa9\x1 3^\xca\xf3r\xd0\x03U\x922\xf26\xec\xb6\xed\x8b\xc3U\x13\x9d\xc5\x170\xa4\xfa^\x9 2\xacDF\x8a\x97\xd6\x19\xfe\xdd\xb8\xbd\x1a\x9a\x19\xa3\x80ankR\x8b\xe5\xd83]\xa 9\xc6\x08\x82f\xf6\xb9"6l$\xb8j@\xc0\x8a\xb0l1..\xbak\x83ls\x15\xbc\xf4\xc1\x13\ xbe\xf8E\xb8\x9d\r\xa8\x9dk\x84\xd3n\xfa\xacQ\x07\xb1%y\xaav\xb4\x08\xe0z\x1b\x1 6\xf5\x04\xe9\xcc\xb9\x08z\x1en7.G\xfc]\xc9\x14\xe1B@\xbb!8`'
50 EMPTY_DATA = b'BZh9\x17rE8P\x90\x00\x00\x00\x00' 51 EMPTY_DATA = b'BZh9\x17rE8P\x90\x00\x00\x00\x00'
52 BAD_DATA = b'this is not a valid bzip2 file'
51 53
52 def setUp(self): 54 def setUp(self):
53 self.filename = support.TESTFN 55 self.filename = support.TESTFN
54 56
55 def tearDown(self): 57 def tearDown(self):
56 if os.path.isfile(self.filename): 58 if os.path.isfile(self.filename):
57 os.unlink(self.filename) 59 os.unlink(self.filename)
58 60
59 if sys.platform == "win32": 61 if sys.platform == "win32":
60 # bunzip2 isn't available to run on Windows. 62 # bunzip2 isn't available to run on Windows.
(...skipping 10 matching lines...) Expand all
71 ret = pop.stdout.read() 73 ret = pop.stdout.read()
72 pop.stdout.close() 74 pop.stdout.close()
73 if pop.wait() != 0: 75 if pop.wait() != 0:
74 ret = bz2.decompress(data) 76 ret = bz2.decompress(data)
75 return ret 77 return ret
76 78
77 79
78 class BZ2FileTest(BaseTest): 80 class BZ2FileTest(BaseTest):
79 "Test the BZ2File class." 81 "Test the BZ2File class."
80 82
81 def createTempFile(self, streams=1): 83 def createTempFile(self, streams=1, suffix=b""):
82 with open(self.filename, "wb") as f: 84 with open(self.filename, "wb") as f:
83 f.write(self.DATA * streams) 85 f.write(self.DATA * streams)
86 f.write(suffix)
84 87
85 def testBadArgs(self): 88 def testBadArgs(self):
86 self.assertRaises(TypeError, BZ2File, 123.456) 89 self.assertRaises(TypeError, BZ2File, 123.456)
87 self.assertRaises(ValueError, BZ2File, "/dev/null", "z") 90 self.assertRaises(ValueError, BZ2File, "/dev/null", "z")
88 self.assertRaises(ValueError, BZ2File, "/dev/null", "rx") 91 self.assertRaises(ValueError, BZ2File, "/dev/null", "rx")
89 self.assertRaises(ValueError, BZ2File, "/dev/null", "rbt") 92 self.assertRaises(ValueError, BZ2File, "/dev/null", "rbt")
90 self.assertRaises(ValueError, BZ2File, "/dev/null", compresslevel=0) 93 self.assertRaises(ValueError, BZ2File, "/dev/null", compresslevel=0)
91 self.assertRaises(ValueError, BZ2File, "/dev/null", compresslevel=10) 94 self.assertRaises(ValueError, BZ2File, "/dev/null", compresslevel=10)
92 95
93 def testRead(self): 96 def testRead(self):
94 self.createTempFile() 97 self.createTempFile()
95 with BZ2File(self.filename) as bz2f: 98 with BZ2File(self.filename) as bz2f:
96 self.assertRaises(TypeError, bz2f.read, None) 99 self.assertRaises(TypeError, bz2f.read, None)
97 self.assertEqual(bz2f.read(), self.TEXT) 100 self.assertEqual(bz2f.read(), self.TEXT)
98 101
102 def testReadBadFile(self):
103 self.createTempFile(streams=0, suffix=self.BAD_DATA)
104 with BZ2File(self.filename) as bz2f:
105 self.assertRaises(OSError, bz2f.read)
106
99 def testReadMultiStream(self): 107 def testReadMultiStream(self):
100 self.createTempFile(streams=5) 108 self.createTempFile(streams=5)
101 with BZ2File(self.filename) as bz2f: 109 with BZ2File(self.filename) as bz2f:
102 self.assertRaises(TypeError, bz2f.read, None) 110 self.assertRaises(TypeError, bz2f.read, None)
103 self.assertEqual(bz2f.read(), self.TEXT * 5) 111 self.assertEqual(bz2f.read(), self.TEXT * 5)
104 112
105 def testReadMonkeyMultiStream(self): 113 def testReadMonkeyMultiStream(self):
106 # Test BZ2File.read() on a multi-stream archive where a stream 114 # Test BZ2File.read() on a multi-stream archive where a stream
107 # boundary coincides with the end of the raw read buffer. 115 # boundary coincides with the end of the raw read buffer.
108 buffer_size = bz2._BUFFER_SIZE 116 buffer_size = bz2._BUFFER_SIZE
109 bz2._BUFFER_SIZE = len(self.DATA) 117 bz2._BUFFER_SIZE = len(self.DATA)
110 try: 118 try:
111 self.createTempFile(streams=5) 119 self.createTempFile(streams=5)
112 with BZ2File(self.filename) as bz2f: 120 with BZ2File(self.filename) as bz2f:
113 self.assertRaises(TypeError, bz2f.read, None) 121 self.assertRaises(TypeError, bz2f.read, None)
114 self.assertEqual(bz2f.read(), self.TEXT * 5) 122 self.assertEqual(bz2f.read(), self.TEXT * 5)
115 finally: 123 finally:
116 bz2._BUFFER_SIZE = buffer_size 124 bz2._BUFFER_SIZE = buffer_size
125
126 def testReadTrailingJunk(self):
127 self.createTempFile(suffix=self.BAD_DATA)
128 with BZ2File(self.filename) as bz2f:
129 self.assertEqual(bz2f.read(), self.TEXT)
130
131 def testReadMultiStreamTrailingJunk(self):
132 self.createTempFile(streams=5, suffix=self.BAD_DATA)
133 with BZ2File(self.filename) as bz2f:
134 self.assertEqual(bz2f.read(), self.TEXT * 5)
117 135
118 def testRead0(self): 136 def testRead0(self):
119 self.createTempFile() 137 self.createTempFile()
120 with BZ2File(self.filename) as bz2f: 138 with BZ2File(self.filename) as bz2f:
121 self.assertRaises(TypeError, bz2f.read, None) 139 self.assertRaises(TypeError, bz2f.read, None)
122 self.assertEqual(bz2f.read(0), b"") 140 self.assertEqual(bz2f.read(0), b"")
123 141
124 def testReadChunk10(self): 142 def testReadChunk10(self):
125 self.createTempFile() 143 self.createTempFile()
126 with BZ2File(self.filename) as bz2f: 144 with BZ2File(self.filename) as bz2f:
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 bz2f.seek(150) 580 bz2f.seek(150)
563 self.assertEqual(bz2f.read(), self.TEXT[150:]) 581 self.assertEqual(bz2f.read(), self.TEXT[150:])
564 582
565 def testSeekBackwardsBytesIO(self): 583 def testSeekBackwardsBytesIO(self):
566 with BytesIO(self.DATA) as bio: 584 with BytesIO(self.DATA) as bio:
567 with BZ2File(bio) as bz2f: 585 with BZ2File(bio) as bz2f:
568 bz2f.read(500) 586 bz2f.read(500)
569 bz2f.seek(-150, 1) 587 bz2f.seek(-150, 1)
570 self.assertEqual(bz2f.read(), self.TEXT[500-150:]) 588 self.assertEqual(bz2f.read(), self.TEXT[500-150:])
571 589
590 def test_read_truncated(self):
591 # Drop the eos_magic field (6 bytes) and CRC (4 bytes).
592 truncated = self.DATA[:-10]
593 with BZ2File(BytesIO(truncated)) as f:
594 self.assertRaises(EOFError, f.read)
595 with BZ2File(BytesIO(truncated)) as f:
596 self.assertEqual(f.read(len(self.TEXT)), self.TEXT)
597 self.assertRaises(EOFError, f.read, 1)
598 # Incomplete 4-byte file header, and block header of at least 146 bits.
599 for i in range(22):
600 with BZ2File(BytesIO(truncated[:i])) as f:
601 self.assertRaises(EOFError, f.read, 1)
602
572 603
573 class BZ2CompressorTest(BaseTest): 604 class BZ2CompressorTest(BaseTest):
574 def testCompress(self): 605 def testCompress(self):
575 bz2c = BZ2Compressor() 606 bz2c = BZ2Compressor()
576 self.assertRaises(TypeError, bz2c.compress) 607 self.assertRaises(TypeError, bz2c.compress)
577 data = bz2c.compress(self.TEXT) 608 data = bz2c.compress(self.TEXT)
578 data += bz2c.flush() 609 data += bz2c.flush()
579 self.assertEqual(self.decompress(data), self.TEXT) 610 self.assertEqual(self.decompress(data), self.TEXT)
580 611
581 def testCompressEmptyString(self): 612 def testCompressEmptyString(self):
(...skipping 25 matching lines...) Expand all
607 compressed += bz2c.flush() 638 compressed += bz2c.flush()
608 finally: 639 finally:
609 data = None # Release memory 640 data = None # Release memory
610 data = bz2.decompress(compressed) 641 data = bz2.decompress(compressed)
611 try: 642 try:
612 self.assertEqual(len(data), size) 643 self.assertEqual(len(data), size)
613 self.assertEqual(len(data.strip(b"x")), 0) 644 self.assertEqual(len(data.strip(b"x")), 0)
614 finally: 645 finally:
615 data = None 646 data = None
616 647
648 def testPickle(self):
649 with self.assertRaises(TypeError):
650 pickle.dumps(BZ2Compressor())
651
652
617 class BZ2DecompressorTest(BaseTest): 653 class BZ2DecompressorTest(BaseTest):
618 def test_Constructor(self): 654 def test_Constructor(self):
619 self.assertRaises(TypeError, BZ2Decompressor, 42) 655 self.assertRaises(TypeError, BZ2Decompressor, 42)
620 656
621 def testDecompress(self): 657 def testDecompress(self):
622 bz2d = BZ2Decompressor() 658 bz2d = BZ2Decompressor()
623 self.assertRaises(TypeError, bz2d.decompress) 659 self.assertRaises(TypeError, bz2d.decompress)
624 text = bz2d.decompress(self.DATA) 660 text = bz2d.decompress(self.DATA)
625 self.assertEqual(text, self.TEXT) 661 self.assertEqual(text, self.TEXT)
626 662
(...skipping 15 matching lines...) Expand all
642 text = bz2d.decompress(self.DATA+unused_data) 678 text = bz2d.decompress(self.DATA+unused_data)
643 self.assertEqual(text, self.TEXT) 679 self.assertEqual(text, self.TEXT)
644 self.assertEqual(bz2d.unused_data, unused_data) 680 self.assertEqual(bz2d.unused_data, unused_data)
645 681
646 def testEOFError(self): 682 def testEOFError(self):
647 bz2d = BZ2Decompressor() 683 bz2d = BZ2Decompressor()
648 text = bz2d.decompress(self.DATA) 684 text = bz2d.decompress(self.DATA)
649 self.assertRaises(EOFError, bz2d.decompress, b"anything") 685 self.assertRaises(EOFError, bz2d.decompress, b"anything")
650 self.assertRaises(EOFError, bz2d.decompress, b"") 686 self.assertRaises(EOFError, bz2d.decompress, b"")
651 687
652 @bigmemtest(size=_4G + 100, memuse=3) 688 @bigmemtest(size=_4G + 100, memuse=3.3)
653 def testDecompress4G(self, size): 689 def testDecompress4G(self, size):
654 # "Test BZ2Decompressor.decompress() with >4GiB input" 690 # "Test BZ2Decompressor.decompress() with >4GiB input"
655 blocksize = 10 * 1024 * 1024 691 blocksize = 10 * 1024 * 1024
656 block = random.getrandbits(blocksize * 8).to_bytes(blocksize, 'little') 692 block = random.getrandbits(blocksize * 8).to_bytes(blocksize, 'little')
657 try: 693 try:
658 data = block * (size // blocksize + 1) 694 data = block * (size // blocksize + 1)
659 compressed = bz2.compress(data) 695 compressed = bz2.compress(data)
660 bz2d = BZ2Decompressor() 696 bz2d = BZ2Decompressor()
661 decompressed = bz2d.decompress(compressed) 697 decompressed = bz2d.decompress(compressed)
662 self.assertEqual(decompressed, data) 698 self.assertEqual(decompressed, data)
663 finally: 699 finally:
664 data = None 700 data = None
665 compressed = None 701 compressed = None
666 decompressed = None 702 decompressed = None
667 703
704 def testPickle(self):
705 with self.assertRaises(TypeError):
706 pickle.dumps(BZ2Decompressor())
707
668 708
669 class CompressDecompressTest(BaseTest): 709 class CompressDecompressTest(BaseTest):
670 def testCompress(self): 710 def testCompress(self):
671 data = bz2.compress(self.TEXT) 711 data = bz2.compress(self.TEXT)
672 self.assertEqual(self.decompress(data), self.TEXT) 712 self.assertEqual(self.decompress(data), self.TEXT)
673 713
674 def testCompressEmptyString(self): 714 def testCompressEmptyString(self):
675 text = bz2.compress(b'') 715 text = bz2.compress(b'')
676 self.assertEqual(text, self.EMPTY_DATA) 716 self.assertEqual(text, self.EMPTY_DATA)
677 717
678 def testDecompress(self): 718 def testDecompress(self):
679 text = bz2.decompress(self.DATA) 719 text = bz2.decompress(self.DATA)
680 self.assertEqual(text, self.TEXT) 720 self.assertEqual(text, self.TEXT)
681 721
682 def testDecompressEmpty(self): 722 def testDecompressEmpty(self):
683 text = bz2.decompress(b"") 723 text = bz2.decompress(b"")
684 self.assertEqual(text, b"") 724 self.assertEqual(text, b"")
685 725
686 def testDecompressToEmptyString(self): 726 def testDecompressToEmptyString(self):
687 text = bz2.decompress(self.EMPTY_DATA) 727 text = bz2.decompress(self.EMPTY_DATA)
688 self.assertEqual(text, b'') 728 self.assertEqual(text, b'')
689 729
690 def testDecompressIncomplete(self): 730 def testDecompressIncomplete(self):
691 self.assertRaises(ValueError, bz2.decompress, self.DATA[:-10]) 731 self.assertRaises(ValueError, bz2.decompress, self.DATA[:-10])
692 732
733 def testDecompressBadData(self):
734 self.assertRaises(OSError, bz2.decompress, self.BAD_DATA)
735
693 def testDecompressMultiStream(self): 736 def testDecompressMultiStream(self):
694 text = bz2.decompress(self.DATA * 5) 737 text = bz2.decompress(self.DATA * 5)
695 self.assertEqual(text, self.TEXT * 5) 738 self.assertEqual(text, self.TEXT * 5)
696 739
740 def testDecompressTrailingJunk(self):
741 text = bz2.decompress(self.DATA + self.BAD_DATA)
742 self.assertEqual(text, self.TEXT)
743
744 def testDecompressMultiStreamTrailingJunk(self):
745 text = bz2.decompress(self.DATA * 5 + self.BAD_DATA)
746 self.assertEqual(text, self.TEXT * 5)
747
697 748
698 class OpenTest(BaseTest): 749 class OpenTest(BaseTest):
699 "Test the open function." 750 "Test the open function."
700 751
701 def open(self, *args, **kwargs): 752 def open(self, *args, **kwargs):
702 return bz2.open(*args, **kwargs) 753 return bz2.open(*args, **kwargs)
703 754
704 def test_binary_modes(self): 755 def test_binary_modes(self):
705 with self.open(self.filename, "wb") as f: 756 for mode in ("wb", "xb"):
706 f.write(self.TEXT) 757 if mode == "xb":
707 with open(self.filename, "rb") as f: 758 unlink(self.filename)
708 file_data = self.decompress(f.read()) 759 with self.open(self.filename, mode) as f:
709 self.assertEqual(file_data, self.TEXT) 760 f.write(self.TEXT)
710 with self.open(self.filename, "rb") as f: 761 with open(self.filename, "rb") as f:
711 self.assertEqual(f.read(), self.TEXT) 762 file_data = self.decompress(f.read())
712 with self.open(self.filename, "ab") as f: 763 self.assertEqual(file_data, self.TEXT)
713 f.write(self.TEXT) 764 with self.open(self.filename, "rb") as f:
714 with open(self.filename, "rb") as f: 765 self.assertEqual(f.read(), self.TEXT)
715 file_data = self.decompress(f.read()) 766 with self.open(self.filename, "ab") as f:
716 self.assertEqual(file_data, self.TEXT * 2) 767 f.write(self.TEXT)
768 with open(self.filename, "rb") as f:
769 file_data = self.decompress(f.read())
770 self.assertEqual(file_data, self.TEXT * 2)
717 771
718 def test_implicit_binary_modes(self): 772 def test_implicit_binary_modes(self):
719 # Test implicit binary modes (no "b" or "t" in mode string). 773 # Test implicit binary modes (no "b" or "t" in mode string).
720 with self.open(self.filename, "w") as f: 774 for mode in ("w", "x"):
721 f.write(self.TEXT) 775 if mode == "x":
722 with open(self.filename, "rb") as f: 776 unlink(self.filename)
723 file_data = self.decompress(f.read()) 777 with self.open(self.filename, mode) as f:
724 self.assertEqual(file_data, self.TEXT) 778 f.write(self.TEXT)
725 with self.open(self.filename, "r") as f: 779 with open(self.filename, "rb") as f:
726 self.assertEqual(f.read(), self.TEXT) 780 file_data = self.decompress(f.read())
727 with self.open(self.filename, "a") as f: 781 self.assertEqual(file_data, self.TEXT)
728 f.write(self.TEXT) 782 with self.open(self.filename, "r") as f:
729 with open(self.filename, "rb") as f: 783 self.assertEqual(f.read(), self.TEXT)
730 file_data = self.decompress(f.read()) 784 with self.open(self.filename, "a") as f:
731 self.assertEqual(file_data, self.TEXT * 2) 785 f.write(self.TEXT)
786 with open(self.filename, "rb") as f:
787 file_data = self.decompress(f.read())
788 self.assertEqual(file_data, self.TEXT * 2)
732 789
733 def test_text_modes(self): 790 def test_text_modes(self):
734 text = self.TEXT.decode("ascii") 791 text = self.TEXT.decode("ascii")
735 text_native_eol = text.replace("\n", os.linesep) 792 text_native_eol = text.replace("\n", os.linesep)
736 with self.open(self.filename, "wt") as f: 793 for mode in ("wt", "xt"):
737 f.write(text) 794 if mode == "xt":
738 with open(self.filename, "rb") as f: 795 unlink(self.filename)
739 file_data = self.decompress(f.read()).decode("ascii") 796 with self.open(self.filename, mode) as f:
740 self.assertEqual(file_data, text_native_eol) 797 f.write(text)
741 with self.open(self.filename, "rt") as f: 798 with open(self.filename, "rb") as f:
742 self.assertEqual(f.read(), text) 799 file_data = self.decompress(f.read()).decode("ascii")
743 with self.open(self.filename, "at") as f: 800 self.assertEqual(file_data, text_native_eol)
744 f.write(text) 801 with self.open(self.filename, "rt") as f:
745 with open(self.filename, "rb") as f: 802 self.assertEqual(f.read(), text)
746 file_data = self.decompress(f.read()).decode("ascii") 803 with self.open(self.filename, "at") as f:
747 self.assertEqual(file_data, text_native_eol * 2) 804 f.write(text)
805 with open(self.filename, "rb") as f:
806 file_data = self.decompress(f.read()).decode("ascii")
807 self.assertEqual(file_data, text_native_eol * 2)
808
809 def test_x_mode(self):
810 for mode in ("x", "xb", "xt"):
811 unlink(self.filename)
812 with self.open(self.filename, mode) as f:
813 pass
814 with self.assertRaises(FileExistsError):
815 with self.open(self.filename, mode) as f:
816 pass
748 817
749 def test_fileobj(self): 818 def test_fileobj(self):
750 with self.open(BytesIO(self.DATA), "r") as f: 819 with self.open(BytesIO(self.DATA), "r") as f:
751 self.assertEqual(f.read(), self.TEXT) 820 self.assertEqual(f.read(), self.TEXT)
752 with self.open(BytesIO(self.DATA), "rb") as f: 821 with self.open(BytesIO(self.DATA), "rb") as f:
753 self.assertEqual(f.read(), self.TEXT) 822 self.assertEqual(f.read(), self.TEXT)
754 text = self.TEXT.decode("ascii") 823 text = self.TEXT.decode("ascii")
755 with self.open(BytesIO(self.DATA), "rt") as f: 824 with self.open(BytesIO(self.DATA), "rt") as f:
756 self.assertEqual(f.read(), text) 825 self.assertEqual(f.read(), text)
757 826
758 def test_bad_params(self): 827 def test_bad_params(self):
759 # Test invalid parameter combinations. 828 # Test invalid parameter combinations.
760 self.assertRaises(ValueError, 829 self.assertRaises(ValueError,
761 self.open, self.filename, "wbt") 830 self.open, self.filename, "wbt")
831 self.assertRaises(ValueError,
832 self.open, self.filename, "xbt")
762 self.assertRaises(ValueError, 833 self.assertRaises(ValueError,
763 self.open, self.filename, "rb", encoding="utf-8") 834 self.open, self.filename, "rb", encoding="utf-8")
764 self.assertRaises(ValueError, 835 self.assertRaises(ValueError,
765 self.open, self.filename, "rb", errors="ignore") 836 self.open, self.filename, "rb", errors="ignore")
766 self.assertRaises(ValueError, 837 self.assertRaises(ValueError,
767 self.open, self.filename, "rb", newline="\n") 838 self.open, self.filename, "rb", newline="\n")
768 839
769 def test_encoding(self): 840 def test_encoding(self):
770 # Test non-default encoding. 841 # Test non-default encoding.
771 text = self.TEXT.decode("ascii") 842 text = self.TEXT.decode("ascii")
(...skipping 28 matching lines...) Expand all
800 BZ2FileTest, 871 BZ2FileTest,
801 BZ2CompressorTest, 872 BZ2CompressorTest,
802 BZ2DecompressorTest, 873 BZ2DecompressorTest,
803 CompressDecompressTest, 874 CompressDecompressTest,
804 OpenTest, 875 OpenTest,
805 ) 876 )
806 support.reap_children() 877 support.reap_children()
807 878
808 if __name__ == '__main__': 879 if __name__ == '__main__':
809 test_main() 880 test_main()
LEFTRIGHT

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