@@ -343,7 +343,8 @@ class _Stream:
343
343
_Stream is intended to be used only internally.
344
344
"""
345
345
346
- def __init__ (self , name , mode , comptype , fileobj , bufsize ):
346
+ def __init__ (self , name , mode , comptype , fileobj , bufsize ,
347
+ compresslevel = 9 ):
347
348
"""Construct a _Stream object.
348
349
"""
349
350
self ._extfileobj = True
@@ -357,14 +358,15 @@ def __init__(self, name, mode, comptype, fileobj, bufsize):
357
358
fileobj = _StreamProxy (fileobj )
358
359
comptype = fileobj .getcomptype ()
359
360
360
- self .name = name or ""
361
- self .mode = mode
361
+ self .name = name or ""
362
+ self .mode = mode
362
363
self .comptype = comptype
363
- self .fileobj = fileobj
364
- self .bufsize = bufsize
365
- self .buf = b""
366
- self .pos = 0
367
- self .closed = False
364
+ self .fileobj = fileobj
365
+ self .bufsize = bufsize
366
+ self .compresslevel = compresslevel
367
+ self .buf = b""
368
+ self .pos = 0
369
+ self .closed = False
368
370
369
371
try :
370
372
if comptype == "gz" :
@@ -390,7 +392,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize):
390
392
self .cmp = bz2 .BZ2Decompressor ()
391
393
self .exception = OSError
392
394
else :
393
- self .cmp = bz2 .BZ2Compressor ()
395
+ self .cmp = bz2 .BZ2Compressor (self . compresslevel )
394
396
395
397
elif comptype == "xz" :
396
398
try :
@@ -420,10 +422,11 @@ def __del__(self):
420
422
def _init_write_gz (self ):
421
423
"""Initialize for writing with gzip compression.
422
424
"""
423
- self .cmp = self .zlib .compressobj (9 , self .zlib .DEFLATED ,
424
- - self .zlib .MAX_WBITS ,
425
- self .zlib .DEF_MEM_LEVEL ,
426
- 0 )
425
+ self .cmp = self .zlib .compressobj (self .compresslevel ,
426
+ self .zlib .DEFLATED ,
427
+ - self .zlib .MAX_WBITS ,
428
+ self .zlib .DEF_MEM_LEVEL ,
429
+ 0 )
427
430
timestamp = struct .pack ("<L" , int (time .time ()))
428
431
self .__write (b"\037 \213 \010 \010 " + timestamp + b"\002 \377 " )
429
432
if self .name .endswith (".gz" ):
@@ -1597,7 +1600,9 @@ def not_compressed(comptype):
1597
1600
if filemode not in ("r" , "w" ):
1598
1601
raise ValueError ("mode must be 'r' or 'w'" )
1599
1602
1600
- stream = _Stream (name , filemode , comptype , fileobj , bufsize )
1603
+ compresslevel = kwargs .pop ("compresslevel" , 9 )
1604
+ stream = _Stream (name , filemode , comptype , fileobj , bufsize ,
1605
+ compresslevel )
1601
1606
try :
1602
1607
t = cls (name , filemode , stream , ** kwargs )
1603
1608
except :
0 commit comments