Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3....
File: gzip.py
"""Functions that read and write gzipped files.
[0] Fix | Delete
[1] Fix | Delete
The user of the file doesn't have to worry about the compression,
[2] Fix | Delete
but random access is not allowed."""
[3] Fix | Delete
[4] Fix | Delete
# based on Andrew Kuchling's minigzip.py distributed with the zlib module
[5] Fix | Delete
[6] Fix | Delete
import struct, sys, time, os
[7] Fix | Delete
import zlib
[8] Fix | Delete
import builtins
[9] Fix | Delete
import io
[10] Fix | Delete
import _compression
[11] Fix | Delete
[12] Fix | Delete
__all__ = ["BadGzipFile", "GzipFile", "open", "compress", "decompress"]
[13] Fix | Delete
[14] Fix | Delete
FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16
[15] Fix | Delete
[16] Fix | Delete
READ, WRITE = 1, 2
[17] Fix | Delete
[18] Fix | Delete
_COMPRESS_LEVEL_FAST = 1
[19] Fix | Delete
_COMPRESS_LEVEL_TRADEOFF = 6
[20] Fix | Delete
_COMPRESS_LEVEL_BEST = 9
[21] Fix | Delete
[22] Fix | Delete
[23] Fix | Delete
def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_BEST,
[24] Fix | Delete
encoding=None, errors=None, newline=None):
[25] Fix | Delete
"""Open a gzip-compressed file in binary or text mode.
[26] Fix | Delete
[27] Fix | Delete
The filename argument can be an actual filename (a str or bytes object), or
[28] Fix | Delete
an existing file object to read from or write to.
[29] Fix | Delete
[30] Fix | Delete
The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or "ab" for
[31] Fix | Delete
binary mode, or "rt", "wt", "xt" or "at" for text mode. The default mode is
[32] Fix | Delete
"rb", and the default compresslevel is 9.
[33] Fix | Delete
[34] Fix | Delete
For binary mode, this function is equivalent to the GzipFile constructor:
[35] Fix | Delete
GzipFile(filename, mode, compresslevel). In this case, the encoding, errors
[36] Fix | Delete
and newline arguments must not be provided.
[37] Fix | Delete
[38] Fix | Delete
For text mode, a GzipFile object is created, and wrapped in an
[39] Fix | Delete
io.TextIOWrapper instance with the specified encoding, error handling
[40] Fix | Delete
behavior, and line ending(s).
[41] Fix | Delete
[42] Fix | Delete
"""
[43] Fix | Delete
if "t" in mode:
[44] Fix | Delete
if "b" in mode:
[45] Fix | Delete
raise ValueError("Invalid mode: %r" % (mode,))
[46] Fix | Delete
else:
[47] Fix | Delete
if encoding is not None:
[48] Fix | Delete
raise ValueError("Argument 'encoding' not supported in binary mode")
[49] Fix | Delete
if errors is not None:
[50] Fix | Delete
raise ValueError("Argument 'errors' not supported in binary mode")
[51] Fix | Delete
if newline is not None:
[52] Fix | Delete
raise ValueError("Argument 'newline' not supported in binary mode")
[53] Fix | Delete
[54] Fix | Delete
gz_mode = mode.replace("t", "")
[55] Fix | Delete
if isinstance(filename, (str, bytes, os.PathLike)):
[56] Fix | Delete
binary_file = GzipFile(filename, gz_mode, compresslevel)
[57] Fix | Delete
elif hasattr(filename, "read") or hasattr(filename, "write"):
[58] Fix | Delete
binary_file = GzipFile(None, gz_mode, compresslevel, filename)
[59] Fix | Delete
else:
[60] Fix | Delete
raise TypeError("filename must be a str or bytes object, or a file")
[61] Fix | Delete
[62] Fix | Delete
if "t" in mode:
[63] Fix | Delete
return io.TextIOWrapper(binary_file, encoding, errors, newline)
[64] Fix | Delete
else:
[65] Fix | Delete
return binary_file
[66] Fix | Delete
[67] Fix | Delete
def write32u(output, value):
[68] Fix | Delete
# The L format writes the bit pattern correctly whether signed
[69] Fix | Delete
# or unsigned.
[70] Fix | Delete
output.write(struct.pack("<L", value))
[71] Fix | Delete
[72] Fix | Delete
class _PaddedFile:
[73] Fix | Delete
"""Minimal read-only file object that prepends a string to the contents
[74] Fix | Delete
of an actual file. Shouldn't be used outside of gzip.py, as it lacks
[75] Fix | Delete
essential functionality."""
[76] Fix | Delete
[77] Fix | Delete
def __init__(self, f, prepend=b''):
[78] Fix | Delete
self._buffer = prepend
[79] Fix | Delete
self._length = len(prepend)
[80] Fix | Delete
self.file = f
[81] Fix | Delete
self._read = 0
[82] Fix | Delete
[83] Fix | Delete
def read(self, size):
[84] Fix | Delete
if self._read is None:
[85] Fix | Delete
return self.file.read(size)
[86] Fix | Delete
if self._read + size <= self._length:
[87] Fix | Delete
read = self._read
[88] Fix | Delete
self._read += size
[89] Fix | Delete
return self._buffer[read:self._read]
[90] Fix | Delete
else:
[91] Fix | Delete
read = self._read
[92] Fix | Delete
self._read = None
[93] Fix | Delete
return self._buffer[read:] + \
[94] Fix | Delete
self.file.read(size-self._length+read)
[95] Fix | Delete
[96] Fix | Delete
def prepend(self, prepend=b''):
[97] Fix | Delete
if self._read is None:
[98] Fix | Delete
self._buffer = prepend
[99] Fix | Delete
else: # Assume data was read since the last prepend() call
[100] Fix | Delete
self._read -= len(prepend)
[101] Fix | Delete
return
[102] Fix | Delete
self._length = len(self._buffer)
[103] Fix | Delete
self._read = 0
[104] Fix | Delete
[105] Fix | Delete
def seek(self, off):
[106] Fix | Delete
self._read = None
[107] Fix | Delete
self._buffer = None
[108] Fix | Delete
return self.file.seek(off)
[109] Fix | Delete
[110] Fix | Delete
def seekable(self):
[111] Fix | Delete
return True # Allows fast-forwarding even in unseekable streams
[112] Fix | Delete
[113] Fix | Delete
[114] Fix | Delete
class BadGzipFile(OSError):
[115] Fix | Delete
"""Exception raised in some cases for invalid gzip files."""
[116] Fix | Delete
[117] Fix | Delete
[118] Fix | Delete
class GzipFile(_compression.BaseStream):
[119] Fix | Delete
"""The GzipFile class simulates most of the methods of a file object with
[120] Fix | Delete
the exception of the truncate() method.
[121] Fix | Delete
[122] Fix | Delete
This class only supports opening files in binary mode. If you need to open a
[123] Fix | Delete
compressed file in text mode, use the gzip.open() function.
[124] Fix | Delete
[125] Fix | Delete
"""
[126] Fix | Delete
[127] Fix | Delete
# Overridden with internal file object to be closed, if only a filename
[128] Fix | Delete
# is passed in
[129] Fix | Delete
myfileobj = None
[130] Fix | Delete
[131] Fix | Delete
def __init__(self, filename=None, mode=None,
[132] Fix | Delete
compresslevel=_COMPRESS_LEVEL_BEST, fileobj=None, mtime=None):
[133] Fix | Delete
"""Constructor for the GzipFile class.
[134] Fix | Delete
[135] Fix | Delete
At least one of fileobj and filename must be given a
[136] Fix | Delete
non-trivial value.
[137] Fix | Delete
[138] Fix | Delete
The new class instance is based on fileobj, which can be a regular
[139] Fix | Delete
file, an io.BytesIO object, or any other object which simulates a file.
[140] Fix | Delete
It defaults to None, in which case filename is opened to provide
[141] Fix | Delete
a file object.
[142] Fix | Delete
[143] Fix | Delete
When fileobj is not None, the filename argument is only used to be
[144] Fix | Delete
included in the gzip file header, which may include the original
[145] Fix | Delete
filename of the uncompressed file. It defaults to the filename of
[146] Fix | Delete
fileobj, if discernible; otherwise, it defaults to the empty string,
[147] Fix | Delete
and in this case the original filename is not included in the header.
[148] Fix | Delete
[149] Fix | Delete
The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x', or
[150] Fix | Delete
'xb' depending on whether the file will be read or written. The default
[151] Fix | Delete
is the mode of fileobj if discernible; otherwise, the default is 'rb'.
[152] Fix | Delete
A mode of 'r' is equivalent to one of 'rb', and similarly for 'w' and
[153] Fix | Delete
'wb', 'a' and 'ab', and 'x' and 'xb'.
[154] Fix | Delete
[155] Fix | Delete
The compresslevel argument is an integer from 0 to 9 controlling the
[156] Fix | Delete
level of compression; 1 is fastest and produces the least compression,
[157] Fix | Delete
and 9 is slowest and produces the most compression. 0 is no compression
[158] Fix | Delete
at all. The default is 9.
[159] Fix | Delete
[160] Fix | Delete
The mtime argument is an optional numeric timestamp to be written
[161] Fix | Delete
to the last modification time field in the stream when compressing.
[162] Fix | Delete
If omitted or None, the current time is used.
[163] Fix | Delete
[164] Fix | Delete
"""
[165] Fix | Delete
[166] Fix | Delete
if mode and ('t' in mode or 'U' in mode):
[167] Fix | Delete
raise ValueError("Invalid mode: {!r}".format(mode))
[168] Fix | Delete
if mode and 'b' not in mode:
[169] Fix | Delete
mode += 'b'
[170] Fix | Delete
if fileobj is None:
[171] Fix | Delete
fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')
[172] Fix | Delete
if filename is None:
[173] Fix | Delete
filename = getattr(fileobj, 'name', '')
[174] Fix | Delete
if not isinstance(filename, (str, bytes)):
[175] Fix | Delete
filename = ''
[176] Fix | Delete
else:
[177] Fix | Delete
filename = os.fspath(filename)
[178] Fix | Delete
if mode is None:
[179] Fix | Delete
mode = getattr(fileobj, 'mode', 'rb')
[180] Fix | Delete
[181] Fix | Delete
if mode.startswith('r'):
[182] Fix | Delete
self.mode = READ
[183] Fix | Delete
raw = _GzipReader(fileobj)
[184] Fix | Delete
self._buffer = io.BufferedReader(raw)
[185] Fix | Delete
self.name = filename
[186] Fix | Delete
[187] Fix | Delete
elif mode.startswith(('w', 'a', 'x')):
[188] Fix | Delete
self.mode = WRITE
[189] Fix | Delete
self._init_write(filename)
[190] Fix | Delete
self.compress = zlib.compressobj(compresslevel,
[191] Fix | Delete
zlib.DEFLATED,
[192] Fix | Delete
-zlib.MAX_WBITS,
[193] Fix | Delete
zlib.DEF_MEM_LEVEL,
[194] Fix | Delete
0)
[195] Fix | Delete
self._write_mtime = mtime
[196] Fix | Delete
else:
[197] Fix | Delete
raise ValueError("Invalid mode: {!r}".format(mode))
[198] Fix | Delete
[199] Fix | Delete
self.fileobj = fileobj
[200] Fix | Delete
[201] Fix | Delete
if self.mode == WRITE:
[202] Fix | Delete
self._write_gzip_header(compresslevel)
[203] Fix | Delete
[204] Fix | Delete
@property
[205] Fix | Delete
def filename(self):
[206] Fix | Delete
import warnings
[207] Fix | Delete
warnings.warn("use the name attribute", DeprecationWarning, 2)
[208] Fix | Delete
if self.mode == WRITE and self.name[-3:] != ".gz":
[209] Fix | Delete
return self.name + ".gz"
[210] Fix | Delete
return self.name
[211] Fix | Delete
[212] Fix | Delete
@property
[213] Fix | Delete
def mtime(self):
[214] Fix | Delete
"""Last modification time read from stream, or None"""
[215] Fix | Delete
return self._buffer.raw._last_mtime
[216] Fix | Delete
[217] Fix | Delete
def __repr__(self):
[218] Fix | Delete
s = repr(self.fileobj)
[219] Fix | Delete
return '<gzip ' + s[1:-1] + ' ' + hex(id(self)) + '>'
[220] Fix | Delete
[221] Fix | Delete
def _init_write(self, filename):
[222] Fix | Delete
self.name = filename
[223] Fix | Delete
self.crc = zlib.crc32(b"")
[224] Fix | Delete
self.size = 0
[225] Fix | Delete
self.writebuf = []
[226] Fix | Delete
self.bufsize = 0
[227] Fix | Delete
self.offset = 0 # Current file offset for seek(), tell(), etc
[228] Fix | Delete
[229] Fix | Delete
def _write_gzip_header(self, compresslevel):
[230] Fix | Delete
self.fileobj.write(b'\037\213') # magic header
[231] Fix | Delete
self.fileobj.write(b'\010') # compression method
[232] Fix | Delete
try:
[233] Fix | Delete
# RFC 1952 requires the FNAME field to be Latin-1. Do not
[234] Fix | Delete
# include filenames that cannot be represented that way.
[235] Fix | Delete
fname = os.path.basename(self.name)
[236] Fix | Delete
if not isinstance(fname, bytes):
[237] Fix | Delete
fname = fname.encode('latin-1')
[238] Fix | Delete
if fname.endswith(b'.gz'):
[239] Fix | Delete
fname = fname[:-3]
[240] Fix | Delete
except UnicodeEncodeError:
[241] Fix | Delete
fname = b''
[242] Fix | Delete
flags = 0
[243] Fix | Delete
if fname:
[244] Fix | Delete
flags = FNAME
[245] Fix | Delete
self.fileobj.write(chr(flags).encode('latin-1'))
[246] Fix | Delete
mtime = self._write_mtime
[247] Fix | Delete
if mtime is None:
[248] Fix | Delete
mtime = time.time()
[249] Fix | Delete
write32u(self.fileobj, int(mtime))
[250] Fix | Delete
if compresslevel == _COMPRESS_LEVEL_BEST:
[251] Fix | Delete
xfl = b'\002'
[252] Fix | Delete
elif compresslevel == _COMPRESS_LEVEL_FAST:
[253] Fix | Delete
xfl = b'\004'
[254] Fix | Delete
else:
[255] Fix | Delete
xfl = b'\000'
[256] Fix | Delete
self.fileobj.write(xfl)
[257] Fix | Delete
self.fileobj.write(b'\377')
[258] Fix | Delete
if fname:
[259] Fix | Delete
self.fileobj.write(fname + b'\000')
[260] Fix | Delete
[261] Fix | Delete
def write(self,data):
[262] Fix | Delete
self._check_not_closed()
[263] Fix | Delete
if self.mode != WRITE:
[264] Fix | Delete
import errno
[265] Fix | Delete
raise OSError(errno.EBADF, "write() on read-only GzipFile object")
[266] Fix | Delete
[267] Fix | Delete
if self.fileobj is None:
[268] Fix | Delete
raise ValueError("write() on closed GzipFile object")
[269] Fix | Delete
[270] Fix | Delete
if isinstance(data, bytes):
[271] Fix | Delete
length = len(data)
[272] Fix | Delete
else:
[273] Fix | Delete
# accept any data that supports the buffer protocol
[274] Fix | Delete
data = memoryview(data)
[275] Fix | Delete
length = data.nbytes
[276] Fix | Delete
[277] Fix | Delete
if length > 0:
[278] Fix | Delete
self.fileobj.write(self.compress.compress(data))
[279] Fix | Delete
self.size += length
[280] Fix | Delete
self.crc = zlib.crc32(data, self.crc)
[281] Fix | Delete
self.offset += length
[282] Fix | Delete
[283] Fix | Delete
return length
[284] Fix | Delete
[285] Fix | Delete
def read(self, size=-1):
[286] Fix | Delete
self._check_not_closed()
[287] Fix | Delete
if self.mode != READ:
[288] Fix | Delete
import errno
[289] Fix | Delete
raise OSError(errno.EBADF, "read() on write-only GzipFile object")
[290] Fix | Delete
return self._buffer.read(size)
[291] Fix | Delete
[292] Fix | Delete
def read1(self, size=-1):
[293] Fix | Delete
"""Implements BufferedIOBase.read1()
[294] Fix | Delete
[295] Fix | Delete
Reads up to a buffer's worth of data if size is negative."""
[296] Fix | Delete
self._check_not_closed()
[297] Fix | Delete
if self.mode != READ:
[298] Fix | Delete
import errno
[299] Fix | Delete
raise OSError(errno.EBADF, "read1() on write-only GzipFile object")
[300] Fix | Delete
[301] Fix | Delete
if size < 0:
[302] Fix | Delete
size = io.DEFAULT_BUFFER_SIZE
[303] Fix | Delete
return self._buffer.read1(size)
[304] Fix | Delete
[305] Fix | Delete
def peek(self, n):
[306] Fix | Delete
self._check_not_closed()
[307] Fix | Delete
if self.mode != READ:
[308] Fix | Delete
import errno
[309] Fix | Delete
raise OSError(errno.EBADF, "peek() on write-only GzipFile object")
[310] Fix | Delete
return self._buffer.peek(n)
[311] Fix | Delete
[312] Fix | Delete
@property
[313] Fix | Delete
def closed(self):
[314] Fix | Delete
return self.fileobj is None
[315] Fix | Delete
[316] Fix | Delete
def close(self):
[317] Fix | Delete
fileobj = self.fileobj
[318] Fix | Delete
if fileobj is None:
[319] Fix | Delete
return
[320] Fix | Delete
self.fileobj = None
[321] Fix | Delete
try:
[322] Fix | Delete
if self.mode == WRITE:
[323] Fix | Delete
fileobj.write(self.compress.flush())
[324] Fix | Delete
write32u(fileobj, self.crc)
[325] Fix | Delete
# self.size may exceed 2 GiB, or even 4 GiB
[326] Fix | Delete
write32u(fileobj, self.size & 0xffffffff)
[327] Fix | Delete
elif self.mode == READ:
[328] Fix | Delete
self._buffer.close()
[329] Fix | Delete
finally:
[330] Fix | Delete
myfileobj = self.myfileobj
[331] Fix | Delete
if myfileobj:
[332] Fix | Delete
self.myfileobj = None
[333] Fix | Delete
myfileobj.close()
[334] Fix | Delete
[335] Fix | Delete
def flush(self,zlib_mode=zlib.Z_SYNC_FLUSH):
[336] Fix | Delete
self._check_not_closed()
[337] Fix | Delete
if self.mode == WRITE:
[338] Fix | Delete
# Ensure the compressor's buffer is flushed
[339] Fix | Delete
self.fileobj.write(self.compress.flush(zlib_mode))
[340] Fix | Delete
self.fileobj.flush()
[341] Fix | Delete
[342] Fix | Delete
def fileno(self):
[343] Fix | Delete
"""Invoke the underlying file object's fileno() method.
[344] Fix | Delete
[345] Fix | Delete
This will raise AttributeError if the underlying file object
[346] Fix | Delete
doesn't support fileno().
[347] Fix | Delete
"""
[348] Fix | Delete
return self.fileobj.fileno()
[349] Fix | Delete
[350] Fix | Delete
def rewind(self):
[351] Fix | Delete
'''Return the uncompressed stream file position indicator to the
[352] Fix | Delete
beginning of the file'''
[353] Fix | Delete
if self.mode != READ:
[354] Fix | Delete
raise OSError("Can't rewind in write mode")
[355] Fix | Delete
self._buffer.seek(0)
[356] Fix | Delete
[357] Fix | Delete
def readable(self):
[358] Fix | Delete
return self.mode == READ
[359] Fix | Delete
[360] Fix | Delete
def writable(self):
[361] Fix | Delete
return self.mode == WRITE
[362] Fix | Delete
[363] Fix | Delete
def seekable(self):
[364] Fix | Delete
return True
[365] Fix | Delete
[366] Fix | Delete
def seek(self, offset, whence=io.SEEK_SET):
[367] Fix | Delete
if self.mode == WRITE:
[368] Fix | Delete
if whence != io.SEEK_SET:
[369] Fix | Delete
if whence == io.SEEK_CUR:
[370] Fix | Delete
offset = self.offset + offset
[371] Fix | Delete
else:
[372] Fix | Delete
raise ValueError('Seek from end not supported')
[373] Fix | Delete
if offset < self.offset:
[374] Fix | Delete
raise OSError('Negative seek in write mode')
[375] Fix | Delete
count = offset - self.offset
[376] Fix | Delete
chunk = b'\0' * 1024
[377] Fix | Delete
for i in range(count // 1024):
[378] Fix | Delete
self.write(chunk)
[379] Fix | Delete
self.write(b'\0' * (count % 1024))
[380] Fix | Delete
elif self.mode == READ:
[381] Fix | Delete
self._check_not_closed()
[382] Fix | Delete
return self._buffer.seek(offset, whence)
[383] Fix | Delete
[384] Fix | Delete
return self.offset
[385] Fix | Delete
[386] Fix | Delete
def readline(self, size=-1):
[387] Fix | Delete
self._check_not_closed()
[388] Fix | Delete
return self._buffer.readline(size)
[389] Fix | Delete
[390] Fix | Delete
[391] Fix | Delete
class _GzipReader(_compression.DecompressReader):
[392] Fix | Delete
def __init__(self, fp):
[393] Fix | Delete
super().__init__(_PaddedFile(fp), zlib.decompressobj,
[394] Fix | Delete
wbits=-zlib.MAX_WBITS)
[395] Fix | Delete
# Set flag indicating start of a new member
[396] Fix | Delete
self._new_member = True
[397] Fix | Delete
self._last_mtime = None
[398] Fix | Delete
[399] Fix | Delete
def _init_read(self):
[400] Fix | Delete
self._crc = zlib.crc32(b"")
[401] Fix | Delete
self._stream_size = 0 # Decompressed size of unconcatenated stream
[402] Fix | Delete
[403] Fix | Delete
def _read_exact(self, n):
[404] Fix | Delete
'''Read exactly *n* bytes from `self._fp`
[405] Fix | Delete
[406] Fix | Delete
This method is required because self._fp may be unbuffered,
[407] Fix | Delete
i.e. return short reads.
[408] Fix | Delete
'''
[409] Fix | Delete
[410] Fix | Delete
data = self._fp.read(n)
[411] Fix | Delete
while len(data) < n:
[412] Fix | Delete
b = self._fp.read(n - len(data))
[413] Fix | Delete
if not b:
[414] Fix | Delete
raise EOFError("Compressed file ended before the "
[415] Fix | Delete
"end-of-stream marker was reached")
[416] Fix | Delete
data += b
[417] Fix | Delete
return data
[418] Fix | Delete
[419] Fix | Delete
def _read_gzip_header(self):
[420] Fix | Delete
magic = self._fp.read(2)
[421] Fix | Delete
if magic == b'':
[422] Fix | Delete
return False
[423] Fix | Delete
[424] Fix | Delete
if magic != b'\037\213':
[425] Fix | Delete
raise BadGzipFile('Not a gzipped file (%r)' % magic)
[426] Fix | Delete
[427] Fix | Delete
(method, flag,
[428] Fix | Delete
self._last_mtime) = struct.unpack("<BBIxx", self._read_exact(8))
[429] Fix | Delete
if method != 8:
[430] Fix | Delete
raise BadGzipFile('Unknown compression method')
[431] Fix | Delete
[432] Fix | Delete
if flag & FEXTRA:
[433] Fix | Delete
# Read & discard the extra field, if present
[434] Fix | Delete
extra_len, = struct.unpack("<H", self._read_exact(2))
[435] Fix | Delete
self._read_exact(extra_len)
[436] Fix | Delete
if flag & FNAME:
[437] Fix | Delete
# Read and discard a null-terminated string containing the filename
[438] Fix | Delete
while True:
[439] Fix | Delete
s = self._fp.read(1)
[440] Fix | Delete
if not s or s==b'\000':
[441] Fix | Delete
break
[442] Fix | Delete
if flag & FCOMMENT:
[443] Fix | Delete
# Read and discard a null-terminated string containing a comment
[444] Fix | Delete
while True:
[445] Fix | Delete
s = self._fp.read(1)
[446] Fix | Delete
if not s or s==b'\000':
[447] Fix | Delete
break
[448] Fix | Delete
if flag & FHCRC:
[449] Fix | Delete
self._read_exact(2) # Read & discard the 16-bit header CRC
[450] Fix | Delete
return True
[451] Fix | Delete
[452] Fix | Delete
def read(self, size=-1):
[453] Fix | Delete
if size < 0:
[454] Fix | Delete
return self.readall()
[455] Fix | Delete
# size=0 is special because decompress(max_length=0) is not supported
[456] Fix | Delete
if not size:
[457] Fix | Delete
return b""
[458] Fix | Delete
[459] Fix | Delete
# For certain input data, a single
[460] Fix | Delete
# call to decompress() may not return
[461] Fix | Delete
# any data. In this case, retry until we get some data or reach EOF.
[462] Fix | Delete
while True:
[463] Fix | Delete
if self._decompressor.eof:
[464] Fix | Delete
# Ending case: we've come to the end of a member in the file,
[465] Fix | Delete
# so finish up this member, and read a new gzip header.
[466] Fix | Delete
# Check the CRC and file size, and set the flag so we read
[467] Fix | Delete
# a new member
[468] Fix | Delete
self._read_eof()
[469] Fix | Delete
self._new_member = True
[470] Fix | Delete
self._decompressor = self._decomp_factory(
[471] Fix | Delete
**self._decomp_args)
[472] Fix | Delete
[473] Fix | Delete
if self._new_member:
[474] Fix | Delete
# If the _new_member flag is set, we have to
[475] Fix | Delete
# jump to the next member, if there is one.
[476] Fix | Delete
self._init_read()
[477] Fix | Delete
if not self._read_gzip_header():
[478] Fix | Delete
self._size = self._pos
[479] Fix | Delete
return b""
[480] Fix | Delete
self._new_member = False
[481] Fix | Delete
[482] Fix | Delete
# Read a chunk of data from the file
[483] Fix | Delete
buf = self._fp.read(io.DEFAULT_BUFFER_SIZE)
[484] Fix | Delete
[485] Fix | Delete
uncompress = self._decompressor.decompress(buf, size)
[486] Fix | Delete
if self._decompressor.unconsumed_tail != b"":
[487] Fix | Delete
self._fp.prepend(self._decompressor.unconsumed_tail)
[488] Fix | Delete
elif self._decompressor.unused_data != b"":
[489] Fix | Delete
# Prepend the already read bytes to the fileobj so they can
[490] Fix | Delete
# be seen by _read_eof() and _read_gzip_header()
[491] Fix | Delete
self._fp.prepend(self._decompressor.unused_data)
[492] Fix | Delete
[493] Fix | Delete
if uncompress != b"":
[494] Fix | Delete
break
[495] Fix | Delete
if buf == b"":
[496] Fix | Delete
raise EOFError("Compressed file ended before the "
[497] Fix | Delete
"end-of-stream marker was reached")
[498] Fix | Delete
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function