Benjamin Peterson | 9efcc4b | 2008-04-14 21:30:21 +0000 | [diff] [blame] | 1 | """The io module provides the Python interfaces to stream handling. The |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +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 | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 6 | separation between reading and writing to streams; implementations are |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 7 | allowed to throw an IOError if they do not support a given operation. |
| 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 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 37 | __author__ = ("Guido van Rossum <guido@python.org>, " |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 38 | "Mike Verdone <mike.verdone@gmail.com>, " |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 39 | "Mark Russell <mark.russell@zen.co.uk>, " |
| 40 | "Antoine Pitrou <solipsis@pitrou.net>, " |
Benjamin Peterson | ef9f2bd | 2009-05-01 20:45:43 +0000 | [diff] [blame] | 41 | "Amaury Forgeot d'Arc <amauryfa@gmail.com>, " |
| 42 | "Benjamin Peterson <benjamin@python.org>") |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 141f767 | 2007-04-10 00:22:16 +0000 | [diff] [blame] | 44 | __all__ = ["BlockingIOError", "open", "IOBase", "RawIOBase", "FileIO", |
Guido van Rossum | 5abbf75 | 2007-08-27 17:39:33 +0000 | [diff] [blame] | 45 | "BytesIO", "StringIO", "BufferedIOBase", |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 46 | "BufferedReader", "BufferedWriter", "BufferedRWPair", |
Benjamin Peterson | 0e4caf4 | 2009-04-01 21:22:20 +0000 | [diff] [blame] | 47 | "BufferedRandom", "TextIOBase", "TextIOWrapper", |
Benjamin Peterson | e12ef04 | 2009-04-29 21:53:47 +0000 | [diff] [blame] | 48 | "UnsupportedOperation", "SEEK_SET", "SEEK_CUR", "SEEK_END"] |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 49 | |
Christian Heimes | deb75f5 | 2008-08-15 18:43:03 +0000 | [diff] [blame] | 50 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 51 | import _io |
| 52 | import abc |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 53 | |
Benjamin Peterson | 113f607 | 2009-03-06 23:59:29 +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 |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 60 | |
Benjamin Peterson | bbf8393 | 2012-05-07 22:19:42 -0400 | [diff] [blame] | 61 | # Pretend this exception was created here. |
| 62 | UnsupportedOperation.__module__ = "io" |
| 63 | |
Benjamin Peterson | 0e4caf4 | 2009-04-01 21:22:20 +0000 | [diff] [blame] | 64 | # for seek() |
| 65 | SEEK_SET = 0 |
| 66 | SEEK_CUR = 1 |
| 67 | SEEK_END = 2 |
| 68 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 69 | # Declaring ABCs in C is tricky so we do it here. |
| 70 | # Method descriptions and default implementations are inherited from the C |
| 71 | # version however. |
| 72 | class IOBase(_io._IOBase, metaclass=abc.ABCMeta): |
Guido van Rossum | 4b5386f | 2007-07-10 09:12:49 +0000 | [diff] [blame] | 73 | pass |
| 74 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 75 | class RawIOBase(_io._RawIOBase, IOBase): |
| 76 | pass |
Guido van Rossum | 4b5386f | 2007-07-10 09:12:49 +0000 | [diff] [blame] | 77 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 78 | class BufferedIOBase(_io._BufferedIOBase, IOBase): |
| 79 | pass |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 80 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 81 | class TextIOBase(_io._TextIOBase, IOBase): |
| 82 | pass |
Guido van Rossum | 17e43e5 | 2007-02-27 15:45:13 +0000 | [diff] [blame] | 83 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 84 | RawIOBase.register(FileIO) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 85 | |
Benjamin Peterson | 113f607 | 2009-03-06 23:59:29 +0000 | [diff] [blame] | 86 | for klass in (BytesIO, BufferedReader, BufferedWriter, BufferedRandom, |
| 87 | BufferedRWPair): |
| 88 | BufferedIOBase.register(klass) |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 89 | |
Benjamin Peterson | 113f607 | 2009-03-06 23:59:29 +0000 | [diff] [blame] | 90 | for klass in (StringIO, TextIOWrapper): |
| 91 | TextIOBase.register(klass) |
| 92 | del klass |