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 raise an OSError 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 an 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 __author__ = ("Guido van Rossum <guido@python.org>, "
38 "Mike Verdone <mike.verdone@gmail.com>, "
39 "Mark Russell <mark.russell@zen.co.uk>, "
40 "Antoine Pitrou <solipsis@pitrou.net>, "
41 "Amaury Forgeot d'Arc <amauryfa@gmail.com>, "
42 "Benjamin Peterson <benjamin@python.org>")
43
44 __all__ = ["BlockingIOError", "open", "open_code", "IOBase", "RawIOBase",
45 "FileIO", "BytesIO", "StringIO", "BufferedIOBase",
46 "BufferedReader", "BufferedWriter", "BufferedRWPair",
47 "BufferedRandom", "TextIOBase", "TextIOWrapper",
48 "UnsupportedOperation", "SEEK_SET", "SEEK_CUR", "SEEK_END",
49 "DEFAULT_BUFFER_SIZE", "text_encoding", "IncrementalNewlineDecoder"]
50
51
52 import _io
53 import abc
54
55 from _io import (DEFAULT_BUFFER_SIZE, BlockingIOError, UnsupportedOperation,
56 open, open_code, FileIO, BytesIO, StringIO, BufferedReader,
57 BufferedWriter, BufferedRWPair, BufferedRandom,
58 IncrementalNewlineDecoder, text_encoding, TextIOWrapper)
59
60
61 def __getattr__(name):
62 if name == "OpenWrapper":
63 # bpo-43680: Until Python 3.9, _pyio.open was not a static method and
64 # builtins.open was set to OpenWrapper to not become a bound method
65 # when set to a class variable. _io.open is a built-in function whereas
66 # _pyio.open is a Python function. In Python 3.10, _pyio.open() is now
67 # a static method, and builtins.open() is now io.open().
68 import warnings
69 warnings.warn('OpenWrapper is deprecated, use open instead',
70 DeprecationWarning, stacklevel=2)
71 global OpenWrapper
72 OpenWrapper = open
73 return OpenWrapper
74 raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
75
76
77 # Pretend this exception was created here.
78 UnsupportedOperation.__module__ = "io"
79
80 # for seek()
81 SEEK_SET = 0
82 SEEK_CUR = 1
83 SEEK_END = 2
84
85 # Declaring ABCs in C is tricky so we do it here.
86 # Method descriptions and default implementations are inherited from the C
87 # version however.
88 class ESC[4;38;5;81mIOBase(ESC[4;38;5;149m_ioESC[4;38;5;149m.ESC[4;38;5;149m_IOBase, metaclass=ESC[4;38;5;149mabcESC[4;38;5;149m.ESC[4;38;5;149mABCMeta):
89 __doc__ = _io._IOBase.__doc__
90
91 class ESC[4;38;5;81mRawIOBase(ESC[4;38;5;149m_ioESC[4;38;5;149m.ESC[4;38;5;149m_RawIOBase, ESC[4;38;5;149mIOBase):
92 __doc__ = _io._RawIOBase.__doc__
93
94 class ESC[4;38;5;81mBufferedIOBase(ESC[4;38;5;149m_ioESC[4;38;5;149m.ESC[4;38;5;149m_BufferedIOBase, ESC[4;38;5;149mIOBase):
95 __doc__ = _io._BufferedIOBase.__doc__
96
97 class ESC[4;38;5;81mTextIOBase(ESC[4;38;5;149m_ioESC[4;38;5;149m.ESC[4;38;5;149m_TextIOBase, ESC[4;38;5;149mIOBase):
98 __doc__ = _io._TextIOBase.__doc__
99
100 RawIOBase.register(FileIO)
101
102 for klass in (BytesIO, BufferedReader, BufferedWriter, BufferedRandom,
103 BufferedRWPair):
104 BufferedIOBase.register(klass)
105
106 for klass in (StringIO, TextIOWrapper):
107 TextIOBase.register(klass)
108 del klass
109
110 try:
111 from _io import _WindowsConsoleIO
112 except ImportError:
113 pass
114 else:
115 RawIOBase.register(_WindowsConsoleIO)