Home | History | Annotate | Download | only in Lib
      1 """The io module provides the Python interfaces to stream handling. The
      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
      6 separation between reading and writing to streams; implementations are
      7 allowed to throw an IOError if they do not support a given operation.
      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
     22 is a in-memory stream for text.
     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 
     37 # 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 
     45 
     46 __author__ = ("Guido van Rossum <guido (at] python.org>, "
     47               "Mike Verdone <mike.verdone (at] gmail.com>, "
     48               "Mark Russell <mark.russell (at] zen.co.uk>, "
     49               "Antoine Pitrou <solipsis (at] pitrou.net>, "
     50               "Amaury Forgeot d'Arc <amauryfa (at] gmail.com>, "
     51               "Benjamin Peterson <benjamin (at] python.org>")
     52 
     53 __all__ = ["BlockingIOError", "open", "IOBase", "RawIOBase", "FileIO",
     54            "BytesIO", "StringIO", "BufferedIOBase",
     55            "BufferedReader", "BufferedWriter", "BufferedRWPair",
     56            "BufferedRandom", "TextIOBase", "TextIOWrapper",
     57            "UnsupportedOperation", "SEEK_SET", "SEEK_CUR", "SEEK_END"]
     58 
     59 
     60 import _io
     61 import abc
     62 
     63 from _io import (DEFAULT_BUFFER_SIZE, BlockingIOError, UnsupportedOperation,
     64                  open, FileIO, BytesIO, StringIO, BufferedReader,
     65                  BufferedWriter, BufferedRWPair, BufferedRandom,
     66                  IncrementalNewlineDecoder, TextIOWrapper)
     67 
     68 OpenWrapper = _io.open # for compatibility with _pyio

     69 
     70 # for seek()

     71 SEEK_SET = 0
     72 SEEK_CUR = 1
     73 SEEK_END = 2
     74 
     75 # Declaring ABCs in C is tricky so we do it here.

     76 # Method descriptions and default implementations are inherited from the C

     77 # version however.

     78 class IOBase(_io._IOBase):
     79     __metaclass__ = abc.ABCMeta
     80 
     81 class RawIOBase(_io._RawIOBase, IOBase):
     82     pass
     83 
     84 class BufferedIOBase(_io._BufferedIOBase, IOBase):
     85     pass
     86 
     87 class TextIOBase(_io._TextIOBase, IOBase):
     88     pass
     89 
     90 RawIOBase.register(FileIO)
     91 
     92 for klass in (BytesIO, BufferedReader, BufferedWriter, BufferedRandom,
     93               BufferedRWPair):
     94     BufferedIOBase.register(klass)
     95 
     96 for klass in (StringIO, TextIOWrapper):
     97     TextIOBase.register(klass)
     98 del klass
     99