1ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh#!/usr/bin/env python 2ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehfrom test import test_support 3ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehfrom test.test_support import TESTFN, _4G, bigmemtest, import_module, findfile 4ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 5ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehimport unittest 6ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehfrom cStringIO import StringIO 7ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehimport os 8ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehimport subprocess 9ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehimport sys 10ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 11ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehtry: 12ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh import threading 13ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehexcept ImportError: 14ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh threading = None 15ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 16ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehbz2 = import_module('bz2') 17ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehfrom bz2 import BZ2File, BZ2Compressor, BZ2Decompressor 18ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 19ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehhas_cmdline_bunzip2 = sys.platform not in ("win32", "os2emx", "riscos") 20ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 21ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehclass BaseTest(unittest.TestCase): 22ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh "Base for other testcases." 23ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh TEXT = 'root:x:0:0:root:/root:/bin/bash\nbin:x:1:1:bin:/bin:\ndaemon:x:2:2:daemon:/sbin:\nadm:x:3:4:adm:/var/adm:\nlp:x:4:7:lp:/var/spool/lpd:\nsync:x:5:0:sync:/sbin:/bin/sync\nshutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\nhalt:x:7:0:halt:/sbin:/sbin/halt\nmail:x:8:12:mail:/var/spool/mail:\nnews:x:9:13:news:/var/spool/news:\nuucp:x:10:14:uucp:/var/spool/uucp:\noperator:x:11:0:operator:/root:\ngames:x:12:100:games:/usr/games:\ngopher:x:13:30:gopher:/usr/lib/gopher-data:\nftp:x:14:50:FTP User:/var/ftp:/bin/bash\nnobody:x:65534:65534:Nobody:/home:\npostfix:x:100:101:postfix:/var/spool/postfix:\nniemeyer:x:500:500::/home/niemeyer:/bin/bash\npostgres:x:101:102:PostgreSQL Server:/var/lib/pgsql:/bin/bash\nmysql:x:102:103:MySQL server:/var/lib/mysql:/bin/bash\nwww:x:103:104::/var/www:/bin/false\n' 24ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh DATA = 'BZh91AY&SY.\xc8N\x18\x00\x01>_\x80\x00\x10@\x02\xff\xf0\x01\x07n\x00?\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\x17VH>\x88\xe5\xcd9gd6\x0b\n\xe9\x9b\xd5\x8a\x99\xf7\x08.K\x8ev\xfb\xf7xw\xbb\xdf\xa1\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\x13^\xca\xf3r\xd0\x03U\x922\xf26\xec\xb6\xed\x8b\xc3U\x13\x9d\xc5\x170\xa4\xfa^\x92\xacDF\x8a\x97\xd6\x19\xfe\xdd\xb8\xbd\x1a\x9a\x19\xa3\x80ankR\x8b\xe5\xd83]\xa9\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\x16\xf5\x04\xe9\xcc\xb9\x08z\x1en7.G\xfc]\xc9\x14\xe1B@\xbb!8`' 25ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh DATA_CRLF = 'BZh91AY&SY\xaez\xbbN\x00\x01H\xdf\x80\x00\x12@\x02\xff\xf0\x01\x07n\x00?\xe7\xff\xe0@\x01\xbc\xc6`\x86*\x8d=M\xa9\x9a\x86\xd0L@\x0fI\xa6!\xa1\x13\xc8\x88jdi\x8d@\x03@\x1a\x1a\x0c\x0c\x83 \x00\xc4h2\x19\x01\x82D\x84e\t\xe8\x99\x89\x19\x1ah\x00\r\x1a\x11\xaf\x9b\x0fG\xf5(\x1b\x1f?\t\x12\xcf\xb5\xfc\x95E\x00ps\x89\x12^\xa4\xdd\xa2&\x05(\x87\x04\x98\x89u\xe40%\xb6\x19\'\x8c\xc4\x89\xca\x07\x0e\x1b!\x91UIFU%C\x994!DI\xd2\xfa\xf0\xf1N8W\xde\x13A\xf5\x9cr%?\x9f3;I45A\xd1\x8bT\xb1<l\xba\xcb_\xc00xY\x17r\x17\x88\x08\x08@\xa0\ry@\x10\x04$)`\xf2\xce\x89z\xb0s\xec\x9b.iW\x9d\x81\xb5-+t\x9f\x1a\'\x97dB\xf5x\xb5\xbe.[.\xd7\x0e\x81\xe7\x08\x1cN`\x88\x10\xca\x87\xc3!"\x80\x92R\xa1/\xd1\xc0\xe6mf\xac\xbd\x99\xcca\xb3\x8780>\xa4\xc7\x8d\x1a\\"\xad\xa1\xabyBg\x15\xb9l\x88\x88\x91k"\x94\xa4\xd4\x89\xae*\xa6\x0b\x10\x0c\xd6\xd4m\xe86\xec\xb5j\x8a\x86j\';\xca.\x01I\xf2\xaaJ\xe8\x88\x8cU+t3\xfb\x0c\n\xa33\x13r2\r\x16\xe0\xb3(\xbf\x1d\x83r\xe7M\xf0D\x1365\xd8\x88\xd3\xa4\x92\xcb2\x06\x04\\\xc1\xb0\xea//\xbek&\xd8\xe6+t\xe5\xa1\x13\xada\x16\xder5"w]\xa2i\xb7[\x97R \xe2IT\xcd;Z\x04dk4\xad\x8a\t\xd3\x81z\x10\xf1:^`\xab\x1f\xc5\xdc\x91N\x14$+\x9e\xae\xd3\x80' 26ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh EMPTY_DATA = 'BZh9\x17rE8P\x90\x00\x00\x00\x00' 27ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 28ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh if has_cmdline_bunzip2: 29ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def decompress(self, data): 30ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh pop = subprocess.Popen("bunzip2", shell=True, 31ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh stdin=subprocess.PIPE, 32ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh stdout=subprocess.PIPE, 33ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh stderr=subprocess.STDOUT) 34ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh pop.stdin.write(data) 35ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh pop.stdin.close() 36ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh ret = pop.stdout.read() 37ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh pop.stdout.close() 38ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh if pop.wait() != 0: 39ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh ret = bz2.decompress(data) 40ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh return ret 41ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 42ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh else: 43ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # bunzip2 isn't available to run on Windows. 44ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def decompress(self, data): 45ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh return bz2.decompress(data) 46ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 47ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 48ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehclass BZ2FileTest(BaseTest): 49ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh "Test BZ2File type miscellaneous methods." 50ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 51ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def setUp(self): 52ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.filename = TESTFN 53ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 54ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def tearDown(self): 55ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh if os.path.isfile(self.filename): 56ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh os.unlink(self.filename) 57ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 58ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def createTempFile(self, crlf=0): 59ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with open(self.filename, "wb") as f: 60ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh if crlf: 61ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh data = self.DATA_CRLF 62ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh else: 63ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh data = self.DATA 64ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh f.write(data) 65ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 66ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testRead(self): 67ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.read()" 68ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 69ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename) as bz2f: 70ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(TypeError, bz2f.read, None) 71ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.read(), self.TEXT) 72ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 73ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testRead0(self): 74ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # Test BBZ2File.read(0)" 75ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 76ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename) as bz2f: 77ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(TypeError, bz2f.read, None) 78ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.read(0), "") 79ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 80ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testReadChunk10(self): 81ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.read() in chunks of 10 bytes" 82ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 83ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename) as bz2f: 84ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text = '' 85ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh while 1: 86ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh str = bz2f.read(10) 87ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh if not str: 88ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh break 89ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text += str 90ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(text, self.TEXT) 91ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 92ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testRead100(self): 93ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.read(100)" 94ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 95ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename) as bz2f: 96ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.read(100), self.TEXT[:100]) 97ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 98ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testReadLine(self): 99ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.readline()" 100ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 101ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename) as bz2f: 102ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(TypeError, bz2f.readline, None) 103ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh sio = StringIO(self.TEXT) 104ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh for line in sio.readlines(): 105ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.readline(), line) 106ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 107ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testReadLines(self): 108ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.readlines()" 109ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 110ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename) as bz2f: 111ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(TypeError, bz2f.readlines, None) 112ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh sio = StringIO(self.TEXT) 113ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.readlines(), sio.readlines()) 114ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 115ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testIterator(self): 116ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test iter(BZ2File)" 117ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 118ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename) as bz2f: 119ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh sio = StringIO(self.TEXT) 120ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(list(iter(bz2f)), sio.readlines()) 121ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 122ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testClosedIteratorDeadlock(self): 123ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test that iteration on a closed bz2file releases the lock." 124ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # http://bugs.python.org/issue3309 125ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 126ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f = BZ2File(self.filename) 127ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.close() 128ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(ValueError, bz2f.next) 129ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # This call will deadlock of the above .next call failed to 130ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # release the lock. 131ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(ValueError, bz2f.readlines) 132ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 133ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testXReadLines(self): 134ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.xreadlines()" 135ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 136ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f = BZ2File(self.filename) 137ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh sio = StringIO(self.TEXT) 138ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(list(bz2f.xreadlines()), sio.readlines()) 139ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.close() 140ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 141ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testUniversalNewlinesLF(self): 142ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.read() with universal newlines (\\n)" 143ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 144ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f = BZ2File(self.filename, "rU") 145ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.read(), self.TEXT) 146ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.newlines, "\n") 147ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.close() 148ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 149ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testUniversalNewlinesCRLF(self): 150ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.read() with universal newlines (\\r\\n)" 151ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile(crlf=1) 152ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f = BZ2File(self.filename, "rU") 153ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.read(), self.TEXT) 154ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.newlines, "\r\n") 155ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.close() 156ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 157ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testWrite(self): 158ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.write()" 159ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename, "w") as bz2f: 160ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(TypeError, bz2f.write) 161ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.write(self.TEXT) 162ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with open(self.filename, 'rb') as f: 163ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(self.decompress(f.read()), self.TEXT) 164ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 165ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testWriteChunks10(self): 166ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.write() with chunks of 10 bytes" 167ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename, "w") as bz2f: 168ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh n = 0 169ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh while 1: 170ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh str = self.TEXT[n*10:(n+1)*10] 171ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh if not str: 172ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh break 173ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.write(str) 174ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh n += 1 175ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with open(self.filename, 'rb') as f: 176ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(self.decompress(f.read()), self.TEXT) 177ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 178ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testWriteLines(self): 179ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.writelines()" 180ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename, "w") as bz2f: 181ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(TypeError, bz2f.writelines) 182ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh sio = StringIO(self.TEXT) 183ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.writelines(sio.readlines()) 184ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # patch #1535500 185ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(ValueError, bz2f.writelines, ["a"]) 186ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with open(self.filename, 'rb') as f: 187ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(self.decompress(f.read()), self.TEXT) 188ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 189ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testWriteMethodsOnReadOnlyFile(self): 190ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename, "w") as bz2f: 191ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.write("abc") 192ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 193ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename, "r") as bz2f: 194ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(IOError, bz2f.write, "a") 195ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(IOError, bz2f.writelines, ["a"]) 196ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 197ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testSeekForward(self): 198ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.seek(150, 0)" 199ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 200ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename) as bz2f: 201ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(TypeError, bz2f.seek) 202ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.seek(150) 203ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.read(), self.TEXT[150:]) 204ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 205ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testSeekBackwards(self): 206ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.seek(-150, 1)" 207ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 208ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename) as bz2f: 209ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.read(500) 210ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.seek(-150, 1) 211ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.read(), self.TEXT[500-150:]) 212ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 213ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testSeekBackwardsFromEnd(self): 214ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.seek(-150, 2)" 215ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 216ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename) as bz2f: 217ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.seek(-150, 2) 218ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.read(), self.TEXT[len(self.TEXT)-150:]) 219ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 220ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testSeekPostEnd(self): 221ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.seek(150000)" 222ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 223ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename) as bz2f: 224ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.seek(150000) 225ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.tell(), len(self.TEXT)) 226ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.read(), "") 227ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 228ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testSeekPostEndTwice(self): 229ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.seek(150000) twice" 230ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 231ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename) as bz2f: 232ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.seek(150000) 233ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.seek(150000) 234ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.tell(), len(self.TEXT)) 235ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.read(), "") 236ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 237ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testSeekPreStart(self): 238ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2File.seek(-150, 0)" 239ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 240ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename) as bz2f: 241ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.seek(-150) 242ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.tell(), 0) 243ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2f.read(), self.TEXT) 244ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 245ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testOpenDel(self): 246ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test opening and deleting a file many times" 247ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 248ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh for i in xrange(10000): 249ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh o = BZ2File(self.filename) 250ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh del o 251ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 252ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testOpenNonexistent(self): 253ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test opening a nonexistent file" 254ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(IOError, BZ2File, "/non/existent") 255ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 256ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testModeU(self): 257ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # Bug #1194181: bz2.BZ2File opened for write with mode "U" 258ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.createTempFile() 259ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f = BZ2File(self.filename, "U") 260ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2f.close() 261ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh f = file(self.filename) 262ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh f.seek(0, 2) 263ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(f.tell(), len(self.DATA)) 264ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh f.close() 265ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 266ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testBug1191043(self): 267ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # readlines() for files containing no newline 268ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh data = 'BZh91AY&SY\xd9b\x89]\x00\x00\x00\x03\x80\x04\x00\x02\x00\x0c\x00 \x00!\x9ah3M\x13<]\xc9\x14\xe1BCe\x8a%t' 269ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with open(self.filename, "wb") as f: 270ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh f.write(data) 271ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename) as bz2f: 272ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh lines = bz2f.readlines() 273ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(lines, ['Test']) 274ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename) as bz2f: 275ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh xlines = list(bz2f.readlines()) 276ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(xlines, ['Test']) 277ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 278ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testContextProtocol(self): 279ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # BZ2File supports the context management protocol 280ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh f = None 281ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename, "wb") as f: 282ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh f.write(b"xxx") 283ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh f = BZ2File(self.filename, "rb") 284ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh f.close() 285ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh try: 286ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with f: 287ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh pass 288ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh except ValueError: 289ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh pass 290ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh else: 291ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.fail("__enter__ on a closed file didn't raise an exception") 292ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh try: 293ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with BZ2File(self.filename, "wb") as f: 294ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 1 // 0 295ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh except ZeroDivisionError: 296ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh pass 297ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh else: 298ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.fail("1 // 0 didn't raise an exception") 299ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 300ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh @unittest.skipUnless(threading, 'Threading required for this test.') 301ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testThreading(self): 302ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # Using a BZ2File from several threads doesn't deadlock (issue #7205). 303ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh data = "1" * 2**20 304ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh nthreads = 10 305ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with bz2.BZ2File(self.filename, 'wb') as f: 306ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def comp(): 307ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh for i in range(5): 308ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh f.write(data) 309ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh threads = [threading.Thread(target=comp) for i in range(nthreads)] 310ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh for t in threads: 311ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh t.start() 312ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh for t in threads: 313ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh t.join() 314ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 315ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testMixedIterationReads(self): 316ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # Issue #8397: mixed iteration and reads should be forbidden. 317ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with bz2.BZ2File(self.filename, 'wb') as f: 318ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # The internal buffer size is hard-wired to 8192 bytes, we must 319ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # write out more than that for the test to stop half through 320ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # the buffer. 321ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh f.write(self.TEXT * 100) 322ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh with bz2.BZ2File(self.filename, 'rb') as f: 323ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh next(f) 324ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(ValueError, f.read) 325ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(ValueError, f.readline) 326ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(ValueError, f.readlines) 327ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 328ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehclass BZ2CompressorTest(BaseTest): 329ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testCompress(self): 330ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2Compressor.compress()/flush()" 331ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2c = BZ2Compressor() 332ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(TypeError, bz2c.compress) 333ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh data = bz2c.compress(self.TEXT) 334ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh data += bz2c.flush() 335ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(self.decompress(data), self.TEXT) 336ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 337ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testCompressEmptyString(self): 338ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2Compressor.compress()/flush() of empty string" 339ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2c = BZ2Compressor() 340ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh data = bz2c.compress('') 341ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh data += bz2c.flush() 342ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(data, self.EMPTY_DATA) 343ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 344ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testCompressChunks10(self): 345ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2Compressor.compress()/flush() with chunks of 10 bytes" 346ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2c = BZ2Compressor() 347ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh n = 0 348ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh data = '' 349ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh while 1: 350ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh str = self.TEXT[n*10:(n+1)*10] 351ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh if not str: 352ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh break 353ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh data += bz2c.compress(str) 354ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh n += 1 355ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh data += bz2c.flush() 356ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(self.decompress(data), self.TEXT) 357ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 358ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh @bigmemtest(_4G, memuse=1.25) 359ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testBigmem(self, size): 360ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text = "a" * size 361ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2c = bz2.BZ2Compressor() 362ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh data = bz2c.compress(text) + bz2c.flush() 363ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh del text 364ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text = self.decompress(data) 365ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(len(text), size) 366ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(text.strip("a"), "") 367ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 368ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 369ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehclass BZ2DecompressorTest(BaseTest): 370ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def test_Constructor(self): 371ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(TypeError, BZ2Decompressor, 42) 372ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 373ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testDecompress(self): 374ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2Decompressor.decompress()" 375ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2d = BZ2Decompressor() 376ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(TypeError, bz2d.decompress) 377ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text = bz2d.decompress(self.DATA) 378ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(text, self.TEXT) 379ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 380ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testDecompressChunks10(self): 381ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2Decompressor.decompress() with chunks of 10 bytes" 382ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2d = BZ2Decompressor() 383ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text = '' 384ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh n = 0 385ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh while 1: 386ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh str = self.DATA[n*10:(n+1)*10] 387ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh if not str: 388ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh break 389ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text += bz2d.decompress(str) 390ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh n += 1 391ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(text, self.TEXT) 392ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 393ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testDecompressUnusedData(self): 394ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test BZ2Decompressor.decompress() with unused data" 395ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2d = BZ2Decompressor() 396ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh unused_data = "this is unused data" 397ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text = bz2d.decompress(self.DATA+unused_data) 398ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(text, self.TEXT) 399ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(bz2d.unused_data, unused_data) 400ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 401ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testEOFError(self): 402ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Calling BZ2Decompressor.decompress() after EOS must raise EOFError" 403ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh bz2d = BZ2Decompressor() 404ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text = bz2d.decompress(self.DATA) 405ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(EOFError, bz2d.decompress, "anything") 406ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(EOFError, bz2d.decompress, "") 407ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 408ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh @bigmemtest(_4G, memuse=1.25) 409ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testBigmem(self, size): 410ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # Issue #14398: decompression fails when output data is >=2GB. 411ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh if size < _4G: 412ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.skipTest("Test needs 5GB of memory to run.") 413ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh compressed = bz2.compress("a" * _4G) 414ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text = bz2.BZ2Decompressor().decompress(compressed) 415ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(len(text), _4G) 416ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(text.strip("a"), "") 417ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 418ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 419ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehclass FuncTest(BaseTest): 420ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh "Test module functions" 421ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 422ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testCompress(self): 423ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test compress() function" 424ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh data = bz2.compress(self.TEXT) 425ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(self.decompress(data), self.TEXT) 426ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 427ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testCompressEmptyString(self): 428ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test compress() of empty string" 429ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text = bz2.compress('') 430ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(text, self.EMPTY_DATA) 431ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 432ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testDecompress(self): 433ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test decompress() function" 434ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text = bz2.decompress(self.DATA) 435ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(text, self.TEXT) 436ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 437ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testDecompressEmpty(self): 438ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test decompress() function with empty string" 439ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text = bz2.decompress("") 440ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(text, "") 441ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 442ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testDecompressToEmptyString(self): 443ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test decompress() of minimal bz2 data to empty string" 444ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text = bz2.decompress(self.EMPTY_DATA) 445ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(text, '') 446ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 447ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testDecompressIncomplete(self): 448ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # "Test decompress() function with incomplete data" 449ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertRaises(ValueError, bz2.decompress, self.DATA[:-10]) 450ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 451ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh @bigmemtest(_4G, memuse=1.25) 452ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testCompressBigmem(self, size): 453ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text = "a" * size 454ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh data = bz2.compress(text) 455ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh del text 456ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text = self.decompress(data) 457ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(len(text), size) 458ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(text.strip("a"), "") 459ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 460ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh @bigmemtest(_4G, memuse=1.25) 461ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh def testDecompressBigmem(self, size): 462ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh # Issue #14398: decompression fails when output data is >=2GB. 463ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh if size < _4G: 464ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.skipTest("Test needs 5GB of memory to run.") 465ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh compressed = bz2.compress("a" * _4G) 466ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh text = bz2.decompress(compressed) 467ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(len(text), _4G) 468ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh self.assertEqual(text.strip("a"), "") 469ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 470ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehdef test_main(): 471ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh test_support.run_unittest( 472ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh BZ2FileTest, 473ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh BZ2CompressorTest, 474ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh BZ2DecompressorTest, 475ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh FuncTest 476ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh ) 477ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh test_support.reap_children() 478ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 479ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsiehif __name__ == '__main__': 480ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh test_main() 481ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh 482ffab958fd8d42ed7227d83007350e61555a1fa36Andrew Hsieh# vim:ts=4:sw=4 483