io.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """The io module provides the Python interfaces to stream handling. The
  2. builtin open function is defined in this module.
  3. At the top of the I/O hierarchy is the abstract base class IOBase. It
  4. defines the basic interface to a stream. Note, however, that there is no
  5. separation between reading and writing to streams; implementations are
  6. allowed to raise an OSError if they do not support a given operation.
  7. Extending IOBase is RawIOBase which deals simply with the reading and
  8. writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide
  9. an interface to OS files.
  10. BufferedIOBase deals with buffering on a raw byte stream (RawIOBase). Its
  11. subclasses, BufferedWriter, BufferedReader, and BufferedRWPair buffer
  12. streams that are readable, writable, and both respectively.
  13. BufferedRandom provides a buffered interface to random access
  14. streams. BytesIO is a simple stream of in-memory bytes.
  15. Another IOBase subclass, TextIOBase, deals with the encoding and decoding
  16. of streams into text. TextIOWrapper, which extends it, is a buffered text
  17. interface to a buffered raw stream (`BufferedIOBase`). Finally, StringIO
  18. is an in-memory stream for text.
  19. Argument names are not part of the specification, and only the arguments
  20. of open() are intended to be used as keyword arguments.
  21. data:
  22. DEFAULT_BUFFER_SIZE
  23. An int containing the default buffer size used by the module's buffered
  24. I/O classes. open() uses the file's blksize (as obtained by os.stat) if
  25. possible.
  26. """
  27. # New I/O library conforming to PEP 3116.
  28. __author__ = ("Guido van Rossum <guido@python.org>, "
  29. "Mike Verdone <mike.verdone@gmail.com>, "
  30. "Mark Russell <mark.russell@zen.co.uk>, "
  31. "Antoine Pitrou <solipsis@pitrou.net>, "
  32. "Amaury Forgeot d'Arc <amauryfa@gmail.com>, "
  33. "Benjamin Peterson <benjamin@python.org>")
  34. __all__ = ["BlockingIOError", "open", "open_code", "IOBase", "RawIOBase",
  35. "FileIO", "BytesIO", "StringIO", "BufferedIOBase",
  36. "BufferedReader", "BufferedWriter", "BufferedRWPair",
  37. "BufferedRandom", "TextIOBase", "TextIOWrapper",
  38. "UnsupportedOperation", "SEEK_SET", "SEEK_CUR", "SEEK_END"]
  39. import _io
  40. import abc
  41. from _io import (DEFAULT_BUFFER_SIZE, BlockingIOError, UnsupportedOperation,
  42. open, open_code, FileIO, BytesIO, StringIO, BufferedReader,
  43. BufferedWriter, BufferedRWPair, BufferedRandom,
  44. IncrementalNewlineDecoder, TextIOWrapper)
  45. OpenWrapper = _io.open # for compatibility with _pyio
  46. # Pretend this exception was created here.
  47. UnsupportedOperation.__module__ = "io"
  48. # for seek()
  49. SEEK_SET = 0
  50. SEEK_CUR = 1
  51. SEEK_END = 2
  52. # Declaring ABCs in C is tricky so we do it here.
  53. # Method descriptions and default implementations are inherited from the C
  54. # version however.
  55. class IOBase(_io._IOBase, metaclass=abc.ABCMeta):
  56. __doc__ = _io._IOBase.__doc__
  57. class RawIOBase(_io._RawIOBase, IOBase):
  58. __doc__ = _io._RawIOBase.__doc__
  59. class BufferedIOBase(_io._BufferedIOBase, IOBase):
  60. __doc__ = _io._BufferedIOBase.__doc__
  61. class TextIOBase(_io._TextIOBase, IOBase):
  62. __doc__ = _io._TextIOBase.__doc__
  63. RawIOBase.register(FileIO)
  64. for klass in (BytesIO, BufferedReader, BufferedWriter, BufferedRandom,
  65. BufferedRWPair):
  66. BufferedIOBase.register(klass)
  67. for klass in (StringIO, TextIOWrapper):
  68. TextIOBase.register(klass)
  69. del klass
  70. try:
  71. from _io import _WindowsConsoleIO
  72. except ImportError:
  73. pass
  74. else:
  75. RawIOBase.register(_WindowsConsoleIO)