U kgk@s|dZddlZddlZddlZddlZddlZddlZddlmZ ej dkrXddl m Z ndZ ddlZddlmZmZmZmZdddhZeed reejeejd ZeZeed pejjZd7ddZddZz ejZWne k reZYnXGdddZ!GdddZ"z ej#Z#Wn(e k rHGddde$e%Z#YnXGdddej&dZ'ej'(e'Gddde'Z)ej)(e)ddl*m+Z+e)(e+Gdd d e'Z,ej,(e,Gd!d"d"e,Z-Gd#d$d$e,Z.Gd%d&d&e-Z/Gd'd(d(e-Z0Gd)d*d*e,Z1Gd+d,d,e0e/Z2Gd-d.d.e)Z+Gd/d0d0e'Z3ej3(e3Gd1d2d2ej4Z5Gd3d4d4e3Z6Gd5d6d6e6Z7dS)8z) Python implementation of the io module. N) allocate_lock>cygwinwin32)setmode)__all__SEEK_SETSEEK_CURSEEK_END SEEK_HOLEi ZgettotalrefcountrTc Cst|tst|}t|tttfs0td|t|tsFtd|t|ts\td||dk rzt|tsztd||dk rt|tstd|t|}|tdst|t|krt d|d|k} d |k} d |k} d |k} d |k} d |k}d|k}d|krD| s"| s"| s"| r*t dddl }| dt dd} |rX|rXt d| | | | dkrvt d| s| s| s| st d|r|dk rt d|r|dk rt d|r|dk rt d|r|dkrddl }| dt dt|| rdpd| r"d p$d| r2d p4d| rBd pDd| rRd pTd||d}|}z$d}|dks|dkr|rd }d}|dkrt}zt|j}Wnttfk rYnX|dkr|}|dkrt d!|dkr|r|WSt d"| r t||}n<| s2| s2| r>t||}n| rPt||}n t d#||}|rl|WSt|||||}|}||_|WS|YnXdS)$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: %rNinvalid encoding: %rinvalid errors: %rzaxrwb+tUxr wa+tbUz4mode U cannot be combined with 'x', 'w', 'a', or '+'rz'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 argumentzaline buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used)openerFrzinvalid buffering sizezcan't have unbuffered text I/Ozunknown mode: %r) isinstanceintosfspathstrbytes TypeErrorsetlen ValueErrorwarningswarnDeprecationWarningRuntimeWarningFileIOisattyDEFAULT_BUFFER_SIZEfstatfileno st_blksizeOSErrorAttributeErrorBufferedRandomBufferedWriterBufferedReader TextIOWrappermodeclose)filer4 bufferingencodingerrorsnewlineclosefdrZmodesZcreatingZreadingZwritingZ appendingZupdatingtextZbinaryr$rawresultline_bufferingZbsbufferrA/usr/lib/python3.8/_pyio.pyopen)s{                    rCcCs ddl}|dtdt|dS)azOpens the provided file with mode ``'rb'``. This function should be used when the intent is to treat the contents as executable code. ``path`` should be an absolute path. When supported by the runtime, this function can be hooked in order to allow embedders more control over code files. This functionality is not supported on the current runtime. rNz(_pyio.open_code() may not be using hooksr rb)r$r%r'rC)pathr$rArArB_open_code_with_warnings rFc@seZdZdZdddZdS) DocDescriptorz%Helper for builtins.open.__doc__ NcCs dtjS)Nz\open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True) )rC__doc__)selfobjtyprArArB__get__szDocDescriptor.__get__)N)__name__ __module__ __qualname__rHrLrArArArBrGsrGc@seZdZdZeZddZdS) 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||SN)rC)clsargskwargsrArArB__new__,szOpenWrapper.__new__N)rMrNrOrHrGrUrArArArBrP"srPc@s eZdZdS)UnsupportedOperationN)rMrNrOrArArArBrV5srVc@seZdZdZddZd6ddZddZd7d d Zd d ZdZ ddZ ddZ ddZ d8ddZ ddZd9ddZddZd:ddZedd Zd;d!d"Zd#d$Zd%d&Zd'd(Zd)d*ZdIOBaseaThe 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 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. 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!') cCstd|jj|fdS)z@Internal: raise an OSError exception for unsupported operations.z%s.%s() not supportedN)rV __class__rM)rInamerArArB _unsupported\s zIOBase._unsupportedrcCs|ddS)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. seekNrZrIposwhencerArArBr[csz IOBase.seekcCs |ddS)z5Return an int indicating the current stream position.rr )r[rIrArArBtellssz IOBase.tellNcCs|ddS)zTruncate file to size bytes. Size defaults to the current IO position as reported by tell(). Return the new size. truncateNr\rIr^rArArBrbwszIOBase.truncatecCs |dS)zuFlush write buffers, if applicable. This is not implemented for read-only and non-blocking streams. N _checkClosedr`rArArBflushsz IOBase.flushFcCs |jsz |W5d|_XdS)ziFlush and close the IO object. This method has no effect if the file is already closed. TN)_IOBase__closedrfr`rArArBr5s z IOBase.closecCsVz |j}Wntk r YdSX|r*dStr8|nz |Wn YnXdS)zDestructor. Calls close().N)closedr/_IOBASE_EMITS_UNRAISABLEr5)rIrhrArArB__del__s   zIOBase.__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(). FrAr`rArArBseekableszIOBase.seekablecCs |st|dkrdn|dS)zEInternal: raise UnsupportedOperation if file is not seekable NzFile or stream is not seekable.)rkrVrImsgrArArB_checkSeekables zIOBase._checkSeekablecCsdS)zvReturn a bool indicating whether object was opened for reading. If False, read() will raise OSError. FrAr`rArArBreadableszIOBase.readablecCs |st|dkrdn|dS)zEInternal: raise UnsupportedOperation if file is not readable NzFile or stream is not readable.)rorVrlrArArB_checkReadables zIOBase._checkReadablecCsdS)zReturn a bool indicating whether object was opened for writing. If False, write() and truncate() will raise OSError. FrAr`rArArBwritableszIOBase.writablecCs |st|dkrdn|dS)zEInternal: raise UnsupportedOperation if file is not writable NzFile or stream is not writable.)rqrVrlrArArB_checkWritables zIOBase._checkWritablecCs|jS)zclosed: bool. True iff the file has been closed. For backwards compatibility, this is a property, not a predicate. )rgr`rArArBrhsz IOBase.closedcCs|jrt|dkrdn|dS)z7Internal: raise a ValueError if file is closed NI/O operation on closed file.rhr#rlrArArBres zIOBase._checkClosedcCs ||S)zCContext management protocol. Returns self (an instance of IOBase).rdr`rArArB __enter__szIOBase.__enter__cGs |dS)z+Context management protocol. Calls close()N)r5)rIrSrArArB__exit__szIOBase.__exit__cCs|ddS)zReturns underlying file descriptor (an int) if one exists. An OSError is raised if the IO object does not use a file descriptor. r,Nr\r`rArArBr,sz IOBase.filenocCs |dS)z{Return a bool indicating whether this is an 'interactive' stream. Return False if it can't be determined. Frdr`rArArBr)sz IOBase.isattyrcstdrfdd}ndd}dkr0dn4z j}Wn"tk r\tdYnX|t}dks~t|kr|}|sq||7}|d rjqqjt|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. peekcs>d}|sdS|ddp&t|}dkr:t|}|S)Nr  r)rwfindr"min)Z readaheadnrIsizerArB nreadaheads  z#IOBase.readline..nreadaheadcSsdSNr rArArArArBr~ sNr is not an integerrrx) hasattr __index__r/r bytearrayr"readendswithr)rIr}r~ size_indexresrrAr|rBreadline s&     zIOBase.readlinecCs ||SrQrdr`rArArB__iter__5szIOBase.__iter__cCs|}|st|SrQ)r StopIterationrIlinerArArB__next__9szIOBase.__next__cCsP|dks|dkrt|Sd}g}|D]&}|||t|7}||kr$qLq$|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")rIZhintr{linesrrArArB readlines?s  zIOBase.readlinescCs ||D]}||q dS)zWrite a list of lines to the stream. Line separators are not added, so it is usual for each of the lines provided to have a line separator at the end. N)rewrite)rIrrrArArB writelinesQszIOBase.writelines)r)N)N)N)N)N)r)N)rMrNrOrHrZr[rarbrfrgr5rjrkrnrorprqrrpropertyrhrerurvr,r)rrrrrrArArArBrW9s6!         * rW) metaclassc@s2eZdZdZd ddZddZddZd d Zd S) RawIOBasezBase class for raw binary I/O.rcCsP|dkr d}|dkr|St|}||}|dkr>dS||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. Nrr)readallrrreadintor)rIr}rr{rArArBrls   zRawIOBase.readcCs4t}|t}|sq ||7}q|r,t|S|SdS)z+Read until EOF, using multiple read() call.N)rrr*r)rIrdatarArArBr}s  zRawIOBase.readallcCs|ddS)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. rNr\rIrrArArBrszRawIOBase.readintocCs|ddS)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. rNr\rrArArBrszRawIOBase.writeN)r)rMrNrOrHrrrrrArArArBr^s  r)r(c@sLeZdZdZdddZdddZddZd 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. rcCs|ddS)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. rNr\r|rArArBrszBufferedIOBase.readcCs|ddS)zaRead up to size bytes with at most one read() system call, where size is an int. read1Nr\r|rArArBrszBufferedIOBase.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. Fr _readintorrArArBrs 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. TrrrrArArB readinto1s zBufferedIOBase.readinto1cCsVt|tst|}|d}|r0|t|}n|t|}t|}||d|<|S)NB)r memoryviewcastrr"r)rIrrrr{rArArBrs   zBufferedIOBase._readintocCs|ddS)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. rNr\rrArArBrs zBufferedIOBase.writecCs|ddS)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. detachNr\r`rArArBrszBufferedIOBase.detachN)r)r) rMrNrOrHrrrrrrrrArArArBrs    rc@seZdZdZddZd$ddZddZd%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 ||_dSrQ_rawrIr=rArArB__init__sz_BufferedIOMixin.__init__rcCs"|j||}|dkrtd|S)Nrz#seek() returned an invalid position)r=r[r.)rIr^r_Z new_positionrArArBr[sz_BufferedIOMixin.seekcCs|j}|dkrtd|S)Nrz#tell() returned an invalid position)r=rar.rcrArArBras z_BufferedIOMixin.tellNcCs$||dkr|}|j|SrQ)rfrar=rbrcrArArBrb$sz_BufferedIOMixin.truncatecCs|jrtd|jdS)Nflush on closed file)rhr#r=rfr`rArArBrf2sz_BufferedIOMixin.flushcCs.|jdk r*|js*z |W5|jXdSrQ)r=rhr5rfr`rArArBr57s z_BufferedIOMixin.closecCs*|jdkrtd||j}d|_|S)Nzraw stream already detached)r=r#rfrrrArArBr?s  z_BufferedIOMixin.detachcCs |jSrQ)r=rkr`rArArBrkIsz_BufferedIOMixin.seekablecCs|jSrQrr`rArArBr=Lsz_BufferedIOMixin.rawcCs|jjSrQ)r=rhr`rArArBrhPsz_BufferedIOMixin.closedcCs|jjSrQ)r=rYr`rArArBrYTsz_BufferedIOMixin.namecCs|jjSrQ)r=r4r`rArArBr4Xsz_BufferedIOMixin.modecCstd|jjddSNzcannot pickle z objectr rXrMr`rArArB __getstate__\sz_BufferedIOMixin.__getstate__cCsN|jj}|jj}z |j}Wn tk r:d||YSXd|||SdS)Nz<{}.{}>z<{}.{} name={!r}>)rXrNrOrYr/format)rImodnameZclsnamerYrArArB__repr___s z_BufferedIOMixin.__repr__cCs |jSrQ)r=r,r`rArArBr,ksz_BufferedIOMixin.filenocCs |jSrQ)r=r)r`rArArBr)nsz_BufferedIOMixin.isatty)r)N)rMrNrOrHrr[rarbrfr5rrkrr=rhrYr4rrr,r)rArArArBr s*        rcseZdZdZdZd!ddZddZddZd d Zfd d Z d"ddZ d#ddZ ddZ d$ddZ ddZd%ddZddZddZdd ZZS)&BytesIOzsz"FileIO.__init__..r rzKMust have exactly one of create/read/write/append mode and at most one plusrTr rrO_BINARYZ O_NOINHERIT O_CLOEXECNz'Cannot use closefd=False with file nameizexpected integer from openerzNegative file descriptorFr-)-_fd_closefdrr5rfloatr rr#rr!sumcount_created _writableO_EXCLO_CREAT _readableO_TRUNC _appendingO_APPENDO_RDWRO_RDONLYO_WRONLYgetattrrCr.set_inheritabler+statS_ISDIRst_modeIsADirectoryErrorrZEISDIRrr/_blksizer*_setmoderrYlseekr ZESPIPE) rIr6r4r;rfdflagsZnoinherit_flagZowned_fdZfdfstatrrArArBrs     $                    zFileIO.__init__cCsB|jdkr>|jr>|js>ddl}|jd|ftd|d|dS)Nrzunclosed file %rr ) stacklevelsource)rrrhr$r%ResourceWarningr5)rIr$rArArBrjAszFileIO.__del__cCstd|jjddSrrr`rArArBrHszFileIO.__getstate__cCspd|jj|jjf}|jr"d|Sz |j}Wn*tk rVd||j|j|jfYSXd|||j|jfSdS)Nz%s.%sz <%s [closed]>z<%s fd=%d mode=%r closefd=%r>z<%s name=%r mode=%r closefd=%r>) rXrNrOrhrYr/rr4r)rI class_namerYrArArBrKs  zFileIO.__repr__cCs|jstddS)NzFile not open for reading)rrVr`rArArBrpYszFileIO._checkReadablecCs|jstddS)NzFile not open for writing)rrVrlrArArBrr]szFileIO._checkWritablecCsT|||dks |dkr(|Szt|j|WStk rNYdSXdS)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)rerprrrrrr|rArArBrasz FileIO.readcCs||t}z6t|jdt}t|jj}||krH||d}Wnt k r^YnXt }t ||krt |}|t |t7}|t |}zt |j|}Wntk r|rYqYdSX|sq||7}qft|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)rerpr*rrrrr+st_sizer.rr"rrrr)rIbufsizer^endr>r{rrArArBrqs2   zFileIO.readallcCs4t|d}|t|}t|}||d|<|S)zSame as RawIOBase.readinto().rN)rrrr")rIrmrr{rArArBrs  zFileIO.readintocCs<||zt|j|WStk r6YdSXdS)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)rerrrrrrrrArArBrs z FileIO.writecCs*t|trtd|t|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)rrr rerrrr]rArArBr[s z FileIO.seekcCs|t|jdtS)zYtell() -> int. Current file position. Can raise OSError for non seekable files.r)rerrrrr`rArArBrasz FileIO.tellcCs2|||dkr |}t|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)rerrrar ftruncaterr|rArArBrbs zFileIO.truncatecs.|js*z|jrt|jW5tXdS)zClose the file. A closed file cannot be used for further I/O operations. close() may be called more than once without error. N)rhrr5rrrr`rrArBr5s z FileIO.closecCsF||jdkr@z |Wntk r8d|_YnXd|_|jS)z$True if file supports random-access.NFT)re _seekablerar.r`rArArBrks   zFileIO.seekablecCs||jS)z'True if file was opened in a read mode.)rerr`rArArBroszFileIO.readablecCs||jS)z(True if file was opened in a write mode.)rerr`rArArBrqszFileIO.writablecCs||jS)z3Return the underlying file descriptor (an integer).)rerr`rArArBr,sz FileIO.filenocCs|t|jS)z.True if the file is connected to a TTY device.)rerr)rr`rArArBr)sz FileIO.isattycCs|jS)z6True if the file descriptor will be closed by close().)rr`rArArBr;szFileIO.closefdcCsJ|jr|jrdSdSn0|jr,|jr&dSdSn|jrB|jrZ#d?d@Z$dSdAdBZ%dCdDZ&dTdEdFZ'dUdGdHZ(dIdJZ)dVdKdLZ*e dMdNZ+dS)Wr3aCharacter 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|||dkrvzt|}Wnttfk r<YnX|dkrvz ddl}Wntk rjd}Yn X|d}t |t st d|t |jsd}t|||dkrd}nt |t st d|||_d|_d|_d|_|j|_|_t|jd |_||||||dS) NrasciiFrzG%r is not a text encoding; use codecs.open() to handle arbitrary codecsrrrr)_check_newlinerdevice_encodingr,r/rVlocale ImportErrorZgetpreferredencodingrrr#rlookup_is_text_encoding LookupErrorr_decoded_chars_decoded_chars_used _snapshotr@rkr_tellingr _has_read1 _configure) rIr@r8r9r:r? write_throughr4rmrArArBrs>           zTextIOWrapper.__init__cCs>|dk r$t|ts$tdt|f|dkr:td|fdS)Nzillegal newline type: %r)Nrr"r r!zillegal newline value: %r)rrr typer#)rIr:rArArBr2szTextIOWrapper._check_newlinecCs||_||_d|_d|_d|_| |_|dk|_||_|dk|_|pHt j |_ ||_ ||_ |jr|r|j}|dkrz|dWntk rYnXdS)Nrr) _encoding_errors_encoder_decoder _b2cratio_readuniversal_readtranslate_readnl_writetranslaterlinesep_writenl_line_buffering_write_throughrrqr@ra _get_encoderr-r8)rIr8r9r:r?r?positionrArArBr>s&    zTextIOWrapper._configurecCsd|jj|jj}z |j}Wntk r2YnX|d|7}z |j}Wntk r`YnX|d|7}|d|jS)Nz<{}.{}z name={0!r}z mode={0!r}z encoding={0!r}>)rrXrNrOrYr/r4r8)rIr>rYr4rArArBr!s   zTextIOWrapper.__repr__cCs|jSrQ)rBr`rArArBr82szTextIOWrapper.encodingcCs|jSrQ)rCr`rArArBr96szTextIOWrapper.errorscCs|jSrQ)rMr`rArArBr?:szTextIOWrapper.line_bufferingcCs|jSrQ)rNr`rArArBr?>szTextIOWrapper.write_throughcCs|jSrQ)rr`rArArBr@BszTextIOWrapper.buffer)r8r9r:r?r?cCs|jdk r*|dk s"|dk s"|tk r*td|dkrH|dkrB|j}q^d}nt|ts^td||dkrn|j}nt|tstd||tkr|j}| ||dkr|j }|dkr|j }| | |||||dS)z`Reconfigure the text stream with new parameters. This also flushes the stream. NzPIt is not possible to set the encoding or newline of stream after the first readrrr)rEEllipsisrVrCrrr rBrIr2r?r?rfr>)rIr8r9r:r?r?rArArB reconfigureFs@       zTextIOWrapper.reconfigurecCs|jrtd|jS)Nrs)rhr#rr`rArArBrkoszTextIOWrapper.seekablecCs |jSrQ)r@ror`rArArBrotszTextIOWrapper.readablecCs |jSrQ)r@rqr`rArArBrqwszTextIOWrapper.writablecCs|j|j|_dSrQ)r@rfrr<r`rArArBrfzs zTextIOWrapper.flushcCs.|jdk r*|js*z |W5|jXdSrQ)r@rhr5rfr`rArArBr5~s zTextIOWrapper.closecCs|jjSrQ)r@rhr`rArArBrhszTextIOWrapper.closedcCs|jjSrQ)r@rYr`rArArBrYszTextIOWrapper.namecCs |jSrQ)r@r,r`rArArBr,szTextIOWrapper.filenocCs |jSrQ)r@r)r`rArArBr)szTextIOWrapper.isattycCs|jrtdt|ts(td|jjt|}|js<|j oBd|k}|rf|jrf|j dkrf| d|j }|j pr| }||}|j||j r|sd|kr||dd|_|jr|j|S)zWrite data, where s is a strrzcan't write %s to text streamr"r rN)rhr#rrr rXrMr"rJrMrLr'rDrOencoder@rrf_set_decoded_charsr;rEr/)rIrZlengthZhaslfencoderrrArArBrs(     zTextIOWrapper.writecCst|j}||j|_|jSrQ)rgetincrementalencoderrBrCrD)rIZ make_encoderrArArBrOs  zTextIOWrapper._get_encodercCs2t|j}||j}|jr(t||j}||_|SrQ)rgetincrementaldecoderrBrCrGrrHrE)rIZ make_decoderrrArArB _get_decoders    zTextIOWrapper._get_decodercCs||_d|_dS)zSet the _decoded_chars buffer.rN)r9r:)rIcharsrArArBrTsz TextIOWrapper._set_decoded_charscCsF|j}|dkr|j|d}n|j|||}|jt|7_|S)z'Advance into the _decoded_chars buffer.N)r:r9r")rIr{offsetrYrArArB_get_decoded_charss z TextIOWrapper._get_decoded_charscCs$|j|krtd|j|8_dS)z!Rewind the _decoded_chars buffer.z"rewind decoded_chars out of boundsN)r:AssertionErrorrrArArB_rewind_decoded_charss z#TextIOWrapper._rewind_decoded_charscCs|jdkrtd|jr&|j\}}|jr<|j|j}n|j|j}| }|j ||}| ||rt |t |j |_ nd|_ |jr|||f|_| S)zQ Read and decode the next chunk of data from the BufferedReader. Nz no decoderrA)rEr#r<r*r=r@r _CHUNK_SIZErr#rTr"r9rFr;)rI dec_buffer dec_flags input_chunkeofZ decoded_charsrArArB _read_chunks  zTextIOWrapper._read_chunkrcCs(||d>B|d>B|d>Bt|d>BS)N@)r,)rIrPr` bytes_to_feedneed_eof chars_to_skiprArArB _pack_cookies  zTextIOWrapper._pack_cookiecCsFt|d\}}t|d\}}t|d\}}t|d\}}|||||fS)Nl)divmod)rIZbigintrestrPr`rhrirjrArArB_unpack_cookie s zTextIOWrapper._unpack_cookiec CsR|jstd|jstd||j}|j}|dksF|jdkrX|j rTt d|S|j\}}|t |8}|j }|dkr| ||S|}zt|j|}d}|t |kst |dkr4|d|ft ||d|} | |kr"|\} } | s| }|| 8}qF|t | 8}d}q||8}|d}qd}|d|f||} |} |dkrl| | | WSd}d}d}t|t |D]x}|d7}|t ||||d7}|\}}|s||kr| |7} ||8}|dd} }}||krq,q|t |jddd 7}d}||kr,td | | | |||WS||XdS) N!underlying stream is not seekablez(telling position disabled by next() callzpending decoded textrr rr Trz'can't reconstruct logical file position)rrVr<r.rfr@rarEr;r9r\r"r:rkr*r-rrFr#range)rIrPrr`Z next_inputrjZ saved_stateZ skip_bytesZ skip_backr{rd start_posZ start_flagsZ bytes_fedriZ chars_decodedir_rArArBra s              zTextIOWrapper.tellcCs$||dkr|}|j|SrQ)rfrar@rbrcrArArBrbm szTextIOWrapper.truncatecCs*|jdkrtd||j}d|_|S)Nzbuffer is already detached)r@r#rfr)rIr@rArArBrs s  zTextIOWrapper.detachc sfdd}jrtdjs(td|tkrN|dkr@tdd}}nZ|tkr|dkrftdj d|} dd_ j rj |||S|dkrtd |f|dkrtd |f|\}}}}} j | dd_ |dkr*j r*j n@j s>|s>| rjj pL_ j d |f|d f_ | rj|} j | ||| f_ tj| krtd | _|||S) NcsHzjp}Wntk r&YnX|dkr<|dn|dS)z9Reset the encoder (merely useful for proper BOM handling)rN)rDrOr8r-r/)rPrUr`rArB_reset_encoder| s z*TextIOWrapper.seek.._reset_encoderrrorz#can't do nonzero cur-relative seeksz#can't do nonzero end-relative seeksrzunsupported whence (%r)rrz#can't restore logical file position)rhr#rrVrrar rfr@r[rTr;rEr/rnrXr-rr#r"r9r.r:) rIZcookier_rtrPrrr`rhrirjrarAr`rBr[{ s`            zTextIOWrapper.seekcCs||dkrd}n4z |j}Wn"tk rBt|dYnX|}|jpV|}|dkr||j|j dd}| dd|_ |Sd}||}t ||kr|s| }|||t |7}q|SdS)NrrrTrrF)rprr/r rErXr[r#r@rrTr;r"rc)rIr}rrr>rbrArArBr s,    zTextIOWrapper.readcCs(d|_|}|s$d|_|j|_t|S)NF)r<rr;rrrrArArBr szTextIOWrapper.__next__c Cs |jrtd|dkrd}n4z |j}Wn"tk rHt|dYnX|}|}d}|jsj|d}}|jr| d|}|dkr|d}qnt |}n|j rF| d|}| d|}|dkr|dkrt |}n |d}qnX|dkr|d}qn@||kr|d}qn(||dkr8|d}qn |d}qn(| |j }|dkrn|t |j }q|dkrt ||kr|}q| r|jrqq|jr||7}qr|d d|_|Sqr|dkr||kr|}|t |||d|S) Nrrrrr"r r r r)rhr#rr/r r[rErXrHryr"rGrIrcr9rTr;r]) rIr}rrstartr^endposZnlposZcrposrArArBr st            zTextIOWrapper.readlinecCs|jr|jjSdSrQ)rErr`rArArBrH szTextIOWrapper.newlines)NNNFF)NNNFF)N)rrrr)N)r)N)N),rMrNrOrHr^rrr2r>rrr8r9r?r?r@rQrRrkrorqrfr5rhrYr,r)rrOrXrTr[r]rcrkrnrarbrr[rrrrrArArArBr3s| ( $      )    * c  K  ]r3csReZdZdZdfdd ZddZdd Zed d Zed d Z ddZ Z 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. rr"csftt|jtdd|d|dkr(d|_|dk rbt|tsNtdt |j | || ddS)Nzutf-8 surrogatepass)r8r9r:Fz*initial_value must be str or None, not {0}r) rrwrrrJrrr rr@rMrr[)rIZ initial_valuer:rrArBrT s  zStringIO.__init__c CsP||jp|}|}|z|j|jddWS||XdS)NTr) rfrErXr*r/r-r#r@r)rIrZ old_staterArArBrd szStringIO.getvaluecCs t|SrQ)objectrr`rArArBrn szStringIO.__repr__cCsdSrQrAr`rArArBr9s szStringIO.errorscCsdSrQrAr`rArArBr8w szStringIO.encodingcCs|ddS)Nrr\r`rArArBr{ szStringIO.detach)rr") rMrNrOrHrrrrr9r8rrrArArrBrwM s   rw)r rNNNTN)8rHrabcrrrsys_threadrrplatformZmsvcrtrriorrrr rraddr SEEK_DATAr*rrdev_moderirCrF open_coder/rGrPrVr.r#ABCMetarWregisterr_ior(rrrr2r1rr0rrrr3rwrArArArBs       [    $ =   g hCiIJY@ U$