Back to index

python3.2  3.2.2
io.py
Go to the documentation of this file.
00001 """The io module provides the Python interfaces to stream handling. The
00002 builtin open function is defined in this module.
00003 
00004 At the top of the I/O hierarchy is the abstract base class IOBase. It
00005 defines the basic interface to a stream. Note, however, that there is no
00006 separation between reading and writing to streams; implementations are
00007 allowed to throw an IOError if they do not support a given operation.
00008 
00009 Extending IOBase is RawIOBase which deals simply with the reading and
00010 writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide
00011 an interface to OS files.
00012 
00013 BufferedIOBase deals with buffering on a raw byte stream (RawIOBase). Its
00014 subclasses, BufferedWriter, BufferedReader, and BufferedRWPair buffer
00015 streams that are readable, writable, and both respectively.
00016 BufferedRandom provides a buffered interface to random access
00017 streams. BytesIO is a simple stream of in-memory bytes.
00018 
00019 Another IOBase subclass, TextIOBase, deals with the encoding and decoding
00020 of streams into text. TextIOWrapper, which extends it, is a buffered text
00021 interface to a buffered raw stream (`BufferedIOBase`). Finally, StringIO
00022 is a in-memory stream for text.
00023 
00024 Argument names are not part of the specification, and only the arguments
00025 of open() are intended to be used as keyword arguments.
00026 
00027 data:
00028 
00029 DEFAULT_BUFFER_SIZE
00030 
00031    An int containing the default buffer size used by the module's buffered
00032    I/O classes. open() uses the file's blksize (as obtained by os.stat) if
00033    possible.
00034 """
00035 # New I/O library conforming to PEP 3116.
00036 
00037 # XXX edge cases when switching between reading/writing
00038 # XXX need to support 1 meaning line-buffered
00039 # XXX whenever an argument is None, use the default value
00040 # XXX read/write ops should check readable/writable
00041 # XXX buffered readinto should work with arbitrary buffer objects
00042 # XXX use incremental encoder for text output, at least for UTF-16 and UTF-8-SIG
00043 # XXX check writable, readable and seekable in appropriate places
00044 
00045 
00046 __author__ = ("Guido van Rossum <guido@python.org>, "
00047               "Mike Verdone <mike.verdone@gmail.com>, "
00048               "Mark Russell <mark.russell@zen.co.uk>, "
00049               "Antoine Pitrou <solipsis@pitrou.net>, "
00050               "Amaury Forgeot d'Arc <amauryfa@gmail.com>, "
00051               "Benjamin Peterson <benjamin@python.org>")
00052 
00053 __all__ = ["BlockingIOError", "open", "IOBase", "RawIOBase", "FileIO",
00054            "BytesIO", "StringIO", "BufferedIOBase",
00055            "BufferedReader", "BufferedWriter", "BufferedRWPair",
00056            "BufferedRandom", "TextIOBase", "TextIOWrapper",
00057            "UnsupportedOperation", "SEEK_SET", "SEEK_CUR", "SEEK_END"]
00058 
00059 
00060 import _io
00061 import abc
00062 
00063 from _io import (DEFAULT_BUFFER_SIZE, BlockingIOError, UnsupportedOperation,
00064                  open, FileIO, BytesIO, StringIO, BufferedReader,
00065                  BufferedWriter, BufferedRWPair, BufferedRandom,
00066                  IncrementalNewlineDecoder, TextIOWrapper)
00067 
00068 OpenWrapper = _io.open # for compatibility with _pyio
00069 
00070 # for seek()
00071 SEEK_SET = 0
00072 SEEK_CUR = 1
00073 SEEK_END = 2
00074 
00075 # Declaring ABCs in C is tricky so we do it here.
00076 # Method descriptions and default implementations are inherited from the C
00077 # version however.
00078 class IOBase(_io._IOBase, metaclass=abc.ABCMeta):
00079     pass
00080 
00081 class RawIOBase(_io._RawIOBase, IOBase):
00082     pass
00083 
00084 class BufferedIOBase(_io._BufferedIOBase, IOBase):
00085     pass
00086 
00087 class TextIOBase(_io._TextIOBase, IOBase):
00088     pass
00089 
00090 RawIOBase.register(FileIO)
00091 
00092 for klass in (BytesIO, BufferedReader, BufferedWriter, BufferedRandom,
00093               BufferedRWPair):
00094     BufferedIOBase.register(klass)
00095 
00096 for klass in (StringIO, TextIOWrapper):
00097     TextIOBase.register(klass)
00098 del klass