Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../proc/self/root/lib64/python3....
File: _pyio.py
"""
[0] Fix | Delete
Python implementation of the io module.
[1] Fix | Delete
"""
[2] Fix | Delete
[3] Fix | Delete
import os
[4] Fix | Delete
import abc
[5] Fix | Delete
import codecs
[6] Fix | Delete
import errno
[7] Fix | Delete
import stat
[8] Fix | Delete
import sys
[9] Fix | Delete
# Import _thread instead of threading to reduce startup cost
[10] Fix | Delete
try:
[11] Fix | Delete
from _thread import allocate_lock as Lock
[12] Fix | Delete
except ImportError:
[13] Fix | Delete
from _dummy_thread import allocate_lock as Lock
[14] Fix | Delete
if sys.platform in {'win32', 'cygwin'}:
[15] Fix | Delete
from msvcrt import setmode as _setmode
[16] Fix | Delete
else:
[17] Fix | Delete
_setmode = None
[18] Fix | Delete
[19] Fix | Delete
import io
[20] Fix | Delete
from io import (__all__, SEEK_SET, SEEK_CUR, SEEK_END)
[21] Fix | Delete
[22] Fix | Delete
valid_seek_flags = {0, 1, 2} # Hardwired values
[23] Fix | Delete
if hasattr(os, 'SEEK_HOLE') :
[24] Fix | Delete
valid_seek_flags.add(os.SEEK_HOLE)
[25] Fix | Delete
valid_seek_flags.add(os.SEEK_DATA)
[26] Fix | Delete
[27] Fix | Delete
# open() uses st_blksize whenever we can
[28] Fix | Delete
DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes
[29] Fix | Delete
[30] Fix | Delete
# NOTE: Base classes defined here are registered with the "official" ABCs
[31] Fix | Delete
# defined in io.py. We don't use real inheritance though, because we don't want
[32] Fix | Delete
# to inherit the C implementations.
[33] Fix | Delete
[34] Fix | Delete
# Rebind for compatibility
[35] Fix | Delete
BlockingIOError = BlockingIOError
[36] Fix | Delete
[37] Fix | Delete
[38] Fix | Delete
def open(file, mode="r", buffering=-1, encoding=None, errors=None,
[39] Fix | Delete
newline=None, closefd=True, opener=None):
[40] Fix | Delete
[41] Fix | Delete
r"""Open file and return a stream. Raise OSError upon failure.
[42] Fix | Delete
[43] Fix | Delete
file is either a text or byte string giving the name (and the path
[44] Fix | Delete
if the file isn't in the current working directory) of the file to
[45] Fix | Delete
be opened or an integer file descriptor of the file to be
[46] Fix | Delete
wrapped. (If a file descriptor is given, it is closed when the
[47] Fix | Delete
returned I/O object is closed, unless closefd is set to False.)
[48] Fix | Delete
[49] Fix | Delete
mode is an optional string that specifies the mode in which the file is
[50] Fix | Delete
opened. It defaults to 'r' which means open for reading in text mode. Other
[51] Fix | Delete
common values are 'w' for writing (truncating the file if it already
[52] Fix | Delete
exists), 'x' for exclusive creation of a new file, and 'a' for appending
[53] Fix | Delete
(which on some Unix systems, means that all writes append to the end of the
[54] Fix | Delete
file regardless of the current seek position). In text mode, if encoding is
[55] Fix | Delete
not specified the encoding used is platform dependent. (For reading and
[56] Fix | Delete
writing raw bytes use binary mode and leave encoding unspecified.) The
[57] Fix | Delete
available modes are:
[58] Fix | Delete
[59] Fix | Delete
========= ===============================================================
[60] Fix | Delete
Character Meaning
[61] Fix | Delete
--------- ---------------------------------------------------------------
[62] Fix | Delete
'r' open for reading (default)
[63] Fix | Delete
'w' open for writing, truncating the file first
[64] Fix | Delete
'x' create a new file and open it for writing
[65] Fix | Delete
'a' open for writing, appending to the end of the file if it exists
[66] Fix | Delete
'b' binary mode
[67] Fix | Delete
't' text mode (default)
[68] Fix | Delete
'+' open a disk file for updating (reading and writing)
[69] Fix | Delete
'U' universal newline mode (deprecated)
[70] Fix | Delete
========= ===============================================================
[71] Fix | Delete
[72] Fix | Delete
The default mode is 'rt' (open for reading text). For binary random
[73] Fix | Delete
access, the mode 'w+b' opens and truncates the file to 0 bytes, while
[74] Fix | Delete
'r+b' opens the file without truncation. The 'x' mode implies 'w' and
[75] Fix | Delete
raises an `FileExistsError` if the file already exists.
[76] Fix | Delete
[77] Fix | Delete
Python distinguishes between files opened in binary and text modes,
[78] Fix | Delete
even when the underlying operating system doesn't. Files opened in
[79] Fix | Delete
binary mode (appending 'b' to the mode argument) return contents as
[80] Fix | Delete
bytes objects without any decoding. In text mode (the default, or when
[81] Fix | Delete
't' is appended to the mode argument), the contents of the file are
[82] Fix | Delete
returned as strings, the bytes having been first decoded using a
[83] Fix | Delete
platform-dependent encoding or using the specified encoding if given.
[84] Fix | Delete
[85] Fix | Delete
'U' mode is deprecated and will raise an exception in future versions
[86] Fix | Delete
of Python. It has no effect in Python 3. Use newline to control
[87] Fix | Delete
universal newlines mode.
[88] Fix | Delete
[89] Fix | Delete
buffering is an optional integer used to set the buffering policy.
[90] Fix | Delete
Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
[91] Fix | Delete
line buffering (only usable in text mode), and an integer > 1 to indicate
[92] Fix | Delete
the size of a fixed-size chunk buffer. When no buffering argument is
[93] Fix | Delete
given, the default buffering policy works as follows:
[94] Fix | Delete
[95] Fix | Delete
* Binary files are buffered in fixed-size chunks; the size of the buffer
[96] Fix | Delete
is chosen using a heuristic trying to determine the underlying device's
[97] Fix | Delete
"block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.
[98] Fix | Delete
On many systems, the buffer will typically be 4096 or 8192 bytes long.
[99] Fix | Delete
[100] Fix | Delete
* "Interactive" text files (files for which isatty() returns True)
[101] Fix | Delete
use line buffering. Other text files use the policy described above
[102] Fix | Delete
for binary files.
[103] Fix | Delete
[104] Fix | Delete
encoding is the str name of the encoding used to decode or encode the
[105] Fix | Delete
file. This should only be used in text mode. The default encoding is
[106] Fix | Delete
platform dependent, but any encoding supported by Python can be
[107] Fix | Delete
passed. See the codecs module for the list of supported encodings.
[108] Fix | Delete
[109] Fix | Delete
errors is an optional string that specifies how encoding errors are to
[110] Fix | Delete
be handled---this argument should not be used in binary mode. Pass
[111] Fix | Delete
'strict' to raise a ValueError exception if there is an encoding error
[112] Fix | Delete
(the default of None has the same effect), or pass 'ignore' to ignore
[113] Fix | Delete
errors. (Note that ignoring encoding errors can lead to data loss.)
[114] Fix | Delete
See the documentation for codecs.register for a list of the permitted
[115] Fix | Delete
encoding error strings.
[116] Fix | Delete
[117] Fix | Delete
newline is a string controlling how universal newlines works (it only
[118] Fix | Delete
applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works
[119] Fix | Delete
as follows:
[120] Fix | Delete
[121] Fix | Delete
* On input, if newline is None, universal newlines mode is
[122] Fix | Delete
enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
[123] Fix | Delete
these are translated into '\n' before being returned to the
[124] Fix | Delete
caller. If it is '', universal newline mode is enabled, but line
[125] Fix | Delete
endings are returned to the caller untranslated. If it has any of
[126] Fix | Delete
the other legal values, input lines are only terminated by the given
[127] Fix | Delete
string, and the line ending is returned to the caller untranslated.
[128] Fix | Delete
[129] Fix | Delete
* On output, if newline is None, any '\n' characters written are
[130] Fix | Delete
translated to the system default line separator, os.linesep. If
[131] Fix | Delete
newline is '', no translation takes place. If newline is any of the
[132] Fix | Delete
other legal values, any '\n' characters written are translated to
[133] Fix | Delete
the given string.
[134] Fix | Delete
[135] Fix | Delete
closedfd is a bool. If closefd is False, the underlying file descriptor will
[136] Fix | Delete
be kept open when the file is closed. This does not work when a file name is
[137] Fix | Delete
given and must be True in that case.
[138] Fix | Delete
[139] Fix | Delete
The newly created file is non-inheritable.
[140] Fix | Delete
[141] Fix | Delete
A custom opener can be used by passing a callable as *opener*. The
[142] Fix | Delete
underlying file descriptor for the file object is then obtained by calling
[143] Fix | Delete
*opener* with (*file*, *flags*). *opener* must return an open file
[144] Fix | Delete
descriptor (passing os.open as *opener* results in functionality similar to
[145] Fix | Delete
passing None).
[146] Fix | Delete
[147] Fix | Delete
open() returns a file object whose type depends on the mode, and
[148] Fix | Delete
through which the standard file operations such as reading and writing
[149] Fix | Delete
are performed. When open() is used to open a file in a text mode ('w',
[150] Fix | Delete
'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
[151] Fix | Delete
a file in a binary mode, the returned class varies: in read binary
[152] Fix | Delete
mode, it returns a BufferedReader; in write binary and append binary
[153] Fix | Delete
modes, it returns a BufferedWriter, and in read/write mode, it returns
[154] Fix | Delete
a BufferedRandom.
[155] Fix | Delete
[156] Fix | Delete
It is also possible to use a string or bytearray as a file for both
[157] Fix | Delete
reading and writing. For strings StringIO can be used like a file
[158] Fix | Delete
opened in a text mode, and for bytes a BytesIO can be used like a file
[159] Fix | Delete
opened in a binary mode.
[160] Fix | Delete
"""
[161] Fix | Delete
if not isinstance(file, int):
[162] Fix | Delete
file = os.fspath(file)
[163] Fix | Delete
if not isinstance(file, (str, bytes, int)):
[164] Fix | Delete
raise TypeError("invalid file: %r" % file)
[165] Fix | Delete
if not isinstance(mode, str):
[166] Fix | Delete
raise TypeError("invalid mode: %r" % mode)
[167] Fix | Delete
if not isinstance(buffering, int):
[168] Fix | Delete
raise TypeError("invalid buffering: %r" % buffering)
[169] Fix | Delete
if encoding is not None and not isinstance(encoding, str):
[170] Fix | Delete
raise TypeError("invalid encoding: %r" % encoding)
[171] Fix | Delete
if errors is not None and not isinstance(errors, str):
[172] Fix | Delete
raise TypeError("invalid errors: %r" % errors)
[173] Fix | Delete
modes = set(mode)
[174] Fix | Delete
if modes - set("axrwb+tU") or len(mode) > len(modes):
[175] Fix | Delete
raise ValueError("invalid mode: %r" % mode)
[176] Fix | Delete
creating = "x" in modes
[177] Fix | Delete
reading = "r" in modes
[178] Fix | Delete
writing = "w" in modes
[179] Fix | Delete
appending = "a" in modes
[180] Fix | Delete
updating = "+" in modes
[181] Fix | Delete
text = "t" in modes
[182] Fix | Delete
binary = "b" in modes
[183] Fix | Delete
if "U" in modes:
[184] Fix | Delete
if creating or writing or appending or updating:
[185] Fix | Delete
raise ValueError("mode U cannot be combined with 'x', 'w', 'a', or '+'")
[186] Fix | Delete
import warnings
[187] Fix | Delete
warnings.warn("'U' mode is deprecated",
[188] Fix | Delete
DeprecationWarning, 2)
[189] Fix | Delete
reading = True
[190] Fix | Delete
if text and binary:
[191] Fix | Delete
raise ValueError("can't have text and binary mode at once")
[192] Fix | Delete
if creating + reading + writing + appending > 1:
[193] Fix | Delete
raise ValueError("can't have read/write/append mode at once")
[194] Fix | Delete
if not (creating or reading or writing or appending):
[195] Fix | Delete
raise ValueError("must have exactly one of read/write/append mode")
[196] Fix | Delete
if binary and encoding is not None:
[197] Fix | Delete
raise ValueError("binary mode doesn't take an encoding argument")
[198] Fix | Delete
if binary and errors is not None:
[199] Fix | Delete
raise ValueError("binary mode doesn't take an errors argument")
[200] Fix | Delete
if binary and newline is not None:
[201] Fix | Delete
raise ValueError("binary mode doesn't take a newline argument")
[202] Fix | Delete
raw = FileIO(file,
[203] Fix | Delete
(creating and "x" or "") +
[204] Fix | Delete
(reading and "r" or "") +
[205] Fix | Delete
(writing and "w" or "") +
[206] Fix | Delete
(appending and "a" or "") +
[207] Fix | Delete
(updating and "+" or ""),
[208] Fix | Delete
closefd, opener=opener)
[209] Fix | Delete
result = raw
[210] Fix | Delete
try:
[211] Fix | Delete
line_buffering = False
[212] Fix | Delete
if buffering == 1 or buffering < 0 and raw.isatty():
[213] Fix | Delete
buffering = -1
[214] Fix | Delete
line_buffering = True
[215] Fix | Delete
if buffering < 0:
[216] Fix | Delete
buffering = DEFAULT_BUFFER_SIZE
[217] Fix | Delete
try:
[218] Fix | Delete
bs = os.fstat(raw.fileno()).st_blksize
[219] Fix | Delete
except (OSError, AttributeError):
[220] Fix | Delete
pass
[221] Fix | Delete
else:
[222] Fix | Delete
if bs > 1:
[223] Fix | Delete
buffering = bs
[224] Fix | Delete
if buffering < 0:
[225] Fix | Delete
raise ValueError("invalid buffering size")
[226] Fix | Delete
if buffering == 0:
[227] Fix | Delete
if binary:
[228] Fix | Delete
return result
[229] Fix | Delete
raise ValueError("can't have unbuffered text I/O")
[230] Fix | Delete
if updating:
[231] Fix | Delete
buffer = BufferedRandom(raw, buffering)
[232] Fix | Delete
elif creating or writing or appending:
[233] Fix | Delete
buffer = BufferedWriter(raw, buffering)
[234] Fix | Delete
elif reading:
[235] Fix | Delete
buffer = BufferedReader(raw, buffering)
[236] Fix | Delete
else:
[237] Fix | Delete
raise ValueError("unknown mode: %r" % mode)
[238] Fix | Delete
result = buffer
[239] Fix | Delete
if binary:
[240] Fix | Delete
return result
[241] Fix | Delete
text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering)
[242] Fix | Delete
result = text
[243] Fix | Delete
text.mode = mode
[244] Fix | Delete
return result
[245] Fix | Delete
except:
[246] Fix | Delete
result.close()
[247] Fix | Delete
raise
[248] Fix | Delete
[249] Fix | Delete
[250] Fix | Delete
class DocDescriptor:
[251] Fix | Delete
"""Helper for builtins.open.__doc__
[252] Fix | Delete
"""
[253] Fix | Delete
def __get__(self, obj, typ):
[254] Fix | Delete
return (
[255] Fix | Delete
"open(file, mode='r', buffering=-1, encoding=None, "
[256] Fix | Delete
"errors=None, newline=None, closefd=True)\n\n" +
[257] Fix | Delete
open.__doc__)
[258] Fix | Delete
[259] Fix | Delete
class OpenWrapper:
[260] Fix | Delete
"""Wrapper for builtins.open
[261] Fix | Delete
[262] Fix | Delete
Trick so that open won't become a bound method when stored
[263] Fix | Delete
as a class variable (as dbm.dumb does).
[264] Fix | Delete
[265] Fix | Delete
See initstdio() in Python/pylifecycle.c.
[266] Fix | Delete
"""
[267] Fix | Delete
__doc__ = DocDescriptor()
[268] Fix | Delete
[269] Fix | Delete
def __new__(cls, *args, **kwargs):
[270] Fix | Delete
return open(*args, **kwargs)
[271] Fix | Delete
[272] Fix | Delete
[273] Fix | Delete
# In normal operation, both `UnsupportedOperation`s should be bound to the
[274] Fix | Delete
# same object.
[275] Fix | Delete
try:
[276] Fix | Delete
UnsupportedOperation = io.UnsupportedOperation
[277] Fix | Delete
except AttributeError:
[278] Fix | Delete
class UnsupportedOperation(OSError, ValueError):
[279] Fix | Delete
pass
[280] Fix | Delete
[281] Fix | Delete
[282] Fix | Delete
class IOBase(metaclass=abc.ABCMeta):
[283] Fix | Delete
[284] Fix | Delete
"""The abstract base class for all I/O classes, acting on streams of
[285] Fix | Delete
bytes. There is no public constructor.
[286] Fix | Delete
[287] Fix | Delete
This class provides dummy implementations for many methods that
[288] Fix | Delete
derived classes can override selectively; the default implementations
[289] Fix | Delete
represent a file that cannot be read, written or seeked.
[290] Fix | Delete
[291] Fix | Delete
Even though IOBase does not declare read, readinto, or write because
[292] Fix | Delete
their signatures will vary, implementations and clients should
[293] Fix | Delete
consider those methods part of the interface. Also, implementations
[294] Fix | Delete
may raise UnsupportedOperation when operations they do not support are
[295] Fix | Delete
called.
[296] Fix | Delete
[297] Fix | Delete
The basic type used for binary data read from or written to a file is
[298] Fix | Delete
bytes. Other bytes-like objects are accepted as method arguments too. In
[299] Fix | Delete
some cases (such as readinto), a writable object is required. Text I/O
[300] Fix | Delete
classes work with str data.
[301] Fix | Delete
[302] Fix | Delete
Note that calling any method (even inquiries) on a closed stream is
[303] Fix | Delete
undefined. Implementations may raise OSError in this case.
[304] Fix | Delete
[305] Fix | Delete
IOBase (and its subclasses) support the iterator protocol, meaning
[306] Fix | Delete
that an IOBase object can be iterated over yielding the lines in a
[307] Fix | Delete
stream.
[308] Fix | Delete
[309] Fix | Delete
IOBase also supports the :keyword:`with` statement. In this example,
[310] Fix | Delete
fp is closed after the suite of the with statement is complete:
[311] Fix | Delete
[312] Fix | Delete
with open('spam.txt', 'r') as fp:
[313] Fix | Delete
fp.write('Spam and eggs!')
[314] Fix | Delete
"""
[315] Fix | Delete
[316] Fix | Delete
### Internal ###
[317] Fix | Delete
[318] Fix | Delete
def _unsupported(self, name):
[319] Fix | Delete
"""Internal: raise an OSError exception for unsupported operations."""
[320] Fix | Delete
raise UnsupportedOperation("%s.%s() not supported" %
[321] Fix | Delete
(self.__class__.__name__, name))
[322] Fix | Delete
[323] Fix | Delete
### Positioning ###
[324] Fix | Delete
[325] Fix | Delete
def seek(self, pos, whence=0):
[326] Fix | Delete
"""Change stream position.
[327] Fix | Delete
[328] Fix | Delete
Change the stream position to byte offset pos. Argument pos is
[329] Fix | Delete
interpreted relative to the position indicated by whence. Values
[330] Fix | Delete
for whence are ints:
[331] Fix | Delete
[332] Fix | Delete
* 0 -- start of stream (the default); offset should be zero or positive
[333] Fix | Delete
* 1 -- current stream position; offset may be negative
[334] Fix | Delete
* 2 -- end of stream; offset is usually negative
[335] Fix | Delete
Some operating systems / file systems could provide additional values.
[336] Fix | Delete
[337] Fix | Delete
Return an int indicating the new absolute position.
[338] Fix | Delete
"""
[339] Fix | Delete
self._unsupported("seek")
[340] Fix | Delete
[341] Fix | Delete
def tell(self):
[342] Fix | Delete
"""Return an int indicating the current stream position."""
[343] Fix | Delete
return self.seek(0, 1)
[344] Fix | Delete
[345] Fix | Delete
def truncate(self, pos=None):
[346] Fix | Delete
"""Truncate file to size bytes.
[347] Fix | Delete
[348] Fix | Delete
Size defaults to the current IO position as reported by tell(). Return
[349] Fix | Delete
the new size.
[350] Fix | Delete
"""
[351] Fix | Delete
self._unsupported("truncate")
[352] Fix | Delete
[353] Fix | Delete
### Flush and close ###
[354] Fix | Delete
[355] Fix | Delete
def flush(self):
[356] Fix | Delete
"""Flush write buffers, if applicable.
[357] Fix | Delete
[358] Fix | Delete
This is not implemented for read-only and non-blocking streams.
[359] Fix | Delete
"""
[360] Fix | Delete
self._checkClosed()
[361] Fix | Delete
# XXX Should this return the number of bytes written???
[362] Fix | Delete
[363] Fix | Delete
__closed = False
[364] Fix | Delete
[365] Fix | Delete
def close(self):
[366] Fix | Delete
"""Flush and close the IO object.
[367] Fix | Delete
[368] Fix | Delete
This method has no effect if the file is already closed.
[369] Fix | Delete
"""
[370] Fix | Delete
if not self.__closed:
[371] Fix | Delete
try:
[372] Fix | Delete
self.flush()
[373] Fix | Delete
finally:
[374] Fix | Delete
self.__closed = True
[375] Fix | Delete
[376] Fix | Delete
def __del__(self):
[377] Fix | Delete
"""Destructor. Calls close()."""
[378] Fix | Delete
# The try/except block is in case this is called at program
[379] Fix | Delete
# exit time, when it's possible that globals have already been
[380] Fix | Delete
# deleted, and then the close() call might fail. Since
[381] Fix | Delete
# there's nothing we can do about such failures and they annoy
[382] Fix | Delete
# the end users, we suppress the traceback.
[383] Fix | Delete
try:
[384] Fix | Delete
self.close()
[385] Fix | Delete
except:
[386] Fix | Delete
pass
[387] Fix | Delete
[388] Fix | Delete
### Inquiries ###
[389] Fix | Delete
[390] Fix | Delete
def seekable(self):
[391] Fix | Delete
"""Return a bool indicating whether object supports random access.
[392] Fix | Delete
[393] Fix | Delete
If False, seek(), tell() and truncate() will raise OSError.
[394] Fix | Delete
This method may need to do a test seek().
[395] Fix | Delete
"""
[396] Fix | Delete
return False
[397] Fix | Delete
[398] Fix | Delete
def _checkSeekable(self, msg=None):
[399] Fix | Delete
"""Internal: raise UnsupportedOperation if file is not seekable
[400] Fix | Delete
"""
[401] Fix | Delete
if not self.seekable():
[402] Fix | Delete
raise UnsupportedOperation("File or stream is not seekable."
[403] Fix | Delete
if msg is None else msg)
[404] Fix | Delete
[405] Fix | Delete
def readable(self):
[406] Fix | Delete
"""Return a bool indicating whether object was opened for reading.
[407] Fix | Delete
[408] Fix | Delete
If False, read() will raise OSError.
[409] Fix | Delete
"""
[410] Fix | Delete
return False
[411] Fix | Delete
[412] Fix | Delete
def _checkReadable(self, msg=None):
[413] Fix | Delete
"""Internal: raise UnsupportedOperation if file is not readable
[414] Fix | Delete
"""
[415] Fix | Delete
if not self.readable():
[416] Fix | Delete
raise UnsupportedOperation("File or stream is not readable."
[417] Fix | Delete
if msg is None else msg)
[418] Fix | Delete
[419] Fix | Delete
def writable(self):
[420] Fix | Delete
"""Return a bool indicating whether object was opened for writing.
[421] Fix | Delete
[422] Fix | Delete
If False, write() and truncate() will raise OSError.
[423] Fix | Delete
"""
[424] Fix | Delete
return False
[425] Fix | Delete
[426] Fix | Delete
def _checkWritable(self, msg=None):
[427] Fix | Delete
"""Internal: raise UnsupportedOperation if file is not writable
[428] Fix | Delete
"""
[429] Fix | Delete
if not self.writable():
[430] Fix | Delete
raise UnsupportedOperation("File or stream is not writable."
[431] Fix | Delete
if msg is None else msg)
[432] Fix | Delete
[433] Fix | Delete
@property
[434] Fix | Delete
def closed(self):
[435] Fix | Delete
"""closed: bool. True iff the file has been closed.
[436] Fix | Delete
[437] Fix | Delete
For backwards compatibility, this is a property, not a predicate.
[438] Fix | Delete
"""
[439] Fix | Delete
return self.__closed
[440] Fix | Delete
[441] Fix | Delete
def _checkClosed(self, msg=None):
[442] Fix | Delete
"""Internal: raise a ValueError if file is closed
[443] Fix | Delete
"""
[444] Fix | Delete
if self.closed:
[445] Fix | Delete
raise ValueError("I/O operation on closed file."
[446] Fix | Delete
if msg is None else msg)
[447] Fix | Delete
[448] Fix | Delete
### Context manager ###
[449] Fix | Delete
[450] Fix | Delete
def __enter__(self): # That's a forward reference
[451] Fix | Delete
"""Context management protocol. Returns self (an instance of IOBase)."""
[452] Fix | Delete
self._checkClosed()
[453] Fix | Delete
return self
[454] Fix | Delete
[455] Fix | Delete
def __exit__(self, *args):
[456] Fix | Delete
"""Context management protocol. Calls close()"""
[457] Fix | Delete
self.close()
[458] Fix | Delete
[459] Fix | Delete
### Lower-level APIs ###
[460] Fix | Delete
[461] Fix | Delete
# XXX Should these be present even if unimplemented?
[462] Fix | Delete
[463] Fix | Delete
def fileno(self):
[464] Fix | Delete
"""Returns underlying file descriptor (an int) if one exists.
[465] Fix | Delete
[466] Fix | Delete
An OSError is raised if the IO object does not use a file descriptor.
[467] Fix | Delete
"""
[468] Fix | Delete
self._unsupported("fileno")
[469] Fix | Delete
[470] Fix | Delete
def isatty(self):
[471] Fix | Delete
"""Return a bool indicating whether this is an 'interactive' stream.
[472] Fix | Delete
[473] Fix | Delete
Return False if it can't be determined.
[474] Fix | Delete
"""
[475] Fix | Delete
self._checkClosed()
[476] Fix | Delete
return False
[477] Fix | Delete
[478] Fix | Delete
### Readline[s] and writelines ###
[479] Fix | Delete
[480] Fix | Delete
def readline(self, size=-1):
[481] Fix | Delete
r"""Read and return a line of bytes from the stream.
[482] Fix | Delete
[483] Fix | Delete
If size is specified, at most size bytes will be read.
[484] Fix | Delete
Size should be an int.
[485] Fix | Delete
[486] Fix | Delete
The line terminator is always b'\n' for binary files; for text
[487] Fix | Delete
files, the newlines argument to open can be used to select the line
[488] Fix | Delete
terminator(s) recognized.
[489] Fix | Delete
"""
[490] Fix | Delete
# For backwards compatibility, a (slowish) readline().
[491] Fix | Delete
if hasattr(self, "peek"):
[492] Fix | Delete
def nreadahead():
[493] Fix | Delete
readahead = self.peek(1)
[494] Fix | Delete
if not readahead:
[495] Fix | Delete
return 1
[496] Fix | Delete
n = (readahead.find(b"\n") + 1) or len(readahead)
[497] Fix | Delete
if size >= 0:
[498] Fix | Delete
n = min(n, size)
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function