logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

io.py (4196B)


  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, text_encoding, TextIOWrapper)
  45. def __getattr__(name):
  46. if name == "OpenWrapper":
  47. # bpo-43680: Until Python 3.9, _pyio.open was not a static method and
  48. # builtins.open was set to OpenWrapper to not become a bound method
  49. # when set to a class variable. _io.open is a built-in function whereas
  50. # _pyio.open is a Python function. In Python 3.10, _pyio.open() is now
  51. # a static method, and builtins.open() is now io.open().
  52. import warnings
  53. warnings.warn('OpenWrapper is deprecated, use open instead',
  54. DeprecationWarning, stacklevel=2)
  55. global OpenWrapper
  56. OpenWrapper = open
  57. return OpenWrapper
  58. raise AttributeError(name)
  59. # Pretend this exception was created here.
  60. UnsupportedOperation.__module__ = "io"
  61. # for seek()
  62. SEEK_SET = 0
  63. SEEK_CUR = 1
  64. SEEK_END = 2
  65. # Declaring ABCs in C is tricky so we do it here.
  66. # Method descriptions and default implementations are inherited from the C
  67. # version however.
  68. class IOBase(_io._IOBase, metaclass=abc.ABCMeta):
  69. __doc__ = _io._IOBase.__doc__
  70. class RawIOBase(_io._RawIOBase, IOBase):
  71. __doc__ = _io._RawIOBase.__doc__
  72. class BufferedIOBase(_io._BufferedIOBase, IOBase):
  73. __doc__ = _io._BufferedIOBase.__doc__
  74. class TextIOBase(_io._TextIOBase, IOBase):
  75. __doc__ = _io._TextIOBase.__doc__
  76. RawIOBase.register(FileIO)
  77. for klass in (BytesIO, BufferedReader, BufferedWriter, BufferedRandom,
  78. BufferedRWPair):
  79. BufferedIOBase.register(klass)
  80. for klass in (StringIO, TextIOWrapper):
  81. TextIOBase.register(klass)
  82. del klass
  83. try:
  84. from _io import _WindowsConsoleIO
  85. except ImportError:
  86. pass
  87. else:
  88. RawIOBase.register(_WindowsConsoleIO)