YfW@sadZddlZddlZddlZddlZddlZddlZddlZyddlm Z Wn"e k rddl m Z YnXej ddhkrddlmZndZddlZddlmZmZmZmZddd hZeed r-ejejejejd d ZeZd d dddddddZGdddZGdddZy ej Z Wn+e!k rGddde"e#Z YnXGddddej$Z%ej%j&e%Gddde%Z'ej'j&e'ddl(m)Z)e'j&e)Gddde%Z*ej*j&e*Gdd d e*Z+Gd!d"d"e*Z,Gd#d$d$e+Z-Gd%d&d&e+Z.Gd'd(d(e*Z/Gd)d*d*e.e-Z0Gd+d,d,e'Z)Gd-d.d.e%Z1ej1j&e1Gd/d0d0ej2Z3Gd1d2d2e1Z4Gd3d4d4e4Z5dS)5z) Python implementation of the io module. N) allocate_lockwin32cygwin)setmode)__all__SEEK_SETSEEK_CURSEEK_END SEEK_HOLEirTcCsSt|tttfs(td|t|tsGtd|t|tsftd||dk rt|t rtd||dk rt|t rtd|t|}|tdst|t|krtd|d|k} d |k} d |k} d |k} d |k} d |k}d|k}d|kr| st| st| rtdddl}|j dt dd} |r|rtd| | | | dkrtd| p| p| p| std|r#|dk r#td|rA|dk rAtd|r_|dk r_tdt || rqdptd| rd pd| rd pd| rd pd| rd pd|d|}|}ynd}|dks|dkr|j rd"}d}|dkr]t }ytj|jj}Wnttfk rJYnX|dkr]|}|dkrutd|dkr|r|Std | rt||}nL| s| s| rt||}n(| rt||}ntd!||}|r |St|||||}|}||_|SWn|jYnXdS)#aOpen file and return a stream. Raise OSError upon failure. file is either a text or byte string giving the name (and the path if the file isn't in the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped. (If a file descriptor is given, it is closed when the returned I/O object is closed, unless closefd is set to False.) mode is an optional string that specifies the mode in which the file is opened. It defaults to 'r' which means open for reading in text mode. Other common values are 'w' for writing (truncating the file if it already exists), 'x' for exclusive creation of a new file, and 'a' for appending (which on some Unix systems, means that all writes append to the end of the file regardless of the current seek position). In text mode, if encoding is not specified the encoding used is platform dependent. (For reading and writing raw bytes use binary mode and leave encoding unspecified.) The available modes are: ========= =============================================================== Character Meaning --------- --------------------------------------------------------------- 'r' open for reading (default) 'w' open for writing, truncating the file first 'x' create a new file and open it for writing 'a' open for writing, appending to the end of the file if it exists 'b' binary mode 't' text mode (default) '+' open a disk file for updating (reading and writing) 'U' universal newline mode (deprecated) ========= =============================================================== The default mode is 'rt' (open for reading text). For binary random access, the mode 'w+b' opens and truncates the file to 0 bytes, while 'r+b' opens the file without truncation. The 'x' mode implies 'w' and raises an `FileExistsError` if the file already exists. Python distinguishes between files opened in binary and text modes, even when the underlying operating system doesn't. Files opened in binary mode (appending 'b' to the mode argument) return contents as bytes objects without any decoding. In text mode (the default, or when 't' is appended to the mode argument), the contents of the file are returned as strings, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given. 'U' mode is deprecated and will raise an exception in future versions of Python. It has no effect in Python 3. Use newline to control universal newlines mode. buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode), 1 to select line buffering (only usable in text mode), and an integer > 1 to indicate the size of a fixed-size chunk buffer. When no buffering argument is given, the default buffering policy works as follows: * Binary files are buffered in fixed-size chunks; the size of the buffer is chosen using a heuristic trying to determine the underlying device's "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`. On many systems, the buffer will typically be 4096 or 8192 bytes long. * "Interactive" text files (files for which isatty() returns True) use line buffering. Other text files use the policy described above for binary files. encoding is the str name of the encoding used to decode or encode the file. This should only be used in text mode. The default encoding is platform dependent, but any encoding supported by Python can be passed. See the codecs module for the list of supported encodings. errors is an optional string that specifies how encoding errors are to be handled---this argument should not be used in binary mode. Pass 'strict' to raise a ValueError exception if there is an encoding error (the default of None has the same effect), or pass 'ignore' to ignore errors. (Note that ignoring encoding errors can lead to data loss.) See the documentation for codecs.register for a list of the permitted encoding error strings. newline is a string controlling how universal newlines works (it only applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works as follows: * On input, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newline mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated. * On output, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string. closedfd is a bool. If closefd is False, the underlying file descriptor will be kept open when the file is closed. This does not work when a file name is given and must be True in that case. The newly created file is non-inheritable. A custom opener can be used by passing a callable as *opener*. The underlying file descriptor for the file object is then obtained by calling *opener* with (*file*, *flags*). *opener* must return an open file descriptor (passing os.open as *opener* results in functionality similar to passing None). open() returns a file object whose type depends on the mode, and through which the standard file operations such as reading and writing are performed. When open() is used to open a file in a text mode ('w', 'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open a file in a binary mode, the returned class varies: in read binary mode, it returns a BufferedReader; in write binary and append binary modes, it returns a BufferedWriter, and in read/write mode, it returns a BufferedRandom. It is also possible to use a string or bytearray as a file for both reading and writing. For strings StringIO can be used like a file opened in a text mode, and for bytes a BytesIO can be used like a file opened in a binary mode. zinvalid file: %rzinvalid mode: %rzinvalid buffering: %rNzinvalid encoding: %rzinvalid errors: %rzaxrwb+tUxrwa+tbUz$can't use U and writing mode at oncerz'U' mode is deprecatedr Tz'can't have text and binary mode at oncer z)can't have read/write/append mode at oncez/must have exactly one of read/write/append modez-binary mode doesn't take an encoding argumentz+binary mode doesn't take an errors argumentz+binary mode doesn't take a newline argumentopenerFzinvalid buffering sizezcan't have unbuffered text I/Ozunknown mode: %r) isinstancestrbytesint TypeErrorsetlen ValueErrorwarningswarnDeprecationWarningFileIOisattyDEFAULT_BUFFER_SIZEosfstatfileno st_blksizeOSErrorAttributeErrorBufferedRandomBufferedWriterBufferedReader TextIOWrappermodeclose)filer1 bufferingencodingerrorsnewlineclosefdrZmodesZcreatingZreadingZwritingZ appendingZupdatingtextZbinaryr!rawresultline_bufferingZbsbufferr>*/opt/alt/python35/lib64/python3.5/_pyio.pyopen)s{ (                   ?$        r@c@s"eZdZdZddZdS) DocDescriptorz%Helper for builtins.open.__doc__ cCs dtjS)Nz\open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True) )r@__doc__)selfobjtypr>r>r?__get__szDocDescriptor.__get__N)__name__ __module__ __qualname__rBrFr>r>r>r?rAs rAc@s+eZdZdZeZddZdS) OpenWrapperzWrapper for builtins.open Trick so that open won't become a bound method when stored as a class variable (as dbm.dumb does). See initstdio() in Python/pylifecycle.c. cOs t||S)N)r@)clsargskwargsr>r>r?__new__szOpenWrapper.__new__N)rGrHrIrBrArNr>r>r>r?rJs  rJc@seZdZdS)UnsupportedOperationN)rGrHrIr>r>r>r?rOs rOc@sZeZdZdZddZdddZddZd d d Zd d ZdZ ddZ ddZ ddZ d ddZ ddZd ddZddZd ddZedd Zd d!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd6d,d-Zd.d/Zd0d1Zd d2d3Zd4d5Zd S)7IOBaseagThe abstract base class for all I/O classes, acting on streams of bytes. There is no public constructor. This class provides dummy implementations for many methods that derived classes can override selectively; the default implementations represent a file that cannot be read, written or seeked. Even though IOBase does not declare read, readinto, or write because their signatures will vary, implementations and clients should consider those methods part of the interface. Also, implementations may raise UnsupportedOperation when operations they do not support are called. The basic type used for binary data read from or written to a file is bytes. Other bytes-like objects are accepted as method arguments too. In some cases (such as readinto), a writable object is required. Text I/O classes work with str data. Note that calling any method (even inquiries) on a closed stream is undefined. Implementations may raise OSError in this case. IOBase (and its subclasses) support the iterator protocol, meaning that an IOBase object can be iterated over yielding the lines in a stream. IOBase also supports the :keyword:`with` statement. In this example, fp is closed after the suite of the with statement is complete: with open('spam.txt', 'r') as fp: fp.write('Spam and eggs!') cCs td|jj|fdS)z@Internal: raise an OSError exception for unsupported operations.z%s.%s() not supportedN)rO __class__rG)rCnamer>r>r? _unsupported?szIOBase._unsupportedrcCs|jddS)a$Change stream position. Change the stream position to byte offset pos. Argument pos is interpreted relative to the position indicated by whence. Values for whence are ints: * 0 -- start of stream (the default); offset should be zero or positive * 1 -- current stream position; offset may be negative * 2 -- end of stream; offset is usually negative Some operating systems / file systems could provide additional values. Return an int indicating the new absolute position. seekN)rS)rCposwhencer>r>r?rTFsz IOBase.seekcCs|jddS)z5Return an int indicating the current stream position.rr )rT)rCr>r>r?tellVsz IOBase.tellNcCs|jddS)zTruncate file to size bytes. Size defaults to the current IO position as reported by tell(). Return the new size. truncateN)rS)rCrUr>r>r?rXZszIOBase.truncatecCs|jdS)zuFlush write buffers, if applicable. This is not implemented for read-only and non-blocking streams. N) _checkClosed)rCr>r>r?flushdsz IOBase.flushFc Cs(|js$z|jWdd|_XdS)ziFlush and close the IO object. This method has no effect if the file is already closed. NT)_IOBase__closedrZ)rCr>r>r?r2ns z IOBase.closec Csy|jWnYnXdS)zDestructor. Calls close().N)r2)rCr>r>r?__del__yszIOBase.__del__cCsdS)zReturn a bool indicating whether object supports random access. If False, seek(), tell() and truncate() will raise OSError. This method may need to do a test seek(). Fr>)rCr>r>r?seekableszIOBase.seekablecCs.|js*t|dkr!dn|dS)zEInternal: raise UnsupportedOperation if file is not seekable NzFile or stream is not seekable.)r]rO)rCmsgr>r>r?_checkSeekables zIOBase._checkSeekablecCsdS)zvReturn a bool indicating whether object was opened for reading. If False, read() will raise OSError. Fr>)rCr>r>r?readableszIOBase.readablecCs.|js*t|dkr!dn|dS)zEInternal: raise UnsupportedOperation if file is not readable NzFile or stream is not readable.)r`rO)rCr^r>r>r?_checkReadables zIOBase._checkReadablecCsdS)zReturn a bool indicating whether object was opened for writing. If False, write() and truncate() will raise OSError. Fr>)rCr>r>r?writableszIOBase.writablecCs.|js*t|dkr!dn|dS)zEInternal: raise UnsupportedOperation if file is not writable NzFile or stream is not writable.)rbrO)rCr^r>r>r?_checkWritables zIOBase._checkWritablecCs|jS)zclosed: bool. True iff the file has been closed. For backwards compatibility, this is a property, not a predicate. )r[)rCr>r>r?closedsz IOBase.closedcCs+|jr't|dkrdn|dS)z7Internal: raise a ValueError if file is closed NzI/O operation on closed file.)rdr )rCr^r>r>r?rYs zIOBase._checkClosedcCs|j|S)zCContext management protocol. Returns self (an instance of IOBase).)rY)rCr>r>r? __enter__s zIOBase.__enter__cGs|jdS)z+Context management protocol. Calls close()N)r2)rCrLr>r>r?__exit__szIOBase.__exit__cCs|jddS)zReturns underlying file descriptor (an int) if one exists. An OSError is raised if the IO object does not use a file descriptor. r)N)rS)rCr>r>r?r)sz IOBase.filenocCs|jdS)z{Return a bool indicating whether this is an 'interactive' stream. Return False if it can't be determined. F)rY)rCr>r>r?r%s z IOBase.isattyr cstdr'fdd}n dd}dkrHd nttsctdt}xUdkst|krj|}|sP||7}|jd roPqoWt|S) aNRead and return a line of bytes from the stream. If size is specified, at most size bytes will be read. Size should be an int. The line terminator is always b'\n' for binary files; for text files, the newlines argument to open can be used to select the line terminator(s) recognized. peekcsWjd}|sdS|jddp5t|}dkrSt|}|S)Nr s r)rgfindrmin)Z readaheadn)rCsizer>r? nreadaheads z#IOBase.readline..nreadaheadcSsdS)Nr r>r>r>r>r?rlsNr zsize must be an integerrs r) hasattrrrr bytearrayrreadendswithr)rCrkrlresrr>)rCrkr?readlines      ! zIOBase.readlinecCs|j|S)N)rY)rCr>r>r?__iter__s zIOBase.__iter__cCs|j}|st|S)N)rr StopIteration)rCliner>r>r?__next__ s zIOBase.__next__cCsm|dks|dkr"t|Sd}g}x8|D]0}|j||t|7}||kr5Pq5W|S)zReturn a list of lines from the stream. hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint. Nr)listappendr)rCZhintrjlinesrur>r>r? readliness    zIOBase.readlinescCs,|jx|D]}|j|qWdS)N)rYwrite)rCryrur>r>r? writelines"s  zIOBase.writelinesr)rGrHrIrBrSrTrWrXrZr[r2r\r]r_r`rarbrcpropertyrdrYrerfr)r%rrrsrvrzr|r>r>r>r?rPs4            %  rP metaclassc@sIeZdZdZd ddZddZddZd d Zd S) RawIOBasezBase class for raw binary I/O.r cCsp|dkrd}|dkr(|jSt|j}|j|}|dkrYdS||d=t|S)zRead and return up to size bytes, where size is an int. Returns an empty bytes object on EOF, or None if the object is set not to block and has no data to read. Nr rr)readallrn __index__readintor)rCrkrrjr>r>r?ro8s     zRawIOBase.readcCsHt}x$|jt}|s"P||7}q W|r@t|S|SdS)z+Read until EOF, using multiple read() call.N)rnror&r)rCrqdatar>r>r?rIs  zRawIOBase.readallcCs|jddS)zRead bytes into a pre-allocated bytes-like object b. Returns an int representing the number of bytes read (0 for EOF), or None if the object is set not to block and has no data to read. rN)rS)rCrr>r>r?rWszRawIOBase.readintocCs|jddS)zWrite the given buffer to the IO stream. Returns the number of bytes written, which may be less than the length of b in bytes. r{N)rS)rCrr>r>r?r{_szRawIOBase.writeNr)rGrHrIrBrorrr{r>r>r>r?r*s    r)r$c@speZdZdZdddZdddZddZd d Zd d Zd dZ ddZ dS)BufferedIOBaseaBase class for buffered IO objects. The main difference with RawIOBase is that the read() method supports omitting the size argument, and does not have a default implementation that defers to readinto(). In addition, read(), readinto() and write() may raise BlockingIOError if the underlying raw stream is in non-blocking mode and not ready; unlike their raw counterparts, they will never return None. A typical implementation should not inherit from a RawIOBase implementation, but wrap one. NcCs|jddS)aRead and return up to size bytes, where size is an int. If the argument is omitted, None, or negative, reads and returns all data until EOF. If the argument is positive, and the underlying raw stream is not 'interactive', multiple raw reads may be issued to satisfy the byte count (unless EOF is reached first). But for interactive raw streams (XXX and for pipes?), at most one raw read will be issued, and a short result does not imply that EOF is imminent. Returns an empty bytes array on EOF. Raises BlockingIOError if the underlying raw stream has no data at the moment. roN)rS)rCrkr>r>r?ro}szBufferedIOBase.readcCs|jddS)zaRead up to size bytes with at most one read() system call, where size is an int. read1N)rS)rCrkr>r>r?rszBufferedIOBase.read1cCs|j|ddS)afRead bytes into a pre-allocated bytes-like object b. Like read(), this may issue multiple reads to the underlying raw stream, unless the latter is 'interactive'. Returns an int representing the number of bytes read (0 for EOF). Raises BlockingIOError if the underlying raw stream has no data at the moment. rF) _readinto)rCrr>r>r?rs zBufferedIOBase.readintocCs|j|ddS)zRead bytes into buffer *b*, using at most one system call Returns an int representing the number of bytes read (0 for EOF). Raises BlockingIOError if the underlying raw stream has no data at the moment. rT)r)rCrr>r>r? readinto1s zBufferedIOBase.readinto1cCs}t|tst|}|jd}|rH|jt|}n|jt|}t|}||d|<|S)NB)r memoryviewcastrrro)rCrrrrjr>r>r?rs  zBufferedIOBase._readintocCs|jddS)aWrite the given bytes buffer to the IO stream. Return the number of bytes written, which is always the length of b in bytes. Raises BlockingIOError if the buffer is full and the underlying raw stream cannot accept more data at the moment. r{N)rS)rCrr>r>r?r{s zBufferedIOBase.writecCs|jddS)z Separate the underlying raw stream from the buffer and return it. After the raw stream has been detached, the buffer is in an unusable state. detachN)rS)rCr>r>r?rszBufferedIOBase.detach) rGrHrIrBrorrrrr{rr>r>r>r?rls    rc@seZdZdZddZdddZddZd d d Zd d ZddZ ddZ ddZ e ddZ e ddZe ddZe ddZddZddZd d!Zd"d#Zd S)$_BufferedIOMixinzA mixin implementation of BufferedIOBase with an underlying raw stream. This passes most requests on to the underlying raw stream. It does *not* provide implementations of read(), readinto() or write(). cCs ||_dS)N)_raw)rCr:r>r>r?__init__sz_BufferedIOMixin.__init__rcCs1|jj||}|dkr-td|S)Nrz#seek() returned an invalid position)r:rTr+)rCrUrVZ new_positionr>r>r?rTs  z_BufferedIOMixin.seekcCs+|jj}|dkr'td|S)Nrz#tell() returned an invalid position)r:rWr+)rCrUr>r>r?rWs  z_BufferedIOMixin.tellNcCs2|j|dkr"|j}|jj|S)N)rZrWr:rX)rCrUr>r>r?rXs   z_BufferedIOMixin.truncatecCs&|jrtd|jjdS)Nzflush of closed file)rdr r:rZ)rCr>r>r?rZs  z_BufferedIOMixin.flushc Cs<|jdk r8|j r8z|jWd|jjXdS)N)r:rdrZr2)rCr>r>r?r2sz_BufferedIOMixin.closecCs;|jdkrtd|j|j}d|_|S)Nzraw stream already detached)r:r rZr)rCr:r>r>r?r s     z_BufferedIOMixin.detachcCs |jjS)N)r:r])rCr>r>r?r]sz_BufferedIOMixin.seekablecCs|jS)N)r)rCr>r>r?r:sz_BufferedIOMixin.rawcCs |jjS)N)r:rd)rCr>r>r?rdsz_BufferedIOMixin.closedcCs |jjS)N)r:rR)rCr>r>r?rR sz_BufferedIOMixin.namecCs |jjS)N)r:r1)rCr>r>r?r1$sz_BufferedIOMixin.modecCstdj|jjdS)Nz can not serialize a '{0}' object)rformatrQrG)rCr>r>r? __getstate__(s z_BufferedIOMixin.__getstate__c Csa|jj}|jj}y |j}Wn"tk rIdj||SYnXdj|||SdS)Nz<{}.{}>z<{}.{} name={!r}>)rQrHrIrR Exceptionr)rCmodnameZclsnamerRr>r>r?__repr__,s    z_BufferedIOMixin.__repr__cCs |jjS)N)r:r))rCr>r>r?r)8sz_BufferedIOMixin.filenocCs |jjS)N)r:r%)rCr>r>r?r%;sz_BufferedIOMixin.isatty)rGrHrIrBrrTrWrXrZr2rr]r}r:rdrRr1rrr)r%r>r>r>r?rs"        rcseZdZdZdddZddZddZd d Zfd d Zdd dZ ddZ ddZ dddZ ddZ dddZddZddZddZS) BytesIOzr>r?rCs     zBytesIO.__init__cCs"|jrtd|jjS)Nz__getstate__ on closed file)rdr __dict__copy)rCr>r>r?rJs  zBytesIO.__getstate__cCs"|jrtdt|jS)z8Return the bytes value (contents) of the buffer zgetvalue on closed file)rdr rr)rCr>r>r?getvalueOs  zBytesIO.getvaluecCs"|jrtdt|jS)z;Return a readable and writable view of the buffer. zgetbuffer on closed file)rdr rr)rCr>r>r? getbufferVs  zBytesIO.getbuffercs|jjtjdS)N)rclearsuperr2)rC)rQr>r?r2]s z BytesIO.closecCs|jrtd|dkr'd}|dkrBt|j}t|j|jkr^dStt|j|j|}|j|j|}||_t|S)Nzread from closed filer rr)rdr rrrrir)rCrkZnewposrr>r>r?roas     z BytesIO.readcCs |j|S)z"This is the same as read. )ro)rCrkr>r>r?rosz BytesIO.read1c Cs|jrtdt|tr0tdt|}|j}WdQRX|dkr_dS|j}|t|j krd|t|j }|j |7_ ||j |||<|j|7_|S)Nzwrite to closed filez can't write str to binary streamrs) rdr rrrrnbytesrrr)rCrZviewrjrUZpaddingr>r>r?r{ts     z BytesIO.writercCs|jrtdy |jWn4tk rV}ztd|WYdd}~XnX|dkr|dkrtd|f||_nb|dkrtd|j||_n:|dkrtdt|j||_n td|jS)Nzseek on closed filezan integer is requiredrznegative seek position %rr r zunsupported whence value) rdr rr,rrmaxrr)rCrUrVerrr>r>r?rTs    "     " z BytesIO.seekcCs|jrtd|jS)Nztell on closed file)rdr r)rCr>r>r?rWs  z BytesIO.tellcCs|jrtd|dkr-|j}nay |jWn4tk rn}ztd|WYdd}~XnX|dkrtd|f|j|d=|S)Nztruncate on closed filezan integer is requiredrznegative truncate position %r)rdr rrr,rr)rCrUrr>r>r?rXs     " zBytesIO.truncatecCs|jrtddS)NzI/O operation on closed file.T)rdr )rCr>r>r?r`s  zBytesIO.readablecCs|jrtddS)NzI/O operation on closed file.T)rdr )rCr>r>r?rbs  zBytesIO.writablecCs|jrtddS)NzI/O operation on closed file.T)rdr )rCr>r>r?r]s  zBytesIO.seekable)rGrHrIrBrrrrr2rorr{rTrWrXr`rbr]r>r>)rQr?r?s         rc@seZdZdZeddZddZddZdd d Zdd d Z d ddZ d ddZ ddZ ddZ ddZd ddZdS)r/aBufferedReader(raw[, buffer_size]) A buffer for a readable, sequential BaseRawIO object. The constructor creates a BufferedReader for the given readable raw stream and buffer_size. If buffer_size is omitted, DEFAULT_BUFFER_SIZE is used. cCsc|jstdtj|||dkr@td||_|jt|_dS)zMCreate a new buffered reader using the given readable raw IO object. z "raw" argument must be readable.rzinvalid buffer sizeN) r`r+rrr buffer_size_reset_read_bufLock _read_lock)rCr:rr>r>r?rs      zBufferedReader.__init__cCs |jjS)N)r:r`)rCr>r>r?r`szBufferedReader.readablecCsd|_d|_dS)Nrr) _read_buf _read_pos)rCr>r>r?rs zBufferedReader._reset_read_bufNc CsF|dk r$|dkr$td|j|j|SWdQRXdS)zRead size bytes. Returns exactly size bytes of data unless the underlying raw IO stream reaches EOF or if the call would block in non-blocking mode. If size is negative, read until EOF or until read() would block. Nr zinvalid number of bytes to readr)r r_read_unlocked)rCrkr>r>r?ros  zBufferedReader.readc Csd}d}|j}|j}|dks6|dkr|jt|jdr|jj}|dkr||dpdS||d|S||dg}d}xC|jj}||kr|}P|t|7}|j|qWdj |p|St||} || krB|j|7_||||S||dg}t |j |} xR| |kr|jj| }||kr|}P| t|7} |j|qjWt || }dj |} | |d|_d|_| r | d|S|S)Nrr rr)rNr) rrrrmr:rrorrxjoinrrri) rCrjZ nodata_valZ empty_valuesrrUchunkZchunksZ current_sizeavailZwantedoutr>r>r?rsN        zBufferedReader._read_unlockedrc Cs"|j|j|SWdQRXdS)zReturns buffered bytes without advancing the position. The argument indicates a desired minimal number of bytes; we do at most one raw read to satisfy it. We never return more than self.buffer_size. N)r_peek_unlocked)rCrkr>r>r?rgs zBufferedReader.peekcCst||j}t|j|j}||ks@|dkr|j|}|jj|}|r|j|jd||_d|_|j|jdS)Nr)rirrrrr:ro)rCrjZwantZhaveZto_readZcurrentr>r>r?r(s  zBufferedReader._peek_unlockedc Csp|dkrtd|dkr(dS|j8|jd|jt|t|j|jSWdQRXdS)zr>r?r3s     zBufferedReader.read1c Cset|tst|}|jdkr.dS|jd}d}|jx |t|krYtt|j|jt|}|r|j|j|j|||||<|j|7_||7}|t|krPt|||j kr-|j j ||d}|s P||7}n|o6|sI|j dsIP|rP|rPPqPWWdQRX|S)z2Read data into *buf* with at most one system call.rrNr ) rrrrrrrirrrr:rr)rCrrwrittenrrjr>r>r?rEs4  %+     zBufferedReader._readintocCs!tj|t|j|jS)N)rrWrrr)rCr>r>r?rWsszBufferedReader.tellc Csv|tkrtd|jN|dkrH|t|j|j8}tj|||}|j|SWdQRXdS)Nzinvalid whence valuer ) valid_seek_flagsr rrrrrrTr)rCrUrVr>r>r?rTvs     zBufferedReader.seek)rGrHrIrBr&rr`rrorrgrrrrWrTr>r>r>r?r/s    4   . r/c@seZdZdZeddZddZddZdd d Zd d Z d dZ ddZ dddZ dS)r.zA buffer for a writeable sequential RawIO object. The constructor creates a BufferedWriter for the given writeable raw stream. If the buffer_size is not given, it defaults to DEFAULT_BUFFER_SIZE. cCse|jstdtj|||dkr@td||_t|_t|_ dS)Nz "raw" argument must be writable.rzinvalid buffer size) rbr+rrr rrn _write_bufr _write_lock)rCr:rr>r>r?rs      zBufferedWriter.__init__cCs |jjS)N)r:rb)rCr>r>r?rbszBufferedWriter.writablecCsT|jrtdt|tr0td|jt|j|jkr\|j t|j}|jj |t|j|}t|j|jkrEy|j Wnt k rD}znt|j|jkr2t|j|j}||8}|jd|j|_t |j |j |WYdd}~XnX|SWdQRXdS)Nzwrite to closed filez can't write str to binary stream)rdr rrrrrrr_flush_unlockedextendBlockingIOErrorerrnostrerror)rCrZbeforereZoverager>r>r?r{s(      +zBufferedWriter.writeNc CsJ|j:|j|dkr/|jj}|jj|SWdQRXdS)N)rrr:rWrX)rCrUr>r>r?rXs    zBufferedWriter.truncatec Cs|j|jWdQRXdS)N)rr)rCr>r>r?rZs zBufferedWriter.flushc Cs|jrtdx|jry|jj|j}Wntk rZtdYnX|dkr|ttjdd|t |jks|dkrt d|jd|=qWdS)Nzflush of closed filezHself.raw should implement RawIOBase: it should not raise BlockingIOErrorz)write could not complete without blockingrz*write() returned incorrect number of bytes) rdr rr:r{r RuntimeErrorrZEAGAINrr+)rCrjr>r>r?rs      ! zBufferedWriter._flush_unlockedcCstj|t|jS)N)rrWrr)rCr>r>r?rWszBufferedWriter.tellrc CsJ|tkrtd|j"|jtj|||SWdQRXdS)Nzinvalid whence value)rr rrrrT)rCrUrVr>r>r?rTs     zBufferedWriter.seek) rGrHrIrBr&rrbr{rXrZrrWrTr>r>r>r?r.s      r.c@seZdZdZeddZdddZddZd d Zd d d Z ddZ ddZ ddZ ddZ ddZddZddZeddZdS)BufferedRWPairaA buffered reader and writer object together. A buffered reader object and buffered writer object put together to form a sequential IO object that can read and write. This is typically used with a socket or two-way pipe. reader and writer are RawIOBase objects that are readable and writeable respectively. If the buffer_size is omitted it defaults to DEFAULT_BUFFER_SIZE. cCsX|jstd|js0tdt|||_t|||_dS)zEConstructor. The arguments are two RawIO instances. z#"reader" argument must be readable.z#"writer" argument must be writable.N)r`r+rbr/readerr.writer)rCrrrr>r>r?rs     zBufferedRWPair.__init__NcCs"|dkrd}|jj|S)Nr r)rro)rCrkr>r>r?ros zBufferedRWPair.readcCs|jj|S)N)rr)rCrr>r>r?rszBufferedRWPair.readintocCs|jj|S)N)rr{)rCrr>r>r?r{szBufferedRWPair.writercCs|jj|S)N)rrg)rCrkr>r>r?rgszBufferedRWPair.peekcCs|jj|S)N)rr)rCrkr>r>r?rszBufferedRWPair.read1cCs|jj|S)N)rr)rCrr>r>r?r szBufferedRWPair.readinto1cCs |jjS)N)rr`)rCr>r>r?r` szBufferedRWPair.readablecCs |jjS)N)rrb)rCr>r>r?rbszBufferedRWPair.writablecCs |jjS)N)rrZ)rCr>r>r?rZszBufferedRWPair.flushc Cs&z|jjWd|jjXdS)N)rr2r)rCr>r>r?r2szBufferedRWPair.closecCs|jjp|jjS)N)rr%r)rCr>r>r?r%szBufferedRWPair.isattycCs |jjS)N)rrd)rCr>r>r?rdszBufferedRWPair.closed)rGrHrIrBr&rrorr{rgrrr`rbrZr2r%r}rdr>r>r>r?rs          rc@seZdZdZeddZdddZddZd d d Zd d d Z ddZ dddZ ddZ ddZ ddZd S)r-zA buffered interface to random access streams. The constructor creates a reader and writer for a seekable stream, raw, given in the first argument. If the buffer_size is omitted it defaults to DEFAULT_BUFFER_SIZE. cCs4|jtj|||tj|||dS)N)r_r/rr.)rCr:rr>r>r?r,s zBufferedRandom.__init__rcCs|tkrtd|j|jr_|j(|jj|jt|jdWdQRX|jj||}|j|j WdQRX|dkrt d|S)Nzinvalid whence valuer rz seek() returned invalid position) rr rZrrr:rTrrrr+)rCrUrVr>r>r?rT1s     *   zBufferedRandom.seekcCs'|jrtj|Stj|SdS)N)rr.rWr/)rCr>r>r?rWBs  zBufferedRandom.tellNcCs(|dkr|j}tj||S)N)rWr.rX)rCrUr>r>r?rXHs  zBufferedRandom.truncatecCs,|dkrd}|jtj||S)Nr r)rZr/ro)rCrkr>r>r?roNs  zBufferedRandom.readcCs|jtj||S)N)rZr/r)rCrr>r>r?rTs zBufferedRandom.readintocCs|jtj||S)N)rZr/rg)rCrkr>r>r?rgXs zBufferedRandom.peekcCs|jtj||S)N)rZr/r)rCrkr>r>r?r\s zBufferedRandom.read1cCs|jtj||S)N)rZr/r)rCrr>r>r?r`s zBufferedRandom.readinto1c CsW|jrG|j2|jj|jt|jd|jWdQRXtj||S)Nr ) rrr:rTrrrr.r{)rCrr>r>r?r{ds   #zBufferedRandom.write)rGrHrIrBr&rrTrWrXrorrgrrr{r>r>r>r?r-#s     r-cs]eZdZd0ZdZdZdZdZdZdZ dddddZ dd Z d d Z d d Z ddZdddZdddZddZddZddZeddZddZdddZfd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zed,d-Zed.d/Z S)1r$r FNTrc Cs|jdkr9z|jr+tj|jWdd|_Xt|trTtdt|tr|}|dkrtdnd}t|t std|ft |t dkstd|ft dd |Ddks |j d dkrtd d |krHd |_ d |_tjtjB}ntd|krfd |_d}nVd|krd |_tjtjB}n.d|krd |_d |_tjtjB}d |krd |_d |_|jr|jr|tjO}n&|jr|tjO}n |tjO}|ttddO}ttddpYttdd}||O}d}yp|dkr|std|dkrtj||d}nB|||}t|tstd|dkrtd|}|stj|d||_tj|} y7tj| jr]t t!j"tj#t!j"|Wnt$k rrYnXt| dd|_%|j%dkrt&|_%t'rt'|tj(||_)|jrtj*|dt+Wn$|dk rtj|YnX||_dS)adOpen a file. The mode can be 'r' (default), 'w', 'x' or 'a' for reading, writing, exclusive creation or appending. The file will be created if it doesn't exist when opened for writing or appending; it will be truncated when opened for writing. A FileExistsError will be raised if it already exists when opened for creating. Opening a file for creating implies writing so this mode behaves in a similar way to 'w'. Add a '+' to the mode to allow simultaneous reading and writing. A custom opener can be used by passing a callable as *opener*. The underlying file descriptor for the file object is then obtained by calling opener with (*name*, *flags*). *opener* must return an open file descriptor (passing os.open as *opener* results in functionality similar to passing None). rNr z$integer argument expected, got floatznegative file descriptorzinvalid mode: %szxrwab+css|]}|dkVqdS)ZrwaxNr>).0cr>r>r? sz"FileIO.__init__..rzKMust have exactly one of create/read/write/append mode and at most one plusrTrrrO_BINARYZ O_NOINHERIT O_CLOEXECz'Cannot use closefd=False with file nameizexpected integer from openerzNegative file descriptorFr*rr),_fd_closefdr'r2rfloatrrr rrsumcount_created _writableO_EXCLO_CREAT _readableO_TRUNC _appendingO_APPENDO_RDWRO_RDONLYO_WRONLYgetattrr@r+set_inheritabler(statS_ISDIRst_modeIsADirectoryErrorrZEISDIRrr,_blksizer&_setmoderrRlseekr ) rCr3r1r8rfdflagsZnoinherit_flagZowned_fdZfdfstatr>r>r?rvs     4                                zFileIO.__init__cCsY|jdkrU|jrU|j rUddl}|jd|ftdd|jdS)Nrzunclosed file %r stacklevelr )rrrdr!r"ResourceWarningr2)rCr!r>r>r?r\s " zFileIO.__del__cCstd|jjdS)Nzcannot serialize '%s' object)rrQrG)rCr>r>r?rszFileIO.__getstate__c Csd|jj|jjf}|jr-d|Sy |j}Wn/tk rkd||j|j|jfSYnXd|||j|jfSdS)Nz%s.%sz <%s [closed]>z<%s fd=%d mode=%r closefd=%r>z<%s name=%r mode=%r closefd=%r>) rQrHrIrdrRr,rr1r)rCZ class_namerRr>r>r?rs    zFileIO.__repr__cCs|jstddS)NzFile not open for reading)rrO)rCr>r>r?ras zFileIO._checkReadablecCs|jstddS)NzFile not open for writing)rrO)rCr^r>r>r?rcs zFileIO._checkWritablec Csj|j|j|dks,|dkr6|jSytj|j|SWntk redSYnXdS)zRead at most size bytes, returned as bytes. Only makes one system call, so less data may be returned than requested In non-blocking mode, returns None if no data is available. Return an empty bytes object at EOF. Nr)rYrarr'rorr)rCrkr>r>r?ro s    z FileIO.readcCs|j|jt}yKtj|jdt}tj|jj}||krd||d}Wnt k ryYnXt }xt ||krt |}|t |t7}|t |}ytj |j|}Wntk r|rPdSYnX|sP||7}qWt|S)zRead all data from the file, returned as bytes. In non-blocking mode, returns as much as is immediately available, or None if no data is available. Return an empty bytes object at EOF. rr N)rYrar&r'rrrr(st_sizer+rnrrrorr)rCbufsizerUendr;rjrr>r>r?rs4        zFileIO.readallcCsJt|jd}|jt|}t|}||d|<|S)zSame as RawIOBase.readinto().rN)rrror)rCrmrrjr>r>r?r>s  zFileIO.readintoc CsH|j|jytj|j|SWntk rCdSYnXdS)aWrite bytes b to file, return number written. Only makes one system call, so not all of the data may be written. The number of bytes actually written is returned. In non-blocking mode, returns None if the write would block. N)rYrcr'r{rr)rCrr>r>r?r{Fs    z FileIO.writecCs;t|trtd|jtj|j||S)aMove to new file position. Argument offset is a byte count. Optional argument whence defaults to SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values are SEEK_CUR or 1 (move relative to current position, positive or negative), and SEEK_END or 2 (move relative to end of file, usually negative, although many platforms allow seeking beyond the end of a file). Note that not all file objects are seekable. zan integer is required)rrrrYr'rr)rCrUrVr>r>r?rTTs   z FileIO.seekcCs |jtj|jdtS)zYtell() -> int. Current file position. Can raise OSError for non seekable files.r)rYr'rrr)rCr>r>r?rWds z FileIO.tellcCsC|j|j|dkr,|j}tj|j||S)zTruncate the file to at most size bytes. Size defaults to the current file position, as returned by tell(). The current file position is changed to the value of size. N)rYrcrWr' ftruncater)rCrkr>r>r?rXks     zFileIO.truncatec s;|js7z|jr%tj|jWdtjXdS)zClose the file. A closed file cannot be used for further I/O operations. close() may be called more than once without error. N)rdrr'r2rr)rC)rQr>r?r2xs   z FileIO.closec CsU|j|jdkrNy|jWntk rDd|_Yn Xd|_|jS)z$True if file supports random-access.NFT)rY _seekablerWr+)rCr>r>r?r]s   zFileIO.seekablecCs|j|jS)z'True if file was opened in a read mode.)rYr)rCr>r>r?r`s zFileIO.readablecCs|j|jS)z(True if file was opened in a write mode.)rYr)rCr>r>r?rbs zFileIO.writablecCs|j|jS)z3Return the underlying file descriptor (an integer).)rYr)rCr>r>r?r)s z FileIO.filenocCs|jtj|jS)z.True if the file is connected to a TTY device.)rYr'r%r)rCr>r>r?r%s z FileIO.isattycCs|jS)z6True if the file descriptor will be closed by close().)r)rCr>r>r?r8szFileIO.closefdcCs_|jr|jrdSdSn>|jr:|jr3dSdSn!|jrW|jrPdSdSndSdS) zString giving the file modezxb+Zxbzab+Zabzrb+rbwbN)rrrr)rCr>r>r?r1s      z FileIO.moder)!rGrHrIrrrrrrrrr\rrrarcrorrr{rrTrWrXr2r]r`rbr)r%r}r8r1r>r>)rQr?r$ms8 u     #        r$c@seZdZdZdddZddZddd Zd d Zd d Ze ddZ e ddZ e ddZ dS) TextIOBasezBase class for text I/O. This class provides a character and line based interface to stream I/O. There is no readinto method because Python's character strings are immutable. There is no public constructor. r cCs|jddS)zRead at most size characters from stream, where size is an int. Read from underlying buffer until we have size characters or we hit EOF. If size is negative or omitted, read until EOF. Returns a string. roN)rS)rCrkr>r>r?roszTextIOBase.readcCs|jddS)z.Write string s to stream and returning an int.r{N)rS)rCsr>r>r?r{szTextIOBase.writeNcCs|jddS)z*Truncate size to pos, where pos is an int.rXN)rS)rCrUr>r>r?rXszTextIOBase.truncatecCs|jddS)z_Read until newline or EOF. Returns an empty string if EOF is hit immediately. rrN)rS)rCr>r>r?rrszTextIOBase.readlinecCs|jddS)z Separate the underlying buffer from the TextIOBase and return it. After the underlying buffer has been detached, the TextIO is in an unusable state. rN)rS)rCr>r>r?rszTextIOBase.detachcCsdS)zSubclasses should override.Nr>)rCr>r>r?r5szTextIOBase.encodingcCsdS)zLine endings translated so far. Only line endings translated during reading are considered. Subclasses should override. Nr>)rCr>r>r?newlinesszTextIOBase.newlinescCsdS)zMError setting of the decoder or encoder. Subclasses should override.Nr>)rCr>r>r?r6szTextIOBase.errorsr) rGrHrIrBror{rXrrrr}r5rr6r>r>r>r?rs     rc@s|eZdZdZdddZdddZdd Zd d Zd d ZdZ dZ dZ e ddZ dS)IncrementalNewlineDecodera+Codec used when reading a file in universal newlines mode. It wraps another incremental decoder, translating \r\n and \r into \n. It also records the types of newlines encountered. When used with translate=False, it ensures that the newline sequence is returned in one piece. strictcCs>tjj|d|||_||_d|_d|_dS)Nr6rF)codecsIncrementalDecoderr translatedecoderseennl pendingcr)rCrrr6r>r>r?r s    z"IncrementalNewlineDecoder.__init__FcCs+|jdkr|}n|jj|d|}|jrX|sE|rXd|}d|_|jdr| r|dd}d|_|jd}|jd|}|jd|}|j|o|j|o|jB|o|jBO_|j r'|r|j dd}|r'|j dd}|S) Nfinal Fr Tz  r) rdecoderrprr_LF_CR_CRLFrreplace)rCinputroutputZcrlfZcrZlfr>r>r?rs(    + z IncrementalNewlineDecoder.decodecCsZ|jdkrd}d}n|jj\}}|dK}|jrP|dO}||fS)Nrrr )rgetstater)rCrflagr>r>r?r1s    z"IncrementalNewlineDecoder.getstatecCsL|\}}t|d@|_|jdk rH|jj||d?fdS)Nr )boolrrsetstate)rCstaterrr>r>r?r<s z"IncrementalNewlineDecoder.setstatecCs2d|_d|_|jdk r.|jjdS)NrF)rrrreset)rCr>r>r?rBs  zIncrementalNewlineDecoder.resetr r c Cs d|jS) Nrr rrrrrrrrr)Nrrrrrrr)r)rCr>r>r?rLsz"IncrementalNewlineDecoder.newlinesN)rGrHrIrBrrrrrrrrr}rr>r>r>r?rs   rc@seZdZdZdZdddddddZddZed d Zed d Z ed dZ eddZ ddZ ddZ ddZddZddZeddZeddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zdd+d,Zd-d.Zd/d0Zd1d1d1d1d2d3Zd4d5Zd6d7Zdd8d9Zd:d;Z d1d<d=Z!dd>d?Z"d@dAZ#ddBdCZ$edDdEZ%dS)Fr0aCharacter and line based layer over a BufferedIOBase object, buffer. encoding gives the name of the encoding that the stream will be decoded or encoded with. It defaults to locale.getpreferredencoding(False). errors determines the strictness of encoding and decoding (see the codecs.register) and defaults to "strict". newline can be None, '', '\n', '\r', or '\r\n'. It controls the handling of line endings. If it is None, universal newlines is enabled. With this enabled, on input, the lines endings '\n', '\r', or '\r\n' are translated to '\n' before being returned to the caller. Conversely, on output, '\n' is translated to the system default line separator, os.linesep. If newline is any other of its legal values, that newline becomes the newline when the file is read and it is returned untranslated. On output, '\n' is converted to the newline. If line_buffering is True, a call to flush is implied when a call to write contains a newline character. iNFc Cs|dk r5t|t r5tdt|f|dkrTtd|f|dkrytj|j}Wntt fk rYnX|dkryddl }Wnt k rd}YnX|j d }t|tstd |t j|js!d }t|||dkr6d }nt|tsUtd |||_||_||_||_| |_|dk|_||_|dk|_|ptj|_d|_d|_d|_d|_d|_|j j!|_"|_#t$|j d|_%d|_&|j"r||j'r||j j(} | dkr|y|j)j*dWntk r{YnXdS)Nzillegal newline type: %rrrr zillegal newline value: %rrasciiFzinvalid encoding: %rzG%r is not a text encoding; use codecs.open() to handle arbitrary codecsrzinvalid errors: %rrg)Nrrrr)+rrrtyper r'device_encodingr)r,rOlocale ImportErrorgetpreferredencodingrlookup_is_text_encoding LookupErrorr_line_buffering _encoding_errors_readuniversal_readtranslate_readnl_writetranslatelinesep_writenl_encoder_decoder_decoded_chars_decoded_chars_used _snapshotr=r]r_tellingrm _has_read1 _b2cratiorbrW _get_encoderr) rCr=r5r6r7r<Z write_throughr r^positionr>r>r?rvs`                     zTextIOWrapper.__init__cCsdj|jj|jj}y |j}Wntk r?YnX|dj|7}y |j}Wntk rtYnX|dj|7}|dj|jS)Nz<{}.{}z name={0!r}z mode={0!r}z encoding={0!r}>)rrQrHrIrRrr1r5)rCr;rRr1r>r>r?rs    zTextIOWrapper.__repr__cCs|jS)N)r)rCr>r>r?r5szTextIOWrapper.encodingcCs|jS)N)r)rCr>r>r?r6szTextIOWrapper.errorscCs|jS)N)r)rCr>r>r?r<szTextIOWrapper.line_bufferingcCs|jS)N)r)rCr>r>r?r=szTextIOWrapper.buffercCs|jrtd|jS)NzI/O operation on closed file.)rdr r)rCr>r>r?r]s  zTextIOWrapper.seekablecCs |jjS)N)r=r`)rCr>r>r?r`szTextIOWrapper.readablecCs |jjS)N)r=rb)rCr>r>r?rbszTextIOWrapper.writablecCs|jj|j|_dS)N)r=rZrr)rCr>r>r?rZs zTextIOWrapper.flushc Cs<|jdk r8|j r8z|jWd|jjXdS)N)r=rdrZr2)rCr>r>r?r2szTextIOWrapper.closecCs |jjS)N)r=rd)rCr>r>r?rdszTextIOWrapper.closedcCs |jjS)N)r=rR)rCr>r>r?rRszTextIOWrapper.namecCs |jjS)N)r=r))rCr>r>r?r)szTextIOWrapper.filenocCs |jjS)N)r=r%)rCr>r>r?r%szTextIOWrapper.isattycCs|jrtdt|ts:td|jjt|}|jsX|j oad|k}|r|jr|j dkr|j d|j }|j p|j }|j|}|jj||j r|sd|kr|jd|_|jr|jj|S)zWrite data, where s is a strzwrite to closed filezcan't write %s to text streamrrN)rdr rrrrQrGrrrrrrr"encoder=r{rZrrr)rCrZlengthZhaslfencoderrr>r>r?r{s$       zTextIOWrapper.writecCs+tj|j}||j|_|jS)N)rgetincrementalencoderrrr)rCZ make_encoderr>r>r?r"szTextIOWrapper._get_encodercCsItj|j}||j}|jr<t||j}||_|S)N)rgetincrementaldecoderrrrrrr)rCZ make_decoderrr>r>r? _get_decoders   zTextIOWrapper._get_decodercCs||_d|_dS)zSet the _decoded_chars buffer.rN)rr)rCcharsr>r>r?_set_decoded_chars's z TextIOWrapper._set_decoded_charscCs[|j}|dkr+|j|d}n|j|||}|jt|7_|S)z'Advance into the _decoded_chars buffer.N)rrr)rCrjoffsetr)r>r>r?_get_decoded_chars,s   z TextIOWrapper._get_decoded_charscCs.|j|krtd|j|8_dS)z!Rewind the _decoded_chars buffer.z"rewind decoded_chars out of boundsN)rAssertionError)rCrjr>r>r?_rewind_decoded_chars6s z#TextIOWrapper._rewind_decoded_charscCs|jdkrtd|jr9|jj\}}|jrZ|jj|j}n|jj|j}| }|jj ||}|j ||rt |t |j |_ n d|_ |jr|||f|_| S)zQ Read and decode the next chunk of data from the BufferedReader. Nz no decoderg)rr rrr r=r _CHUNK_SIZErorr*rrr!r)rC dec_buffer dec_flags input_chunkeofZ decoded_charsr>r>r? _read_chunk<s       zTextIOWrapper._read_chunkrcCs*||d>B|d>B|d>Bt|d>BS)N@)r)rCr#r1 bytes_to_feedneed_eof chars_to_skipr>r>r? _pack_cookiefszTextIOWrapper._pack_cookiecCsgt|d\}}t|d\}}t|d\}}t|d\}}|||||fS)Nr r5llll)divmod)rCZbigintrestr#r1r9r:r;r>r>r?_unpack_cookieps zTextIOWrapper._unpack_cookiecCs|jstd|js*td|j|jj}|j}|dksg|jdkr|j r|t d|S|j\}}|t |8}|j }|dkr|j ||S|j}z4t|j|}d}|t |kst x|dkr|jd|ft |j|d|} | |kr|j\} } | sz| }|| 8}P|t | 8}d}q||8}|d}qWd}|jd|f||} |} |dkr|j | | Sd}d}d}xt|t |D]}|d7}|t |j|||d7}|j\}}| r||kr| |7} ||8}|dd} }}||krPqW|t |jddd 7}d}||krtd |j | | |||SWd|j|XdS) Nz!underlying stream is not seekablez(telling position disabled by next() callzpending decoded textrr rr rTz'can't reconstruct logical file position)rrOrr+rZr=rWrrrr-rrr<rrr!rrrange)rCr#rr1Z next_inputr;Z saved_stateZ skip_bytesZ skip_backrjrd start_posZ start_flagsZ bytes_fedr:Z chars_decodedir0r>r>r?rWwsx                  '     zTextIOWrapper.tellcCs2|j|dkr"|j}|jj|S)N)rZrWr=rX)rCrUr>r>r?rXs   zTextIOWrapper.truncatecCs;|jdkrtd|j|j}d|_|S)Nzbuffer is already detached)r=r rZr)rCr=r>r>r?rs     zTextIOWrapper.detachc smfdd}jr'tdjs<td|dkrr|dkr`tdd}j}|dkr|dkrtd jjjdd}jd d_ j rj j |||S|dkrtd |f|dkr-td |fjj |\}}}}} jj|jd d_ |dkrj rj j nRj s|s| rj pj _ j jd |f|d f_ | r_jj|} jj j| ||| f_ tj| krVtd| _|||S)Nc sXyjpj}Wntk r-Yn'X|dkrJ|jdn |jdS)z9Reset the encoder (merely useful for proper BOM handling)rN)rr"rrr)r#r%)rCr>r?_reset_encoders  z*TextIOWrapper.seek.._reset_encoderztell on closed filez!underlying stream is not seekabler rz#can't do nonzero cur-relative seeksr z#can't do nonzero end-relative seeksrzunsupported whence (%r)znegative seek position %rrz#can't restore logical file position)rdr rrOrWrZr=rTr*rrrr?r(rrorrrr+r) rCZcookierVrDr#rBr1r9r:r;r2r>)rCr?rTs\                         zTextIOWrapper.seekcCs(|j|dkrd}|jp.|j}y |jWn4tk rr}ztd|WYdd}~XnX|dkr|j|j|jj dd}|j dd|_ |Sd}|j|}xGt ||kr| r|j }||j|t |7}qW|SdS) Nr zan integer is requiredrrTrFr)rarr(rr,rr,rr=ror*rrr4)rCrkrrr;r3r>r>r?ro3 s(   "     !zTextIOWrapper.readcCs:d|_|j}|s6d|_|j|_t|S)NF)rrrrrrt)rCrur>r>r?rvL s    zTextIOWrapper.__next__cCsn|jrtd|dkr*d }nt|tsEtd|j}d}|jsj|jd}}x|jr|j d|}|dkr|d}Pqt |}n|j r}|j d|}|j d|}|d kr|d krt |}qz|d}Pq|d kr7|d}Pq||krQ|d}Pq||dkro|d}Pq|d}Pn2|j |j }|dkr|t |j }P|dkrt ||kr|}Px|j r|jrPqW|jr ||j7}qw|jdd|_|SqwW|dkrI||krI|}|jt |||d|S) Nzread from closed filer zsize must be an integerrrrr rrrrr)rdr rrrr,rr(rrhrrrr4rr*rr.)rCrkrustartrUendposZnlposZcrposr>r>r?rrU sp                           zTextIOWrapper.readlinecCs|jr|jjSdS)N)rr)rCr>r>r?r szTextIOWrapper.newlines)&rGrHrIrBr/rrr}r5r6r<r=r]r`rbrZr2rdrRr)r%r{r"r(r*r,r.r4r<r?rWrXrrTrorvrrrr>r>r>r?r0YsH  E             *  c K Xr0csveZdZdZddfddZddZdd Zed d Zed d Z ddZ S)StringIOzText I/O implementation using an in-memory buffer. The initial_value argument sets the value of object. The newline argument is like the one of TextIOWrapper's constructor. rrcstt|jtddddd||dkr@d|_|dk rt|tsytdjt |j |j ||j ddS) Nr5zutf-8r6 surrogatepassr7Fz*initial_value must be str or None, not {0}r) rrGrrrrrrrr rGr{rT)rCZ initial_valuer7)rQr>r?r s     zStringIO.__init__c Csj|j|jp|j}|j}|jz |j|jjddSWd|j|XdS)NrT) rZrr(rrrr=rr)rCrZ old_stater>r>r?r s    zStringIO.getvaluecCs tj|S)N)objectr)rCr>r>r?r szStringIO.__repr__cCsdS)Nr>)rCr>r>r?r6 szStringIO.errorscCsdS)Nr>)rCr>r>r?r5 szStringIO.encodingcCs|jddS)Nr)rS)rCr>r>r?r szStringIO.detach) rGrHrIrBrrrr}r6r5rr>r>)rQr?rG s  rG)6rBr'abcrrZarrayrsys_threadrrr Z _dummy_threadplatformZmsvcrtrriorrrr rrmaddr SEEK_DATAr&rr@rArJrOr,r+r ABCMetarPregisterr_ior$rrrr/r.rr-rrrr0rGr>r>r>r?sl         "      = giZIJTAU[