| 1 |
from test import support |
| 2 |
from test.support import bigmemtest, _4G |
| 3 |
|
| 4 |
import array |
| 5 |
import unittest |
| 6 |
import io |
| 7 |
from io import BytesIO, DEFAULT_BUFFER_SIZE |
| 8 |
import os |
| 9 |
import pickle |
| 10 |
import glob |
| 11 |
import tempfile |
| 12 |
import random |
| 13 |
import shutil |
| 14 |
import subprocess |
| 15 |
import threading |
| 16 |
from test.support import import_helper |
| 17 |
from test.support import threading_helper |
| 18 |
from test.support.os_helper import unlink, FakePath |
| 19 |
from compression._common import _streams |
| 20 |
import sys |
| 21 |
|
| 22 |
|
| 23 |
# Skip tests if the bz2 module doesn't exist. |
| 24 |
bz2 = import_helper.import_module('bz2') |
| 25 |
from bz2 import BZ2File, BZ2Compressor, BZ2Decompressor |
| 26 |
|
| 27 |
has_cmdline_bunzip2 = None |
| 28 |
|
| 29 |
def ext_decompress(data): |
| 30 |
global has_cmdline_bunzip2 |
| 31 |
if has_cmdline_bunzip2 is None: |
| 32 |
has_cmdline_bunzip2 = bool(shutil.which('bunzip2')) |
| 33 |
if has_cmdline_bunzip2: |
| 34 |
return subprocess.check_output(['bunzip2'], input=data) |
| 35 |
else: |
| 36 |
return bz2.decompress(data) |
| 37 |
|
| 38 |
class BaseTest(unittest.TestCase): |
| 39 |
"Base for other testcases." |
| 40 |
|
| 41 |
TEXT_LINES = [ |
| 42 |
b'root:x:0:0:root:/root:/bin/bash\n', |
| 43 |
b'bin:x:1:1:bin:/bin:\n', |
| 44 |
b'daemon:x:2:2:daemon:/sbin:\n', |
| 45 |
b'adm:x:3:4:adm:/var/adm:\n', |
| 46 |
b'lp:x:4:7:lp:/var/spool/lpd:\n', |
| 47 |
b'sync:x:5:0:sync:/sbin:/bin/sync\n', |
| 48 |
b'shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\n', |
| 49 |
b'halt:x:7:0:halt:/sbin:/sbin/halt\n', |
| 50 |
b'mail:x:8:12:mail:/var/spool/mail:\n', |
| 51 |
b'news:x:9:13:news:/var/spool/news:\n', |
| 52 |
b'uucp:x:10:14:uucp:/var/spool/uucp:\n', |
| 53 |
b'operator:x:11:0:operator:/root:\n', |
| 54 |
b'games:x:12:100:games:/usr/games:\n', |
| 55 |
b'gopher:x:13:30:gopher:/usr/lib/gopher-data:\n', |
| 56 |
b'ftp:x:14:50:FTP User:/var/ftp:/bin/bash\n', |
| 57 |
b'nobody:x:65534:65534:Nobody:/home:\n', |
| 58 |
b'postfix:x:100:101:postfix:/var/spool/postfix:\n', |
| 59 |
b'niemeyer:x:500:500::/home/niemeyer:/bin/bash\n', |
| 60 |
b'postgres:x:101:102:PostgreSQL Server:/var/lib/pgsql:/bin/bash\n', |
| 61 |
b'mysql:x:102:103:MySQL server:/var/lib/mysql:/bin/bash\n', |
| 62 |
b'www:x:103:104::/var/www:/bin/false\n', |
| 63 |
] |
| 64 |
TEXT = b''.join(TEXT_LINES) |
| 65 |
DATA = b'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`' |
| 66 |
EMPTY_DATA = b'BZh9\x17rE8P\x90\x00\x00\x00\x00' |
| 67 |
BAD_DATA = b'this is not a valid bzip2 file' |
| 68 |
|
| 69 |
# Some tests need more than one block of data. The bz2 module does not |
| 70 |
# support flushing a block during compression, so we must read in data until |
| 71 |
# there are at least 2 blocks. Since different orderings of Python files may |
| 72 |
# be compressed differently, we need to check the compression output for |
| 73 |
# more than one bzip2 block header magic, a hex encoding of Pi |
| 74 |
# (0x314159265359) |
| 75 |
bz2_block_magic = bytes.fromhex('314159265359') |
| 76 |
test_size = 0 |
| 77 |
BIG_TEXT = b'' |
| 78 |
BIG_DATA = b'' |
| 79 |
compressor = BZ2Compressor(1) |
| 80 |
for fname in glob.glob(os.path.join(glob.escape(os.path.dirname(__file__)), '*.py')): |
| 81 |
with open(fname, 'rb') as fh: |
| 82 |
data = fh.read() |
| 83 |
BIG_DATA += compressor.compress(data) |
| 84 |
BIG_TEXT += data |
| 85 |
# TODO(emmatyping): if it is impossible for a block header to cross |
| 86 |
# multiple outputs, we can just search the output of each compress call |
| 87 |
# which should be more efficient |
| 88 |
if BIG_DATA.count(bz2_block_magic) > 1: |
| 89 |
BIG_DATA += compressor.flush() |
| 90 |
break |
| 91 |
|
| 92 |
def setUp(self): |
| 93 |
fd, self.filename = tempfile.mkstemp() |
| 94 |
os.close(fd) |
| 95 |
|
| 96 |
def tearDown(self): |
| 97 |
unlink(self.filename) |
| 98 |
|
| 99 |
|
| 100 |
class BZ2FileTest(BaseTest): |
| 101 |
"Test the BZ2File class." |
| 102 |
|
| 103 |
def createTempFile(self, streams=1, suffix=b""): |
| 104 |
with open(self.filename, "wb") as f: |
| 105 |
f.write(self.DATA * streams) |
| 106 |
f.write(suffix) |
| 107 |
|
| 108 |
def testBadArgs(self): |
| 109 |
self.assertRaises(TypeError, BZ2File, 123.456) |
| 110 |
self.assertRaises(ValueError, BZ2File, os.devnull, "z") |
| 111 |
self.assertRaises(ValueError, BZ2File, os.devnull, "rx") |
| 112 |
self.assertRaises(ValueError, BZ2File, os.devnull, "rbt") |
| 113 |
self.assertRaises(ValueError, BZ2File, os.devnull, compresslevel=0) |
| 114 |
self.assertRaises(ValueError, BZ2File, os.devnull, compresslevel=10) |
| 115 |
|
| 116 |
# compresslevel is keyword-only |
| 117 |
self.assertRaises(TypeError, BZ2File, os.devnull, "r", 3) |
| 118 |
|
| 119 |
def testRead(self): |
| 120 |
self.createTempFile() |
| 121 |
with BZ2File(self.filename) as bz2f: |
| 122 |
self.assertRaises(TypeError, bz2f.read, float()) |
| 123 |
self.assertEqual(bz2f.read(), self.TEXT) |
| 124 |
|
| 125 |
def testReadBadFile(self): |
| 126 |
self.createTempFile(streams=0, suffix=self.BAD_DATA) |
| 127 |
with BZ2File(self.filename) as bz2f: |
| 128 |
self.assertRaises(OSError, bz2f.read) |
| 129 |
|
| 130 |
def testReadMultiStream(self): |
| 131 |
self.createTempFile(streams=5) |
| 132 |
with BZ2File(self.filename) as bz2f: |
| 133 |
self.assertRaises(TypeError, bz2f.read, float()) |
| 134 |
self.assertEqual(bz2f.read(), self.TEXT * 5) |
| 135 |
|
| 136 |
def testReadMonkeyMultiStream(self): |
| 137 |
# Test BZ2File.read() on a multi-stream archive where a stream |
| 138 |
# boundary coincides with the end of the raw read buffer. |
| 139 |
buffer_size = _streams.BUFFER_SIZE |
| 140 |
_streams.BUFFER_SIZE = len(self.DATA) |
| 141 |
try: |
| 142 |
self.createTempFile(streams=5) |
| 143 |
with BZ2File(self.filename) as bz2f: |
| 144 |
self.assertRaises(TypeError, bz2f.read, float()) |
| 145 |
self.assertEqual(bz2f.read(), self.TEXT * 5) |
| 146 |
finally: |
| 147 |
_streams.BUFFER_SIZE = buffer_size |
| 148 |
|
| 149 |
def testReadTrailingJunk(self): |
| 150 |
self.createTempFile(suffix=self.BAD_DATA) |
| 151 |
with BZ2File(self.filename) as bz2f: |
| 152 |
self.assertEqual(bz2f.read(), self.TEXT) |
| 153 |
|
| 154 |
def testReadMultiStreamTrailingJunk(self): |
| 155 |
self.createTempFile(streams=5, suffix=self.BAD_DATA) |
| 156 |
with BZ2File(self.filename) as bz2f: |
| 157 |
self.assertEqual(bz2f.read(), self.TEXT * 5) |
| 158 |
|
| 159 |
def testRead0(self): |
| 160 |
self.createTempFile() |
| 161 |
with BZ2File(self.filename) as bz2f: |
| 162 |
self.assertRaises(TypeError, bz2f.read, float()) |
| 163 |
self.assertEqual(bz2f.read(0), b"") |
| 164 |
|
| 165 |
def testReadChunk10(self): |
| 166 |
self.createTempFile() |
| 167 |
with BZ2File(self.filename) as bz2f: |
| 168 |
text = b'' |
| 169 |
while True: |
| 170 |
str = bz2f.read(10) |
| 171 |
if not str: |
| 172 |
break |
| 173 |
text += str |
| 174 |
self.assertEqual(text, self.TEXT) |
| 175 |
|
| 176 |
def testReadChunk10MultiStream(self): |
| 177 |
self.createTempFile(streams=5) |
| 178 |
with BZ2File(self.filename) as bz2f: |
| 179 |
text = b'' |
| 180 |
while True: |
| 181 |
str = bz2f.read(10) |
| 182 |
if not str: |
| 183 |
break |
| 184 |
text += str |
| 185 |
self.assertEqual(text, self.TEXT * 5) |
| 186 |
|
| 187 |
def testRead100(self): |
| 188 |
self.createTempFile() |
| 189 |
with BZ2File(self.filename) as bz2f: |
| 190 |
self.assertEqual(bz2f.read(100), self.TEXT[:100]) |
| 191 |
|
| 192 |
def testPeek(self): |
| 193 |
self.createTempFile() |
| 194 |
with BZ2File(self.filename) as bz2f: |
| 195 |
pdata = bz2f.peek() |
| 196 |
self.assertNotEqual(len(pdata), 0) |
| 197 |
self.assertStartsWith(self.TEXT, pdata) |
| 198 |
self.assertEqual(bz2f.read(), self.TEXT) |
| 199 |
|
| 200 |
def testReadInto(self): |
| 201 |
self.createTempFile() |
| 202 |
with BZ2File(self.filename) as bz2f: |
| 203 |
n = 128 |
| 204 |
b = bytearray(n) |
| 205 |
self.assertEqual(bz2f.readinto(b), n) |
| 206 |
self.assertEqual(b, self.TEXT[:n]) |
| 207 |
n = len(self.TEXT) - n |
| 208 |
b = bytearray(len(self.TEXT)) |
| 209 |
self.assertEqual(bz2f.readinto(b), n) |
| 210 |
self.assertEqual(b[:n], self.TEXT[-n:]) |
| 211 |
|
| 212 |
def testReadLine(self): |
| 213 |
self.createTempFile() |
| 214 |
with BZ2File(self.filename) as bz2f: |
| 215 |
self.assertRaises(TypeError, bz2f.readline, None) |
| 216 |
for line in self.TEXT_LINES: |
| 217 |
self.assertEqual(bz2f.readline(), line) |
| 218 |
|
| 219 |
def testReadLineMultiStream(self): |
| 220 |
self.createTempFile(streams=5) |
| 221 |
with BZ2File(self.filename) as bz2f: |
| 222 |
self.assertRaises(TypeError, bz2f.readline, None) |
| 223 |
for line in self.TEXT_LINES * 5: |
| 224 |
self.assertEqual(bz2f.readline(), line) |
| 225 |
|
| 226 |
def testReadLines(self): |
| 227 |
self.createTempFile() |
| 228 |
with BZ2File(self.filename) as bz2f: |
| 229 |
self.assertRaises(TypeError, bz2f.readlines, None) |
| 230 |
self.assertEqual(bz2f.readlines(), self.TEXT_LINES) |
| 231 |
|
| 232 |
def testReadLinesMultiStream(self): |
| 233 |
self.createTempFile(streams=5) |
| 234 |
with BZ2File(self.filename) as bz2f: |
| 235 |
self.assertRaises(TypeError, bz2f.readlines, None) |
| 236 |
self.assertEqual(bz2f.readlines(), self.TEXT_LINES * 5) |
| 237 |
|
| 238 |
def testIterator(self): |
| 239 |
self.createTempFile() |
| 240 |
with BZ2File(self.filename) as bz2f: |
| 241 |
self.assertEqual(list(iter(bz2f)), self.TEXT_LINES) |
| 242 |
|
| 243 |
def testIteratorMultiStream(self): |
| 244 |
self.createTempFile(streams=5) |
| 245 |
with BZ2File(self.filename) as bz2f: |
| 246 |
self.assertEqual(list(iter(bz2f)), self.TEXT_LINES * 5) |
| 247 |
|
| 248 |
def testClosedIteratorDeadlock(self): |
| 249 |
# Issue #3309: Iteration on a closed BZ2File should release the lock. |
| 250 |
self.createTempFile() |
| 251 |
bz2f = BZ2File(self.filename) |
| 252 |
bz2f.close() |
| 253 |
self.assertRaises(ValueError, next, bz2f) |
| 254 |
# This call will deadlock if the above call failed to release the lock. |
| 255 |
self.assertRaises(ValueError, bz2f.readlines) |
| 256 |
|
| 257 |
def testWrite(self): |
| 258 |
with BZ2File(self.filename, "w") as bz2f: |
| 259 |
self.assertRaises(TypeError, bz2f.write) |
| 260 |
bz2f.write(self.TEXT) |
| 261 |
with open(self.filename, 'rb') as f: |
| 262 |
self.assertEqual(ext_decompress(f.read()), self.TEXT) |
| 263 |
|
| 264 |
def testWriteChunks10(self): |
| 265 |
with BZ2File(self.filename, "w") as bz2f: |
| 266 |
n = 0 |
| 267 |
while True: |
| 268 |
str = self.TEXT[n*10:(n+1)*10] |
| 269 |
if not str: |
| 270 |
break |
| 271 |
bz2f.write(str) |
| 272 |
n += 1 |
| 273 |
with open(self.filename, 'rb') as f: |
| 274 |
self.assertEqual(ext_decompress(f.read()), self.TEXT) |
| 275 |
|
| 276 |
def testWriteNonDefaultCompressLevel(self): |
| 277 |
expected = bz2.compress(self.TEXT, compresslevel=5) |
| 278 |
with BZ2File(self.filename, "w", compresslevel=5) as bz2f: |
| 279 |
bz2f.write(self.TEXT) |
| 280 |
with open(self.filename, "rb") as f: |
| 281 |
self.assertEqual(f.read(), expected) |
| 282 |
|
| 283 |
def testWriteLines(self): |
| 284 |
with BZ2File(self.filename, "w") as bz2f: |
| 285 |
self.assertRaises(TypeError, bz2f.writelines) |
| 286 |
bz2f.writelines(self.TEXT_LINES) |
| 287 |
# Issue #1535500: Calling writelines() on a closed BZ2File |
| 288 |
# should raise an exception. |
| 289 |
self.assertRaises(ValueError, bz2f.writelines, ["a"]) |
| 290 |
with open(self.filename, 'rb') as f: |
| 291 |
self.assertEqual(ext_decompress(f.read()), self.TEXT) |
| 292 |
|
| 293 |
def testWriteMethodsOnReadOnlyFile(self): |
| 294 |
with BZ2File(self.filename, "w") as bz2f: |
| 295 |
bz2f.write(b"abc") |
| 296 |
|
| 297 |
with BZ2File(self.filename, "r") as bz2f: |
| 298 |
self.assertRaises(OSError, bz2f.write, b"a") |
| 299 |
self.assertRaises(OSError, bz2f.writelines, [b"a"]) |
| 300 |
|
| 301 |
def testAppend(self): |
| 302 |
with BZ2File(self.filename, "w") as bz2f: |
| 303 |
self.assertRaises(TypeError, bz2f.write) |
| 304 |
bz2f.write(self.TEXT) |
| 305 |
with BZ2File(self.filename, "a") as bz2f: |
| 306 |
self.assertRaises(TypeError, bz2f.write) |
| 307 |
bz2f.write(self.TEXT) |
| 308 |
with open(self.filename, 'rb') as f: |
| 309 |
self.assertEqual(ext_decompress(f.read()), self.TEXT * 2) |
| 310 |
|
| 311 |
def testSeekForward(self): |
| 312 |
self.createTempFile() |
| 313 |
with BZ2File(self.filename) as bz2f: |
| 314 |
self.assertRaises(TypeError, bz2f.seek) |
| 315 |
bz2f.seek(150) |
| 316 |
self.assertEqual(bz2f.read(), self.TEXT[150:]) |
| 317 |
|
| 318 |
def testSeekForwardAcrossStreams(self): |
| 319 |
self.createTempFile(streams=2) |
| 320 |
with BZ2File(self.filename) as bz2f: |
| 321 |
self.assertRaises(TypeError, bz2f.seek) |
| 322 |
bz2f.seek(len(self.TEXT) + 150) |
| 323 |
self.assertEqual(bz2f.read(), self.TEXT[150:]) |
| 324 |
|
| 325 |
def testSeekBackwards(self): |
| 326 |
self.createTempFile() |
| 327 |
with BZ2File(self.filename) as bz2f: |
| 328 |
bz2f.read(500) |
| 329 |
bz2f.seek(-150, 1) |
| 330 |
self.assertEqual(bz2f.read(), self.TEXT[500-150:]) |
| 331 |
|
| 332 |
def testSeekBackwardsAcrossStreams(self): |
| 333 |
self.createTempFile(streams=2) |
| 334 |
with BZ2File(self.filename) as bz2f: |
| 335 |
readto = len(self.TEXT) + 100 |
| 336 |
while readto > 0: |
| 337 |
readto -= len(bz2f.read(readto)) |
| 338 |
bz2f.seek(-150, 1) |
| 339 |
self.assertEqual(bz2f.read(), self.TEXT[100-150:] + self.TEXT) |
| 340 |
|
| 341 |
def testSeekBackwardsFromEnd(self): |
| 342 |
self.createTempFile() |
| 343 |
with BZ2File(self.filename) as bz2f: |
| 344 |
bz2f.seek(-150, 2) |
| 345 |
self.assertEqual(bz2f.read(), self.TEXT[len(self.TEXT)-150:]) |
| 346 |
|
| 347 |
def testSeekBackwardsFromEndAcrossStreams(self): |
| 348 |
self.createTempFile(streams=2) |
| 349 |
with BZ2File(self.filename) as bz2f: |
| 350 |
bz2f.seek(-1000, 2) |
| 351 |
self.assertEqual(bz2f.read(), (self.TEXT * 2)[-1000:]) |
| 352 |
|
| 353 |
def testSeekPostEnd(self): |
| 354 |
self.createTempFile() |
| 355 |
with BZ2File(self.filename) as bz2f: |
| 356 |
bz2f.seek(150000) |
| 357 |
self.assertEqual(bz2f.tell(), len(self.TEXT)) |
| 358 |
self.assertEqual(bz2f.read(), b"") |
| 359 |
|
| 360 |
def testSeekPostEndMultiStream(self): |
| 361 |
self.createTempFile(streams=5) |
| 362 |
with BZ2File(self.filename) as bz2f: |
| 363 |
bz2f.seek(150000) |
| 364 |
self.assertEqual(bz2f.tell(), len(self.TEXT) * 5) |
| 365 |
self.assertEqual(bz2f.read(), b"") |
| 366 |
|
| 367 |
def testSeekPostEndTwice(self): |
| 368 |
self.createTempFile() |
| 369 |
with BZ2File(self.filename) as bz2f: |
| 370 |
bz2f.seek(150000) |
| 371 |
bz2f.seek(150000) |
| 372 |
self.assertEqual(bz2f.tell(), len(self.TEXT)) |
| 373 |
self.assertEqual(bz2f.read(), b"") |
| 374 |
|
| 375 |
def testSeekPostEndTwiceMultiStream(self): |
| 376 |
self.createTempFile(streams=5) |
| 377 |
with BZ2File(self.filename) as bz2f: |
| 378 |
bz2f.seek(150000) |
| 379 |
bz2f.seek(150000) |
| 380 |
self.assertEqual(bz2f.tell(), len(self.TEXT) * 5) |
| 381 |
self.assertEqual(bz2f.read(), b"") |
| 382 |
|
| 383 |
def testSeekPreStart(self): |
| 384 |
self.createTempFile() |
| 385 |
with BZ2File(self.filename) as bz2f: |
| 386 |
bz2f.seek(-150) |
| 387 |
self.assertEqual(bz2f.tell(), 0) |
| 388 |
self.assertEqual(bz2f.read(), self.TEXT) |
| 389 |
|
| 390 |
def testSeekPreStartMultiStream(self): |
| 391 |
self.createTempFile(streams=2) |
| 392 |
with BZ2File(self.filename) as bz2f: |
| 393 |
bz2f.seek(-150) |
| 394 |
self.assertEqual(bz2f.tell(), 0) |
| 395 |
self.assertEqual(bz2f.read(), self.TEXT * 2) |
| 396 |
|
| 397 |
def testFileno(self): |
| 398 |
self.createTempFile() |
| 399 |
with open(self.filename, 'rb') as rawf: |
| 400 |
bz2f = BZ2File(rawf) |
| 401 |
try: |
| 402 |
self.assertEqual(bz2f.fileno(), rawf.fileno()) |
| 403 |
finally: |
| 404 |
bz2f.close() |
| 405 |
self.assertRaises(ValueError, bz2f.fileno) |
| 406 |
|
| 407 |
def testSeekable(self): |
| 408 |
bz2f = BZ2File(BytesIO(self.DATA)) |
| 409 |
try: |
| 410 |
self.assertTrue(bz2f.seekable()) |
| 411 |
bz2f.read() |
| 412 |
self.assertTrue(bz2f.seekable()) |
| 413 |
finally: |
| 414 |
bz2f.close() |
| 415 |
self.assertRaises(ValueError, bz2f.seekable) |
| 416 |
|
| 417 |
bz2f = BZ2File(BytesIO(), "w") |
| 418 |
try: |
| 419 |
self.assertFalse(bz2f.seekable()) |
| 420 |
finally: |
| 421 |
bz2f.close() |
| 422 |
self.assertRaises(ValueError, bz2f.seekable) |
| 423 |
|
| 424 |
src = BytesIO(self.DATA) |
| 425 |
src.seekable = lambda: False |
| 426 |
bz2f = BZ2File(src) |
| 427 |
try: |
| 428 |
self.assertFalse(bz2f.seekable()) |
| 429 |
finally: |
| 430 |
bz2f.close() |
| 431 |
self.assertRaises(ValueError, bz2f.seekable) |
| 432 |
|
| 433 |
def testReadable(self): |
| 434 |
bz2f = BZ2File(BytesIO(self.DATA)) |
| 435 |
try: |
| 436 |
self.assertTrue(bz2f.readable()) |
| 437 |
bz2f.read() |
| 438 |
self.assertTrue(bz2f.readable()) |
| 439 |
finally: |
| 440 |
bz2f.close() |
| 441 |
self.assertRaises(ValueError, bz2f.readable) |
| 442 |
|
| 443 |
bz2f = BZ2File(BytesIO(), "w") |
| 444 |
try: |
| 445 |
self.assertFalse(bz2f.readable()) |
| 446 |
finally: |
| 447 |
bz2f.close() |
| 448 |
self.assertRaises(ValueError, bz2f.readable) |
| 449 |
|
| 450 |
def testWritable(self): |
| 451 |
bz2f = BZ2File(BytesIO(self.DATA)) |
| 452 |
try: |
| 453 |
self.assertFalse(bz2f.writable()) |
| 454 |
bz2f.read() |
| 455 |
self.assertFalse(bz2f.writable()) |
| 456 |
finally: |
| 457 |
bz2f.close() |
| 458 |
self.assertRaises(ValueError, bz2f.writable) |
| 459 |
|
| 460 |
bz2f = BZ2File(BytesIO(), "w") |
| 461 |
try: |
| 462 |
self.assertTrue(bz2f.writable()) |
| 463 |
finally: |
| 464 |
bz2f.close() |
| 465 |
self.assertRaises(ValueError, bz2f.writable) |
| 466 |
|
| 467 |
def testOpenDel(self): |
| 468 |
self.createTempFile() |
| 469 |
for i in range(10000): |
| 470 |
o = BZ2File(self.filename) |
| 471 |
del o |
| 472 |
|
| 473 |
def testOpenNonexistent(self): |
| 474 |
self.assertRaises(OSError, BZ2File, "/non/existent") |
| 475 |
|
| 476 |
def testReadlinesNoNewline(self): |
| 477 |
# Issue #1191043: readlines() fails on a file containing no newline. |
| 478 |
data = b'BZh91AY&SY\xd9b\x89]\x00\x00\x00\x03\x80\x04\x00\x02\x00\x0c\x00 \x00!\x9ah3M\x13<]\xc9\x14\xe1BCe\x8a%t' |
| 479 |
with open(self.filename, "wb") as f: |
| 480 |
f.write(data) |
| 481 |
with BZ2File(self.filename) as bz2f: |
| 482 |
lines = bz2f.readlines() |
| 483 |
self.assertEqual(lines, [b'Test']) |
| 484 |
with BZ2File(self.filename) as bz2f: |
| 485 |
xlines = list(bz2f.readlines()) |
| 486 |
self.assertEqual(xlines, [b'Test']) |
| 487 |
|
| 488 |
def testContextProtocol(self): |
| 489 |
with BZ2File(self.filename, "wb") as f: |
| 490 |
f.write(b"xxx") |
| 491 |
f = BZ2File(self.filename, "rb") |
| 492 |
f.close() |
| 493 |
try: |
| 494 |
with f: |
| 495 |
pass |
| 496 |
except ValueError: |
| 497 |
pass |
| 498 |
else: |
| 499 |
self.fail("__enter__ on a closed file didn't raise an exception") |
| 500 |
try: |
| 501 |
with BZ2File(self.filename, "wb") as f: |
| 502 |
1/0 |
| 503 |
except ZeroDivisionError: |
| 504 |
pass |
| 505 |
else: |
| 506 |
self.fail("1/0 didn't raise an exception") |
| 507 |
|
| 508 |
@threading_helper.requires_working_threading() |
| 509 |
def testThreading(self): |
| 510 |
# Issue #7205: Using a BZ2File from several threads shouldn't deadlock. |
| 511 |
data = b"1" * 2**20 |
| 512 |
nthreads = 10 |
| 513 |
with BZ2File(self.filename, 'wb') as f: |
| 514 |
def comp(): |
| 515 |
for i in range(5): |
| 516 |
f.write(data) |
| 517 |
threads = [threading.Thread(target=comp) for i in range(nthreads)] |
| 518 |
with threading_helper.start_threads(threads): |
| 519 |
pass |
| 520 |
|
| 521 |
def testMixedIterationAndReads(self): |
| 522 |
self.createTempFile() |
| 523 |
linelen = len(self.TEXT_LINES[0]) |
| 524 |
halflen = linelen // 2 |
| 525 |
with BZ2File(self.filename) as bz2f: |
| 526 |
bz2f.read(halflen) |
| 527 |
self.assertEqual(next(bz2f), self.TEXT_LINES[0][halflen:]) |
| 528 |
self.assertEqual(bz2f.read(), self.TEXT[linelen:]) |
| 529 |
with BZ2File(self.filename) as bz2f: |
| 530 |
bz2f.readline() |
| 531 |
self.assertEqual(next(bz2f), self.TEXT_LINES[1]) |
| 532 |
self.assertEqual(bz2f.readline(), self.TEXT_LINES[2]) |
| 533 |
with BZ2File(self.filename) as bz2f: |
| 534 |
bz2f.readlines() |
| 535 |
self.assertRaises(StopIteration, next, bz2f) |
| 536 |
self.assertEqual(bz2f.readlines(), []) |
| 537 |
|
| 538 |
def testMultiStreamOrdering(self): |
| 539 |
# Test the ordering of streams when reading a multi-stream archive. |
| 540 |
data1 = b"foo" * 1000 |
| 541 |
data2 = b"bar" * 1000 |
| 542 |
with BZ2File(self.filename, "w") as bz2f: |
| 543 |
bz2f.write(data1) |
| 544 |
with BZ2File(self.filename, "a") as bz2f: |
| 545 |
bz2f.write(data2) |
| 546 |
with BZ2File(self.filename) as bz2f: |
| 547 |
self.assertEqual(bz2f.read(), data1 + data2) |
| 548 |
|
| 549 |
def testOpenFilename(self): |
| 550 |
with BZ2File(self.filename, "wb") as f: |
| 551 |
f.write(b'content') |
| 552 |
self.assertEqual(f.name, self.filename) |
| 553 |
self.assertIsInstance(f.fileno(), int) |
| 554 |
self.assertEqual(f.mode, 'wb') |
| 555 |
self.assertIs(f.readable(), False) |
| 556 |
self.assertIs(f.writable(), True) |
| 557 |
self.assertIs(f.seekable(), False) |
| 558 |
self.assertIs(f.closed, False) |
| 559 |
self.assertIs(f.closed, True) |
| 560 |
with self.assertRaises(ValueError): |
| 561 |
f.name |
| 562 |
self.assertRaises(ValueError, f.fileno) |
| 563 |
self.assertEqual(f.mode, 'wb') |
| 564 |
self.assertRaises(ValueError, f.readable) |
| 565 |
self.assertRaises(ValueError, f.writable) |
| 566 |
self.assertRaises(ValueError, f.seekable) |
| 567 |
|
| 568 |
with BZ2File(self.filename, "ab") as f: |
| 569 |
f.write(b'appendix') |
| 570 |
self.assertEqual(f.name, self.filename) |
| 571 |
self.assertIsInstance(f.fileno(), int) |
| 572 |
self.assertEqual(f.mode, 'wb') |
| 573 |
self.assertIs(f.readable(), False) |
| 574 |
self.assertIs(f.writable(), True) |
| 575 |
self.assertIs(f.seekable(), False) |
| 576 |
self.assertIs(f.closed, False) |
| 577 |
self.assertIs(f.closed, True) |
| 578 |
with self.assertRaises(ValueError): |
| 579 |
f.name |
| 580 |
self.assertRaises(ValueError, f.fileno) |
| 581 |
self.assertEqual(f.mode, 'wb') |
| 582 |
self.assertRaises(ValueError, f.readable) |
| 583 |
self.assertRaises(ValueError, f.writable) |
| 584 |
self.assertRaises(ValueError, f.seekable) |
| 585 |
|
| 586 |
with BZ2File(self.filename, 'rb') as f: |
| 587 |
self.assertEqual(f.read(), b'contentappendix') |
| 588 |
self.assertEqual(f.name, self.filename) |
| 589 |
self.assertIsInstance(f.fileno(), int) |
| 590 |
self.assertEqual(f.mode, 'rb') |
| 591 |
self.assertIs(f.readable(), True) |
| 592 |
self.assertIs(f.writable(), False) |
| 593 |
self.assertIs(f.seekable(), True) |
| 594 |
self.assertIs(f.closed, False) |
| 595 |
self.assertIs(f.closed, True) |
| 596 |
with self.assertRaises(ValueError): |
| 597 |
f.name |
| 598 |
self.assertRaises(ValueError, f.fileno) |
| 599 |
self.assertEqual(f.mode, 'rb') |
| 600 |
self.assertRaises(ValueError, f.readable) |
| 601 |
self.assertRaises(ValueError, f.writable) |
| 602 |
self.assertRaises(ValueError, f.seekable) |
| 603 |
|
| 604 |
def testOpenFileWithName(self): |
| 605 |
with open(self.filename, 'wb') as raw: |
| 606 |
with BZ2File(raw, 'wb') as f: |
| 607 |
f.write(b'content') |
| 608 |
self.assertEqual(f.name, raw.name) |
| 609 |
self.assertEqual(f.fileno(), raw.fileno()) |
| 610 |
self.assertEqual(f.mode, 'wb') |
| 611 |
self.assertIs(f.readable(), False) |
| 612 |
self.assertIs(f.writable(), True) |
| 613 |
self.assertIs(f.seekable(), False) |
| 614 |
self.assertIs(f.closed, False) |
| 615 |
self.assertIs(f.closed, True) |
| 616 |
with self.assertRaises(ValueError): |
| 617 |
f.name |
| 618 |
self.assertRaises(ValueError, f.fileno) |
| 619 |
self.assertEqual(f.mode, 'wb') |
| 620 |
self.assertRaises(ValueError, f.readable) |
| 621 |
self.assertRaises(ValueError, f.writable) |
| 622 |
self.assertRaises(ValueError, f.seekable) |
| 623 |
|
| 624 |
with open(self.filename, 'ab') as raw: |
| 625 |
with BZ2File(raw, 'ab') as f: |
| 626 |
f.write(b'appendix') |
| 627 |
self.assertEqual(f.name, raw.name) |
| 628 |
self.assertEqual(f.fileno(), raw.fileno()) |
| 629 |
self.assertEqual(f.mode, 'wb') |
| 630 |
self.assertIs(f.readable(), False) |
| 631 |
self.assertIs(f.writable(), True) |
| 632 |
self.assertIs(f.seekable(), False) |
| 633 |
self.assertIs(f.closed, False) |
| 634 |
self.assertIs(f.closed, True) |
| 635 |
with self.assertRaises(ValueError): |
| 636 |
f.name |
| 637 |
self.assertRaises(ValueError, f.fileno) |
| 638 |
self.assertEqual(f.mode, 'wb') |
| 639 |
self.assertRaises(ValueError, f.readable) |
| 640 |
self.assertRaises(ValueError, f.writable) |
| 641 |
self.assertRaises(ValueError, f.seekable) |
| 642 |
|
| 643 |
with open(self.filename, 'rb') as raw: |
| 644 |
with BZ2File(raw, 'rb') as f: |
| 645 |
self.assertEqual(f.read(), b'contentappendix') |
| 646 |
self.assertEqual(f.name, raw.name) |
| 647 |
self.assertEqual(f.fileno(), raw.fileno()) |
| 648 |
self.assertEqual(f.mode, 'rb') |
| 649 |
self.assertIs(f.readable(), True) |
| 650 |
self.assertIs(f.writable(), False) |
| 651 |
self.assertIs(f.seekable(), True) |
| 652 |
self.assertIs(f.closed, False) |
| 653 |
self.assertIs(f.closed, True) |
| 654 |
with self.assertRaises(ValueError): |
| 655 |
f.name |
| 656 |
self.assertRaises(ValueError, f.fileno) |
| 657 |
self.assertEqual(f.mode, 'rb') |
| 658 |
self.assertRaises(ValueError, f.readable) |
| 659 |
self.assertRaises(ValueError, f.writable) |
| 660 |
self.assertRaises(ValueError, f.seekable) |
| 661 |
|
| 662 |
def testOpenFileWithoutName(self): |
| 663 |
bio = BytesIO() |
| 664 |
with BZ2File(bio, 'wb') as f: |
| 665 |
f.write(b'content') |
| 666 |
with self.assertRaises(AttributeError): |
| 667 |
f.name |
| 668 |
self.assertRaises(io.UnsupportedOperation, f.fileno) |
| 669 |
self.assertEqual(f.mode, 'wb') |
| 670 |
with self.assertRaises(ValueError): |
| 671 |
f.name |
| 672 |
self.assertRaises(ValueError, f.fileno) |
| 673 |
|
| 674 |
with BZ2File(bio, 'ab') as f: |
| 675 |
f.write(b'appendix') |
| 676 |
with self.assertRaises(AttributeError): |
| 677 |
f.name |
| 678 |
self.assertRaises(io.UnsupportedOperation, f.fileno) |
| 679 |
self.assertEqual(f.mode, 'wb') |
| 680 |
with self.assertRaises(ValueError): |
| 681 |
f.name |
| 682 |
self.assertRaises(ValueError, f.fileno) |
| 683 |
|
| 684 |
bio.seek(0) |
| 685 |
with BZ2File(bio, 'rb') as f: |
| 686 |
self.assertEqual(f.read(), b'contentappendix') |
| 687 |
with self.assertRaises(AttributeError): |
| 688 |
f.name |
| 689 |
self.assertRaises(io.UnsupportedOperation, f.fileno) |
| 690 |
self.assertEqual(f.mode, 'rb') |
| 691 |
with self.assertRaises(ValueError): |
| 692 |
f.name |
| 693 |
self.assertRaises(ValueError, f.fileno) |
| 694 |
|
| 695 |
def testOpenFileWithIntName(self): |
| 696 |
fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) |
| 697 |
with open(fd, 'wb') as raw: |
| 698 |
with BZ2File(raw, 'wb') as f: |
| 699 |
f.write(b'content') |
| 700 |
self.assertEqual(f.name, raw.name) |
| 701 |
self.assertEqual(f.fileno(), raw.fileno()) |
| 702 |
self.assertEqual(f.mode, 'wb') |
| 703 |
with self.assertRaises(ValueError): |
| 704 |
f.name |
| 705 |
self.assertRaises(ValueError, f.fileno) |
| 706 |
|
| 707 |
fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT | os.O_APPEND) |
| 708 |
with open(fd, 'ab') as raw: |
| 709 |
with BZ2File(raw, 'ab') as f: |
| 710 |
f.write(b'appendix') |
| 711 |
self.assertEqual(f.name, raw.name) |
| 712 |
self.assertEqual(f.fileno(), raw.fileno()) |
| 713 |
self.assertEqual(f.mode, 'wb') |
| 714 |
with self.assertRaises(ValueError): |
| 715 |
f.name |
| 716 |
self.assertRaises(ValueError, f.fileno) |
| 717 |
|
| 718 |
fd = os.open(self.filename, os.O_RDONLY) |
| 719 |
with open(fd, 'rb') as raw: |
| 720 |
with BZ2File(raw, 'rb') as f: |
| 721 |
self.assertEqual(f.read(), b'contentappendix') |
| 722 |
self.assertEqual(f.name, raw.name) |
| 723 |
self.assertEqual(f.fileno(), raw.fileno()) |
| 724 |
self.assertEqual(f.mode, 'rb') |
| 725 |
with self.assertRaises(ValueError): |
| 726 |
f.name |
| 727 |
self.assertRaises(ValueError, f.fileno) |
| 728 |
|
| 729 |
def testOpenBytesFilename(self): |
| 730 |
str_filename = self.filename |
| 731 |
bytes_filename = os.fsencode(str_filename) |
| 732 |
with BZ2File(bytes_filename, "wb") as f: |
| 733 |
f.write(self.DATA) |
| 734 |
self.assertEqual(f.name, bytes_filename) |
| 735 |
with BZ2File(bytes_filename, "rb") as f: |
| 736 |
self.assertEqual(f.read(), self.DATA) |
| 737 |
self.assertEqual(f.name, bytes_filename) |
| 738 |
# Sanity check that we are actually operating on the right file. |
| 739 |
with BZ2File(str_filename, "rb") as f: |
| 740 |
self.assertEqual(f.read(), self.DATA) |
| 741 |
self.assertEqual(f.name, str_filename) |
| 742 |
|
| 743 |
def testOpenPathLikeFilename(self): |
| 744 |
filename = FakePath(self.filename) |
| 745 |
with BZ2File(filename, "wb") as f: |
| 746 |
f.write(self.DATA) |
| 747 |
self.assertEqual(f.name, self.filename) |
| 748 |
with BZ2File(filename, "rb") as f: |
| 749 |
self.assertEqual(f.read(), self.DATA) |
| 750 |
self.assertEqual(f.name, self.filename) |
| 751 |
|
| 752 |
def testDecompressLimited(self): |
| 753 |
"""Decompressed data buffering should be limited""" |
| 754 |
bomb = bz2.compress(b'\0' * int(2e6), compresslevel=9) |
| 755 |
self.assertLess(len(bomb), _streams.BUFFER_SIZE) |
| 756 |
|
| 757 |
decomp = BZ2File(BytesIO(bomb)) |
| 758 |
self.assertEqual(decomp.read(1), b'\0') |
| 759 |
max_decomp = 1 + DEFAULT_BUFFER_SIZE |
| 760 |
self.assertLessEqual(decomp._buffer.raw.tell(), max_decomp, |
| 761 |
"Excessive amount of data was decompressed") |
| 762 |
|
| 763 |
|
| 764 |
# Tests for a BZ2File wrapping another file object: |
| 765 |
|
| 766 |
def testReadBytesIO(self): |
| 767 |
with BytesIO(self.DATA) as bio: |
| 768 |
with BZ2File(bio) as bz2f: |
| 769 |
self.assertRaises(TypeError, bz2f.read, float()) |
| 770 |
self.assertEqual(bz2f.read(), self.TEXT) |
| 771 |
with self.assertRaises(AttributeError): |
| 772 |
bz2.name |
| 773 |
self.assertEqual(bz2f.mode, 'rb') |
| 774 |
self.assertFalse(bio.closed) |
| 775 |
|
| 776 |
def testPeekBytesIO(self): |
| 777 |
with BytesIO(self.DATA) as bio: |
| 778 |
with BZ2File(bio) as bz2f: |
| 779 |
pdata = bz2f.peek() |
| 780 |
self.assertNotEqual(len(pdata), 0) |
| 781 |
self.assertStartsWith(self.TEXT, pdata) |
| 782 |
self.assertEqual(bz2f.read(), self.TEXT) |
| 783 |
|
| 784 |
def testWriteBytesIO(self): |
| 785 |
with BytesIO() as bio: |
| 786 |
with BZ2File(bio, "w") as bz2f: |
| 787 |
self.assertRaises(TypeError, bz2f.write) |
| 788 |
bz2f.write(self.TEXT) |
| 789 |
with self.assertRaises(AttributeError): |
| 790 |
bz2.name |
| 791 |
self.assertEqual(bz2f.mode, 'wb') |
| 792 |
self.assertEqual(ext_decompress(bio.getvalue()), self.TEXT) |
| 793 |
self.assertFalse(bio.closed) |
| 794 |
|
| 795 |
def testSeekForwardBytesIO(self): |
| 796 |
with BytesIO(self.DATA) as bio: |
| 797 |
with BZ2File(bio) as bz2f: |
| 798 |
self.assertRaises(TypeError, bz2f.seek) |
| 799 |
bz2f.seek(150) |
| 800 |
self.assertEqual(bz2f.read(), self.TEXT[150:]) |
| 801 |
|
| 802 |
def testSeekBackwardsBytesIO(self): |
| 803 |
with BytesIO(self.DATA) as bio: |
| 804 |
with BZ2File(bio) as bz2f: |
| 805 |
bz2f.read(500) |
| 806 |
bz2f.seek(-150, 1) |
| 807 |
self.assertEqual(bz2f.read(), self.TEXT[500-150:]) |
| 808 |
|
| 809 |
def test_read_truncated(self): |
| 810 |
# Drop the eos_magic field (6 bytes) and CRC (4 bytes). |
| 811 |
truncated = self.DATA[:-10] |
| 812 |
with BZ2File(BytesIO(truncated)) as f: |
| 813 |
self.assertRaises(EOFError, f.read) |
| 814 |
with BZ2File(BytesIO(truncated)) as f: |
| 815 |
self.assertEqual(f.read(len(self.TEXT)), self.TEXT) |
| 816 |
self.assertRaises(EOFError, f.read, 1) |
| 817 |
# Incomplete 4-byte file header, and block header of at least 146 bits. |
| 818 |
for i in range(22): |
| 819 |
with BZ2File(BytesIO(truncated[:i])) as f: |
| 820 |
self.assertRaises(EOFError, f.read, 1) |
| 821 |
|
| 822 |
def test_issue44439(self): |
| 823 |
q = array.array('Q', [1, 2, 3, 4, 5]) |
| 824 |
LENGTH = len(q) * q.itemsize |
| 825 |
|
| 826 |
with BZ2File(BytesIO(), 'w') as f: |
| 827 |
self.assertEqual(f.write(q), LENGTH) |
| 828 |
self.assertEqual(f.tell(), LENGTH) |
| 829 |
|
| 830 |
|
| 831 |
class BZ2CompressorTest(BaseTest): |
| 832 |
def testCompress(self): |
| 833 |
bz2c = BZ2Compressor() |
| 834 |
self.assertRaises(TypeError, bz2c.compress) |
| 835 |
data = bz2c.compress(self.TEXT) |
| 836 |
data += bz2c.flush() |
| 837 |
self.assertEqual(ext_decompress(data), self.TEXT) |
| 838 |
|
| 839 |
def testCompressEmptyString(self): |
| 840 |
bz2c = BZ2Compressor() |
| 841 |
data = bz2c.compress(b'') |
| 842 |
data += bz2c.flush() |
| 843 |
self.assertEqual(data, self.EMPTY_DATA) |
| 844 |
|
| 845 |
def testCompressChunks10(self): |
| 846 |
bz2c = BZ2Compressor() |
| 847 |
n = 0 |
| 848 |
data = b'' |
| 849 |
while True: |
| 850 |
str = self.TEXT[n*10:(n+1)*10] |
| 851 |
if not str: |
| 852 |
break |
| 853 |
data += bz2c.compress(str) |
| 854 |
n += 1 |
| 855 |
data += bz2c.flush() |
| 856 |
self.assertEqual(ext_decompress(data), self.TEXT) |
| 857 |
|
| 858 |
@support.skip_if_pgo_task |
| 859 |
@bigmemtest(size=_4G + 100, memuse=2) |
| 860 |
def testCompress4G(self, size): |
| 861 |
# "Test BZ2Compressor.compress()/flush() with >4GiB input" |
| 862 |
bz2c = BZ2Compressor() |
| 863 |
data = b"x" * size |
| 864 |
try: |
| 865 |
compressed = bz2c.compress(data) |
| 866 |
compressed += bz2c.flush() |
| 867 |
finally: |
| 868 |
data = None # Release memory |
| 869 |
data = bz2.decompress(compressed) |
| 870 |
try: |
| 871 |
self.assertEqual(len(data), size) |
| 872 |
self.assertEqual(len(data.strip(b"x")), 0) |
| 873 |
finally: |
| 874 |
data = None |
| 875 |
|
| 876 |
def testPickle(self): |
| 877 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 878 |
with self.assertRaises(TypeError): |
| 879 |
pickle.dumps(BZ2Compressor(), proto) |
| 880 |
|
| 881 |
|
| 882 |
class BZ2DecompressorTest(BaseTest): |
| 883 |
def test_Constructor(self): |
| 884 |
self.assertRaises(TypeError, BZ2Decompressor, 42) |
| 885 |
|
| 886 |
def testDecompress(self): |
| 887 |
bz2d = BZ2Decompressor() |
| 888 |
self.assertRaises(TypeError, bz2d.decompress) |
| 889 |
text = bz2d.decompress(self.DATA) |
| 890 |
self.assertEqual(text, self.TEXT) |
| 891 |
|
| 892 |
def testDecompressChunks10(self): |
| 893 |
bz2d = BZ2Decompressor() |
| 894 |
text = b'' |
| 895 |
n = 0 |
| 896 |
while True: |
| 897 |
str = self.DATA[n*10:(n+1)*10] |
| 898 |
if not str: |
| 899 |
break |
| 900 |
text += bz2d.decompress(str) |
| 901 |
n += 1 |
| 902 |
self.assertEqual(text, self.TEXT) |
| 903 |
|
| 904 |
def testDecompressUnusedData(self): |
| 905 |
bz2d = BZ2Decompressor() |
| 906 |
unused_data = b"this is unused data" |
| 907 |
text = bz2d.decompress(self.DATA+unused_data) |
| 908 |
self.assertEqual(text, self.TEXT) |
| 909 |
self.assertEqual(bz2d.unused_data, unused_data) |
| 910 |
|
| 911 |
def testEOFError(self): |
| 912 |
bz2d = BZ2Decompressor() |
| 913 |
text = bz2d.decompress(self.DATA) |
| 914 |
self.assertRaises(EOFError, bz2d.decompress, b"anything") |
| 915 |
self.assertRaises(EOFError, bz2d.decompress, b"") |
| 916 |
|
| 917 |
@support.skip_if_pgo_task |
| 918 |
@bigmemtest(size=_4G + 100, memuse=3.3) |
| 919 |
def testDecompress4G(self, size): |
| 920 |
# "Test BZ2Decompressor.decompress() with >4GiB input" |
| 921 |
blocksize = min(10 * 1024 * 1024, size) |
| 922 |
block = random.randbytes(blocksize) |
| 923 |
try: |
| 924 |
data = block * ((size-1) // blocksize + 1) |
| 925 |
compressed = bz2.compress(data) |
| 926 |
bz2d = BZ2Decompressor() |
| 927 |
decompressed = bz2d.decompress(compressed) |
| 928 |
self.assertTrue(decompressed == data) |
| 929 |
finally: |
| 930 |
data = None |
| 931 |
compressed = None |
| 932 |
decompressed = None |
| 933 |
|
| 934 |
def testPickle(self): |
| 935 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 936 |
with self.assertRaises(TypeError): |
| 937 |
pickle.dumps(BZ2Decompressor(), proto) |
| 938 |
|
| 939 |
def testDecompressorChunksMaxsize(self): |
| 940 |
bzd = BZ2Decompressor() |
| 941 |
max_length = 100 |
| 942 |
out = [] |
| 943 |
|
| 944 |
# Feed some input |
| 945 |
len_ = len(self.BIG_DATA) - 64 |
| 946 |
out.append(bzd.decompress(self.BIG_DATA[:len_], |
| 947 |
max_length=max_length)) |
| 948 |
self.assertFalse(bzd.needs_input) |
| 949 |
self.assertEqual(len(out[-1]), max_length) |
| 950 |
|
| 951 |
# Retrieve more data without providing more input |
| 952 |
out.append(bzd.decompress(b'', max_length=max_length)) |
| 953 |
self.assertFalse(bzd.needs_input) |
| 954 |
self.assertEqual(len(out[-1]), max_length) |
| 955 |
|
| 956 |
# Retrieve more data while providing more input |
| 957 |
out.append(bzd.decompress(self.BIG_DATA[len_:], |
| 958 |
max_length=max_length)) |
| 959 |
self.assertLessEqual(len(out[-1]), max_length) |
| 960 |
|
| 961 |
# Retrieve remaining uncompressed data |
| 962 |
while not bzd.eof: |
| 963 |
out.append(bzd.decompress(b'', max_length=max_length)) |
| 964 |
self.assertLessEqual(len(out[-1]), max_length) |
| 965 |
|
| 966 |
out = b"".join(out) |
| 967 |
self.assertEqual(out, self.BIG_TEXT) |
| 968 |
self.assertEqual(bzd.unused_data, b"") |
| 969 |
|
| 970 |
def test_decompressor_inputbuf_1(self): |
| 971 |
# Test reusing input buffer after moving existing |
| 972 |
# contents to beginning |
| 973 |
bzd = BZ2Decompressor() |
| 974 |
out = [] |
| 975 |
|
| 976 |
# Create input buffer and fill it |
| 977 |
self.assertEqual(bzd.decompress(self.DATA[:100], |
| 978 |
max_length=0), b'') |
| 979 |
|
| 980 |
# Retrieve some results, freeing capacity at beginning |
| 981 |
# of input buffer |
| 982 |
out.append(bzd.decompress(b'', 2)) |
| 983 |
|
| 984 |
# Add more data that fits into input buffer after |
| 985 |
# moving existing data to beginning |
| 986 |
out.append(bzd.decompress(self.DATA[100:105], 15)) |
| 987 |
|
| 988 |
# Decompress rest of data |
| 989 |
out.append(bzd.decompress(self.DATA[105:])) |
| 990 |
self.assertEqual(b''.join(out), self.TEXT) |
| 991 |
|
| 992 |
def test_decompressor_inputbuf_2(self): |
| 993 |
# Test reusing input buffer by appending data at the |
| 994 |
# end right away |
| 995 |
bzd = BZ2Decompressor() |
| 996 |
out = [] |
| 997 |
|
| 998 |
# Create input buffer and empty it |
| 999 |
self.assertEqual(bzd.decompress(self.DATA[:200], |
| 1000 |
max_length=0), b'') |
| 1001 |
out.append(bzd.decompress(b'')) |
| 1002 |
|
| 1003 |
# Fill buffer with new data |
| 1004 |
out.append(bzd.decompress(self.DATA[200:280], 2)) |
| 1005 |
|
| 1006 |
# Append some more data, not enough to require resize |
| 1007 |
out.append(bzd.decompress(self.DATA[280:300], 2)) |
| 1008 |
|
| 1009 |
# Decompress rest of data |
| 1010 |
out.append(bzd.decompress(self.DATA[300:])) |
| 1011 |
self.assertEqual(b''.join(out), self.TEXT) |
| 1012 |
|
| 1013 |
def test_decompressor_inputbuf_3(self): |
| 1014 |
# Test reusing input buffer after extending it |
| 1015 |
|
| 1016 |
bzd = BZ2Decompressor() |
| 1017 |
out = [] |
| 1018 |
|
| 1019 |
# Create almost full input buffer |
| 1020 |
out.append(bzd.decompress(self.DATA[:200], 5)) |
| 1021 |
|
| 1022 |
# Add even more data to it, requiring resize |
| 1023 |
out.append(bzd.decompress(self.DATA[200:300], 5)) |
| 1024 |
|
| 1025 |
# Decompress rest of data |
| 1026 |
out.append(bzd.decompress(self.DATA[300:])) |
| 1027 |
self.assertEqual(b''.join(out), self.TEXT) |
| 1028 |
|
| 1029 |
def test_failure(self): |
| 1030 |
bzd = BZ2Decompressor() |
| 1031 |
self.assertRaises(Exception, bzd.decompress, self.BAD_DATA * 30) |
| 1032 |
# Previously, a second call could crash due to internal inconsistency |
| 1033 |
self.assertRaises(Exception, bzd.decompress, self.BAD_DATA * 30) |
| 1034 |
|
| 1035 |
def test_decompress_after_data_error(self): |
| 1036 |
data = bytes.fromhex( |
| 1037 |
"425a6839314159265359000000000000007fffff000000000000000000000000" |
| 1038 |
"00000000000000000000000000000000000000e0370000000000000000000000" |
| 1039 |
"000000000000000000000000000000000000000000000000000083f3" |
| 1040 |
) |
| 1041 |
bzd = BZ2Decompressor() |
| 1042 |
with self.assertRaisesRegex(OSError, "Invalid data stream"): |
| 1043 |
bzd.decompress(data) |
| 1044 |
# Previously, a second call could crash due to internal inconsistency |
| 1045 |
self.assertFalse(bzd.needs_input) |
| 1046 |
self.assertFalse(bzd.eof) |
| 1047 |
with self.assertRaisesRegex(ValueError, "previous error"): |
| 1048 |
bzd.decompress(b'\x00' * 18) |
| 1049 |
|
| 1050 |
@support.refcount_test |
| 1051 |
def test_refleaks_in___init__(self): |
| 1052 |
gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount') |
| 1053 |
bzd = BZ2Decompressor() |
| 1054 |
refs_before = gettotalrefcount() |
| 1055 |
for i in range(100): |
| 1056 |
bzd.__init__() |
| 1057 |
self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10) |
| 1058 |
|
| 1059 |
def test_uninitialized_BZ2Decompressor_crash(self): |
| 1060 |
self.assertEqual(BZ2Decompressor.__new__(BZ2Decompressor). |
| 1061 |
decompress(bytes()), b'') |
| 1062 |
|
| 1063 |
|
| 1064 |
class CompressDecompressTest(BaseTest): |
| 1065 |
def testCompress(self): |
| 1066 |
data = bz2.compress(self.TEXT) |
| 1067 |
self.assertEqual(ext_decompress(data), self.TEXT) |
| 1068 |
|
| 1069 |
def testCompressEmptyString(self): |
| 1070 |
text = bz2.compress(b'') |
| 1071 |
self.assertEqual(text, self.EMPTY_DATA) |
| 1072 |
|
| 1073 |
def testDecompress(self): |
| 1074 |
text = bz2.decompress(self.DATA) |
| 1075 |
self.assertEqual(text, self.TEXT) |
| 1076 |
|
| 1077 |
def testDecompressEmpty(self): |
| 1078 |
text = bz2.decompress(b"") |
| 1079 |
self.assertEqual(text, b"") |
| 1080 |
|
| 1081 |
def testDecompressToEmptyString(self): |
| 1082 |
text = bz2.decompress(self.EMPTY_DATA) |
| 1083 |
self.assertEqual(text, b'') |
| 1084 |
|
| 1085 |
def testDecompressIncomplete(self): |
| 1086 |
self.assertRaises(ValueError, bz2.decompress, self.DATA[:-10]) |
| 1087 |
|
| 1088 |
def testDecompressBadData(self): |
| 1089 |
self.assertRaises(OSError, bz2.decompress, self.BAD_DATA) |
| 1090 |
|
| 1091 |
def testDecompressMultiStream(self): |
| 1092 |
text = bz2.decompress(self.DATA * 5) |
| 1093 |
self.assertEqual(text, self.TEXT * 5) |
| 1094 |
|
| 1095 |
def testDecompressTrailingJunk(self): |
| 1096 |
text = bz2.decompress(self.DATA + self.BAD_DATA) |
| 1097 |
self.assertEqual(text, self.TEXT) |
| 1098 |
|
| 1099 |
def testDecompressMultiStreamTrailingJunk(self): |
| 1100 |
text = bz2.decompress(self.DATA * 5 + self.BAD_DATA) |
| 1101 |
self.assertEqual(text, self.TEXT * 5) |
| 1102 |
|
| 1103 |
|
| 1104 |
class OpenTest(BaseTest): |
| 1105 |
"Test the open function." |
| 1106 |
|
| 1107 |
def open(self, *args, **kwargs): |
| 1108 |
return bz2.open(*args, **kwargs) |
| 1109 |
|
| 1110 |
def test_binary_modes(self): |
| 1111 |
for mode in ("wb", "xb"): |
| 1112 |
if mode == "xb": |
| 1113 |
unlink(self.filename) |
| 1114 |
with self.open(self.filename, mode) as f: |
| 1115 |
f.write(self.TEXT) |
| 1116 |
with open(self.filename, "rb") as f: |
| 1117 |
file_data = ext_decompress(f.read()) |
| 1118 |
self.assertEqual(file_data, self.TEXT) |
| 1119 |
with self.open(self.filename, "rb") as f: |
| 1120 |
self.assertEqual(f.read(), self.TEXT) |
| 1121 |
with self.open(self.filename, "ab") as f: |
| 1122 |
f.write(self.TEXT) |
| 1123 |
with open(self.filename, "rb") as f: |
| 1124 |
file_data = ext_decompress(f.read()) |
| 1125 |
self.assertEqual(file_data, self.TEXT * 2) |
| 1126 |
|
| 1127 |
def test_implicit_binary_modes(self): |
| 1128 |
# Test implicit binary modes (no "b" or "t" in mode string). |
| 1129 |
for mode in ("w", "x"): |
| 1130 |
if mode == "x": |
| 1131 |
unlink(self.filename) |
| 1132 |
with self.open(self.filename, mode) as f: |
| 1133 |
f.write(self.TEXT) |
| 1134 |
with open(self.filename, "rb") as f: |
| 1135 |
file_data = ext_decompress(f.read()) |
| 1136 |
self.assertEqual(file_data, self.TEXT) |
| 1137 |
with self.open(self.filename, "r") as f: |
| 1138 |
self.assertEqual(f.read(), self.TEXT) |
| 1139 |
with self.open(self.filename, "a") as f: |
| 1140 |
f.write(self.TEXT) |
| 1141 |
with open(self.filename, "rb") as f: |
| 1142 |
file_data = ext_decompress(f.read()) |
| 1143 |
self.assertEqual(file_data, self.TEXT * 2) |
| 1144 |
|
| 1145 |
def test_text_modes(self): |
| 1146 |
text = self.TEXT.decode("ascii") |
| 1147 |
text_native_eol = text.replace("\n", os.linesep) |
| 1148 |
for mode in ("wt", "xt"): |
| 1149 |
if mode == "xt": |
| 1150 |
unlink(self.filename) |
| 1151 |
with self.open(self.filename, mode, encoding="ascii") as f: |
| 1152 |
f.write(text) |
| 1153 |
with open(self.filename, "rb") as f: |
| 1154 |
file_data = ext_decompress(f.read()).decode("ascii") |
| 1155 |
self.assertEqual(file_data, text_native_eol) |
| 1156 |
with self.open(self.filename, "rt", encoding="ascii") as f: |
| 1157 |
self.assertEqual(f.read(), text) |
| 1158 |
with self.open(self.filename, "at", encoding="ascii") as f: |
| 1159 |
f.write(text) |
| 1160 |
with open(self.filename, "rb") as f: |
| 1161 |
file_data = ext_decompress(f.read()).decode("ascii") |
| 1162 |
self.assertEqual(file_data, text_native_eol * 2) |
| 1163 |
|
| 1164 |
def test_x_mode(self): |
| 1165 |
for mode in ("x", "xb", "xt"): |
| 1166 |
unlink(self.filename) |
| 1167 |
encoding = "utf-8" if "t" in mode else None |
| 1168 |
with self.open(self.filename, mode, encoding=encoding) as f: |
| 1169 |
pass |
| 1170 |
with self.assertRaises(FileExistsError): |
| 1171 |
with self.open(self.filename, mode) as f: |
| 1172 |
pass |
| 1173 |
|
| 1174 |
def test_fileobj(self): |
| 1175 |
with self.open(BytesIO(self.DATA), "r") as f: |
| 1176 |
self.assertEqual(f.read(), self.TEXT) |
| 1177 |
with self.open(BytesIO(self.DATA), "rb") as f: |
| 1178 |
self.assertEqual(f.read(), self.TEXT) |
| 1179 |
text = self.TEXT.decode("ascii") |
| 1180 |
with self.open(BytesIO(self.DATA), "rt", encoding="utf-8") as f: |
| 1181 |
self.assertEqual(f.read(), text) |
| 1182 |
|
| 1183 |
def test_bad_params(self): |
| 1184 |
# Test invalid parameter combinations. |
| 1185 |
self.assertRaises(ValueError, |
| 1186 |
self.open, self.filename, "wbt") |
| 1187 |
self.assertRaises(ValueError, |
| 1188 |
self.open, self.filename, "xbt") |
| 1189 |
self.assertRaises(ValueError, |
| 1190 |
self.open, self.filename, "rb", encoding="utf-8") |
| 1191 |
self.assertRaises(ValueError, |
| 1192 |
self.open, self.filename, "rb", errors="ignore") |
| 1193 |
self.assertRaises(ValueError, |
| 1194 |
self.open, self.filename, "rb", newline="\n") |
| 1195 |
|
| 1196 |
def test_encoding(self): |
| 1197 |
# Test non-default encoding. |
| 1198 |
text = self.TEXT.decode("ascii") |
| 1199 |
text_native_eol = text.replace("\n", os.linesep) |
| 1200 |
with self.open(self.filename, "wt", encoding="utf-16-le") as f: |
| 1201 |
f.write(text) |
| 1202 |
with open(self.filename, "rb") as f: |
| 1203 |
file_data = ext_decompress(f.read()).decode("utf-16-le") |
| 1204 |
self.assertEqual(file_data, text_native_eol) |
| 1205 |
with self.open(self.filename, "rt", encoding="utf-16-le") as f: |
| 1206 |
self.assertEqual(f.read(), text) |
| 1207 |
|
| 1208 |
def test_encoding_error_handler(self): |
| 1209 |
# Test with non-default encoding error handler. |
| 1210 |
with self.open(self.filename, "wb") as f: |
| 1211 |
f.write(b"foo\xffbar") |
| 1212 |
with self.open(self.filename, "rt", encoding="ascii", errors="ignore") \ |
| 1213 |
as f: |
| 1214 |
self.assertEqual(f.read(), "foobar") |
| 1215 |
|
| 1216 |
def test_newline(self): |
| 1217 |
# Test with explicit newline (universal newline mode disabled). |
| 1218 |
text = self.TEXT.decode("ascii") |
| 1219 |
with self.open(self.filename, "wt", encoding="utf-8", newline="\n") as f: |
| 1220 |
f.write(text) |
| 1221 |
with self.open(self.filename, "rt", encoding="utf-8", newline="\r") as f: |
| 1222 |
self.assertEqual(f.readlines(), [text]) |
| 1223 |
|
| 1224 |
|
| 1225 |
def tearDownModule(): |
| 1226 |
support.reap_children() |
| 1227 |
|
| 1228 |
|
| 1229 |
if __name__ == '__main__': |
| 1230 |
unittest.main() |