io.py 3.12 KB
Newer Older
1
"""The io module provides the Python interfaces to stream handling. The
2 3 4 5
builtin open function is defined in this module.

At the top of the I/O hierarchy is the abstract base class IOBase. It
defines the basic interface to a stream. Note, however, that there is no
6
separation between reading and writing to streams; implementations are
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
allowed to throw an IOError if they do not support a given operation.

Extending IOBase is RawIOBase which deals simply with the reading and
writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide
an interface to OS files.

BufferedIOBase deals with buffering on a raw byte stream (RawIOBase). Its
subclasses, BufferedWriter, BufferedReader, and BufferedRWPair buffer
streams that are readable, writable, and both respectively.
BufferedRandom provides a buffered interface to random access
streams. BytesIO is a simple stream of in-memory bytes.

Another IOBase subclass, TextIOBase, deals with the encoding and decoding
of streams into text. TextIOWrapper, which extends it, is a buffered text
interface to a buffered raw stream (`BufferedIOBase`). Finally, StringIO
is a in-memory stream for text.

Argument names are not part of the specification, and only the arguments
of open() are intended to be used as keyword arguments.

data:

DEFAULT_BUFFER_SIZE

   An int containing the default buffer size used by the module's buffered
   I/O classes. open() uses the file's blksize (as obtained by os.stat) if
   possible.
"""
# New I/O library conforming to PEP 3116.

37
__author__ = ("Guido van Rossum <guido@python.org>, "
38
              "Mike Verdone <mike.verdone@gmail.com>, "
39 40
              "Mark Russell <mark.russell@zen.co.uk>, "
              "Antoine Pitrou <solipsis@pitrou.net>, "
Benjamin Peterson's avatar
Benjamin Peterson committed
41 42
              "Amaury Forgeot d'Arc <amauryfa@gmail.com>, "
              "Benjamin Peterson <benjamin@python.org>")
43

44
__all__ = ["BlockingIOError", "open", "IOBase", "RawIOBase", "FileIO",
45
           "BytesIO", "StringIO", "BufferedIOBase",
46
           "BufferedReader", "BufferedWriter", "BufferedRWPair",
47
           "BufferedRandom", "TextIOBase", "TextIOWrapper",
48
           "UnsupportedOperation", "SEEK_SET", "SEEK_CUR", "SEEK_END"]
49

50

51 52
import _io
import abc
53

54 55 56 57 58 59
from _io import (DEFAULT_BUFFER_SIZE, BlockingIOError, UnsupportedOperation,
                 open, FileIO, BytesIO, StringIO, BufferedReader,
                 BufferedWriter, BufferedRWPair, BufferedRandom,
                 IncrementalNewlineDecoder, TextIOWrapper)

OpenWrapper = _io.open # for compatibility with _pyio
60

61 62 63 64 65
# for seek()
SEEK_SET = 0
SEEK_CUR = 1
SEEK_END = 2

66 67 68 69
# Declaring ABCs in C is tricky so we do it here.
# Method descriptions and default implementations are inherited from the C
# version however.
class IOBase(_io._IOBase, metaclass=abc.ABCMeta):
70 71
    pass

72 73
class RawIOBase(_io._RawIOBase, IOBase):
    pass
74

75 76
class BufferedIOBase(_io._BufferedIOBase, IOBase):
    pass
77

78 79
class TextIOBase(_io._TextIOBase, IOBase):
    pass
80

81 82
RawIOBase.register(FileIO)

83 84 85
for klass in (BytesIO, BufferedReader, BufferedWriter, BufferedRandom,
              BufferedRWPair):
    BufferedIOBase.register(klass)
86

87 88 89
for klass in (StringIO, TextIOWrapper):
    TextIOBase.register(klass)
del klass