blob: 8a8cf699af63d1c9a8184ad8d5e0da4956cb70d5 [file] [log] [blame]
Benjamin Peterson9efcc4b2008-04-14 21:30:21 +00001"""The io module provides the Python interfaces to stream handling. The
Benjamin Peterson2c5f8282008-04-13 00:27:46 +00002builtin open function is defined in this module.
3
4At the top of the I/O hierarchy is the abstract base class IOBase. It
5defines the basic interface to a stream. Note, however, that there is no
Mark Dickinson934896d2009-02-21 20:59:32 +00006separation between reading and writing to streams; implementations are
Benjamin Peterson2c5f8282008-04-13 00:27:46 +00007allowed to throw an IOError if they do not support a given operation.
8
9Extending IOBase is RawIOBase which deals simply with the reading and
10writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide
11an interface to OS files.
12
13BufferedIOBase deals with buffering on a raw byte stream (RawIOBase). Its
14subclasses, BufferedWriter, BufferedReader, and BufferedRWPair buffer
15streams that are readable, writable, and both respectively.
16BufferedRandom provides a buffered interface to random access
17streams. BytesIO is a simple stream of in-memory bytes.
18
19Another IOBase subclass, TextIOBase, deals with the encoding and decoding
20of streams into text. TextIOWrapper, which extends it, is a buffered text
21interface to a buffered raw stream (`BufferedIOBase`). Finally, StringIO
22is a in-memory stream for text.
23
24Argument names are not part of the specification, and only the arguments
25of open() are intended to be used as keyword arguments.
26
27data:
28
29DEFAULT_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
Benjamin Peterson2c5f8282008-04-13 00:27:46 +000037# XXX edge cases when switching between reading/writing
38# XXX need to support 1 meaning line-buffered
39# XXX whenever an argument is None, use the default value
40# XXX read/write ops should check readable/writable
41# XXX buffered readinto should work with arbitrary buffer objects
42# XXX use incremental encoder for text output, at least for UTF-16 and UTF-8-SIG
43# XXX check writable, readable and seekable in appropriate places
44
Guido van Rossum28524c72007-02-27 05:47:44 +000045
Guido van Rossum68bbcd22007-02-27 17:19:33 +000046__author__ = ("Guido van Rossum <guido@python.org>, "
Guido van Rossum78892e42007-04-06 17:31:18 +000047 "Mike Verdone <mike.verdone@gmail.com>, "
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000048 "Mark Russell <mark.russell@zen.co.uk>, "
49 "Antoine Pitrou <solipsis@pitrou.net>, "
Antoine Pitrou1fcadce2009-03-04 11:18:52 +000050 "Amaury Forgeot d'Arc <amauryfa@gmail.com>")
Guido van Rossum28524c72007-02-27 05:47:44 +000051
Guido van Rossum141f7672007-04-10 00:22:16 +000052__all__ = ["BlockingIOError", "open", "IOBase", "RawIOBase", "FileIO",
Guido van Rossum5abbf752007-08-27 17:39:33 +000053 "BytesIO", "StringIO", "BufferedIOBase",
Guido van Rossum01a27522007-03-07 01:00:12 +000054 "BufferedReader", "BufferedWriter", "BufferedRWPair",
Guido van Rossum141f7672007-04-10 00:22:16 +000055 "BufferedRandom", "TextIOBase", "TextIOWrapper"]
Guido van Rossum28524c72007-02-27 05:47:44 +000056
Christian Heimesdeb75f52008-08-15 18:43:03 +000057
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000058import _io
59import abc
Guido van Rossum28524c72007-02-27 05:47:44 +000060
Benjamin Peterson113f6072009-03-06 23:59:29 +000061from _io import (DEFAULT_BUFFER_SIZE, BlockingIOError, UnsupportedOperation,
62 open, FileIO, BytesIO, StringIO, BufferedReader,
63 BufferedWriter, BufferedRWPair, BufferedRandom,
64 IncrementalNewlineDecoder, TextIOWrapper)
65
66OpenWrapper = _io.open # for compatibility with _pyio
Guido van Rossum01a27522007-03-07 01:00:12 +000067
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000068# Declaring ABCs in C is tricky so we do it here.
69# Method descriptions and default implementations are inherited from the C
70# version however.
71class IOBase(_io._IOBase, metaclass=abc.ABCMeta):
Guido van Rossum4b5386f2007-07-10 09:12:49 +000072 pass
73
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000074class RawIOBase(_io._RawIOBase, IOBase):
75 pass
Guido van Rossum4b5386f2007-07-10 09:12:49 +000076
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000077class BufferedIOBase(_io._BufferedIOBase, IOBase):
78 pass
Guido van Rossum28524c72007-02-27 05:47:44 +000079
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000080class TextIOBase(_io._TextIOBase, IOBase):
81 pass
Guido van Rossum17e43e52007-02-27 15:45:13 +000082
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000083RawIOBase.register(FileIO)
Guido van Rossum53807da2007-04-10 19:01:47 +000084
Benjamin Peterson113f6072009-03-06 23:59:29 +000085for klass in (BytesIO, BufferedReader, BufferedWriter, BufferedRandom,
86 BufferedRWPair):
87 BufferedIOBase.register(klass)
Benjamin Peterson2c5f8282008-04-13 00:27:46 +000088
Benjamin Peterson113f6072009-03-06 23:59:29 +000089for klass in (StringIO, TextIOWrapper):
90 TextIOBase.register(klass)
91del klass