File indexing completed on 2024-04-28 15:53:57
0001 #!/usr/bin/env python2.7 0002 # -*- coding: utf-8 -*- 0003 """:synopsis: Core tools for working with streams. 0004 """ 0005 """ 0006 An int containing the default buffer size used by the module's buffered I/O 0007 classes. :func:`.open` uses the file's blksize (as obtained by 0008 :func:`os.stat`) if possible. 0009 0010 """ 0011 DEFAULT_BUFFER_SIZE = None 0012 def open(file,mode='r',buffering=_1,encoding=None,errors=None,newline=None,closefd=True): 0013 """ 0014 Open *file* and return a corresponding stream. If the file cannot be opened, 0015 an :exc:`IOError` is raised. 0016 0017 *file* is either a string giving the pathname (absolute or 0018 relative to the current working directory) of the file to be opened or 0019 an integer file descriptor of the file to be wrapped. (If a file descriptor 0020 is given, it is closed when the returned I/O object is closed, unless 0021 *closefd* is set to ``False``.) 0022 0023 *mode* is an optional string that specifies the mode in which the file is 0024 opened. It defaults to ``'r'`` which means open for reading in text mode. 0025 Other common values are ``'w'`` for writing (truncating the file if it 0026 already exists), and ``'a'`` for appending (which on *some* Unix systems, 0027 means that *all* writes append to the end of the file regardless of the 0028 current seek position). In text mode, if *encoding* is not specified the 0029 encoding used is platform dependent. (For reading and writing raw bytes use 0030 binary mode and leave *encoding* unspecified.) The available modes are: 0031 0032 ========= =============================================================== 0033 Character Meaning 0034 --------- --------------------------------------------------------------- 0035 ``'r'`` open for reading (default) 0036 ``'w'`` open for writing, truncating the file first 0037 ``'a'`` open for writing, appending to the end of the file if it exists 0038 ``'b'`` binary mode 0039 ``'t'`` text mode (default) 0040 ``'+'`` open a disk file for updating (reading and writing) 0041 ``'U'`` universal newline mode (for backwards compatibility; should 0042 not be used in new code) 0043 ========= =============================================================== 0044 0045 The default mode is ``'rt'`` (open for reading text). For binary random 0046 access, the mode ``'w+b'`` opens and truncates the file to 0 bytes, while 0047 ``'r+b'`` opens the file without truncation. 0048 0049 Python distinguishes between files opened in binary and text modes, even when 0050 the underlying operating system doesn't. Files opened in binary mode 0051 (including ``'b'`` in the *mode* argument) return contents as :class:`bytes` 0052 objects without any decoding. In text mode (the default, or when ``'t'`` is 0053 included in the *mode* argument), the contents of the file are returned as 0054 :class:`unicode` strings, the bytes having been first decoded using a 0055 platform-dependent encoding or using the specified *encoding* if given. 0056 0057 *buffering* is an optional integer used to set the buffering policy. 0058 Pass 0 to switch buffering off (only allowed in binary mode), 1 to select 0059 line buffering (only usable in text mode), and an integer > 1 to indicate 0060 the size of a fixed-size chunk buffer. When no *buffering* argument is 0061 given, the default buffering policy works as follows: 0062 0063 * Binary files are buffered in fixed-size chunks; the size of the buffer 0064 is chosen using a heuristic trying to determine the underlying device's 0065 "block size" and falling back on :attr:`DEFAULT_BUFFER_SIZE`. 0066 On many systems, the buffer will typically be 4096 or 8192 bytes long. 0067 0068 * "Interactive" text files (files for which :meth:`isatty` returns True) 0069 use line buffering. Other text files use the policy described above 0070 for binary files. 0071 0072 *encoding* is the name of the encoding used to decode or encode the file. 0073 This should only be used in text mode. The default encoding is platform 0074 dependent (whatever :func:`locale.getpreferredencoding` returns), but any 0075 encoding supported by Python can be used. See the :mod:`codecs` module for 0076 the list of supported encodings. 0077 0078 *errors* is an optional string that specifies how encoding and decoding 0079 errors are to be handled--this cannot be used in binary mode. Pass 0080 ``'strict'`` to raise a :exc:`ValueError` exception if there is an encoding 0081 error (the default of ``None`` has the same effect), or pass ``'ignore'`` to 0082 ignore errors. (Note that ignoring encoding errors can lead to data loss.) 0083 ``'replace'`` causes a replacement marker (such as ``'?'``) to be inserted 0084 where there is malformed data. When writing, ``'xmlcharrefreplace'`` 0085 (replace with the appropriate XML character reference) or 0086 ``'backslashreplace'`` (replace with backslashed escape sequences) can be 0087 used. Any other error handling name that has been registered with 0088 :func:`codecs.register_error` is also valid. 0089 0090 *newline* controls how universal newlines works (it only applies to text 0091 mode). It can be ``None``, ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It 0092 works as follows: 0093 0094 * On input, if *newline* is ``None``, universal newlines mode is enabled. 0095 Lines in the input can end in ``'\n'``, ``'\r'``, or ``'\r\n'``, and these 0096 are translated into ``'\n'`` before being returned to the caller. If it is 0097 ``''``, universal newline mode is enabled, but line endings are returned to 0098 the caller untranslated. If it has any of the other legal values, input 0099 lines are only terminated by the given string, and the line ending is 0100 returned to the caller untranslated. 0101 0102 * On output, if *newline* is ``None``, any ``'\n'`` characters written are 0103 translated to the system default line separator, :data:`os.linesep`. If 0104 *newline* is ``''``, no translation takes place. If *newline* is any of 0105 the other legal values, any ``'\n'`` characters written are translated to 0106 the given string. 0107 0108 If *closefd* is ``False`` and a file descriptor rather than a filename was 0109 given, the underlying file descriptor will be kept open when the file is 0110 closed. If a filename is given *closefd* has no effect and must be ``True`` 0111 (the default). 0112 0113 The type of file object returned by the :func:`.open` function depends on the 0114 mode. When :func:`.open` is used to open a file in a text mode (``'w'``, 0115 ``'r'``, ``'wt'``, ``'rt'``, etc.), it returns a subclass of 0116 :class:`TextIOBase` (specifically :class:`TextIOWrapper`). When used to open 0117 a file in a binary mode with buffering, the returned class is a subclass of 0118 :class:`BufferedIOBase`. The exact class varies: in read binary mode, it 0119 returns a :class:`BufferedReader`; in write binary and append binary modes, 0120 it returns a :class:`BufferedWriter`, and in read/write mode, it returns a 0121 :class:`BufferedRandom`. When buffering is disabled, the raw stream, a 0122 subclass of :class:`RawIOBase`, :class:`FileIO`, is returned. 0123 0124 It is also possible to use an :class:`unicode` or :class:`bytes` string 0125 as a file for both reading and writing. For :class:`unicode` strings 0126 :class:`StringIO` can be used like a file opened in text mode, 0127 and for :class:`bytes` a :class:`BytesIO` can be used like a 0128 file opened in a binary mode. 0129 0130 0131 """ 0132 pass 0133 0134 class IOBase: 0135 0136 0137 """ 0138 The abstract base class for all I/O classes, acting on streams of bytes. 0139 There is no public constructor. 0140 0141 This class provides empty abstract implementations for many methods 0142 that derived classes can override selectively; the default 0143 implementations represent a file that cannot be read, written or 0144 seeked. 0145 0146 Even though :class:`IOBase` does not declare :meth:`read`, :meth:`readinto`, 0147 or :meth:`write` because their signatures will vary, implementations and 0148 clients should consider those methods part of the interface. Also, 0149 implementations may raise a :exc:`IOError` when operations they do not 0150 support are called. 0151 0152 The basic type used for binary data read from or written to a file is 0153 :class:`bytes` (also known as :class:`str`). :class:`bytearray`\s are 0154 accepted too, and in some cases (such as :class:`readinto`) required. 0155 Text I/O classes work with :class:`unicode` data. 0156 0157 Note that calling any method (even inquiries) on a closed stream is 0158 undefined. Implementations may raise :exc:`IOError` in this case. 0159 0160 IOBase (and its subclasses) support the iterator protocol, meaning that an 0161 :class:`IOBase` object can be iterated over yielding the lines in a stream. 0162 Lines are defined slightly differently depending on whether the stream is 0163 a binary stream (yielding :class:`bytes`), or a text stream (yielding 0164 :class:`unicode` strings). See :meth:`readline` below. 0165 0166 IOBase is also a context manager and therefore supports the 0167 :keyword:`with` statement. In this example, *file* is closed after the 0168 :keyword:`with` statement's suite is finished---even if an exception occurs:: 0169 0170 with io.open('spam.txt', 'w') as file: 0171 file.write(u'Spam and eggs!') 0172 0173 :class:`IOBase` provides these data attributes and methods: 0174 0175 """ 0176 0177 0178 def __init__(self, ): 0179 pass 0180 0181 def close(self, ): 0182 """ 0183 Flush and close this stream. This method has no effect if the file is 0184 already closed. Once the file is closed, any operation on the file 0185 (e.g. reading or writing) will raise a :exc:`ValueError`. 0186 0187 As a convenience, it is allowed to call this method more than once; 0188 only the first call, however, will have an effect. 0189 0190 """ 0191 pass 0192 0193 def fileno(self, ): 0194 """ 0195 Return the underlying file descriptor (an integer) of the stream if it 0196 exists. An :exc:`IOError` is raised if the IO object does not use a file 0197 descriptor. 0198 0199 """ 0200 pass 0201 0202 def flush(self, ): 0203 """ 0204 Flush the write buffers of the stream if applicable. This does nothing 0205 for read-only and non-blocking streams. 0206 0207 """ 0208 pass 0209 0210 def isatty(self, ): 0211 """ 0212 Return ``True`` if the stream is interactive (i.e., connected to 0213 a terminal/tty device). 0214 0215 """ 0216 pass 0217 0218 def readable(self, ): 0219 """ 0220 Return ``True`` if the stream can be read from. If False, :meth:`read` 0221 will raise :exc:`IOError`. 0222 0223 """ 0224 pass 0225 0226 def readline(self, limit=_1): 0227 """ 0228 Read and return one line from the stream. If *limit* is specified, at 0229 most *limit* bytes will be read. 0230 0231 The line terminator is always ``b'\n'`` for binary files; for text files, 0232 the *newlines* argument to :func:`.open` can be used to select the line 0233 terminator(s) recognized. 0234 0235 """ 0236 pass 0237 0238 def readlines(self, hint=_1): 0239 """ 0240 Read and return a list of lines from the stream. *hint* can be specified 0241 to control the number of lines read: no more lines will be read if the 0242 total size (in bytes/characters) of all lines so far exceeds *hint*. 0243 0244 """ 0245 pass 0246 0247 def seek(self, offset,whence=SEEK_SET): 0248 """ 0249 Change the stream position to the given byte *offset*. *offset* is 0250 interpreted relative to the position indicated by *whence*. Values for 0251 *whence* are: 0252 0253 * :data:`SEEK_SET` or ``0`` -- start of the stream (the default); 0254 *offset* should be zero or positive 0255 * :data:`SEEK_CUR` or ``1`` -- current stream position; *offset* may 0256 be negative 0257 * :data:`SEEK_END` or ``2`` -- end of the stream; *offset* is usually 0258 negative 0259 0260 Return the new absolute position. 0261 0262 """ 0263 pass 0264 0265 def seekable(self, ): 0266 """ 0267 Return ``True`` if the stream supports random access. If ``False``, 0268 :meth:`seek`, :meth:`tell` and :meth:`truncate` will raise :exc:`IOError`. 0269 0270 """ 0271 pass 0272 0273 def tell(self, ): 0274 """ 0275 Return the current stream position. 0276 0277 """ 0278 pass 0279 0280 def truncate(self, size=None): 0281 """ 0282 Resize the stream to the given *size* in bytes (or the current position 0283 if *size* is not specified). The current stream position isn't changed. 0284 This resizing can extend or reduce the current file size. In case of 0285 extension, the contents of the new file area depend on the platform 0286 (on most systems, additional bytes are zero-filled, on Windows they're 0287 undetermined). The new file size is returned. 0288 0289 """ 0290 pass 0291 0292 def writable(self, ): 0293 """ 0294 Return ``True`` if the stream supports writing. If ``False``, 0295 :meth:`write` and :meth:`truncate` will raise :exc:`IOError`. 0296 0297 """ 0298 pass 0299 0300 def writelines(self, lines): 0301 """ 0302 Write a list of lines to the stream. Line separators are not added, so it 0303 is usual for each of the lines provided to have a line separator at the 0304 end. 0305 0306 0307 """ 0308 pass 0309 0310 0311 0312 0313 class RawIOBase: 0314 0315 0316 """ 0317 Base class for raw binary I/O. It inherits :class:`IOBase`. There is no 0318 public constructor. 0319 0320 Raw binary I/O typically provides low-level access to an underlying OS 0321 device or API, and does not try to encapsulate it in high-level primitives 0322 (this is left to Buffered I/O and Text I/O, described later in this page). 0323 0324 In addition to the attributes and methods from :class:`IOBase`, 0325 RawIOBase provides the following methods: 0326 0327 """ 0328 0329 0330 def __init__(self, ): 0331 pass 0332 0333 def read(self, n=_1): 0334 """ 0335 Read up to *n* bytes from the object and return them. As a convenience, 0336 if *n* is unspecified or -1, :meth:`readall` is called. Otherwise, 0337 only one system call is ever made. Fewer than *n* bytes may be 0338 returned if the operating system call returns fewer than *n* bytes. 0339 0340 If 0 bytes are returned, and *n* was not 0, this indicates end of file. 0341 If the object is in non-blocking mode and no bytes are available, 0342 ``None`` is returned. 0343 0344 """ 0345 pass 0346 0347 def readall(self, ): 0348 """ 0349 Read and return all the bytes from the stream until EOF, using multiple 0350 calls to the stream if necessary. 0351 0352 """ 0353 pass 0354 0355 def readinto(self, b): 0356 """ 0357 Read up to len(b) bytes into bytearray *b* and return the number 0358 of bytes read. If the object is in non-blocking mode and no 0359 bytes are available, ``None`` is returned. 0360 0361 """ 0362 pass 0363 0364 def write(self, b): 0365 """ 0366 Write the given bytes or bytearray object, *b*, to the underlying raw 0367 stream and return the number of bytes written. This can be less than 0368 ``len(b)``, depending on specifics of the underlying raw stream, and 0369 especially if it is in non-blocking mode. ``None`` is returned if the 0370 raw stream is set not to block and no single byte could be readily 0371 written to it. 0372 0373 0374 """ 0375 pass 0376 0377 0378 0379 0380 class BufferedIOBase: 0381 0382 0383 """ 0384 Base class for binary streams that support some kind of buffering. 0385 It inherits :class:`IOBase`. There is no public constructor. 0386 0387 The main difference with :class:`RawIOBase` is that methods :meth:`read`, 0388 :meth:`readinto` and :meth:`write` will try (respectively) to read as much 0389 input as requested or to consume all given output, at the expense of 0390 making perhaps more than one system call. 0391 0392 In addition, those methods can raise :exc:`BlockingIOError` if the 0393 underlying raw stream is in non-blocking mode and cannot take or give 0394 enough data; unlike their :class:`RawIOBase` counterparts, they will 0395 never return ``None``. 0396 0397 Besides, the :meth:`read` method does not have a default 0398 implementation that defers to :meth:`readinto`. 0399 0400 A typical :class:`BufferedIOBase` implementation should not inherit from a 0401 :class:`RawIOBase` implementation, but wrap one, like 0402 :class:`BufferedWriter` and :class:`BufferedReader` do. 0403 0404 :class:`BufferedIOBase` provides or overrides these members in addition to 0405 those from :class:`IOBase`: 0406 0407 """ 0408 0409 0410 def __init__(self, ): 0411 pass 0412 0413 def detach(self, ): 0414 """ 0415 Separate the underlying raw stream from the buffer and return it. 0416 0417 After the raw stream has been detached, the buffer is in an unusable 0418 state. 0419 0420 Some buffers, like :class:`BytesIO`, do not have the concept of a single 0421 raw stream to return from this method. They raise 0422 :exc:`UnsupportedOperation`. 0423 0424 """ 0425 pass 0426 0427 def read(self, n=_1): 0428 """ 0429 Read and return up to *n* bytes. If the argument is omitted, ``None``, or 0430 negative, data is read and returned until EOF is reached. An empty bytes 0431 object is returned if the stream is already at EOF. 0432 0433 If the argument is positive, and the underlying raw stream is not 0434 interactive, multiple raw reads may be issued to satisfy the byte count 0435 (unless EOF is reached first). But for interactive raw streams, at most 0436 one raw read will be issued, and a short result does not imply that EOF is 0437 imminent. 0438 0439 A :exc:`BlockingIOError` is raised if the underlying raw stream is in 0440 non blocking-mode, and has no data available at the moment. 0441 0442 """ 0443 pass 0444 0445 def read1(self, n=_1): 0446 """ 0447 Read and return up to *n* bytes, with at most one call to the underlying 0448 raw stream's :meth:`~RawIOBase.read` method. This can be useful if you 0449 are implementing your own buffering on top of a :class:`BufferedIOBase` 0450 object. 0451 0452 """ 0453 pass 0454 0455 def readinto(self, b): 0456 """ 0457 Read up to len(b) bytes into bytearray *b* and return the number of bytes 0458 read. 0459 0460 Like :meth:`read`, multiple reads may be issued to the underlying raw 0461 stream, unless the latter is 'interactive'. 0462 0463 A :exc:`BlockingIOError` is raised if the underlying raw stream is in 0464 non blocking-mode, and has no data available at the moment. 0465 0466 """ 0467 pass 0468 0469 def write(self, b): 0470 """ 0471 Write the given bytes or bytearray object, *b* and return the number 0472 of bytes written (never less than ``len(b)``, since if the write fails 0473 an :exc:`IOError` will be raised). Depending on the actual 0474 implementation, these bytes may be readily written to the underlying 0475 stream, or held in a buffer for performance and latency reasons. 0476 0477 When in non-blocking mode, a :exc:`BlockingIOError` is raised if the 0478 data needed to be written to the raw stream but it couldn't accept 0479 all the data without blocking. 0480 0481 0482 Raw File I/O 0483 """ 0484 pass 0485 0486 0487 0488 0489 class FileIO: 0490 0491 0492 """ 0493 :class:`FileIO` represents an OS-level file containing bytes data. 0494 It implements the :class:`RawIOBase` interface (and therefore the 0495 :class:`IOBase` interface, too). 0496 0497 The *name* can be one of two things: 0498 0499 * a string representing the path to the file which will be opened; 0500 * an integer representing the number of an existing OS-level file descriptor 0501 to which the resulting :class:`FileIO` object will give access. 0502 0503 The *mode* can be ``'r'``, ``'w'`` or ``'a'`` for reading (default), writing, 0504 or appending. The file will be created if it doesn't exist when opened for 0505 writing or appending; it will be truncated when opened for writing. Add a 0506 ``'+'`` to the mode to allow simultaneous reading and writing. 0507 0508 The :meth:`read` (when called with a positive argument), :meth:`readinto` 0509 and :meth:`write` methods on this class will only make one system call. 0510 0511 In addition to the attributes and methods from :class:`IOBase` and 0512 :class:`RawIOBase`, :class:`FileIO` provides the following data 0513 attributes and methods: 0514 0515 """ 0516 0517 0518 def __init__(self, ): 0519 pass 0520 0521 0522 0523 0524 class BytesIO: 0525 0526 0527 """ 0528 A stream implementation using an in-memory bytes buffer. It inherits 0529 :class:`BufferedIOBase`. 0530 0531 The argument *initial_bytes* is an optional initial :class:`bytes`. 0532 0533 :class:`BytesIO` provides or overrides these methods in addition to those 0534 from :class:`BufferedIOBase` and :class:`IOBase`: 0535 0536 """ 0537 0538 0539 def __init__(self, ): 0540 pass 0541 0542 def getvalue(self, ): 0543 """ 0544 Return ``bytes`` containing the entire contents of the buffer. 0545 0546 """ 0547 pass 0548 0549 def read1(self, ): 0550 """ 0551 In :class:`BytesIO`, this is the same as :meth:`read`. 0552 0553 0554 """ 0555 pass 0556 0557 0558 0559 0560 class BufferedReader: 0561 0562 0563 """ 0564 A buffer providing higher-level access to a readable, sequential 0565 :class:`RawIOBase` object. It inherits :class:`BufferedIOBase`. 0566 When reading data from this object, a larger amount of data may be 0567 requested from the underlying raw stream, and kept in an internal buffer. 0568 The buffered data can then be returned directly on subsequent reads. 0569 0570 The constructor creates a :class:`BufferedReader` for the given readable 0571 *raw* stream and *buffer_size*. If *buffer_size* is omitted, 0572 :data:`DEFAULT_BUFFER_SIZE` is used. 0573 0574 :class:`BufferedReader` provides or overrides these methods in addition to 0575 those from :class:`BufferedIOBase` and :class:`IOBase`: 0576 0577 """ 0578 0579 0580 def __init__(self, ): 0581 pass 0582 0583 def peek(self, n): 0584 """ 0585 Return bytes from the stream without advancing the position. At most one 0586 single read on the raw stream is done to satisfy the call. The number of 0587 bytes returned may be less or more than requested. 0588 0589 """ 0590 pass 0591 0592 def read(self, n): 0593 """ 0594 Read and return *n* bytes, or if *n* is not given or negative, until EOF 0595 or if the read call would block in non-blocking mode. 0596 0597 """ 0598 pass 0599 0600 def read1(self, n): 0601 """ 0602 Read and return up to *n* bytes with only one call on the raw stream. If 0603 at least one byte is buffered, only buffered bytes are returned. 0604 Otherwise, one raw stream read call is made. 0605 0606 0607 """ 0608 pass 0609 0610 0611 0612 0613 class BufferedWriter: 0614 0615 0616 """ 0617 A buffer providing higher-level access to a writeable, sequential 0618 :class:`RawIOBase` object. It inherits :class:`BufferedIOBase`. 0619 When writing to this object, data is normally held into an internal 0620 buffer. The buffer will be written out to the underlying :class:`RawIOBase` 0621 object under various conditions, including: 0622 0623 * when the buffer gets too small for all pending data; 0624 * when :meth:`flush()` is called; 0625 * when a :meth:`seek()` is requested (for :class:`BufferedRandom` objects); 0626 * when the :class:`BufferedWriter` object is closed or destroyed. 0627 0628 The constructor creates a :class:`BufferedWriter` for the given writeable 0629 *raw* stream. If the *buffer_size* is not given, it defaults to 0630 :data:`DEFAULT_BUFFER_SIZE`. 0631 0632 A third argument, *max_buffer_size*, is supported, but unused and deprecated. 0633 0634 :class:`BufferedWriter` provides or overrides these methods in addition to 0635 those from :class:`BufferedIOBase` and :class:`IOBase`: 0636 0637 """ 0638 0639 0640 def __init__(self, ): 0641 pass 0642 0643 def flush(self, ): 0644 """ 0645 Force bytes held in the buffer into the raw stream. A 0646 :exc:`BlockingIOError` should be raised if the raw stream blocks. 0647 0648 """ 0649 pass 0650 0651 def write(self, b): 0652 """ 0653 Write the bytes or bytearray object, *b* and return the number of bytes 0654 written. When in non-blocking mode, a :exc:`BlockingIOError` is raised 0655 if the buffer needs to be written out but the raw stream blocks. 0656 0657 0658 """ 0659 pass 0660 0661 0662 0663 0664 class BufferedRWPair: 0665 0666 0667 """ 0668 A buffered I/O object giving a combined, higher-level access to two 0669 sequential :class:`RawIOBase` objects: one readable, the other writeable. 0670 It is useful for pairs of unidirectional communication channels 0671 (pipes, for instance). It inherits :class:`BufferedIOBase`. 0672 0673 *reader* and *writer* are :class:`RawIOBase` objects that are readable and 0674 writeable respectively. If the *buffer_size* is omitted it defaults to 0675 :data:`DEFAULT_BUFFER_SIZE`. 0676 0677 A fourth argument, *max_buffer_size*, is supported, but unused and 0678 deprecated. 0679 0680 :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods 0681 except for :meth:`~BufferedIOBase.detach`, which raises 0682 :exc:`UnsupportedOperation`. 0683 0684 0685 """ 0686 0687 0688 def __init__(self, ): 0689 pass 0690 0691 0692 0693 0694 class BufferedRandom: 0695 0696 0697 """ 0698 A buffered interface to random access streams. It inherits 0699 :class:`BufferedReader` and :class:`BufferedWriter`, and further supports 0700 :meth:`seek` and :meth:`tell` functionality. 0701 0702 The constructor creates a reader and writer for a seekable raw stream, given 0703 in the first argument. If the *buffer_size* is omitted it defaults to 0704 :data:`DEFAULT_BUFFER_SIZE`. 0705 0706 A third argument, *max_buffer_size*, is supported, but unused and deprecated. 0707 0708 :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or 0709 :class:`BufferedWriter` can do. 0710 0711 0712 Text I/O 0713 -------- 0714 0715 """ 0716 0717 0718 def __init__(self, ): 0719 pass 0720 0721 0722 0723 0724 class TextIOBase: 0725 0726 0727 """ 0728 Base class for text streams. This class provides an unicode character 0729 and line based interface to stream I/O. There is no :meth:`readinto` 0730 method because Python's :class:`unicode` strings are immutable. 0731 It inherits :class:`IOBase`. There is no public constructor. 0732 0733 :class:`TextIOBase` provides or overrides these data attributes and 0734 methods in addition to those from :class:`IOBase`: 0735 0736 """ 0737 0738 0739 def __init__(self, ): 0740 pass 0741 0742 def detach(self, ): 0743 """ 0744 Separate the underlying binary buffer from the :class:`TextIOBase` and 0745 return it. 0746 0747 After the underlying buffer has been detached, the :class:`TextIOBase` is 0748 in an unusable state. 0749 0750 Some :class:`TextIOBase` implementations, like :class:`StringIO`, may not 0751 have the concept of an underlying buffer and calling this method will 0752 raise :exc:`UnsupportedOperation`. 0753 0754 """ 0755 pass 0756 0757 def read(self, n): 0758 """ 0759 Read and return at most *n* characters from the stream as a single 0760 :class:`unicode`. If *n* is negative or ``None``, reads until EOF. 0761 0762 """ 0763 pass 0764 0765 def readline(self, ): 0766 """ 0767 Read until newline or EOF and return a single ``unicode``. If the 0768 stream is already at EOF, an empty string is returned. 0769 0770 """ 0771 pass 0772 0773 def write(self, s): 0774 """ 0775 Write the :class:`unicode` string *s* to the stream and return the 0776 number of characters written. 0777 0778 0779 """ 0780 pass 0781 0782 0783 0784 0785 class TextIOWrapper: 0786 0787 0788 """ 0789 A buffered text stream over a :class:`BufferedIOBase` binary stream. 0790 It inherits :class:`TextIOBase`. 0791 0792 *encoding* gives the name of the encoding that the stream will be decoded or 0793 encoded with. It defaults to :func:`locale.getpreferredencoding`. 0794 0795 *errors* is an optional string that specifies how encoding and decoding 0796 errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError` 0797 exception if there is an encoding error (the default of ``None`` has the same 0798 effect), or pass ``'ignore'`` to ignore errors. (Note that ignoring encoding 0799 errors can lead to data loss.) ``'replace'`` causes a replacement marker 0800 (such as ``'?'``) to be inserted where there is malformed data. When 0801 writing, ``'xmlcharrefreplace'`` (replace with the appropriate XML character 0802 reference) or ``'backslashreplace'`` (replace with backslashed escape 0803 sequences) can be used. Any other error handling name that has been 0804 registered with :func:`codecs.register_error` is also valid. 0805 0806 *newline* can be ``None``, ``''``, ``'\n'``, ``'\r'``, or ``'\r\n'``. It 0807 controls the handling of line endings. If it is ``None``, universal newlines 0808 is enabled. With this enabled, on input, the lines endings ``'\n'``, 0809 ``'\r'``, or ``'\r\n'`` are translated to ``'\n'`` before being returned to 0810 the caller. Conversely, on output, ``'\n'`` is translated to the system 0811 default line separator, :data:`os.linesep`. If *newline* is any other of its 0812 legal values, that newline becomes the newline when the file is read and it 0813 is returned untranslated. On output, ``'\n'`` is converted to the *newline*. 0814 0815 If *line_buffering* is ``True``, :meth:`flush` is implied when a call to 0816 write contains a newline character. 0817 0818 :class:`TextIOWrapper` provides one attribute in addition to those of 0819 :class:`TextIOBase` and its parents: 0820 0821 """ 0822 0823 0824 def __init__(self, ): 0825 pass 0826 0827 0828 0829 0830 class StringIO: 0831 0832 0833 """ 0834 An in-memory stream for unicode text. It inherits :class:`TextIOWrapper`. 0835 0836 The initial value of the buffer (an empty unicode string by default) can 0837 be set by providing *initial_value*. The *newline* argument works like 0838 that of :class:`TextIOWrapper`. The default is to do no newline 0839 translation. 0840 0841 :class:`StringIO` provides this method in addition to those from 0842 :class:`TextIOWrapper` and its parents: 0843 0844 """ 0845 0846 0847 def __init__(self, ): 0848 pass 0849 0850 def getvalue(self, ): 0851 """ 0852 Return a ``unicode`` containing the entire contents of the buffer at any 0853 time before the :class:`StringIO` object's :meth:`close` method is 0854 called. 0855 0856 Example usage:: 0857 0858 import io 0859 0860 output = io.StringIO() 0861 output.write(u'First line.\n') 0862 output.write(u'Second line.\n') 0863 0864 # Retrieve file contents -- this will be 0865 # u'First line.\nSecond line.\n' 0866 contents = output.getvalue() 0867 0868 # Close object and discard memory buffer -- 0869 # .getvalue() will now raise an exception. 0870 output.close() 0871 0872 0873 """ 0874 pass 0875 0876 0877 0878