1 /*
2     An implementation of the new I/O lib as defined by PEP 3116 - "New I/O"
3 
4     Classes defined here: UnsupportedOperation, BlockingIOError.
5     Functions defined here: open().
6 
7     Mostly written by Amaury Forgeot d'Arc
8 */
9 
10 #define PY_SSIZE_T_CLEAN
11 #include "Python.h"
12 #include "_iomodule.h"
13 #include "pycore_pystate.h"       // _PyInterpreterState_GET()
14 
15 #ifdef HAVE_SYS_TYPES_H
16 #include <sys/types.h>
17 #endif /* HAVE_SYS_TYPES_H */
18 
19 #ifdef HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif /* HAVE_SYS_STAT_H */
22 
23 #ifdef MS_WINDOWS
24 #include <windows.h>
25 #endif
26 
27 /* Various interned strings */
28 
29 PyObject *_PyIO_str_close = NULL;
30 PyObject *_PyIO_str_closed = NULL;
31 PyObject *_PyIO_str_decode = NULL;
32 PyObject *_PyIO_str_encode = NULL;
33 PyObject *_PyIO_str_fileno = NULL;
34 PyObject *_PyIO_str_flush = NULL;
35 PyObject *_PyIO_str_getstate = NULL;
36 PyObject *_PyIO_str_isatty = NULL;
37 PyObject *_PyIO_str_locale = NULL;
38 PyObject *_PyIO_str_newlines = NULL;
39 PyObject *_PyIO_str_nl = NULL;
40 PyObject *_PyIO_str_peek = NULL;
41 PyObject *_PyIO_str_read = NULL;
42 PyObject *_PyIO_str_read1 = NULL;
43 PyObject *_PyIO_str_readable = NULL;
44 PyObject *_PyIO_str_readall = NULL;
45 PyObject *_PyIO_str_readinto = NULL;
46 PyObject *_PyIO_str_readline = NULL;
47 PyObject *_PyIO_str_reset = NULL;
48 PyObject *_PyIO_str_seek = NULL;
49 PyObject *_PyIO_str_seekable = NULL;
50 PyObject *_PyIO_str_setstate = NULL;
51 PyObject *_PyIO_str_tell = NULL;
52 PyObject *_PyIO_str_truncate = NULL;
53 PyObject *_PyIO_str_writable = NULL;
54 PyObject *_PyIO_str_write = NULL;
55 
56 PyObject *_PyIO_empty_str = NULL;
57 PyObject *_PyIO_empty_bytes = NULL;
58 
59 PyDoc_STRVAR(module_doc,
60 "The io module provides the Python interfaces to stream handling. The\n"
61 "builtin open function is defined in this module.\n"
62 "\n"
63 "At the top of the I/O hierarchy is the abstract base class IOBase. It\n"
64 "defines the basic interface to a stream. Note, however, that there is no\n"
65 "separation between reading and writing to streams; implementations are\n"
66 "allowed to raise an OSError if they do not support a given operation.\n"
67 "\n"
68 "Extending IOBase is RawIOBase which deals simply with the reading and\n"
69 "writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide\n"
70 "an interface to OS files.\n"
71 "\n"
72 "BufferedIOBase deals with buffering on a raw byte stream (RawIOBase). Its\n"
73 "subclasses, BufferedWriter, BufferedReader, and BufferedRWPair buffer\n"
74 "streams that are readable, writable, and both respectively.\n"
75 "BufferedRandom provides a buffered interface to random access\n"
76 "streams. BytesIO is a simple stream of in-memory bytes.\n"
77 "\n"
78 "Another IOBase subclass, TextIOBase, deals with the encoding and decoding\n"
79 "of streams into text. TextIOWrapper, which extends it, is a buffered text\n"
80 "interface to a buffered raw stream (`BufferedIOBase`). Finally, StringIO\n"
81 "is an in-memory stream for text.\n"
82 "\n"
83 "Argument names are not part of the specification, and only the arguments\n"
84 "of open() are intended to be used as keyword arguments.\n"
85 "\n"
86 "data:\n"
87 "\n"
88 "DEFAULT_BUFFER_SIZE\n"
89 "\n"
90 "   An int containing the default buffer size used by the module's buffered\n"
91 "   I/O classes. open() uses the file's blksize (as obtained by os.stat) if\n"
92 "   possible.\n"
93     );
94 
95 
96 /*
97  * The main open() function
98  */
99 /*[clinic input]
100 module _io
101 
102 _io.open
103     file: object
104     mode: str = "r"
105     buffering: int = -1
106     encoding: str(accept={str, NoneType}) = None
107     errors: str(accept={str, NoneType}) = None
108     newline: str(accept={str, NoneType}) = None
109     closefd: bool(accept={int}) = True
110     opener: object = None
111 
112 Open file and return a stream.  Raise OSError upon failure.
113 
114 file is either a text or byte string giving the name (and the path
115 if the file isn't in the current working directory) of the file to
116 be opened or an integer file descriptor of the file to be
117 wrapped. (If a file descriptor is given, it is closed when the
118 returned I/O object is closed, unless closefd is set to False.)
119 
120 mode is an optional string that specifies the mode in which the file
121 is opened. It defaults to 'r' which means open for reading in text
122 mode.  Other common values are 'w' for writing (truncating the file if
123 it already exists), 'x' for creating and writing to a new file, and
124 'a' for appending (which on some Unix systems, means that all writes
125 append to the end of the file regardless of the current seek position).
126 In text mode, if encoding is not specified the encoding used is platform
127 dependent: locale.getpreferredencoding(False) is called to get the
128 current locale encoding. (For reading and writing raw bytes use binary
129 mode and leave encoding unspecified.) The available modes are:
130 
131 ========= ===============================================================
132 Character Meaning
133 --------- ---------------------------------------------------------------
134 'r'       open for reading (default)
135 'w'       open for writing, truncating the file first
136 'x'       create a new file and open it for writing
137 'a'       open for writing, appending to the end of the file if it exists
138 'b'       binary mode
139 't'       text mode (default)
140 '+'       open a disk file for updating (reading and writing)
141 'U'       universal newline mode (deprecated)
142 ========= ===============================================================
143 
144 The default mode is 'rt' (open for reading text). For binary random
145 access, the mode 'w+b' opens and truncates the file to 0 bytes, while
146 'r+b' opens the file without truncation. The 'x' mode implies 'w' and
147 raises an `FileExistsError` if the file already exists.
148 
149 Python distinguishes between files opened in binary and text modes,
150 even when the underlying operating system doesn't. Files opened in
151 binary mode (appending 'b' to the mode argument) return contents as
152 bytes objects without any decoding. In text mode (the default, or when
153 't' is appended to the mode argument), the contents of the file are
154 returned as strings, the bytes having been first decoded using a
155 platform-dependent encoding or using the specified encoding if given.
156 
157 'U' mode is deprecated and will raise an exception in future versions
158 of Python.  It has no effect in Python 3.  Use newline to control
159 universal newlines mode.
160 
161 buffering is an optional integer used to set the buffering policy.
162 Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
163 line buffering (only usable in text mode), and an integer > 1 to indicate
164 the size of a fixed-size chunk buffer.  When no buffering argument is
165 given, the default buffering policy works as follows:
166 
167 * Binary files are buffered in fixed-size chunks; the size of the buffer
168   is chosen using a heuristic trying to determine the underlying device's
169   "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.
170   On many systems, the buffer will typically be 4096 or 8192 bytes long.
171 
172 * "Interactive" text files (files for which isatty() returns True)
173   use line buffering.  Other text files use the policy described above
174   for binary files.
175 
176 encoding is the name of the encoding used to decode or encode the
177 file. This should only be used in text mode. The default encoding is
178 platform dependent, but any encoding supported by Python can be
179 passed.  See the codecs module for the list of supported encodings.
180 
181 errors is an optional string that specifies how encoding errors are to
182 be handled---this argument should not be used in binary mode. Pass
183 'strict' to raise a ValueError exception if there is an encoding error
184 (the default of None has the same effect), or pass 'ignore' to ignore
185 errors. (Note that ignoring encoding errors can lead to data loss.)
186 See the documentation for codecs.register or run 'help(codecs.Codec)'
187 for a list of the permitted encoding error strings.
188 
189 newline controls how universal newlines works (it only applies to text
190 mode). It can be None, '', '\n', '\r', and '\r\n'.  It works as
191 follows:
192 
193 * On input, if newline is None, universal newlines mode is
194   enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
195   these are translated into '\n' before being returned to the
196   caller. If it is '', universal newline mode is enabled, but line
197   endings are returned to the caller untranslated. If it has any of
198   the other legal values, input lines are only terminated by the given
199   string, and the line ending is returned to the caller untranslated.
200 
201 * On output, if newline is None, any '\n' characters written are
202   translated to the system default line separator, os.linesep. If
203   newline is '' or '\n', no translation takes place. If newline is any
204   of the other legal values, any '\n' characters written are translated
205   to the given string.
206 
207 If closefd is False, the underlying file descriptor will be kept open
208 when the file is closed. This does not work when a file name is given
209 and must be True in that case.
210 
211 A custom opener can be used by passing a callable as *opener*. The
212 underlying file descriptor for the file object is then obtained by
213 calling *opener* with (*file*, *flags*). *opener* must return an open
214 file descriptor (passing os.open as *opener* results in functionality
215 similar to passing None).
216 
217 open() returns a file object whose type depends on the mode, and
218 through which the standard file operations such as reading and writing
219 are performed. When open() is used to open a file in a text mode ('w',
220 'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
221 a file in a binary mode, the returned class varies: in read binary
222 mode, it returns a BufferedReader; in write binary and append binary
223 modes, it returns a BufferedWriter, and in read/write mode, it returns
224 a BufferedRandom.
225 
226 It is also possible to use a string or bytearray as a file for both
227 reading and writing. For strings StringIO can be used like a file
228 opened in a text mode, and for bytes a BytesIO can be used like a file
229 opened in a binary mode.
230 [clinic start generated code]*/
231 
232 static PyObject *
_io_open_impl(PyObject * module,PyObject * file,const char * mode,int buffering,const char * encoding,const char * errors,const char * newline,int closefd,PyObject * opener)233 _io_open_impl(PyObject *module, PyObject *file, const char *mode,
234               int buffering, const char *encoding, const char *errors,
235               const char *newline, int closefd, PyObject *opener)
236 /*[clinic end generated code: output=aefafc4ce2b46dc0 input=7295902222e6b311]*/
237 {
238     unsigned i;
239 
240     int creating = 0, reading = 0, writing = 0, appending = 0, updating = 0;
241     int text = 0, binary = 0, universal = 0;
242 
243     char rawmode[6], *m;
244     int line_buffering, is_number;
245     long isatty = 0;
246 
247     PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL, *path_or_fd = NULL;
248 
249     _Py_IDENTIFIER(_blksize);
250     _Py_IDENTIFIER(isatty);
251     _Py_IDENTIFIER(mode);
252     _Py_IDENTIFIER(close);
253 
254     is_number = PyNumber_Check(file);
255 
256     if (is_number) {
257         path_or_fd = file;
258         Py_INCREF(path_or_fd);
259     } else {
260         path_or_fd = PyOS_FSPath(file);
261         if (path_or_fd == NULL) {
262             return NULL;
263         }
264     }
265 
266     if (!is_number &&
267         !PyUnicode_Check(path_or_fd) &&
268         !PyBytes_Check(path_or_fd)) {
269         PyErr_Format(PyExc_TypeError, "invalid file: %R", file);
270         goto error;
271     }
272 
273     /* Decode mode */
274     for (i = 0; i < strlen(mode); i++) {
275         char c = mode[i];
276 
277         switch (c) {
278         case 'x':
279             creating = 1;
280             break;
281         case 'r':
282             reading = 1;
283             break;
284         case 'w':
285             writing = 1;
286             break;
287         case 'a':
288             appending = 1;
289             break;
290         case '+':
291             updating = 1;
292             break;
293         case 't':
294             text = 1;
295             break;
296         case 'b':
297             binary = 1;
298             break;
299         case 'U':
300             universal = 1;
301             reading = 1;
302             break;
303         default:
304             goto invalid_mode;
305         }
306 
307         /* c must not be duplicated */
308         if (strchr(mode+i+1, c)) {
309           invalid_mode:
310             PyErr_Format(PyExc_ValueError, "invalid mode: '%s'", mode);
311             goto error;
312         }
313 
314     }
315 
316     m = rawmode;
317     if (creating)  *(m++) = 'x';
318     if (reading)   *(m++) = 'r';
319     if (writing)   *(m++) = 'w';
320     if (appending) *(m++) = 'a';
321     if (updating)  *(m++) = '+';
322     *m = '\0';
323 
324     /* Parameters validation */
325     if (universal) {
326         if (creating || writing || appending || updating) {
327             PyErr_SetString(PyExc_ValueError,
328                             "mode U cannot be combined with 'x', 'w', 'a', or '+'");
329             goto error;
330         }
331         if (PyErr_WarnEx(PyExc_DeprecationWarning,
332                          "'U' mode is deprecated", 1) < 0)
333             goto error;
334         reading = 1;
335     }
336 
337     if (text && binary) {
338         PyErr_SetString(PyExc_ValueError,
339                         "can't have text and binary mode at once");
340         goto error;
341     }
342 
343     if (creating + reading + writing + appending > 1) {
344         PyErr_SetString(PyExc_ValueError,
345                         "must have exactly one of create/read/write/append mode");
346         goto error;
347     }
348 
349     if (binary && encoding != NULL) {
350         PyErr_SetString(PyExc_ValueError,
351                         "binary mode doesn't take an encoding argument");
352         goto error;
353     }
354 
355     if (binary && errors != NULL) {
356         PyErr_SetString(PyExc_ValueError,
357                         "binary mode doesn't take an errors argument");
358         goto error;
359     }
360 
361     if (binary && newline != NULL) {
362         PyErr_SetString(PyExc_ValueError,
363                         "binary mode doesn't take a newline argument");
364         goto error;
365     }
366 
367     if (binary && buffering == 1) {
368         if (PyErr_WarnEx(PyExc_RuntimeWarning,
369                          "line buffering (buffering=1) isn't supported in "
370                          "binary mode, the default buffer size will be used",
371                          1) < 0) {
372            goto error;
373         }
374     }
375 
376     /* Create the Raw file stream */
377     {
378         PyObject *RawIO_class = (PyObject *)&PyFileIO_Type;
379 #ifdef MS_WINDOWS
380         const PyConfig *config = _Py_GetConfig();
381         if (!config->legacy_windows_stdio && _PyIO_get_console_type(path_or_fd) != '\0') {
382             RawIO_class = (PyObject *)&PyWindowsConsoleIO_Type;
383             encoding = "utf-8";
384         }
385 #endif
386         raw = PyObject_CallFunction(RawIO_class, "OsOO",
387                                     path_or_fd, rawmode,
388                                     closefd ? Py_True : Py_False,
389                                     opener);
390     }
391 
392     if (raw == NULL)
393         goto error;
394     result = raw;
395 
396     Py_DECREF(path_or_fd);
397     path_or_fd = NULL;
398 
399     modeobj = PyUnicode_FromString(mode);
400     if (modeobj == NULL)
401         goto error;
402 
403     /* buffering */
404     if (buffering < 0) {
405         PyObject *res = _PyObject_CallMethodIdNoArgs(raw, &PyId_isatty);
406         if (res == NULL)
407             goto error;
408         isatty = PyLong_AsLong(res);
409         Py_DECREF(res);
410         if (isatty == -1 && PyErr_Occurred())
411             goto error;
412     }
413 
414     if (buffering == 1 || isatty) {
415         buffering = -1;
416         line_buffering = 1;
417     }
418     else
419         line_buffering = 0;
420 
421     if (buffering < 0) {
422         PyObject *blksize_obj;
423         blksize_obj = _PyObject_GetAttrId(raw, &PyId__blksize);
424         if (blksize_obj == NULL)
425             goto error;
426         buffering = PyLong_AsLong(blksize_obj);
427         Py_DECREF(blksize_obj);
428         if (buffering == -1 && PyErr_Occurred())
429             goto error;
430     }
431     if (buffering < 0) {
432         PyErr_SetString(PyExc_ValueError,
433                         "invalid buffering size");
434         goto error;
435     }
436 
437     /* if not buffering, returns the raw file object */
438     if (buffering == 0) {
439         if (!binary) {
440             PyErr_SetString(PyExc_ValueError,
441                             "can't have unbuffered text I/O");
442             goto error;
443         }
444 
445         Py_DECREF(modeobj);
446         return result;
447     }
448 
449     /* wraps into a buffered file */
450     {
451         PyObject *Buffered_class;
452 
453         if (updating)
454             Buffered_class = (PyObject *)&PyBufferedRandom_Type;
455         else if (creating || writing || appending)
456             Buffered_class = (PyObject *)&PyBufferedWriter_Type;
457         else if (reading)
458             Buffered_class = (PyObject *)&PyBufferedReader_Type;
459         else {
460             PyErr_Format(PyExc_ValueError,
461                          "unknown mode: '%s'", mode);
462             goto error;
463         }
464 
465         buffer = PyObject_CallFunction(Buffered_class, "Oi", raw, buffering);
466     }
467     if (buffer == NULL)
468         goto error;
469     result = buffer;
470     Py_DECREF(raw);
471 
472 
473     /* if binary, returns the buffered file */
474     if (binary) {
475         Py_DECREF(modeobj);
476         return result;
477     }
478 
479     /* wraps into a TextIOWrapper */
480     wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type,
481                                     "OsssO",
482                                     buffer,
483                                     encoding, errors, newline,
484                                     line_buffering ? Py_True : Py_False);
485     if (wrapper == NULL)
486         goto error;
487     result = wrapper;
488     Py_DECREF(buffer);
489 
490     if (_PyObject_SetAttrId(wrapper, &PyId_mode, modeobj) < 0)
491         goto error;
492     Py_DECREF(modeobj);
493     return result;
494 
495   error:
496     if (result != NULL) {
497         PyObject *exc, *val, *tb, *close_result;
498         PyErr_Fetch(&exc, &val, &tb);
499         close_result = _PyObject_CallMethodIdNoArgs(result, &PyId_close);
500         _PyErr_ChainExceptions(exc, val, tb);
501         Py_XDECREF(close_result);
502         Py_DECREF(result);
503     }
504     Py_XDECREF(path_or_fd);
505     Py_XDECREF(modeobj);
506     return NULL;
507 }
508 
509 
510 /*[clinic input]
511 _io.text_encoding
512     encoding: object
513     stacklevel: int = 2
514     /
515 
516 A helper function to choose the text encoding.
517 
518 When encoding is not None, just return it.
519 Otherwise, return the default text encoding (i.e. "locale").
520 
521 This function emits an EncodingWarning if encoding is None and
522 sys.flags.warn_default_encoding is true.
523 
524 This can be used in APIs with an encoding=None parameter.
525 However, please consider using encoding="utf-8" for new APIs.
526 [clinic start generated code]*/
527 
528 static PyObject *
_io_text_encoding_impl(PyObject * module,PyObject * encoding,int stacklevel)529 _io_text_encoding_impl(PyObject *module, PyObject *encoding, int stacklevel)
530 /*[clinic end generated code: output=91b2cfea6934cc0c input=bf70231213e2a7b4]*/
531 {
532     if (encoding == NULL || encoding == Py_None) {
533         PyInterpreterState *interp = _PyInterpreterState_GET();
534         if (_PyInterpreterState_GetConfig(interp)->warn_default_encoding) {
535             if (PyErr_WarnEx(PyExc_EncodingWarning,
536                              "'encoding' argument not specified", stacklevel)) {
537                 return NULL;
538             }
539         }
540         Py_INCREF(_PyIO_str_locale);
541         return _PyIO_str_locale;
542     }
543     Py_INCREF(encoding);
544     return encoding;
545 }
546 
547 
548 /*[clinic input]
549 _io.open_code
550 
551     path : unicode
552 
553 Opens the provided file with the intent to import the contents.
554 
555 This may perform extra validation beyond open(), but is otherwise interchangeable
556 with calling open(path, 'rb').
557 
558 [clinic start generated code]*/
559 
560 static PyObject *
_io_open_code_impl(PyObject * module,PyObject * path)561 _io_open_code_impl(PyObject *module, PyObject *path)
562 /*[clinic end generated code: output=2fe4ecbd6f3d6844 input=f5c18e23f4b2ed9f]*/
563 {
564     return PyFile_OpenCodeObject(path);
565 }
566 
567 /*
568  * Private helpers for the io module.
569  */
570 
571 Py_off_t
PyNumber_AsOff_t(PyObject * item,PyObject * err)572 PyNumber_AsOff_t(PyObject *item, PyObject *err)
573 {
574     Py_off_t result;
575     PyObject *runerr;
576     PyObject *value = _PyNumber_Index(item);
577     if (value == NULL)
578         return -1;
579 
580     /* We're done if PyLong_AsSsize_t() returns without error. */
581     result = PyLong_AsOff_t(value);
582     if (result != -1 || !(runerr = PyErr_Occurred()))
583         goto finish;
584 
585     /* Error handling code -- only manage OverflowError differently */
586     if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
587         goto finish;
588 
589     PyErr_Clear();
590     /* If no error-handling desired then the default clipping
591        is sufficient.
592      */
593     if (!err) {
594         assert(PyLong_Check(value));
595         /* Whether or not it is less than or equal to
596            zero is determined by the sign of ob_size
597         */
598         if (_PyLong_Sign(value) < 0)
599             result = PY_OFF_T_MIN;
600         else
601             result = PY_OFF_T_MAX;
602     }
603     else {
604         /* Otherwise replace the error with caller's error object. */
605         PyErr_Format(err,
606                      "cannot fit '%.200s' into an offset-sized integer",
607                      Py_TYPE(item)->tp_name);
608     }
609 
610  finish:
611     Py_DECREF(value);
612     return result;
613 }
614 
615 static inline _PyIO_State*
get_io_state(PyObject * module)616 get_io_state(PyObject *module)
617 {
618     void *state = PyModule_GetState(module);
619     assert(state != NULL);
620     return (_PyIO_State *)state;
621 }
622 
623 _PyIO_State *
_PyIO_get_module_state(void)624 _PyIO_get_module_state(void)
625 {
626     PyObject *mod = PyState_FindModule(&_PyIO_Module);
627     _PyIO_State *state;
628     if (mod == NULL || (state = get_io_state(mod)) == NULL) {
629         PyErr_SetString(PyExc_RuntimeError,
630                         "could not find io module state "
631                         "(interpreter shutdown?)");
632         return NULL;
633     }
634     return state;
635 }
636 
637 static int
iomodule_traverse(PyObject * mod,visitproc visit,void * arg)638 iomodule_traverse(PyObject *mod, visitproc visit, void *arg) {
639     _PyIO_State *state = get_io_state(mod);
640     if (!state->initialized)
641         return 0;
642     Py_VISIT(state->locale_module);
643     Py_VISIT(state->unsupported_operation);
644     return 0;
645 }
646 
647 
648 static int
iomodule_clear(PyObject * mod)649 iomodule_clear(PyObject *mod) {
650     _PyIO_State *state = get_io_state(mod);
651     if (!state->initialized)
652         return 0;
653     if (state->locale_module != NULL)
654         Py_CLEAR(state->locale_module);
655     Py_CLEAR(state->unsupported_operation);
656     return 0;
657 }
658 
659 static void
iomodule_free(PyObject * mod)660 iomodule_free(PyObject *mod) {
661     iomodule_clear(mod);
662 }
663 
664 
665 /*
666  * Module definition
667  */
668 
669 #include "clinic/_iomodule.c.h"
670 
671 static PyMethodDef module_methods[] = {
672     _IO_OPEN_METHODDEF
673     _IO_TEXT_ENCODING_METHODDEF
674     _IO_OPEN_CODE_METHODDEF
675     {NULL, NULL}
676 };
677 
678 struct PyModuleDef _PyIO_Module = {
679     PyModuleDef_HEAD_INIT,
680     "io",
681     module_doc,
682     sizeof(_PyIO_State),
683     module_methods,
684     NULL,
685     iomodule_traverse,
686     iomodule_clear,
687     (freefunc)iomodule_free,
688 };
689 
690 PyMODINIT_FUNC
PyInit__io(void)691 PyInit__io(void)
692 {
693     PyObject *m = PyModule_Create(&_PyIO_Module);
694     _PyIO_State *state = NULL;
695     if (m == NULL)
696         return NULL;
697     state = get_io_state(m);
698     state->initialized = 0;
699 
700 #define ADD_TYPE(type) \
701     if (PyModule_AddType(m, type) < 0) {  \
702         goto fail; \
703     }
704 
705     /* DEFAULT_BUFFER_SIZE */
706     if (PyModule_AddIntMacro(m, DEFAULT_BUFFER_SIZE) < 0)
707         goto fail;
708 
709     /* UnsupportedOperation inherits from ValueError and OSError */
710     state->unsupported_operation = PyObject_CallFunction(
711         (PyObject *)&PyType_Type, "s(OO){}",
712         "UnsupportedOperation", PyExc_OSError, PyExc_ValueError);
713     if (state->unsupported_operation == NULL)
714         goto fail;
715     Py_INCREF(state->unsupported_operation);
716     if (PyModule_AddObject(m, "UnsupportedOperation",
717                            state->unsupported_operation) < 0)
718         goto fail;
719 
720     /* BlockingIOError, for compatibility */
721     Py_INCREF(PyExc_BlockingIOError);
722     if (PyModule_AddObject(m, "BlockingIOError",
723                            (PyObject *) PyExc_BlockingIOError) < 0)
724         goto fail;
725 
726     /* Concrete base types of the IO ABCs.
727        (the ABCs themselves are declared through inheritance in io.py)
728     */
729     ADD_TYPE(&PyIOBase_Type);
730     ADD_TYPE(&PyRawIOBase_Type);
731     ADD_TYPE(&PyBufferedIOBase_Type);
732     ADD_TYPE(&PyTextIOBase_Type);
733 
734     /* Implementation of concrete IO objects. */
735     /* FileIO */
736     PyFileIO_Type.tp_base = &PyRawIOBase_Type;
737     ADD_TYPE(&PyFileIO_Type);
738 
739     /* BytesIO */
740     PyBytesIO_Type.tp_base = &PyBufferedIOBase_Type;
741     ADD_TYPE(&PyBytesIO_Type);
742     if (PyType_Ready(&_PyBytesIOBuffer_Type) < 0)
743         goto fail;
744 
745     /* StringIO */
746     PyStringIO_Type.tp_base = &PyTextIOBase_Type;
747     ADD_TYPE(&PyStringIO_Type);
748 
749 #ifdef MS_WINDOWS
750     /* WindowsConsoleIO */
751     PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type;
752     ADD_TYPE(&PyWindowsConsoleIO_Type);
753 #endif
754 
755     /* BufferedReader */
756     PyBufferedReader_Type.tp_base = &PyBufferedIOBase_Type;
757     ADD_TYPE(&PyBufferedReader_Type);
758 
759     /* BufferedWriter */
760     PyBufferedWriter_Type.tp_base = &PyBufferedIOBase_Type;
761     ADD_TYPE(&PyBufferedWriter_Type);
762 
763     /* BufferedRWPair */
764     PyBufferedRWPair_Type.tp_base = &PyBufferedIOBase_Type;
765     ADD_TYPE(&PyBufferedRWPair_Type);
766 
767     /* BufferedRandom */
768     PyBufferedRandom_Type.tp_base = &PyBufferedIOBase_Type;
769     ADD_TYPE(&PyBufferedRandom_Type);
770 
771     /* TextIOWrapper */
772     PyTextIOWrapper_Type.tp_base = &PyTextIOBase_Type;
773     ADD_TYPE(&PyTextIOWrapper_Type);
774 
775     /* IncrementalNewlineDecoder */
776     ADD_TYPE(&PyIncrementalNewlineDecoder_Type);
777 
778     /* Interned strings */
779 #define ADD_INTERNED(name) \
780     if (!_PyIO_str_ ## name && \
781         !(_PyIO_str_ ## name = PyUnicode_InternFromString(# name))) \
782         goto fail;
783 
784     ADD_INTERNED(close)
785     ADD_INTERNED(closed)
786     ADD_INTERNED(decode)
787     ADD_INTERNED(encode)
788     ADD_INTERNED(fileno)
789     ADD_INTERNED(flush)
790     ADD_INTERNED(getstate)
791     ADD_INTERNED(isatty)
792     ADD_INTERNED(locale)
793     ADD_INTERNED(newlines)
794     ADD_INTERNED(peek)
795     ADD_INTERNED(read)
796     ADD_INTERNED(read1)
797     ADD_INTERNED(readable)
798     ADD_INTERNED(readall)
799     ADD_INTERNED(readinto)
800     ADD_INTERNED(readline)
801     ADD_INTERNED(reset)
802     ADD_INTERNED(seek)
803     ADD_INTERNED(seekable)
804     ADD_INTERNED(setstate)
805     ADD_INTERNED(tell)
806     ADD_INTERNED(truncate)
807     ADD_INTERNED(write)
808     ADD_INTERNED(writable)
809 
810     if (!_PyIO_str_nl &&
811         !(_PyIO_str_nl = PyUnicode_InternFromString("\n")))
812         goto fail;
813 
814     if (!_PyIO_empty_str &&
815         !(_PyIO_empty_str = PyUnicode_FromStringAndSize(NULL, 0)))
816         goto fail;
817     if (!_PyIO_empty_bytes &&
818         !(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0)))
819         goto fail;
820 
821     state->initialized = 1;
822 
823     return m;
824 
825   fail:
826     Py_XDECREF(state->unsupported_operation);
827     Py_DECREF(m);
828     return NULL;
829 }
830