| 1 |
from __future__ import with_statement |
| 2 |
from rpython.rtyper.tool import rffi_platform as platform |
| 3 |
from rpython.rtyper.lltypesystem import rffi |
| 4 |
from rpython.rtyper.lltypesystem import lltype |
| 5 |
from pypy.interpreter.error import OperationError, oefmt |
| 6 |
from pypy.interpreter.baseobjspace import W_Root |
| 7 |
from pypy.interpreter.typedef import TypeDef, interp_attrproperty |
| 8 |
from pypy.interpreter.typedef import GetSetProperty |
| 9 |
from pypy.interpreter.gateway import interp2app, unwrap_spec |
| 10 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 11 |
from rpython.translator.platform import platform as compiler |
| 12 |
from rpython.rlib.rarithmetic import intmask, r_longlong |
| 13 |
import sys |
| 14 |
|
| 15 |
|
| 16 |
if compiler.name == "msvc": |
| 17 |
libname = 'libbz2' |
| 18 |
else: |
| 19 |
libname = 'bz2' |
| 20 |
eci = ExternalCompilationInfo( |
| 21 |
includes = ['stdio.h', 'sys/types.h', 'bzlib.h'], |
| 22 |
libraries = [libname], |
| 23 |
) |
| 24 |
eci = platform.configure_external_library( |
| 25 |
'bz2', eci, |
| 26 |
[dict(prefix='bzip2-')]) |
| 27 |
if not eci: |
| 28 |
raise ImportError("Could not find bzip2 library") |
| 29 |
|
| 30 |
class CConfig: |
| 31 |
_compilation_info_ = eci |
| 32 |
calling_conv = 'c' |
| 33 |
|
| 34 |
CHECK_LIBRARY = platform.Has('dump("x", (long)&BZ2_bzCompress)') |
| 35 |
|
| 36 |
off_t = platform.SimpleType("off_t", rffi.LONGLONG) |
| 37 |
size_t = platform.SimpleType("size_t", rffi.UNSIGNED) |
| 38 |
BUFSIZ = platform.ConstantInteger("BUFSIZ") |
| 39 |
_alloc_type = lltype.FuncType([rffi.VOIDP, rffi.INT, rffi.INT], rffi.VOIDP) |
| 40 |
_free_type = lltype.FuncType([rffi.VOIDP, rffi.VOIDP], lltype.Void) |
| 41 |
SEEK_SET = platform.ConstantInteger("SEEK_SET") |
| 42 |
bz_stream = platform.Struct('bz_stream', |
| 43 |
[('next_in', rffi.CCHARP), |
| 44 |
('avail_in', rffi.UINT), |
| 45 |
('total_in_lo32', rffi.UINT), |
| 46 |
('total_in_hi32', rffi.UINT), |
| 47 |
('next_out', rffi.CCHARP), |
| 48 |
('avail_out', rffi.UINT), |
| 49 |
('total_out_lo32', rffi.UINT), |
| 50 |
('total_out_hi32', rffi.UINT), |
| 51 |
('state', rffi.VOIDP), |
| 52 |
('bzalloc', lltype.Ptr(_alloc_type)), |
| 53 |
('bzfree', lltype.Ptr(_free_type)), |
| 54 |
('opaque', rffi.VOIDP), |
| 55 |
]) |
| 56 |
|
| 57 |
FILE = rffi.COpaquePtr('FILE') |
| 58 |
BZFILE = rffi.COpaquePtr('BZFILE') |
| 59 |
|
| 60 |
|
| 61 |
constants = {} |
| 62 |
constant_names = ['BZ_RUN', 'BZ_FLUSH', 'BZ_FINISH', 'BZ_OK', |
| 63 |
'BZ_RUN_OK', 'BZ_FLUSH_OK', 'BZ_FINISH_OK', 'BZ_STREAM_END', |
| 64 |
'BZ_SEQUENCE_ERROR', 'BZ_PARAM_ERROR', 'BZ_MEM_ERROR', 'BZ_DATA_ERROR', |
| 65 |
'BZ_DATA_ERROR_MAGIC', 'BZ_IO_ERROR', 'BZ_UNEXPECTED_EOF', |
| 66 |
'BZ_OUTBUFF_FULL', 'BZ_CONFIG_ERROR'] |
| 67 |
for name in constant_names: |
| 68 |
setattr(CConfig, name, platform.DefinedConstantInteger(name)) |
| 69 |
|
| 70 |
class cConfig(object): |
| 71 |
pass |
| 72 |
for k, v in platform.configure(CConfig).items(): |
| 73 |
setattr(cConfig, k, v) |
| 74 |
if not cConfig.CHECK_LIBRARY: |
| 75 |
raise ImportError("Invalid bz2 library") |
| 76 |
|
| 77 |
for name in constant_names: |
| 78 |
value = getattr(cConfig, name) |
| 79 |
if value is not None: |
| 80 |
constants[name] = value |
| 81 |
locals().update(constants) |
| 82 |
|
| 83 |
off_t = cConfig.off_t |
| 84 |
bz_stream = lltype.Ptr(cConfig.bz_stream) |
| 85 |
BUFSIZ = cConfig.BUFSIZ |
| 86 |
SEEK_SET = cConfig.SEEK_SET |
| 87 |
BZ_OK = cConfig.BZ_OK |
| 88 |
BZ_STREAM_END = cConfig.BZ_STREAM_END |
| 89 |
BZ_CONFIG_ERROR = cConfig.BZ_CONFIG_ERROR |
| 90 |
BZ_PARAM_ERROR = cConfig.BZ_PARAM_ERROR |
| 91 |
BZ_DATA_ERROR = cConfig.BZ_DATA_ERROR |
| 92 |
BZ_DATA_ERROR_MAGIC = cConfig.BZ_DATA_ERROR_MAGIC |
| 93 |
BZ_IO_ERROR = cConfig.BZ_IO_ERROR |
| 94 |
BZ_MEM_ERROR = cConfig.BZ_MEM_ERROR |
| 95 |
BZ_UNEXPECTED_EOF = cConfig.BZ_UNEXPECTED_EOF |
| 96 |
BZ_SEQUENCE_ERROR = cConfig.BZ_SEQUENCE_ERROR |
| 97 |
|
| 98 |
if BUFSIZ < 8192: |
| 99 |
INITIAL_BUFFER_SIZE = 8192 |
| 100 |
else: |
| 101 |
INITIAL_BUFFER_SIZE = 8192 |
| 102 |
|
| 103 |
UINT_MAX = 2**32-1 |
| 104 |
MAX_BUFSIZE = int(min(sys.maxint, UINT_MAX)) |
| 105 |
|
| 106 |
if rffi.sizeof(rffi.INT) > 4: |
| 107 |
BIGCHUNK = 512 * 32 |
| 108 |
else: |
| 109 |
BIGCHUNK = 512 * 1024 |
| 110 |
|
| 111 |
if BZ_CONFIG_ERROR: |
| 112 |
if rffi.sizeof(rffi.SIGNED) >= 8: |
| 113 |
def _bzs_total_out(bzs): |
| 114 |
return (rffi.getintfield(bzs, 'c_total_out_hi32') << 32) + \ |
| 115 |
rffi.getintfield(bzs, 'c_total_out_lo32') |
| 116 |
else: |
| 117 |
|
| 118 |
|
| 119 |
def _bzs_total_out(bzs): |
| 120 |
if rffi.getintfield(bzs, 'c_total_out_hi32') != 0 or \ |
| 121 |
rffi.getintfield(bzs, 'c_total_out_lo32') > sys.maxint: |
| 122 |
raise MemoryError |
| 123 |
return rffi.getintfield(bzs, 'c_total_out_lo32') |
| 124 |
else: |
| 125 |
XXX |
| 126 |
def _bzs_total_out(bzs): |
| 127 |
return bzs.total_out |
| 128 |
|
| 129 |
def external(name, args, result, **kwds): |
| 130 |
return rffi.llexternal(name, args, result, compilation_info= |
| 131 |
CConfig._compilation_info_, **kwds) |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
BZ2_bzReadOpen = external('BZ2_bzReadOpen', [rffi.INTP, FILE, rffi.INT, |
| 137 |
rffi.INT, rffi.INTP, rffi.INT], BZFILE) |
| 138 |
BZ2_bzWriteOpen = external('BZ2_bzWriteOpen', [rffi.INTP, FILE, rffi.INT, |
| 139 |
rffi.INT, rffi.INT], BZFILE) |
| 140 |
BZ2_bzReadClose = external('BZ2_bzReadClose', [rffi.INTP, BZFILE], lltype.Void) |
| 141 |
BZ2_bzWriteClose = external('BZ2_bzWriteClose', [rffi.INTP, BZFILE, |
| 142 |
rffi.INT, rffi.UINTP, rffi.UINTP], lltype.Void) |
| 143 |
BZ2_bzRead = external('BZ2_bzRead', [rffi.INTP, BZFILE, rffi.CCHARP, rffi.INT], |
| 144 |
rffi.INT) |
| 145 |
BZ2_bzWrite = external('BZ2_bzWrite', [rffi.INTP, BZFILE, rffi.CCHARP, |
| 146 |
rffi.INT], lltype.Void) |
| 147 |
BZ2_bzCompressInit = external('BZ2_bzCompressInit', [bz_stream, rffi.INT, |
| 148 |
rffi.INT, rffi.INT], rffi.INT) |
| 149 |
BZ2_bzCompressEnd = external('BZ2_bzCompressEnd', [bz_stream], rffi.INT, |
| 150 |
releasegil=False) |
| 151 |
BZ2_bzCompress = external('BZ2_bzCompress', [bz_stream, rffi.INT], rffi.INT) |
| 152 |
BZ2_bzDecompressInit = external('BZ2_bzDecompressInit', [bz_stream, rffi.INT, |
| 153 |
rffi.INT], rffi.INT) |
| 154 |
BZ2_bzDecompressEnd = external('BZ2_bzDecompressEnd', [bz_stream], rffi.INT, |
| 155 |
releasegil=False) |
| 156 |
BZ2_bzDecompress = external('BZ2_bzDecompress', [bz_stream], rffi.INT) |
| 157 |
|
| 158 |
def _catch_bz2_error(space, bzerror): |
| 159 |
if BZ_CONFIG_ERROR and bzerror == BZ_CONFIG_ERROR: |
| 160 |
raise oefmt(space.w_SystemError, |
| 161 |
"the bz2 library was not compiled correctly") |
| 162 |
if bzerror == BZ_PARAM_ERROR: |
| 163 |
raise oefmt(space.w_SystemError, |
| 164 |
"the bz2 library has received wrong parameters") |
| 165 |
elif bzerror == BZ_MEM_ERROR: |
| 166 |
raise OperationError(space.w_MemoryError, space.w_None) |
| 167 |
elif bzerror in (BZ_DATA_ERROR, BZ_DATA_ERROR_MAGIC): |
| 168 |
raise oefmt(space.w_IOError, "invalid data stream") |
| 169 |
elif bzerror == BZ_IO_ERROR: |
| 170 |
raise oefmt(space.w_IOError, "unknown IO error") |
| 171 |
elif bzerror == BZ_UNEXPECTED_EOF: |
| 172 |
raise oefmt(space.w_EOFError, |
| 173 |
"compressed file ended before the logical end-of-stream " |
| 174 |
"was detected") |
| 175 |
elif bzerror == BZ_SEQUENCE_ERROR: |
| 176 |
raise oefmt(space.w_RuntimeError, |
| 177 |
"wrong sequence of bz2 library commands used") |
| 178 |
|
| 179 |
def _new_buffer_size(current_size): |
| 180 |
|
| 181 |
|
| 182 |
if current_size < BIGCHUNK: |
| 183 |
return current_size + current_size |
| 184 |
return current_size |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
class OutBuffer(object): |
| 189 |
"""Handler for the output buffer. A bit custom code trying to |
| 190 |
encapsulate the logic of setting up the fields of 'bzs' and |
| 191 |
allocating raw memory as needed. |
| 192 |
""" |
| 193 |
def __init__(self, bzs, initial_size=INITIAL_BUFFER_SIZE, max_length=-1): |
| 194 |
|
| 195 |
|
| 196 |
self.temp = [] |
| 197 |
self.bzs = bzs |
| 198 |
self.max_length = max_length |
| 199 |
if max_length < 0 or max_length >= initial_size: |
| 200 |
size = initial_size |
| 201 |
else: |
| 202 |
size = max_length |
| 203 |
self._allocate_chunk(size) |
| 204 |
self.left = 0 |
| 205 |
|
| 206 |
def get_data_size(self): |
| 207 |
curr_out = self.current_size - rffi.getintfield(self.bzs, 'c_avail_out') |
| 208 |
total_size = curr_out |
| 209 |
for s in self.temp: |
| 210 |
total_size += len(s) |
| 211 |
return total_size |
| 212 |
|
| 213 |
def _allocate_chunk(self, size): |
| 214 |
self.raw_buf, self.gc_buf, self.case_num = rffi.alloc_buffer(size) |
| 215 |
self.current_size = size |
| 216 |
self.bzs.c_next_out = self.raw_buf |
| 217 |
rffi.setintfield(self.bzs, 'c_avail_out', size) |
| 218 |
|
| 219 |
def _get_chunk(self, chunksize): |
| 220 |
assert 0 <= chunksize <= self.current_size |
| 221 |
raw_buf = self.raw_buf |
| 222 |
gc_buf = self.gc_buf |
| 223 |
case_num = self.case_num |
| 224 |
s = rffi.str_from_buffer(raw_buf, gc_buf, case_num, |
| 225 |
self.current_size, chunksize) |
| 226 |
rffi.keep_buffer_alive_until_here(raw_buf, gc_buf, case_num) |
| 227 |
self.current_size = 0 |
| 228 |
return s |
| 229 |
|
| 230 |
def prepare_next_chunk(self): |
| 231 |
size = self.current_size |
| 232 |
self.temp.append(self._get_chunk(size)) |
| 233 |
newsize = size |
| 234 |
if self.max_length == -1: |
| 235 |
newsize = _new_buffer_size(size) |
| 236 |
else: |
| 237 |
newsize = min(newsize, self.max_length - self.get_data_size()) |
| 238 |
self._allocate_chunk(newsize) |
| 239 |
|
| 240 |
def make_result_string(self): |
| 241 |
count_unoccupied = rffi.getintfield(self.bzs, 'c_avail_out') |
| 242 |
s = self._get_chunk(self.current_size - count_unoccupied) |
| 243 |
if self.temp: |
| 244 |
self.temp.append(s) |
| 245 |
return ''.join(self.temp) |
| 246 |
else: |
| 247 |
return s |
| 248 |
|
| 249 |
def free(self): |
| 250 |
if self.current_size > 0: |
| 251 |
rffi.keep_buffer_alive_until_here(self.raw_buf, self.gc_buf, |
| 252 |
self.case_num) |
| 253 |
|
| 254 |
def __enter__(self): |
| 255 |
return self |
| 256 |
def __exit__(self, *args): |
| 257 |
self.free() |
| 258 |
|
| 259 |
|
| 260 |
@unwrap_spec(compresslevel=int) |
| 261 |
def descr_compressor__new__(space, w_subtype, compresslevel=9): |
| 262 |
x = space.allocate_instance(W_BZ2Compressor, w_subtype) |
| 263 |
W_BZ2Compressor.__init__(x, space, compresslevel) |
| 264 |
return x |
| 265 |
|
| 266 |
class W_BZ2Compressor(W_Root): |
| 267 |
"""BZ2Compressor([compresslevel=9]) -> compressor object |
| 268 |
|
| 269 |
Create a new compressor object. This object may be used to compress |
| 270 |
data sequentially. If you want to compress data in one shot, use the |
| 271 |
compress() function instead. The compresslevel parameter, if given, |
| 272 |
must be a number between 1 and 9.""" |
| 273 |
def __init__(self, space, compresslevel): |
| 274 |
self.space = space |
| 275 |
self._lock = space.allocate_lock() |
| 276 |
self.bzs = lltype.malloc(bz_stream.TO, flavor='raw', zero=True) |
| 277 |
try: |
| 278 |
self.running = False |
| 279 |
self._init_bz2comp(compresslevel) |
| 280 |
except: |
| 281 |
lltype.free(self.bzs, flavor='raw') |
| 282 |
self.bzs = lltype.nullptr(bz_stream.TO) |
| 283 |
raise |
| 284 |
self.register_finalizer(space) |
| 285 |
|
| 286 |
def lock(self): |
| 287 |
if not self._lock.acquire(False): |
| 288 |
self._lock.acquire(True) |
| 289 |
|
| 290 |
def unlock(self): |
| 291 |
self._lock.release() |
| 292 |
|
| 293 |
def _init_bz2comp(self, compresslevel): |
| 294 |
if compresslevel < 1 or compresslevel > 9: |
| 295 |
raise oefmt(self.space.w_ValueError, |
| 296 |
"compresslevel must be between 1 and 9") |
| 297 |
|
| 298 |
bzerror = intmask(BZ2_bzCompressInit(self.bzs, compresslevel, 0, 0)) |
| 299 |
if bzerror != BZ_OK: |
| 300 |
_catch_bz2_error(self.space, bzerror) |
| 301 |
|
| 302 |
self.running = True |
| 303 |
|
| 304 |
def _finalize_(self): |
| 305 |
bzs = self.bzs |
| 306 |
if bzs: |
| 307 |
self.bzs = lltype.nullptr(bz_stream.TO) |
| 308 |
BZ2_bzCompressEnd(bzs) |
| 309 |
lltype.free(bzs, flavor='raw') |
| 310 |
|
| 311 |
def descr_getstate(self): |
| 312 |
raise oefmt(self.space.w_TypeError, "cannot serialize '%T' object", self) |
| 313 |
|
| 314 |
@unwrap_spec(data='bufferstr') |
| 315 |
def compress(self, data): |
| 316 |
"""compress(data) -> string |
| 317 |
|
| 318 |
Provide more data to the compressor object. It will return chunks of |
| 319 |
compressed data whenever possible. When you've finished providing data |
| 320 |
to compress, call the flush() method to finish the compression process, |
| 321 |
and return what is left in the internal buffers.""" |
| 322 |
|
| 323 |
assert data is not None |
| 324 |
try: |
| 325 |
self.lock() |
| 326 |
datasize = len(data) |
| 327 |
|
| 328 |
if datasize == 0: |
| 329 |
return self.space.newbytes("") |
| 330 |
|
| 331 |
if not self.running: |
| 332 |
raise oefmt(self.space.w_ValueError, |
| 333 |
"this object was already flushed") |
| 334 |
|
| 335 |
in_bufsize = datasize |
| 336 |
|
| 337 |
with OutBuffer(self.bzs) as out: |
| 338 |
with rffi.scoped_nonmovingbuffer(data) as in_buf: |
| 339 |
|
| 340 |
self.bzs.c_next_in = in_buf |
| 341 |
rffi.setintfield(self.bzs, 'c_avail_in', in_bufsize) |
| 342 |
|
| 343 |
while True: |
| 344 |
bzerror = BZ2_bzCompress(self.bzs, BZ_RUN) |
| 345 |
if bzerror != BZ_RUN_OK: |
| 346 |
_catch_bz2_error(self.space, bzerror) |
| 347 |
|
| 348 |
if rffi.getintfield(self.bzs, 'c_avail_in') == 0: |
| 349 |
break |
| 350 |
elif rffi.getintfield(self.bzs, 'c_avail_out') == 0: |
| 351 |
out.prepare_next_chunk() |
| 352 |
|
| 353 |
res = out.make_result_string() |
| 354 |
return self.space.newbytes(res) |
| 355 |
finally: |
| 356 |
self.unlock() |
| 357 |
|
| 358 |
def flush(self): |
| 359 |
if not self.running: |
| 360 |
raise oefmt(self.space.w_ValueError, |
| 361 |
"this object was already flushed") |
| 362 |
self.running = False |
| 363 |
|
| 364 |
try: |
| 365 |
self.lock() |
| 366 |
with OutBuffer(self.bzs) as out: |
| 367 |
while True: |
| 368 |
bzerror = BZ2_bzCompress(self.bzs, BZ_FINISH) |
| 369 |
if bzerror == BZ_STREAM_END: |
| 370 |
break |
| 371 |
elif bzerror != BZ_FINISH_OK: |
| 372 |
_catch_bz2_error(self.space, bzerror) |
| 373 |
|
| 374 |
if rffi.getintfield(self.bzs, 'c_avail_out') == 0: |
| 375 |
out.prepare_next_chunk() |
| 376 |
|
| 377 |
res = out.make_result_string() |
| 378 |
return self.space.newbytes(res) |
| 379 |
finally: |
| 380 |
self.unlock() |
| 381 |
|
| 382 |
W_BZ2Compressor.typedef = TypeDef("_bz2.BZ2Compressor", |
| 383 |
__doc__ = W_BZ2Compressor.__doc__, |
| 384 |
__new__ = interp2app(descr_compressor__new__), |
| 385 |
__getstate__ = interp2app(W_BZ2Compressor.descr_getstate), |
| 386 |
compress = interp2app(W_BZ2Compressor.compress), |
| 387 |
flush = interp2app(W_BZ2Compressor.flush), |
| 388 |
) |
| 389 |
W_BZ2Compressor.typedef.acceptable_as_base_class = False |
| 390 |
|
| 391 |
def descr_decompressor__new__(space, w_subtype): |
| 392 |
x = space.allocate_instance(W_BZ2Decompressor, w_subtype) |
| 393 |
W_BZ2Decompressor.__init__(x, space) |
| 394 |
return x |
| 395 |
|
| 396 |
class W_BZ2Decompressor(W_Root): |
| 397 |
"""BZ2Decompressor() -> decompressor object |
| 398 |
|
| 399 |
Create a new decompressor object. This object may be used to decompress |
| 400 |
data sequentially. If you want to decompress data in one shot, use the |
| 401 |
decompress() function instead.""" |
| 402 |
|
| 403 |
def __init__(self, space): |
| 404 |
self.space = space |
| 405 |
self._lock = space.allocate_lock() |
| 406 |
|
| 407 |
self.bzs = lltype.malloc(bz_stream.TO, flavor='raw', zero=True) |
| 408 |
try: |
| 409 |
self.running = False |
| 410 |
self.unused_data = "" |
| 411 |
self.needs_input = True |
| 412 |
self.input_buffer = "" |
| 413 |
self.left_to_process = 0 |
| 414 |
|
| 415 |
self._init_bz2decomp() |
| 416 |
except: |
| 417 |
lltype.free(self.bzs, flavor='raw') |
| 418 |
self.bzs = lltype.nullptr(bz_stream.TO) |
| 419 |
raise |
| 420 |
self.register_finalizer(space) |
| 421 |
|
| 422 |
def lock(self): |
| 423 |
if not self._lock.acquire(False): |
| 424 |
self._lock.acquire(True) |
| 425 |
|
| 426 |
def unlock(self): |
| 427 |
self._lock.release() |
| 428 |
|
| 429 |
def _init_bz2decomp(self): |
| 430 |
bzerror = BZ2_bzDecompressInit(self.bzs, 0, 0) |
| 431 |
if bzerror != BZ_OK: |
| 432 |
_catch_bz2_error(self.space, bzerror) |
| 433 |
|
| 434 |
self.running = True |
| 435 |
|
| 436 |
def _finalize_(self): |
| 437 |
bzs = self.bzs |
| 438 |
if bzs: |
| 439 |
self.bzs = lltype.nullptr(bz_stream.TO) |
| 440 |
BZ2_bzDecompressEnd(bzs) |
| 441 |
lltype.free(bzs, flavor='raw') |
| 442 |
|
| 443 |
def descr_getstate(self): |
| 444 |
raise oefmt(self.space.w_TypeError, "cannot serialize '%T' object", self) |
| 445 |
|
| 446 |
def needs_input_w(self, space): |
| 447 |
""" True if more input is needed before more decompressed |
| 448 |
data can be produced. """ |
| 449 |
return space.newbool(self.needs_input) |
| 450 |
|
| 451 |
def eof_w(self, space): |
| 452 |
if self.running: |
| 453 |
return space.w_False |
| 454 |
else: |
| 455 |
return space.w_True |
| 456 |
|
| 457 |
def _decompress_buf(self, data, max_length): |
| 458 |
total_in = len(data) |
| 459 |
in_bufsize = min(total_in, MAX_BUFSIZE) |
| 460 |
total_in -= in_bufsize |
| 461 |
with rffi.scoped_nonmovingbuffer(data) as in_buf: |
| 462 |
|
| 463 |
self.bzs.c_next_in = in_buf |
| 464 |
rffi.setintfield(self.bzs, 'c_avail_in', in_bufsize) |
| 465 |
self.left_to_process = in_bufsize |
| 466 |
|
| 467 |
with OutBuffer(self.bzs, max_length=max_length) as out: |
| 468 |
while True: |
| 469 |
bzreturn = BZ2_bzDecompress(self.bzs) |
| 470 |
|
| 471 |
avail_in = rffi.getintfield(self.bzs, 'c_avail_in') |
| 472 |
self.left_to_process = avail_in |
| 473 |
if bzreturn == BZ_STREAM_END: |
| 474 |
self.running = False |
| 475 |
break |
| 476 |
if bzreturn != BZ_OK: |
| 477 |
_catch_bz2_error(self.space, bzreturn) |
| 478 |
|
| 479 |
if self.left_to_process == 0: |
| 480 |
break |
| 481 |
elif rffi.getintfield(self.bzs, 'c_avail_out') == 0: |
| 482 |
if out.get_data_size() == max_length: |
| 483 |
break |
| 484 |
out.prepare_next_chunk() |
| 485 |
|
| 486 |
self.left_to_process += total_in |
| 487 |
res = out.make_result_string() |
| 488 |
return self.space.newbytes(res) |
| 489 |
|
| 490 |
@unwrap_spec(data='bufferstr', max_length=int) |
| 491 |
def decompress(self, data, max_length=-1): |
| 492 |
"""decompress(data, max_length=-1) -> bytes |
| 493 |
|
| 494 |
Provide more data to the decompressor object. It will return chunks |
| 495 |
of decompressed data whenever possible. If you try to decompress data |
| 496 |
after the end of stream is found, EOFError will be raised. If any data |
| 497 |
was found after the end of stream, it'll be ignored and saved in |
| 498 |
unused_data attribute.""" |
| 499 |
|
| 500 |
try: |
| 501 |
self.lock() |
| 502 |
if not self.running: |
| 503 |
raise oefmt(self.space.w_EOFError, |
| 504 |
"end of stream was already found") |
| 505 |
datalen = len(data) |
| 506 |
if len(self.input_buffer) > 0: |
| 507 |
data = self.input_buffer + data |
| 508 |
datalen = len(data) |
| 509 |
self.input_buffer = "" |
| 510 |
|
| 511 |
result = self._decompress_buf(data, max_length) |
| 512 |
|
| 513 |
if not self.running: |
| 514 |
self.needs_input = False |
| 515 |
if self.left_to_process != 0: |
| 516 |
start = datalen - self.left_to_process |
| 517 |
assert start > 0 |
| 518 |
self.unused_data = data[start:] |
| 519 |
self.left_to_process = 0 |
| 520 |
elif self.left_to_process == 0: |
| 521 |
self.input_buffer = "" |
| 522 |
self.needs_input = True |
| 523 |
else: |
| 524 |
self.needs_input = False |
| 525 |
if self.left_to_process > 0: |
| 526 |
start = datalen-self.left_to_process |
| 527 |
assert start >= 0 |
| 528 |
self.input_buffer = data[start:] |
| 529 |
return result |
| 530 |
finally: |
| 531 |
self.unlock() |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
W_BZ2Decompressor.typedef = TypeDef("_bz2.BZ2Decompressor", |
| 538 |
__doc__ = W_BZ2Decompressor.__doc__, |
| 539 |
__new__ = interp2app(descr_decompressor__new__), |
| 540 |
__getstate__ = interp2app(W_BZ2Decompressor.descr_getstate), |
| 541 |
unused_data = interp_attrproperty("unused_data", W_BZ2Decompressor, |
| 542 |
wrapfn="newbytes"), |
| 543 |
eof = GetSetProperty(W_BZ2Decompressor.eof_w), |
| 544 |
decompress = interp2app(W_BZ2Decompressor.decompress), |
| 545 |
needs_input = GetSetProperty(W_BZ2Decompressor.needs_input_w), |
| 546 |
) |
| 547 |
W_BZ2Decompressor.typedef.acceptable_as_base_class = False |