Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
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 io
[9] Fix | Delete
import __builtin__
[10] Fix | Delete
[11] Fix | Delete
__all__ = ["GzipFile","open"]
[12] Fix | Delete
[13] Fix | Delete
FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16
[14] Fix | Delete
[15] Fix | Delete
READ, WRITE = 1, 2
[16] Fix | Delete
[17] Fix | Delete
def write32u(output, value):
[18] Fix | Delete
# The L format writes the bit pattern correctly whether signed
[19] Fix | Delete
# or unsigned.
[20] Fix | Delete
output.write(struct.pack("<L", value))
[21] Fix | Delete
[22] Fix | Delete
def read32(input):
[23] Fix | Delete
return struct.unpack("<I", input.read(4))[0]
[24] Fix | Delete
[25] Fix | Delete
def open(filename, mode="rb", compresslevel=9):
[26] Fix | Delete
"""Shorthand for GzipFile(filename, mode, compresslevel).
[27] Fix | Delete
[28] Fix | Delete
The filename argument is required; mode defaults to 'rb'
[29] Fix | Delete
and compresslevel defaults to 9.
[30] Fix | Delete
[31] Fix | Delete
"""
[32] Fix | Delete
return GzipFile(filename, mode, compresslevel)
[33] Fix | Delete
[34] Fix | Delete
class GzipFile(io.BufferedIOBase):
[35] Fix | Delete
"""The GzipFile class simulates most of the methods of a file object with
[36] Fix | Delete
the exception of the readinto() and truncate() methods.
[37] Fix | Delete
[38] Fix | Delete
"""
[39] Fix | Delete
[40] Fix | Delete
myfileobj = None
[41] Fix | Delete
max_read_chunk = 10 * 1024 * 1024 # 10Mb
[42] Fix | Delete
[43] Fix | Delete
def __init__(self, filename=None, mode=None,
[44] Fix | Delete
compresslevel=9, fileobj=None, mtime=None):
[45] Fix | Delete
"""Constructor for the GzipFile class.
[46] Fix | Delete
[47] Fix | Delete
At least one of fileobj and filename must be given a
[48] Fix | Delete
non-trivial value.
[49] Fix | Delete
[50] Fix | Delete
The new class instance is based on fileobj, which can be a regular
[51] Fix | Delete
file, a StringIO object, or any other object which simulates a file.
[52] Fix | Delete
It defaults to None, in which case filename is opened to provide
[53] Fix | Delete
a file object.
[54] Fix | Delete
[55] Fix | Delete
When fileobj is not None, the filename argument is only used to be
[56] Fix | Delete
included in the gzip file header, which may include the original
[57] Fix | Delete
filename of the uncompressed file. It defaults to the filename of
[58] Fix | Delete
fileobj, if discernible; otherwise, it defaults to the empty string,
[59] Fix | Delete
and in this case the original filename is not included in the header.
[60] Fix | Delete
[61] Fix | Delete
The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', or 'wb',
[62] Fix | Delete
depending on whether the file will be read or written. The default
[63] Fix | Delete
is the mode of fileobj if discernible; otherwise, the default is 'rb'.
[64] Fix | Delete
Be aware that only the 'rb', 'ab', and 'wb' values should be used
[65] Fix | Delete
for cross-platform portability.
[66] Fix | Delete
[67] Fix | Delete
The compresslevel argument is an integer from 0 to 9 controlling the
[68] Fix | Delete
level of compression; 1 is fastest and produces the least compression,
[69] Fix | Delete
and 9 is slowest and produces the most compression. 0 is no compression
[70] Fix | Delete
at all. The default is 9.
[71] Fix | Delete
[72] Fix | Delete
The mtime argument is an optional numeric timestamp to be written
[73] Fix | Delete
to the stream when compressing. All gzip compressed streams
[74] Fix | Delete
are required to contain a timestamp. If omitted or None, the
[75] Fix | Delete
current time is used. This module ignores the timestamp when
[76] Fix | Delete
decompressing; however, some programs, such as gunzip, make use
[77] Fix | Delete
of it. The format of the timestamp is the same as that of the
[78] Fix | Delete
return value of time.time() and of the st_mtime member of the
[79] Fix | Delete
object returned by os.stat().
[80] Fix | Delete
[81] Fix | Delete
"""
[82] Fix | Delete
[83] Fix | Delete
# Make sure we don't inadvertently enable universal newlines on the
[84] Fix | Delete
# underlying file object - in read mode, this causes data corruption.
[85] Fix | Delete
if mode:
[86] Fix | Delete
mode = mode.replace('U', '')
[87] Fix | Delete
# guarantee the file is opened in binary mode on platforms
[88] Fix | Delete
# that care about that sort of thing
[89] Fix | Delete
if mode and 'b' not in mode:
[90] Fix | Delete
mode += 'b'
[91] Fix | Delete
if fileobj is None:
[92] Fix | Delete
fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb')
[93] Fix | Delete
if filename is None:
[94] Fix | Delete
# Issue #13781: os.fdopen() creates a fileobj with a bogus name
[95] Fix | Delete
# attribute. Avoid saving this in the gzip header's filename field.
[96] Fix | Delete
if hasattr(fileobj, 'name') and fileobj.name != '<fdopen>':
[97] Fix | Delete
filename = fileobj.name
[98] Fix | Delete
else:
[99] Fix | Delete
filename = ''
[100] Fix | Delete
if mode is None:
[101] Fix | Delete
if hasattr(fileobj, 'mode'): mode = fileobj.mode
[102] Fix | Delete
else: mode = 'rb'
[103] Fix | Delete
[104] Fix | Delete
if mode[0:1] == 'r':
[105] Fix | Delete
self.mode = READ
[106] Fix | Delete
# Set flag indicating start of a new member
[107] Fix | Delete
self._new_member = True
[108] Fix | Delete
# Buffer data read from gzip file. extrastart is offset in
[109] Fix | Delete
# stream where buffer starts. extrasize is number of
[110] Fix | Delete
# bytes remaining in buffer from current stream position.
[111] Fix | Delete
self.extrabuf = ""
[112] Fix | Delete
self.extrasize = 0
[113] Fix | Delete
self.extrastart = 0
[114] Fix | Delete
self.name = filename
[115] Fix | Delete
# Starts small, scales exponentially
[116] Fix | Delete
self.min_readsize = 100
[117] Fix | Delete
[118] Fix | Delete
elif mode[0:1] == 'w' or mode[0:1] == 'a':
[119] Fix | Delete
self.mode = WRITE
[120] Fix | Delete
self._init_write(filename)
[121] Fix | Delete
self.compress = zlib.compressobj(compresslevel,
[122] Fix | Delete
zlib.DEFLATED,
[123] Fix | Delete
-zlib.MAX_WBITS,
[124] Fix | Delete
zlib.DEF_MEM_LEVEL,
[125] Fix | Delete
0)
[126] Fix | Delete
else:
[127] Fix | Delete
raise IOError, "Mode " + mode + " not supported"
[128] Fix | Delete
[129] Fix | Delete
self.fileobj = fileobj
[130] Fix | Delete
self.offset = 0
[131] Fix | Delete
self.mtime = mtime
[132] Fix | Delete
[133] Fix | Delete
if self.mode == WRITE:
[134] Fix | Delete
self._write_gzip_header()
[135] Fix | Delete
[136] Fix | Delete
@property
[137] Fix | Delete
def filename(self):
[138] Fix | Delete
import warnings
[139] Fix | Delete
warnings.warn("use the name attribute", DeprecationWarning, 2)
[140] Fix | Delete
if self.mode == WRITE and self.name[-3:] != ".gz":
[141] Fix | Delete
return self.name + ".gz"
[142] Fix | Delete
return self.name
[143] Fix | Delete
[144] Fix | Delete
def __repr__(self):
[145] Fix | Delete
s = repr(self.fileobj)
[146] Fix | Delete
return '<gzip ' + s[1:-1] + ' ' + hex(id(self)) + '>'
[147] Fix | Delete
[148] Fix | Delete
def _check_closed(self):
[149] Fix | Delete
"""Raises a ValueError if the underlying file object has been closed.
[150] Fix | Delete
[151] Fix | Delete
"""
[152] Fix | Delete
if self.closed:
[153] Fix | Delete
raise ValueError('I/O operation on closed file.')
[154] Fix | Delete
[155] Fix | Delete
def _init_write(self, filename):
[156] Fix | Delete
self.name = filename
[157] Fix | Delete
self.crc = zlib.crc32("") & 0xffffffffL
[158] Fix | Delete
self.size = 0
[159] Fix | Delete
self.writebuf = []
[160] Fix | Delete
self.bufsize = 0
[161] Fix | Delete
[162] Fix | Delete
def _write_gzip_header(self):
[163] Fix | Delete
self.fileobj.write('\037\213') # magic header
[164] Fix | Delete
self.fileobj.write('\010') # compression method
[165] Fix | Delete
try:
[166] Fix | Delete
# RFC 1952 requires the FNAME field to be Latin-1. Do not
[167] Fix | Delete
# include filenames that cannot be represented that way.
[168] Fix | Delete
fname = os.path.basename(self.name)
[169] Fix | Delete
if not isinstance(fname, str):
[170] Fix | Delete
fname = fname.encode('latin-1')
[171] Fix | Delete
if fname.endswith('.gz'):
[172] Fix | Delete
fname = fname[:-3]
[173] Fix | Delete
except UnicodeEncodeError:
[174] Fix | Delete
fname = ''
[175] Fix | Delete
flags = 0
[176] Fix | Delete
if fname:
[177] Fix | Delete
flags = FNAME
[178] Fix | Delete
self.fileobj.write(chr(flags))
[179] Fix | Delete
mtime = self.mtime
[180] Fix | Delete
if mtime is None:
[181] Fix | Delete
mtime = time.time()
[182] Fix | Delete
write32u(self.fileobj, long(mtime))
[183] Fix | Delete
self.fileobj.write('\002')
[184] Fix | Delete
self.fileobj.write('\377')
[185] Fix | Delete
if fname:
[186] Fix | Delete
self.fileobj.write(fname + '\000')
[187] Fix | Delete
[188] Fix | Delete
def _init_read(self):
[189] Fix | Delete
self.crc = zlib.crc32("") & 0xffffffffL
[190] Fix | Delete
self.size = 0
[191] Fix | Delete
[192] Fix | Delete
def _read_gzip_header(self):
[193] Fix | Delete
magic = self.fileobj.read(2)
[194] Fix | Delete
if magic != '\037\213':
[195] Fix | Delete
raise IOError, 'Not a gzipped file'
[196] Fix | Delete
method = ord( self.fileobj.read(1) )
[197] Fix | Delete
if method != 8:
[198] Fix | Delete
raise IOError, 'Unknown compression method'
[199] Fix | Delete
flag = ord( self.fileobj.read(1) )
[200] Fix | Delete
self.mtime = read32(self.fileobj)
[201] Fix | Delete
# extraflag = self.fileobj.read(1)
[202] Fix | Delete
# os = self.fileobj.read(1)
[203] Fix | Delete
self.fileobj.read(2)
[204] Fix | Delete
[205] Fix | Delete
if flag & FEXTRA:
[206] Fix | Delete
# Read & discard the extra field, if present
[207] Fix | Delete
xlen = ord(self.fileobj.read(1))
[208] Fix | Delete
xlen = xlen + 256*ord(self.fileobj.read(1))
[209] Fix | Delete
self.fileobj.read(xlen)
[210] Fix | Delete
if flag & FNAME:
[211] Fix | Delete
# Read and discard a null-terminated string containing the filename
[212] Fix | Delete
while True:
[213] Fix | Delete
s = self.fileobj.read(1)
[214] Fix | Delete
if not s or s=='\000':
[215] Fix | Delete
break
[216] Fix | Delete
if flag & FCOMMENT:
[217] Fix | Delete
# Read and discard a null-terminated string containing a comment
[218] Fix | Delete
while True:
[219] Fix | Delete
s = self.fileobj.read(1)
[220] Fix | Delete
if not s or s=='\000':
[221] Fix | Delete
break
[222] Fix | Delete
if flag & FHCRC:
[223] Fix | Delete
self.fileobj.read(2) # Read & discard the 16-bit header CRC
[224] Fix | Delete
[225] Fix | Delete
def write(self,data):
[226] Fix | Delete
self._check_closed()
[227] Fix | Delete
if self.mode != WRITE:
[228] Fix | Delete
import errno
[229] Fix | Delete
raise IOError(errno.EBADF, "write() on read-only GzipFile object")
[230] Fix | Delete
[231] Fix | Delete
if self.fileobj is None:
[232] Fix | Delete
raise ValueError, "write() on closed GzipFile object"
[233] Fix | Delete
[234] Fix | Delete
# Convert data type if called by io.BufferedWriter.
[235] Fix | Delete
if isinstance(data, memoryview):
[236] Fix | Delete
data = data.tobytes()
[237] Fix | Delete
[238] Fix | Delete
if len(data) > 0:
[239] Fix | Delete
self.fileobj.write(self.compress.compress(data))
[240] Fix | Delete
self.size += len(data)
[241] Fix | Delete
self.crc = zlib.crc32(data, self.crc) & 0xffffffffL
[242] Fix | Delete
self.offset += len(data)
[243] Fix | Delete
[244] Fix | Delete
return len(data)
[245] Fix | Delete
[246] Fix | Delete
def read(self, size=-1):
[247] Fix | Delete
self._check_closed()
[248] Fix | Delete
if self.mode != READ:
[249] Fix | Delete
import errno
[250] Fix | Delete
raise IOError(errno.EBADF, "read() on write-only GzipFile object")
[251] Fix | Delete
[252] Fix | Delete
if self.extrasize <= 0 and self.fileobj is None:
[253] Fix | Delete
return ''
[254] Fix | Delete
[255] Fix | Delete
readsize = 1024
[256] Fix | Delete
if size < 0: # get the whole thing
[257] Fix | Delete
try:
[258] Fix | Delete
while True:
[259] Fix | Delete
self._read(readsize)
[260] Fix | Delete
readsize = min(self.max_read_chunk, readsize * 2)
[261] Fix | Delete
except EOFError:
[262] Fix | Delete
size = self.extrasize
[263] Fix | Delete
else: # just get some more of it
[264] Fix | Delete
try:
[265] Fix | Delete
while size > self.extrasize:
[266] Fix | Delete
self._read(readsize)
[267] Fix | Delete
readsize = min(self.max_read_chunk, readsize * 2)
[268] Fix | Delete
except EOFError:
[269] Fix | Delete
if size > self.extrasize:
[270] Fix | Delete
size = self.extrasize
[271] Fix | Delete
[272] Fix | Delete
offset = self.offset - self.extrastart
[273] Fix | Delete
chunk = self.extrabuf[offset: offset + size]
[274] Fix | Delete
self.extrasize = self.extrasize - size
[275] Fix | Delete
[276] Fix | Delete
self.offset += size
[277] Fix | Delete
return chunk
[278] Fix | Delete
[279] Fix | Delete
def _unread(self, buf):
[280] Fix | Delete
self.extrasize = len(buf) + self.extrasize
[281] Fix | Delete
self.offset -= len(buf)
[282] Fix | Delete
[283] Fix | Delete
def _read(self, size=1024):
[284] Fix | Delete
if self.fileobj is None:
[285] Fix | Delete
raise EOFError, "Reached EOF"
[286] Fix | Delete
[287] Fix | Delete
if self._new_member:
[288] Fix | Delete
# If the _new_member flag is set, we have to
[289] Fix | Delete
# jump to the next member, if there is one.
[290] Fix | Delete
#
[291] Fix | Delete
# First, check if we're at the end of the file;
[292] Fix | Delete
# if so, it's time to stop; no more members to read.
[293] Fix | Delete
pos = self.fileobj.tell() # Save current position
[294] Fix | Delete
self.fileobj.seek(0, 2) # Seek to end of file
[295] Fix | Delete
if pos == self.fileobj.tell():
[296] Fix | Delete
raise EOFError, "Reached EOF"
[297] Fix | Delete
else:
[298] Fix | Delete
self.fileobj.seek( pos ) # Return to original position
[299] Fix | Delete
[300] Fix | Delete
self._init_read()
[301] Fix | Delete
self._read_gzip_header()
[302] Fix | Delete
self.decompress = zlib.decompressobj(-zlib.MAX_WBITS)
[303] Fix | Delete
self._new_member = False
[304] Fix | Delete
[305] Fix | Delete
# Read a chunk of data from the file
[306] Fix | Delete
buf = self.fileobj.read(size)
[307] Fix | Delete
[308] Fix | Delete
# If the EOF has been reached, flush the decompression object
[309] Fix | Delete
# and mark this object as finished.
[310] Fix | Delete
[311] Fix | Delete
if buf == "":
[312] Fix | Delete
uncompress = self.decompress.flush()
[313] Fix | Delete
self._read_eof()
[314] Fix | Delete
self._add_read_data( uncompress )
[315] Fix | Delete
raise EOFError, 'Reached EOF'
[316] Fix | Delete
[317] Fix | Delete
uncompress = self.decompress.decompress(buf)
[318] Fix | Delete
self._add_read_data( uncompress )
[319] Fix | Delete
[320] Fix | Delete
if self.decompress.unused_data != "":
[321] Fix | Delete
# Ending case: we've come to the end of a member in the file,
[322] Fix | Delete
# so seek back to the start of the unused data, finish up
[323] Fix | Delete
# this member, and read a new gzip header.
[324] Fix | Delete
# (The number of bytes to seek back is the length of the unused
[325] Fix | Delete
# data, minus 8 because _read_eof() will rewind a further 8 bytes)
[326] Fix | Delete
self.fileobj.seek( -len(self.decompress.unused_data)+8, 1)
[327] Fix | Delete
[328] Fix | Delete
# Check the CRC and file size, and set the flag so we read
[329] Fix | Delete
# a new member on the next call
[330] Fix | Delete
self._read_eof()
[331] Fix | Delete
self._new_member = True
[332] Fix | Delete
[333] Fix | Delete
def _add_read_data(self, data):
[334] Fix | Delete
self.crc = zlib.crc32(data, self.crc) & 0xffffffffL
[335] Fix | Delete
offset = self.offset - self.extrastart
[336] Fix | Delete
self.extrabuf = self.extrabuf[offset:] + data
[337] Fix | Delete
self.extrasize = self.extrasize + len(data)
[338] Fix | Delete
self.extrastart = self.offset
[339] Fix | Delete
self.size = self.size + len(data)
[340] Fix | Delete
[341] Fix | Delete
def _read_eof(self):
[342] Fix | Delete
# We've read to the end of the file, so we have to rewind in order
[343] Fix | Delete
# to reread the 8 bytes containing the CRC and the file size.
[344] Fix | Delete
# We check the that the computed CRC and size of the
[345] Fix | Delete
# uncompressed data matches the stored values. Note that the size
[346] Fix | Delete
# stored is the true file size mod 2**32.
[347] Fix | Delete
self.fileobj.seek(-8, 1)
[348] Fix | Delete
crc32 = read32(self.fileobj)
[349] Fix | Delete
isize = read32(self.fileobj) # may exceed 2GB
[350] Fix | Delete
if crc32 != self.crc:
[351] Fix | Delete
raise IOError("CRC check failed %s != %s" % (hex(crc32),
[352] Fix | Delete
hex(self.crc)))
[353] Fix | Delete
elif isize != (self.size & 0xffffffffL):
[354] Fix | Delete
raise IOError, "Incorrect length of data produced"
[355] Fix | Delete
[356] Fix | Delete
# Gzip files can be padded with zeroes and still have archives.
[357] Fix | Delete
# Consume all zero bytes and set the file position to the first
[358] Fix | Delete
# non-zero byte. See http://www.gzip.org/#faq8
[359] Fix | Delete
c = "\x00"
[360] Fix | Delete
while c == "\x00":
[361] Fix | Delete
c = self.fileobj.read(1)
[362] Fix | Delete
if c:
[363] Fix | Delete
self.fileobj.seek(-1, 1)
[364] Fix | Delete
[365] Fix | Delete
@property
[366] Fix | Delete
def closed(self):
[367] Fix | Delete
return self.fileobj is None
[368] Fix | Delete
[369] Fix | Delete
def close(self):
[370] Fix | Delete
fileobj = self.fileobj
[371] Fix | Delete
if fileobj is None:
[372] Fix | Delete
return
[373] Fix | Delete
self.fileobj = None
[374] Fix | Delete
try:
[375] Fix | Delete
if self.mode == WRITE:
[376] Fix | Delete
fileobj.write(self.compress.flush())
[377] Fix | Delete
write32u(fileobj, self.crc)
[378] Fix | Delete
# self.size may exceed 2GB, or even 4GB
[379] Fix | Delete
write32u(fileobj, self.size & 0xffffffffL)
[380] Fix | Delete
finally:
[381] Fix | Delete
myfileobj = self.myfileobj
[382] Fix | Delete
if myfileobj:
[383] Fix | Delete
self.myfileobj = None
[384] Fix | Delete
myfileobj.close()
[385] Fix | Delete
[386] Fix | Delete
def flush(self,zlib_mode=zlib.Z_SYNC_FLUSH):
[387] Fix | Delete
self._check_closed()
[388] Fix | Delete
if self.mode == WRITE:
[389] Fix | Delete
# Ensure the compressor's buffer is flushed
[390] Fix | Delete
self.fileobj.write(self.compress.flush(zlib_mode))
[391] Fix | Delete
self.fileobj.flush()
[392] Fix | Delete
[393] Fix | Delete
def fileno(self):
[394] Fix | Delete
"""Invoke the underlying file object's fileno() method.
[395] Fix | Delete
[396] Fix | Delete
This will raise AttributeError if the underlying file object
[397] Fix | Delete
doesn't support fileno().
[398] Fix | Delete
"""
[399] Fix | Delete
return self.fileobj.fileno()
[400] Fix | Delete
[401] Fix | Delete
def rewind(self):
[402] Fix | Delete
'''Return the uncompressed stream file position indicator to the
[403] Fix | Delete
beginning of the file'''
[404] Fix | Delete
if self.mode != READ:
[405] Fix | Delete
raise IOError("Can't rewind in write mode")
[406] Fix | Delete
self.fileobj.seek(0)
[407] Fix | Delete
self._new_member = True
[408] Fix | Delete
self.extrabuf = ""
[409] Fix | Delete
self.extrasize = 0
[410] Fix | Delete
self.extrastart = 0
[411] Fix | Delete
self.offset = 0
[412] Fix | Delete
[413] Fix | Delete
def readable(self):
[414] Fix | Delete
return self.mode == READ
[415] Fix | Delete
[416] Fix | Delete
def writable(self):
[417] Fix | Delete
return self.mode == WRITE
[418] Fix | Delete
[419] Fix | Delete
def seekable(self):
[420] Fix | Delete
return True
[421] Fix | Delete
[422] Fix | Delete
def seek(self, offset, whence=0):
[423] Fix | Delete
if whence:
[424] Fix | Delete
if whence == 1:
[425] Fix | Delete
offset = self.offset + offset
[426] Fix | Delete
else:
[427] Fix | Delete
raise ValueError('Seek from end not supported')
[428] Fix | Delete
if self.mode == WRITE:
[429] Fix | Delete
if offset < self.offset:
[430] Fix | Delete
raise IOError('Negative seek in write mode')
[431] Fix | Delete
count = offset - self.offset
[432] Fix | Delete
for i in xrange(count // 1024):
[433] Fix | Delete
self.write(1024 * '\0')
[434] Fix | Delete
self.write((count % 1024) * '\0')
[435] Fix | Delete
elif self.mode == READ:
[436] Fix | Delete
if offset < self.offset:
[437] Fix | Delete
# for negative seek, rewind and do positive seek
[438] Fix | Delete
self.rewind()
[439] Fix | Delete
count = offset - self.offset
[440] Fix | Delete
for i in xrange(count // 1024):
[441] Fix | Delete
self.read(1024)
[442] Fix | Delete
self.read(count % 1024)
[443] Fix | Delete
[444] Fix | Delete
return self.offset
[445] Fix | Delete
[446] Fix | Delete
def readline(self, size=-1):
[447] Fix | Delete
if size < 0:
[448] Fix | Delete
# Shortcut common case - newline found in buffer.
[449] Fix | Delete
offset = self.offset - self.extrastart
[450] Fix | Delete
i = self.extrabuf.find('\n', offset) + 1
[451] Fix | Delete
if i > 0:
[452] Fix | Delete
self.extrasize -= i - offset
[453] Fix | Delete
self.offset += i - offset
[454] Fix | Delete
return self.extrabuf[offset: i]
[455] Fix | Delete
[456] Fix | Delete
size = sys.maxint
[457] Fix | Delete
readsize = self.min_readsize
[458] Fix | Delete
else:
[459] Fix | Delete
readsize = size
[460] Fix | Delete
bufs = []
[461] Fix | Delete
while size != 0:
[462] Fix | Delete
c = self.read(readsize)
[463] Fix | Delete
i = c.find('\n')
[464] Fix | Delete
[465] Fix | Delete
# We set i=size to break out of the loop under two
[466] Fix | Delete
# conditions: 1) there's no newline, and the chunk is
[467] Fix | Delete
# larger than size, or 2) there is a newline, but the
[468] Fix | Delete
# resulting line would be longer than 'size'.
[469] Fix | Delete
if (size <= i) or (i == -1 and len(c) > size):
[470] Fix | Delete
i = size - 1
[471] Fix | Delete
[472] Fix | Delete
if i >= 0 or c == '':
[473] Fix | Delete
bufs.append(c[:i + 1]) # Add portion of last chunk
[474] Fix | Delete
self._unread(c[i + 1:]) # Push back rest of chunk
[475] Fix | Delete
break
[476] Fix | Delete
[477] Fix | Delete
# Append chunk to list, decrease 'size',
[478] Fix | Delete
bufs.append(c)
[479] Fix | Delete
size = size - len(c)
[480] Fix | Delete
readsize = min(size, readsize * 2)
[481] Fix | Delete
if readsize > self.min_readsize:
[482] Fix | Delete
self.min_readsize = min(readsize, self.min_readsize * 2, 512)
[483] Fix | Delete
return ''.join(bufs) # Return resulting line
[484] Fix | Delete
[485] Fix | Delete
[486] Fix | Delete
def _test():
[487] Fix | Delete
# Act like gzip; with -d, act like gunzip.
[488] Fix | Delete
# The input file is not deleted, however, nor are any other gzip
[489] Fix | Delete
# options or features supported.
[490] Fix | Delete
args = sys.argv[1:]
[491] Fix | Delete
decompress = args and args[0] == "-d"
[492] Fix | Delete
if decompress:
[493] Fix | Delete
args = args[1:]
[494] Fix | Delete
if not args:
[495] Fix | Delete
args = ["-"]
[496] Fix | Delete
for arg in args:
[497] Fix | Delete
if decompress:
[498] Fix | Delete
if arg == "-":
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function