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 |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 7 | allowed to raise an OSError if they do not support a given operation. |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +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 |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 22 | is an in-memory stream for text. |
Benjamin Peterson | 2c5f828 | 2008-04-13 00:27:46 +0000 | [diff] [blame] | 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 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 44 | __all__ = ["BlockingIOError", "open", "open_code", "IOBase", "RawIOBase", |
| 45 | "FileIO", "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, |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 55 | open, open_code, FileIO, BytesIO, StringIO, BufferedReader, |
Benjamin Peterson | 113f607 | 2009-03-06 23:59:29 +0000 | [diff] [blame] | 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): |
Andrew Kuchling | 86fe53e | 2014-04-25 09:29:30 -0400 | [diff] [blame] | 73 | __doc__ = _io._IOBase.__doc__ |
Guido van Rossum | 4b5386f | 2007-07-10 09:12:49 +0000 | [diff] [blame] | 74 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 75 | class RawIOBase(_io._RawIOBase, IOBase): |
Andrew Kuchling | 86fe53e | 2014-04-25 09:29:30 -0400 | [diff] [blame] | 76 | __doc__ = _io._RawIOBase.__doc__ |
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): |
Andrew Kuchling | 86fe53e | 2014-04-25 09:29:30 -0400 | [diff] [blame] | 79 | __doc__ = _io._BufferedIOBase.__doc__ |
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): |
Andrew Kuchling | 86fe53e | 2014-04-25 09:29:30 -0400 | [diff] [blame] | 82 | __doc__ = _io._TextIOBase.__doc__ |
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 |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 93 | |
| 94 | try: |
| 95 | from _io import _WindowsConsoleIO |
| 96 | except ImportError: |
| 97 | pass |
| 98 | else: |
| 99 | RawIOBase.register(_WindowsConsoleIO) |