Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/lib64/python3....
File: codecs.py
""" codecs -- Python Codec Registry, API and helpers.
[0] Fix | Delete
[1] Fix | Delete
[2] Fix | Delete
Written by Marc-Andre Lemburg (mal@lemburg.com).
[3] Fix | Delete
[4] Fix | Delete
(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
[5] Fix | Delete
[6] Fix | Delete
"""#"
[7] Fix | Delete
[8] Fix | Delete
import builtins, sys
[9] Fix | Delete
[10] Fix | Delete
### Registry and builtin stateless codec functions
[11] Fix | Delete
[12] Fix | Delete
try:
[13] Fix | Delete
from _codecs import *
[14] Fix | Delete
except ImportError as why:
[15] Fix | Delete
raise SystemError('Failed to load the builtin codecs: %s' % why)
[16] Fix | Delete
[17] Fix | Delete
__all__ = ["register", "lookup", "open", "EncodedFile", "BOM", "BOM_BE",
[18] Fix | Delete
"BOM_LE", "BOM32_BE", "BOM32_LE", "BOM64_BE", "BOM64_LE",
[19] Fix | Delete
"BOM_UTF8", "BOM_UTF16", "BOM_UTF16_LE", "BOM_UTF16_BE",
[20] Fix | Delete
"BOM_UTF32", "BOM_UTF32_LE", "BOM_UTF32_BE",
[21] Fix | Delete
"CodecInfo", "Codec", "IncrementalEncoder", "IncrementalDecoder",
[22] Fix | Delete
"StreamReader", "StreamWriter",
[23] Fix | Delete
"StreamReaderWriter", "StreamRecoder",
[24] Fix | Delete
"getencoder", "getdecoder", "getincrementalencoder",
[25] Fix | Delete
"getincrementaldecoder", "getreader", "getwriter",
[26] Fix | Delete
"encode", "decode", "iterencode", "iterdecode",
[27] Fix | Delete
"strict_errors", "ignore_errors", "replace_errors",
[28] Fix | Delete
"xmlcharrefreplace_errors",
[29] Fix | Delete
"backslashreplace_errors", "namereplace_errors",
[30] Fix | Delete
"register_error", "lookup_error"]
[31] Fix | Delete
[32] Fix | Delete
### Constants
[33] Fix | Delete
[34] Fix | Delete
#
[35] Fix | Delete
# Byte Order Mark (BOM = ZERO WIDTH NO-BREAK SPACE = U+FEFF)
[36] Fix | Delete
# and its possible byte string values
[37] Fix | Delete
# for UTF8/UTF16/UTF32 output and little/big endian machines
[38] Fix | Delete
#
[39] Fix | Delete
[40] Fix | Delete
# UTF-8
[41] Fix | Delete
BOM_UTF8 = b'\xef\xbb\xbf'
[42] Fix | Delete
[43] Fix | Delete
# UTF-16, little endian
[44] Fix | Delete
BOM_LE = BOM_UTF16_LE = b'\xff\xfe'
[45] Fix | Delete
[46] Fix | Delete
# UTF-16, big endian
[47] Fix | Delete
BOM_BE = BOM_UTF16_BE = b'\xfe\xff'
[48] Fix | Delete
[49] Fix | Delete
# UTF-32, little endian
[50] Fix | Delete
BOM_UTF32_LE = b'\xff\xfe\x00\x00'
[51] Fix | Delete
[52] Fix | Delete
# UTF-32, big endian
[53] Fix | Delete
BOM_UTF32_BE = b'\x00\x00\xfe\xff'
[54] Fix | Delete
[55] Fix | Delete
if sys.byteorder == 'little':
[56] Fix | Delete
[57] Fix | Delete
# UTF-16, native endianness
[58] Fix | Delete
BOM = BOM_UTF16 = BOM_UTF16_LE
[59] Fix | Delete
[60] Fix | Delete
# UTF-32, native endianness
[61] Fix | Delete
BOM_UTF32 = BOM_UTF32_LE
[62] Fix | Delete
[63] Fix | Delete
else:
[64] Fix | Delete
[65] Fix | Delete
# UTF-16, native endianness
[66] Fix | Delete
BOM = BOM_UTF16 = BOM_UTF16_BE
[67] Fix | Delete
[68] Fix | Delete
# UTF-32, native endianness
[69] Fix | Delete
BOM_UTF32 = BOM_UTF32_BE
[70] Fix | Delete
[71] Fix | Delete
# Old broken names (don't use in new code)
[72] Fix | Delete
BOM32_LE = BOM_UTF16_LE
[73] Fix | Delete
BOM32_BE = BOM_UTF16_BE
[74] Fix | Delete
BOM64_LE = BOM_UTF32_LE
[75] Fix | Delete
BOM64_BE = BOM_UTF32_BE
[76] Fix | Delete
[77] Fix | Delete
[78] Fix | Delete
### Codec base classes (defining the API)
[79] Fix | Delete
[80] Fix | Delete
class CodecInfo(tuple):
[81] Fix | Delete
"""Codec details when looking up the codec registry"""
[82] Fix | Delete
[83] Fix | Delete
# Private API to allow Python 3.4 to blacklist the known non-Unicode
[84] Fix | Delete
# codecs in the standard library. A more general mechanism to
[85] Fix | Delete
# reliably distinguish test encodings from other codecs will hopefully
[86] Fix | Delete
# be defined for Python 3.5
[87] Fix | Delete
#
[88] Fix | Delete
# See http://bugs.python.org/issue19619
[89] Fix | Delete
_is_text_encoding = True # Assume codecs are text encodings by default
[90] Fix | Delete
[91] Fix | Delete
def __new__(cls, encode, decode, streamreader=None, streamwriter=None,
[92] Fix | Delete
incrementalencoder=None, incrementaldecoder=None, name=None,
[93] Fix | Delete
*, _is_text_encoding=None):
[94] Fix | Delete
self = tuple.__new__(cls, (encode, decode, streamreader, streamwriter))
[95] Fix | Delete
self.name = name
[96] Fix | Delete
self.encode = encode
[97] Fix | Delete
self.decode = decode
[98] Fix | Delete
self.incrementalencoder = incrementalencoder
[99] Fix | Delete
self.incrementaldecoder = incrementaldecoder
[100] Fix | Delete
self.streamwriter = streamwriter
[101] Fix | Delete
self.streamreader = streamreader
[102] Fix | Delete
if _is_text_encoding is not None:
[103] Fix | Delete
self._is_text_encoding = _is_text_encoding
[104] Fix | Delete
return self
[105] Fix | Delete
[106] Fix | Delete
def __repr__(self):
[107] Fix | Delete
return "<%s.%s object for encoding %s at %#x>" % \
[108] Fix | Delete
(self.__class__.__module__, self.__class__.__qualname__,
[109] Fix | Delete
self.name, id(self))
[110] Fix | Delete
[111] Fix | Delete
class Codec:
[112] Fix | Delete
[113] Fix | Delete
""" Defines the interface for stateless encoders/decoders.
[114] Fix | Delete
[115] Fix | Delete
The .encode()/.decode() methods may use different error
[116] Fix | Delete
handling schemes by providing the errors argument. These
[117] Fix | Delete
string values are predefined:
[118] Fix | Delete
[119] Fix | Delete
'strict' - raise a ValueError error (or a subclass)
[120] Fix | Delete
'ignore' - ignore the character and continue with the next
[121] Fix | Delete
'replace' - replace with a suitable replacement character;
[122] Fix | Delete
Python will use the official U+FFFD REPLACEMENT
[123] Fix | Delete
CHARACTER for the builtin Unicode codecs on
[124] Fix | Delete
decoding and '?' on encoding.
[125] Fix | Delete
'surrogateescape' - replace with private code points U+DCnn.
[126] Fix | Delete
'xmlcharrefreplace' - Replace with the appropriate XML
[127] Fix | Delete
character reference (only for encoding).
[128] Fix | Delete
'backslashreplace' - Replace with backslashed escape sequences.
[129] Fix | Delete
'namereplace' - Replace with \\N{...} escape sequences
[130] Fix | Delete
(only for encoding).
[131] Fix | Delete
[132] Fix | Delete
The set of allowed values can be extended via register_error.
[133] Fix | Delete
[134] Fix | Delete
"""
[135] Fix | Delete
def encode(self, input, errors='strict'):
[136] Fix | Delete
[137] Fix | Delete
""" Encodes the object input and returns a tuple (output
[138] Fix | Delete
object, length consumed).
[139] Fix | Delete
[140] Fix | Delete
errors defines the error handling to apply. It defaults to
[141] Fix | Delete
'strict' handling.
[142] Fix | Delete
[143] Fix | Delete
The method may not store state in the Codec instance. Use
[144] Fix | Delete
StreamWriter for codecs which have to keep state in order to
[145] Fix | Delete
make encoding efficient.
[146] Fix | Delete
[147] Fix | Delete
The encoder must be able to handle zero length input and
[148] Fix | Delete
return an empty object of the output object type in this
[149] Fix | Delete
situation.
[150] Fix | Delete
[151] Fix | Delete
"""
[152] Fix | Delete
raise NotImplementedError
[153] Fix | Delete
[154] Fix | Delete
def decode(self, input, errors='strict'):
[155] Fix | Delete
[156] Fix | Delete
""" Decodes the object input and returns a tuple (output
[157] Fix | Delete
object, length consumed).
[158] Fix | Delete
[159] Fix | Delete
input must be an object which provides the bf_getreadbuf
[160] Fix | Delete
buffer slot. Python strings, buffer objects and memory
[161] Fix | Delete
mapped files are examples of objects providing this slot.
[162] Fix | Delete
[163] Fix | Delete
errors defines the error handling to apply. It defaults to
[164] Fix | Delete
'strict' handling.
[165] Fix | Delete
[166] Fix | Delete
The method may not store state in the Codec instance. Use
[167] Fix | Delete
StreamReader for codecs which have to keep state in order to
[168] Fix | Delete
make decoding efficient.
[169] Fix | Delete
[170] Fix | Delete
The decoder must be able to handle zero length input and
[171] Fix | Delete
return an empty object of the output object type in this
[172] Fix | Delete
situation.
[173] Fix | Delete
[174] Fix | Delete
"""
[175] Fix | Delete
raise NotImplementedError
[176] Fix | Delete
[177] Fix | Delete
class IncrementalEncoder(object):
[178] Fix | Delete
"""
[179] Fix | Delete
An IncrementalEncoder encodes an input in multiple steps. The input can
[180] Fix | Delete
be passed piece by piece to the encode() method. The IncrementalEncoder
[181] Fix | Delete
remembers the state of the encoding process between calls to encode().
[182] Fix | Delete
"""
[183] Fix | Delete
def __init__(self, errors='strict'):
[184] Fix | Delete
"""
[185] Fix | Delete
Creates an IncrementalEncoder instance.
[186] Fix | Delete
[187] Fix | Delete
The IncrementalEncoder may use different error handling schemes by
[188] Fix | Delete
providing the errors keyword argument. See the module docstring
[189] Fix | Delete
for a list of possible values.
[190] Fix | Delete
"""
[191] Fix | Delete
self.errors = errors
[192] Fix | Delete
self.buffer = ""
[193] Fix | Delete
[194] Fix | Delete
def encode(self, input, final=False):
[195] Fix | Delete
"""
[196] Fix | Delete
Encodes input and returns the resulting object.
[197] Fix | Delete
"""
[198] Fix | Delete
raise NotImplementedError
[199] Fix | Delete
[200] Fix | Delete
def reset(self):
[201] Fix | Delete
"""
[202] Fix | Delete
Resets the encoder to the initial state.
[203] Fix | Delete
"""
[204] Fix | Delete
[205] Fix | Delete
def getstate(self):
[206] Fix | Delete
"""
[207] Fix | Delete
Return the current state of the encoder.
[208] Fix | Delete
"""
[209] Fix | Delete
return 0
[210] Fix | Delete
[211] Fix | Delete
def setstate(self, state):
[212] Fix | Delete
"""
[213] Fix | Delete
Set the current state of the encoder. state must have been
[214] Fix | Delete
returned by getstate().
[215] Fix | Delete
"""
[216] Fix | Delete
[217] Fix | Delete
class BufferedIncrementalEncoder(IncrementalEncoder):
[218] Fix | Delete
"""
[219] Fix | Delete
This subclass of IncrementalEncoder can be used as the baseclass for an
[220] Fix | Delete
incremental encoder if the encoder must keep some of the output in a
[221] Fix | Delete
buffer between calls to encode().
[222] Fix | Delete
"""
[223] Fix | Delete
def __init__(self, errors='strict'):
[224] Fix | Delete
IncrementalEncoder.__init__(self, errors)
[225] Fix | Delete
# unencoded input that is kept between calls to encode()
[226] Fix | Delete
self.buffer = ""
[227] Fix | Delete
[228] Fix | Delete
def _buffer_encode(self, input, errors, final):
[229] Fix | Delete
# Overwrite this method in subclasses: It must encode input
[230] Fix | Delete
# and return an (output, length consumed) tuple
[231] Fix | Delete
raise NotImplementedError
[232] Fix | Delete
[233] Fix | Delete
def encode(self, input, final=False):
[234] Fix | Delete
# encode input (taking the buffer into account)
[235] Fix | Delete
data = self.buffer + input
[236] Fix | Delete
(result, consumed) = self._buffer_encode(data, self.errors, final)
[237] Fix | Delete
# keep unencoded input until the next call
[238] Fix | Delete
self.buffer = data[consumed:]
[239] Fix | Delete
return result
[240] Fix | Delete
[241] Fix | Delete
def reset(self):
[242] Fix | Delete
IncrementalEncoder.reset(self)
[243] Fix | Delete
self.buffer = ""
[244] Fix | Delete
[245] Fix | Delete
def getstate(self):
[246] Fix | Delete
return self.buffer or 0
[247] Fix | Delete
[248] Fix | Delete
def setstate(self, state):
[249] Fix | Delete
self.buffer = state or ""
[250] Fix | Delete
[251] Fix | Delete
class IncrementalDecoder(object):
[252] Fix | Delete
"""
[253] Fix | Delete
An IncrementalDecoder decodes an input in multiple steps. The input can
[254] Fix | Delete
be passed piece by piece to the decode() method. The IncrementalDecoder
[255] Fix | Delete
remembers the state of the decoding process between calls to decode().
[256] Fix | Delete
"""
[257] Fix | Delete
def __init__(self, errors='strict'):
[258] Fix | Delete
"""
[259] Fix | Delete
Create an IncrementalDecoder instance.
[260] Fix | Delete
[261] Fix | Delete
The IncrementalDecoder may use different error handling schemes by
[262] Fix | Delete
providing the errors keyword argument. See the module docstring
[263] Fix | Delete
for a list of possible values.
[264] Fix | Delete
"""
[265] Fix | Delete
self.errors = errors
[266] Fix | Delete
[267] Fix | Delete
def decode(self, input, final=False):
[268] Fix | Delete
"""
[269] Fix | Delete
Decode input and returns the resulting object.
[270] Fix | Delete
"""
[271] Fix | Delete
raise NotImplementedError
[272] Fix | Delete
[273] Fix | Delete
def reset(self):
[274] Fix | Delete
"""
[275] Fix | Delete
Reset the decoder to the initial state.
[276] Fix | Delete
"""
[277] Fix | Delete
[278] Fix | Delete
def getstate(self):
[279] Fix | Delete
"""
[280] Fix | Delete
Return the current state of the decoder.
[281] Fix | Delete
[282] Fix | Delete
This must be a (buffered_input, additional_state_info) tuple.
[283] Fix | Delete
buffered_input must be a bytes object containing bytes that
[284] Fix | Delete
were passed to decode() that have not yet been converted.
[285] Fix | Delete
additional_state_info must be a non-negative integer
[286] Fix | Delete
representing the state of the decoder WITHOUT yet having
[287] Fix | Delete
processed the contents of buffered_input. In the initial state
[288] Fix | Delete
and after reset(), getstate() must return (b"", 0).
[289] Fix | Delete
"""
[290] Fix | Delete
return (b"", 0)
[291] Fix | Delete
[292] Fix | Delete
def setstate(self, state):
[293] Fix | Delete
"""
[294] Fix | Delete
Set the current state of the decoder.
[295] Fix | Delete
[296] Fix | Delete
state must have been returned by getstate(). The effect of
[297] Fix | Delete
setstate((b"", 0)) must be equivalent to reset().
[298] Fix | Delete
"""
[299] Fix | Delete
[300] Fix | Delete
class BufferedIncrementalDecoder(IncrementalDecoder):
[301] Fix | Delete
"""
[302] Fix | Delete
This subclass of IncrementalDecoder can be used as the baseclass for an
[303] Fix | Delete
incremental decoder if the decoder must be able to handle incomplete
[304] Fix | Delete
byte sequences.
[305] Fix | Delete
"""
[306] Fix | Delete
def __init__(self, errors='strict'):
[307] Fix | Delete
IncrementalDecoder.__init__(self, errors)
[308] Fix | Delete
# undecoded input that is kept between calls to decode()
[309] Fix | Delete
self.buffer = b""
[310] Fix | Delete
[311] Fix | Delete
def _buffer_decode(self, input, errors, final):
[312] Fix | Delete
# Overwrite this method in subclasses: It must decode input
[313] Fix | Delete
# and return an (output, length consumed) tuple
[314] Fix | Delete
raise NotImplementedError
[315] Fix | Delete
[316] Fix | Delete
def decode(self, input, final=False):
[317] Fix | Delete
# decode input (taking the buffer into account)
[318] Fix | Delete
data = self.buffer + input
[319] Fix | Delete
(result, consumed) = self._buffer_decode(data, self.errors, final)
[320] Fix | Delete
# keep undecoded input until the next call
[321] Fix | Delete
self.buffer = data[consumed:]
[322] Fix | Delete
return result
[323] Fix | Delete
[324] Fix | Delete
def reset(self):
[325] Fix | Delete
IncrementalDecoder.reset(self)
[326] Fix | Delete
self.buffer = b""
[327] Fix | Delete
[328] Fix | Delete
def getstate(self):
[329] Fix | Delete
# additional state info is always 0
[330] Fix | Delete
return (self.buffer, 0)
[331] Fix | Delete
[332] Fix | Delete
def setstate(self, state):
[333] Fix | Delete
# ignore additional state info
[334] Fix | Delete
self.buffer = state[0]
[335] Fix | Delete
[336] Fix | Delete
#
[337] Fix | Delete
# The StreamWriter and StreamReader class provide generic working
[338] Fix | Delete
# interfaces which can be used to implement new encoding submodules
[339] Fix | Delete
# very easily. See encodings/utf_8.py for an example on how this is
[340] Fix | Delete
# done.
[341] Fix | Delete
#
[342] Fix | Delete
[343] Fix | Delete
class StreamWriter(Codec):
[344] Fix | Delete
[345] Fix | Delete
def __init__(self, stream, errors='strict'):
[346] Fix | Delete
[347] Fix | Delete
""" Creates a StreamWriter instance.
[348] Fix | Delete
[349] Fix | Delete
stream must be a file-like object open for writing.
[350] Fix | Delete
[351] Fix | Delete
The StreamWriter may use different error handling
[352] Fix | Delete
schemes by providing the errors keyword argument. These
[353] Fix | Delete
parameters are predefined:
[354] Fix | Delete
[355] Fix | Delete
'strict' - raise a ValueError (or a subclass)
[356] Fix | Delete
'ignore' - ignore the character and continue with the next
[357] Fix | Delete
'replace'- replace with a suitable replacement character
[358] Fix | Delete
'xmlcharrefreplace' - Replace with the appropriate XML
[359] Fix | Delete
character reference.
[360] Fix | Delete
'backslashreplace' - Replace with backslashed escape
[361] Fix | Delete
sequences.
[362] Fix | Delete
'namereplace' - Replace with \\N{...} escape sequences.
[363] Fix | Delete
[364] Fix | Delete
The set of allowed parameter values can be extended via
[365] Fix | Delete
register_error.
[366] Fix | Delete
"""
[367] Fix | Delete
self.stream = stream
[368] Fix | Delete
self.errors = errors
[369] Fix | Delete
[370] Fix | Delete
def write(self, object):
[371] Fix | Delete
[372] Fix | Delete
""" Writes the object's contents encoded to self.stream.
[373] Fix | Delete
"""
[374] Fix | Delete
data, consumed = self.encode(object, self.errors)
[375] Fix | Delete
self.stream.write(data)
[376] Fix | Delete
[377] Fix | Delete
def writelines(self, list):
[378] Fix | Delete
[379] Fix | Delete
""" Writes the concatenated list of strings to the stream
[380] Fix | Delete
using .write().
[381] Fix | Delete
"""
[382] Fix | Delete
self.write(''.join(list))
[383] Fix | Delete
[384] Fix | Delete
def reset(self):
[385] Fix | Delete
[386] Fix | Delete
""" Flushes and resets the codec buffers used for keeping state.
[387] Fix | Delete
[388] Fix | Delete
Calling this method should ensure that the data on the
[389] Fix | Delete
output is put into a clean state, that allows appending
[390] Fix | Delete
of new fresh data without having to rescan the whole
[391] Fix | Delete
stream to recover state.
[392] Fix | Delete
[393] Fix | Delete
"""
[394] Fix | Delete
pass
[395] Fix | Delete
[396] Fix | Delete
def seek(self, offset, whence=0):
[397] Fix | Delete
self.stream.seek(offset, whence)
[398] Fix | Delete
if whence == 0 and offset == 0:
[399] Fix | Delete
self.reset()
[400] Fix | Delete
[401] Fix | Delete
def __getattr__(self, name,
[402] Fix | Delete
getattr=getattr):
[403] Fix | Delete
[404] Fix | Delete
""" Inherit all other methods from the underlying stream.
[405] Fix | Delete
"""
[406] Fix | Delete
return getattr(self.stream, name)
[407] Fix | Delete
[408] Fix | Delete
def __enter__(self):
[409] Fix | Delete
return self
[410] Fix | Delete
[411] Fix | Delete
def __exit__(self, type, value, tb):
[412] Fix | Delete
self.stream.close()
[413] Fix | Delete
[414] Fix | Delete
###
[415] Fix | Delete
[416] Fix | Delete
class StreamReader(Codec):
[417] Fix | Delete
[418] Fix | Delete
charbuffertype = str
[419] Fix | Delete
[420] Fix | Delete
def __init__(self, stream, errors='strict'):
[421] Fix | Delete
[422] Fix | Delete
""" Creates a StreamReader instance.
[423] Fix | Delete
[424] Fix | Delete
stream must be a file-like object open for reading.
[425] Fix | Delete
[426] Fix | Delete
The StreamReader may use different error handling
[427] Fix | Delete
schemes by providing the errors keyword argument. These
[428] Fix | Delete
parameters are predefined:
[429] Fix | Delete
[430] Fix | Delete
'strict' - raise a ValueError (or a subclass)
[431] Fix | Delete
'ignore' - ignore the character and continue with the next
[432] Fix | Delete
'replace'- replace with a suitable replacement character
[433] Fix | Delete
'backslashreplace' - Replace with backslashed escape sequences;
[434] Fix | Delete
[435] Fix | Delete
The set of allowed parameter values can be extended via
[436] Fix | Delete
register_error.
[437] Fix | Delete
"""
[438] Fix | Delete
self.stream = stream
[439] Fix | Delete
self.errors = errors
[440] Fix | Delete
self.bytebuffer = b""
[441] Fix | Delete
self._empty_charbuffer = self.charbuffertype()
[442] Fix | Delete
self.charbuffer = self._empty_charbuffer
[443] Fix | Delete
self.linebuffer = None
[444] Fix | Delete
[445] Fix | Delete
def decode(self, input, errors='strict'):
[446] Fix | Delete
raise NotImplementedError
[447] Fix | Delete
[448] Fix | Delete
def read(self, size=-1, chars=-1, firstline=False):
[449] Fix | Delete
[450] Fix | Delete
""" Decodes data from the stream self.stream and returns the
[451] Fix | Delete
resulting object.
[452] Fix | Delete
[453] Fix | Delete
chars indicates the number of decoded code points or bytes to
[454] Fix | Delete
return. read() will never return more data than requested,
[455] Fix | Delete
but it might return less, if there is not enough available.
[456] Fix | Delete
[457] Fix | Delete
size indicates the approximate maximum number of decoded
[458] Fix | Delete
bytes or code points to read for decoding. The decoder
[459] Fix | Delete
can modify this setting as appropriate. The default value
[460] Fix | Delete
-1 indicates to read and decode as much as possible. size
[461] Fix | Delete
is intended to prevent having to decode huge files in one
[462] Fix | Delete
step.
[463] Fix | Delete
[464] Fix | Delete
If firstline is true, and a UnicodeDecodeError happens
[465] Fix | Delete
after the first line terminator in the input only the first line
[466] Fix | Delete
will be returned, the rest of the input will be kept until the
[467] Fix | Delete
next call to read().
[468] Fix | Delete
[469] Fix | Delete
The method should use a greedy read strategy, meaning that
[470] Fix | Delete
it should read as much data as is allowed within the
[471] Fix | Delete
definition of the encoding and the given size, e.g. if
[472] Fix | Delete
optional encoding endings or state markers are available
[473] Fix | Delete
on the stream, these should be read too.
[474] Fix | Delete
"""
[475] Fix | Delete
# If we have lines cached, first merge them back into characters
[476] Fix | Delete
if self.linebuffer:
[477] Fix | Delete
self.charbuffer = self._empty_charbuffer.join(self.linebuffer)
[478] Fix | Delete
self.linebuffer = None
[479] Fix | Delete
[480] Fix | Delete
if chars < 0:
[481] Fix | Delete
# For compatibility with other read() methods that take a
[482] Fix | Delete
# single argument
[483] Fix | Delete
chars = size
[484] Fix | Delete
[485] Fix | Delete
# read until we get the required number of characters (if available)
[486] Fix | Delete
while True:
[487] Fix | Delete
# can the request be satisfied from the character buffer?
[488] Fix | Delete
if chars >= 0:
[489] Fix | Delete
if len(self.charbuffer) >= chars:
[490] Fix | Delete
break
[491] Fix | Delete
# we need more data
[492] Fix | Delete
if size < 0:
[493] Fix | Delete
newdata = self.stream.read()
[494] Fix | Delete
else:
[495] Fix | Delete
newdata = self.stream.read(size)
[496] Fix | Delete
# decode bytes (those remaining from the last call included)
[497] Fix | Delete
data = self.bytebuffer + newdata
[498] Fix | Delete
if not data:
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function