Antoine Pitrou | 1969059 | 2009-06-12 20:14:08 +0000 | [diff] [blame] | 1 | """The io module provides the Python interfaces to stream handling. The |
Benjamin Peterson | 7bb4d2d | 2008-04-13 02:01:27 +0000 | [diff] [blame] | 2 | builtin open function is defined in this module. |
| 3 | |
| 4 | At the top of the I/O hierarchy is the abstract base class IOBase. It |
| 5 | defines the basic interface to a stream. Note, however, that there is no |
Mark Dickinson | 3e4caeb | 2009-02-21 20:27:01 +0000 | [diff] [blame] | 6 | separation between reading and writing to streams; implementations are |
Andrew Svetlov | 4bb142b | 2012-12-18 21:27:37 +0200 | [diff] [blame^] | 7 | allowed to raise an IOError if they do not support a given operation. |
Benjamin Peterson | 7bb4d2d | 2008-04-13 02:01:27 +0000 | [diff] [blame] | 8 | |
| 9 | Extending IOBase is RawIOBase which deals simply with the reading and |
| 10 | writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide |
| 11 | an interface to OS files. |
| 12 | |
| 13 | BufferedIOBase deals with buffering on a raw byte stream (RawIOBase). Its |
| 14 | subclasses, BufferedWriter, BufferedReader, and BufferedRWPair buffer |
| 15 | streams that are readable, writable, and both respectively. |
| 16 | BufferedRandom provides a buffered interface to random access |
| 17 | streams. BytesIO is a simple stream of in-memory bytes. |
| 18 | |
| 19 | Another IOBase subclass, TextIOBase, deals with the encoding and decoding |
| 20 | of streams into text. TextIOWrapper, which extends it, is a buffered text |
| 21 | interface to a buffered raw stream (`BufferedIOBase`). Finally, StringIO |
| 22 | is a in-memory stream for text. |
| 23 | |
| 24 | Argument names are not part of the specification, and only the arguments |
| 25 | of open() are intended to be used as keyword arguments. |
| 26 | |
| 27 | data: |
| 28 | |
| 29 | DEFAULT_BUFFER_SIZE |
| 30 | |
| 31 | An int containing the default buffer size used by the module's buffered |
| 32 | I/O classes. open() uses the file's blksize (as obtained by os.stat) if |
| 33 | possible. |
| 34 | """ |
| 35 | # New I/O library conforming to PEP 3116. |
| 36 | |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 37 | __author__ = ("Guido van Rossum <guido@python.org>, " |
| 38 | "Mike Verdone <mike.verdone@gmail.com>, " |
Antoine Pitrou | 1969059 | 2009-06-12 20:14:08 +0000 | [diff] [blame] | 39 | "Mark Russell <mark.russell@zen.co.uk>, " |
| 40 | "Antoine Pitrou <solipsis@pitrou.net>, " |
| 41 | "Amaury Forgeot d'Arc <amauryfa@gmail.com>, " |
| 42 | "Benjamin Peterson <benjamin@python.org>") |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 43 | |
| 44 | __all__ = ["BlockingIOError", "open", "IOBase", "RawIOBase", "FileIO", |
| 45 | "BytesIO", "StringIO", "BufferedIOBase", |
| 46 | "BufferedReader", "BufferedWriter", "BufferedRWPair", |
Benjamin Peterson | cafc225 | 2009-04-01 21:12:54 +0000 | [diff] [blame] | 47 | "BufferedRandom", "TextIOBase", "TextIOWrapper", |
Antoine Pitrou | 1969059 | 2009-06-12 20:14:08 +0000 | [diff] [blame] | 48 | "UnsupportedOperation", "SEEK_SET", "SEEK_CUR", "SEEK_END"] |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 49 | |
Antoine Pitrou | 1969059 | 2009-06-12 20:14:08 +0000 | [diff] [blame] | 50 | |
| 51 | import _io |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 52 | import abc |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 53 | |
Antoine Pitrou | 1969059 | 2009-06-12 20:14:08 +0000 | [diff] [blame] | 54 | from _io import (DEFAULT_BUFFER_SIZE, BlockingIOError, UnsupportedOperation, |
| 55 | open, FileIO, BytesIO, StringIO, BufferedReader, |
| 56 | BufferedWriter, BufferedRWPair, BufferedRandom, |
| 57 | IncrementalNewlineDecoder, TextIOWrapper) |
| 58 | |
| 59 | OpenWrapper = _io.open # for compatibility with _pyio |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 60 | |
Georg Brandl | 88ed8f2 | 2009-04-01 21:00:55 +0000 | [diff] [blame] | 61 | # for seek() |
| 62 | SEEK_SET = 0 |
| 63 | SEEK_CUR = 1 |
| 64 | SEEK_END = 2 |
| 65 | |
Antoine Pitrou | 1969059 | 2009-06-12 20:14:08 +0000 | [diff] [blame] | 66 | # Declaring ABCs in C is tricky so we do it here. |
| 67 | # Method descriptions and default implementations are inherited from the C |
| 68 | # version however. |
| 69 | class IOBase(_io._IOBase): |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 70 | __metaclass__ = abc.ABCMeta |
| 71 | |
Antoine Pitrou | 1969059 | 2009-06-12 20:14:08 +0000 | [diff] [blame] | 72 | class RawIOBase(_io._RawIOBase, IOBase): |
| 73 | pass |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 74 | |
Antoine Pitrou | 1969059 | 2009-06-12 20:14:08 +0000 | [diff] [blame] | 75 | class BufferedIOBase(_io._BufferedIOBase, IOBase): |
| 76 | pass |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 77 | |
Antoine Pitrou | 1969059 | 2009-06-12 20:14:08 +0000 | [diff] [blame] | 78 | class TextIOBase(_io._TextIOBase, IOBase): |
| 79 | pass |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | 1969059 | 2009-06-12 20:14:08 +0000 | [diff] [blame] | 81 | RawIOBase.register(FileIO) |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 82 | |
Antoine Pitrou | 1969059 | 2009-06-12 20:14:08 +0000 | [diff] [blame] | 83 | for klass in (BytesIO, BufferedReader, BufferedWriter, BufferedRandom, |
| 84 | BufferedRWPair): |
| 85 | BufferedIOBase.register(klass) |
Benjamin Peterson | 7bb4d2d | 2008-04-13 02:01:27 +0000 | [diff] [blame] | 86 | |
Antoine Pitrou | 1969059 | 2009-06-12 20:14:08 +0000 | [diff] [blame] | 87 | for klass in (StringIO, TextIOWrapper): |
| 88 | TextIOBase.register(klass) |
| 89 | del klass |