| 1 |
import os |
| 2 |
|
| 3 |
if os.name == "nt": |
| 4 |
from pytest import skip |
| 5 |
skip("bz2 module is not available on Windows") |
| 6 |
|
| 7 |
import py |
| 8 |
import glob |
| 9 |
import bz2 |
| 10 |
|
| 11 |
from pypy.module.bz2.test.support import CheckAllocation |
| 12 |
from pypy.module.bz2 import interp_bz2 |
| 13 |
from pypy.interpreter import gateway |
| 14 |
|
| 15 |
HUGE_OK = False |
| 16 |
|
| 17 |
def setup_module(mod): |
| 18 |
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`' |
| 19 |
|
| 20 |
def decompress(cls, data): |
| 21 |
import subprocess |
| 22 |
import bz2 |
| 23 |
pop = subprocess.Popen("bunzip2", stdin=subprocess.PIPE, |
| 24 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 25 |
pop.stdin.write(data) |
| 26 |
stdout, stderr = pop.communicate() |
| 27 |
res = stdout |
| 28 |
if pop.wait() != 0: |
| 29 |
res = bz2.decompress(data) |
| 30 |
return res |
| 31 |
|
| 32 |
mod.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' |
| 33 |
mod.DATA = DATA |
| 34 |
mod.BUGGY_DATA = py.path.local(__file__).dirpath().join('data.bz2').read() |
| 35 |
mod.decompress = decompress |
| 36 |
|
| 37 |
|
| 38 |
mod.OLD_SMALLCHUNK = interp_bz2.INITIAL_BUFFER_SIZE |
| 39 |
interp_bz2.INITIAL_BUFFER_SIZE = 32 |
| 40 |
|
| 41 |
def teardown_module(mod): |
| 42 |
interp_bz2.INITIAL_BUFFER_SIZE = mod.OLD_SMALLCHUNK |
| 43 |
|
| 44 |
class AppTestBZ2Compressor(CheckAllocation): |
| 45 |
spaceconfig = dict(usemodules=('bz2', 'time', 'struct')) |
| 46 |
|
| 47 |
def setup_class(cls): |
| 48 |
cls.w_TEXT = cls.space.newbytes(TEXT) |
| 49 |
if cls.runappdirect: |
| 50 |
cls.w_decompress = decompress |
| 51 |
else: |
| 52 |
@gateway.unwrap_spec(data='bytes') |
| 53 |
def decompress_w(space, data): |
| 54 |
return space.newbytes(decompress(cls, data)) |
| 55 |
cls.w_decompress = cls.space.wrap(gateway.interp2app(decompress_w)) |
| 56 |
cls.w_HUGE_OK = cls.space.wrap(HUGE_OK) |
| 57 |
|
| 58 |
cls.space.appexec([], """(): import warnings""") |
| 59 |
|
| 60 |
def test_creation(self): |
| 61 |
from _bz2 import BZ2Compressor |
| 62 |
|
| 63 |
raises(TypeError, BZ2Compressor, "foo") |
| 64 |
raises(ValueError, BZ2Compressor, 10) |
| 65 |
|
| 66 |
BZ2Compressor(1) |
| 67 |
BZ2Compressor(9) |
| 68 |
|
| 69 |
def test_compress(self): |
| 70 |
from _bz2 import BZ2Compressor |
| 71 |
|
| 72 |
bz2c = BZ2Compressor() |
| 73 |
raises(TypeError, bz2c.compress) |
| 74 |
data = bz2c.compress(self.TEXT) |
| 75 |
data += bz2c.flush() |
| 76 |
assert self.decompress(data) == self.TEXT |
| 77 |
|
| 78 |
def test_compress_huge_data(self): |
| 79 |
if not self.HUGE_OK: |
| 80 |
skip("skipping test requiring lots of memory") |
| 81 |
from _bz2 import BZ2Compressor |
| 82 |
|
| 83 |
HUGE_DATA = self.TEXT * 10000 |
| 84 |
bz2c = BZ2Compressor() |
| 85 |
raises(TypeError, bz2c.compress) |
| 86 |
data = bz2c.compress(HUGE_DATA) |
| 87 |
data += bz2c.flush() |
| 88 |
assert self.decompress(data) == HUGE_DATA |
| 89 |
|
| 90 |
def test_compress_chunks_10(self): |
| 91 |
from _bz2 import BZ2Compressor |
| 92 |
|
| 93 |
bz2c = BZ2Compressor() |
| 94 |
n = 0 |
| 95 |
data = b"" |
| 96 |
while True: |
| 97 |
temp = self.TEXT[n * 10:(n + 1) * 10] |
| 98 |
if not temp: |
| 99 |
break |
| 100 |
data += bz2c.compress(temp) |
| 101 |
n += 1 |
| 102 |
data += bz2c.flush() |
| 103 |
assert self.decompress(data) == self.TEXT |
| 104 |
|
| 105 |
def test_buffer(self): |
| 106 |
from _bz2 import BZ2Compressor |
| 107 |
bz2c = BZ2Compressor() |
| 108 |
data = bz2c.compress(memoryview(self.TEXT)) |
| 109 |
data += bz2c.flush() |
| 110 |
assert self.decompress(data) == self.TEXT |
| 111 |
|
| 112 |
def test_compressor_pickle_error(self): |
| 113 |
from _bz2 import BZ2Compressor |
| 114 |
import _pickle as pickle |
| 115 |
|
| 116 |
exc = raises(TypeError, pickle.dumps, BZ2Compressor()) |
| 117 |
assert exc.value.args[0] == "cannot serialize '_bz2.BZ2Compressor' object" |
| 118 |
|
| 119 |
|
| 120 |
class AppTestBZ2Decompressor(CheckAllocation): |
| 121 |
spaceconfig = dict(usemodules=('bz2', 'time', 'struct')) |
| 122 |
|
| 123 |
def setup_class(cls): |
| 124 |
cls.w_TEXT = cls.space.newbytes(TEXT) |
| 125 |
cls.w_DATA = cls.space.newbytes(DATA) |
| 126 |
cls.w_BUGGY_DATA = cls.space.newbytes(BUGGY_DATA) |
| 127 |
|
| 128 |
cls.space.appexec([], """(): import warnings""") |
| 129 |
|
| 130 |
def test_creation(self): |
| 131 |
from _bz2 import BZ2Decompressor |
| 132 |
|
| 133 |
raises(TypeError, BZ2Decompressor, "foo") |
| 134 |
|
| 135 |
BZ2Decompressor() |
| 136 |
|
| 137 |
def test_attribute(self): |
| 138 |
from _bz2 import BZ2Decompressor |
| 139 |
|
| 140 |
bz2d = BZ2Decompressor() |
| 141 |
assert bz2d.unused_data == b"" |
| 142 |
|
| 143 |
def test_decompress(self): |
| 144 |
from _bz2 import BZ2Decompressor |
| 145 |
|
| 146 |
bz2d = BZ2Decompressor() |
| 147 |
raises(TypeError, bz2d.decompress) |
| 148 |
decompressed_data = bz2d.decompress(self.DATA) |
| 149 |
assert decompressed_data == self.TEXT |
| 150 |
|
| 151 |
def test_decompress_chunks_10(self): |
| 152 |
from _bz2 import BZ2Decompressor |
| 153 |
|
| 154 |
bz2d = BZ2Decompressor() |
| 155 |
decompressed_data = b"" |
| 156 |
n = 0 |
| 157 |
while True: |
| 158 |
temp = self.DATA[n * 10:(n + 1) * 10] |
| 159 |
if not temp: |
| 160 |
break |
| 161 |
decompressed_data += bz2d.decompress(temp) |
| 162 |
n += 1 |
| 163 |
|
| 164 |
assert decompressed_data == self.TEXT |
| 165 |
|
| 166 |
def test_decompress_unused_data(self): |
| 167 |
|
| 168 |
from _bz2 import BZ2Decompressor |
| 169 |
|
| 170 |
bz2d = BZ2Decompressor() |
| 171 |
unused_data = b"this is unused data" |
| 172 |
decompressed_data = bz2d.decompress(self.DATA + unused_data) |
| 173 |
assert decompressed_data == self.TEXT |
| 174 |
assert bz2d.unused_data == unused_data |
| 175 |
|
| 176 |
def test_EOF_error(self): |
| 177 |
from _bz2 import BZ2Decompressor |
| 178 |
|
| 179 |
bz2d = BZ2Decompressor() |
| 180 |
bz2d.decompress(self.DATA) |
| 181 |
raises(EOFError, bz2d.decompress, b"foo") |
| 182 |
raises(EOFError, bz2d.decompress, b"") |
| 183 |
|
| 184 |
def test_buffer(self): |
| 185 |
from _bz2 import BZ2Decompressor |
| 186 |
bz2d = BZ2Decompressor() |
| 187 |
decompressed_data = bz2d.decompress(memoryview(self.DATA)) |
| 188 |
assert decompressed_data == self.TEXT |
| 189 |
|
| 190 |
def test_subsequent_read(self): |
| 191 |
from _bz2 import BZ2Decompressor |
| 192 |
bz2d = BZ2Decompressor() |
| 193 |
decompressed_data = bz2d.decompress(self.BUGGY_DATA) |
| 194 |
assert decompressed_data == b'' |
| 195 |
raises(IOError, bz2d.decompress, self.BUGGY_DATA) |
| 196 |
|
| 197 |
def test_decompressor_pickle_error(self): |
| 198 |
from _bz2 import BZ2Decompressor |
| 199 |
import _pickle as pickle |
| 200 |
|
| 201 |
exc = raises(TypeError, pickle.dumps, BZ2Decompressor()) |
| 202 |
assert exc.value.args[0] == "cannot serialize '_bz2.BZ2Decompressor' object" |
| 203 |
|
| 204 |
def test_decompress_max_length(self): |
| 205 |
from _bz2 import BZ2Decompressor |
| 206 |
|
| 207 |
bz2d = BZ2Decompressor() |
| 208 |
decomp= [] |
| 209 |
|
| 210 |
length = len(self.DATA) |
| 211 |
decomp.append(bz2d.decompress(self.DATA, max_length=100)) |
| 212 |
assert len(decomp[-1]) == 100 |
| 213 |
|
| 214 |
while not bz2d.eof: |
| 215 |
decomp.append(bz2d.decompress(b"", max_length=200)) |
| 216 |
assert len(decomp[-1]) <= 200 |
| 217 |
|
| 218 |
assert b''.join(decomp) == self.TEXT |
| 219 |
|