blob: 14384930c5ce3cd1753ce5f255bdafeecc39a4d2 [file] [log] [blame]
Antoine Pitrou19690592009-06-12 20:14:08 +00001"""The io module provides the Python interfaces to stream handling. The
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +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 Dickinson3e4caeb2009-02-21 20:27:01 +00006separation between reading and writing to streams; implementations are
Andrew Svetlov4bb142b2012-12-18 21:27:37 +02007allowed to raise an IOError if they do not support a given operation.
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +00008
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
Christian Heimes1a6387e2008-03-26 12:49:49 +000037__author__ = ("Guido van Rossum <guido@python.org>, "
38 "Mike Verdone <mike.verdone@gmail.com>, "
Antoine Pitrou19690592009-06-12 20:14:08 +000039 "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 Heimes1a6387e2008-03-26 12:49:49 +000043
44__all__ = ["BlockingIOError", "open", "IOBase", "RawIOBase", "FileIO",
45 "BytesIO", "StringIO", "BufferedIOBase",
46 "BufferedReader", "BufferedWriter", "BufferedRWPair",
Benjamin Petersoncafc2252009-04-01 21:12:54 +000047 "BufferedRandom", "TextIOBase", "TextIOWrapper",
Antoine Pitrou19690592009-06-12 20:14:08 +000048 "UnsupportedOperation", "SEEK_SET", "SEEK_CUR", "SEEK_END"]
Christian Heimes1a6387e2008-03-26 12:49:49 +000049
Antoine Pitrou19690592009-06-12 20:14:08 +000050
51import _io
Christian Heimes1a6387e2008-03-26 12:49:49 +000052import abc
Christian Heimes1a6387e2008-03-26 12:49:49 +000053
Antoine Pitrou19690592009-06-12 20:14:08 +000054from _io import (DEFAULT_BUFFER_SIZE, BlockingIOError, UnsupportedOperation,
55 open, FileIO, BytesIO, StringIO, BufferedReader,
56 BufferedWriter, BufferedRWPair, BufferedRandom,
57 IncrementalNewlineDecoder, TextIOWrapper)
58
59OpenWrapper = _io.open # for compatibility with _pyio
Christian Heimes1a6387e2008-03-26 12:49:49 +000060
Georg Brandl88ed8f22009-04-01 21:00:55 +000061# for seek()
62SEEK_SET = 0
63SEEK_CUR = 1
64SEEK_END = 2
65
Antoine Pitrou19690592009-06-12 20:14:08 +000066# 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.
69class IOBase(_io._IOBase):
Christian Heimes1a6387e2008-03-26 12:49:49 +000070 __metaclass__ = abc.ABCMeta
Andrew Kuchlingb7967cc2014-04-25 09:29:30 -040071 __doc__ = _io._IOBase.__doc__
Christian Heimes1a6387e2008-03-26 12:49:49 +000072
Antoine Pitrou19690592009-06-12 20:14:08 +000073class RawIOBase(_io._RawIOBase, IOBase):
Andrew Kuchlingb7967cc2014-04-25 09:29:30 -040074 __doc__ = _io._RawIOBase.__doc__
Christian Heimes1a6387e2008-03-26 12:49:49 +000075
Antoine Pitrou19690592009-06-12 20:14:08 +000076class BufferedIOBase(_io._BufferedIOBase, IOBase):
Andrew Kuchlingb7967cc2014-04-25 09:29:30 -040077 __doc__ = _io._BufferedIOBase.__doc__
Christian Heimes1a6387e2008-03-26 12:49:49 +000078
Antoine Pitrou19690592009-06-12 20:14:08 +000079class TextIOBase(_io._TextIOBase, IOBase):
Andrew Kuchlingb7967cc2014-04-25 09:29:30 -040080 __doc__ = _io._TextIOBase.__doc__
Christian Heimes1a6387e2008-03-26 12:49:49 +000081
Antoine Pitrou19690592009-06-12 20:14:08 +000082RawIOBase.register(FileIO)
Christian Heimes1a6387e2008-03-26 12:49:49 +000083
Antoine Pitrou19690592009-06-12 20:14:08 +000084for klass in (BytesIO, BufferedReader, BufferedWriter, BufferedRandom,
85 BufferedRWPair):
86 BufferedIOBase.register(klass)
Benjamin Peterson7bb4d2d2008-04-13 02:01:27 +000087
Antoine Pitrou19690592009-06-12 20:14:08 +000088for klass in (StringIO, TextIOWrapper):
89 TextIOBase.register(klass)
90del klass