Home | History | Annotate | Download | only in _io
      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 "structmember.h"
     13 #include "_iomodule.h"
     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_newlines = NULL;
     38 PyObject *_PyIO_str_nl = NULL;
     39 PyObject *_PyIO_str_peek = NULL;
     40 PyObject *_PyIO_str_read = NULL;
     41 PyObject *_PyIO_str_read1 = NULL;
     42 PyObject *_PyIO_str_readable = NULL;
     43 PyObject *_PyIO_str_readall = NULL;
     44 PyObject *_PyIO_str_readinto = NULL;
     45 PyObject *_PyIO_str_readline = NULL;
     46 PyObject *_PyIO_str_reset = NULL;
     47 PyObject *_PyIO_str_seek = NULL;
     48 PyObject *_PyIO_str_seekable = NULL;
     49 PyObject *_PyIO_str_setstate = NULL;
     50 PyObject *_PyIO_str_tell = NULL;
     51 PyObject *_PyIO_str_truncate = NULL;
     52 PyObject *_PyIO_str_writable = NULL;
     53 PyObject *_PyIO_str_write = NULL;
     54 
     55 PyObject *_PyIO_empty_str = NULL;
     56 PyObject *_PyIO_empty_bytes = NULL;
     57 
     58 PyDoc_STRVAR(module_doc,
     59 "The io module provides the Python interfaces to stream handling. The\n"
     60 "builtin open function is defined in this module.\n"
     61 "\n"
     62 "At the top of the I/O hierarchy is the abstract base class IOBase. It\n"
     63 "defines the basic interface to a stream. Note, however, that there is no\n"
     64 "separation between reading and writing to streams; implementations are\n"
     65 "allowed to raise an OSError if they do not support a given operation.\n"
     66 "\n"
     67 "Extending IOBase is RawIOBase which deals simply with the reading and\n"
     68 "writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide\n"
     69 "an interface to OS files.\n"
     70 "\n"
     71 "BufferedIOBase deals with buffering on a raw byte stream (RawIOBase). Its\n"
     72 "subclasses, BufferedWriter, BufferedReader, and BufferedRWPair buffer\n"
     73 "streams that are readable, writable, and both respectively.\n"
     74 "BufferedRandom provides a buffered interface to random access\n"
     75 "streams. BytesIO is a simple stream of in-memory bytes.\n"
     76 "\n"
     77 "Another IOBase subclass, TextIOBase, deals with the encoding and decoding\n"
     78 "of streams into text. TextIOWrapper, which extends it, is a buffered text\n"
     79 "interface to a buffered raw stream (`BufferedIOBase`). Finally, StringIO\n"
     80 "is an in-memory stream for text.\n"
     81 "\n"
     82 "Argument names are not part of the specification, and only the arguments\n"
     83 "of open() are intended to be used as keyword arguments.\n"
     84 "\n"
     85 "data:\n"
     86 "\n"
     87 "DEFAULT_BUFFER_SIZE\n"
     88 "\n"
     89 "   An int containing the default buffer size used by the module's buffered\n"
     90 "   I/O classes. open() uses the file's blksize (as obtained by os.stat) if\n"
     91 "   possible.\n"
     92     );
     93 
     94 
     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}) = NULL
    107     errors: str(accept={str, NoneType}) = NULL
    108     newline: str(accept={str, NoneType}) = NULL
    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 *
    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=03da2940c8a65871]*/
    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;
    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     /* Create the Raw file stream */
    368     {
    369         PyObject *RawIO_class = (PyObject *)&PyFileIO_Type;
    370 #ifdef MS_WINDOWS
    371         if (!Py_LegacyWindowsStdioFlag && _PyIO_get_console_type(path_or_fd) != '\0') {
    372             RawIO_class = (PyObject *)&PyWindowsConsoleIO_Type;
    373             encoding = "utf-8";
    374         }
    375 #endif
    376         raw = PyObject_CallFunction(RawIO_class,
    377                                     "OsiO", path_or_fd, rawmode, closefd, opener);
    378     }
    379 
    380     if (raw == NULL)
    381         goto error;
    382     result = raw;
    383 
    384     Py_DECREF(path_or_fd);
    385     path_or_fd = NULL;
    386 
    387     modeobj = PyUnicode_FromString(mode);
    388     if (modeobj == NULL)
    389         goto error;
    390 
    391     /* buffering */
    392     {
    393         PyObject *res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL);
    394         if (res == NULL)
    395             goto error;
    396         isatty = PyLong_AsLong(res);
    397         Py_DECREF(res);
    398         if (isatty == -1 && PyErr_Occurred())
    399             goto error;
    400     }
    401 
    402     if (buffering == 1 || (buffering < 0 && isatty)) {
    403         buffering = -1;
    404         line_buffering = 1;
    405     }
    406     else
    407         line_buffering = 0;
    408 
    409     if (buffering < 0) {
    410         PyObject *blksize_obj;
    411         blksize_obj = _PyObject_GetAttrId(raw, &PyId__blksize);
    412         if (blksize_obj == NULL)
    413             goto error;
    414         buffering = PyLong_AsLong(blksize_obj);
    415         Py_DECREF(blksize_obj);
    416         if (buffering == -1 && PyErr_Occurred())
    417             goto error;
    418     }
    419     if (buffering < 0) {
    420         PyErr_SetString(PyExc_ValueError,
    421                         "invalid buffering size");
    422         goto error;
    423     }
    424 
    425     /* if not buffering, returns the raw file object */
    426     if (buffering == 0) {
    427         if (!binary) {
    428             PyErr_SetString(PyExc_ValueError,
    429                             "can't have unbuffered text I/O");
    430             goto error;
    431         }
    432 
    433         Py_DECREF(modeobj);
    434         return result;
    435     }
    436 
    437     /* wraps into a buffered file */
    438     {
    439         PyObject *Buffered_class;
    440 
    441         if (updating)
    442             Buffered_class = (PyObject *)&PyBufferedRandom_Type;
    443         else if (creating || writing || appending)
    444             Buffered_class = (PyObject *)&PyBufferedWriter_Type;
    445         else if (reading)
    446             Buffered_class = (PyObject *)&PyBufferedReader_Type;
    447         else {
    448             PyErr_Format(PyExc_ValueError,
    449                          "unknown mode: '%s'", mode);
    450             goto error;
    451         }
    452 
    453         buffer = PyObject_CallFunction(Buffered_class, "Oi", raw, buffering);
    454     }
    455     if (buffer == NULL)
    456         goto error;
    457     result = buffer;
    458     Py_DECREF(raw);
    459 
    460 
    461     /* if binary, returns the buffered file */
    462     if (binary) {
    463         Py_DECREF(modeobj);
    464         return result;
    465     }
    466 
    467     /* wraps into a TextIOWrapper */
    468     wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type,
    469                                     "Osssi",
    470                                     buffer,
    471                                     encoding, errors, newline,
    472                                     line_buffering);
    473     if (wrapper == NULL)
    474         goto error;
    475     result = wrapper;
    476     Py_DECREF(buffer);
    477 
    478     if (_PyObject_SetAttrId(wrapper, &PyId_mode, modeobj) < 0)
    479         goto error;
    480     Py_DECREF(modeobj);
    481     return result;
    482 
    483   error:
    484     if (result != NULL) {
    485         PyObject *exc, *val, *tb, *close_result;
    486         PyErr_Fetch(&exc, &val, &tb);
    487         close_result = _PyObject_CallMethodId(result, &PyId_close, NULL);
    488         _PyErr_ChainExceptions(exc, val, tb);
    489         Py_XDECREF(close_result);
    490         Py_DECREF(result);
    491     }
    492     Py_XDECREF(path_or_fd);
    493     Py_XDECREF(modeobj);
    494     return NULL;
    495 }
    496 
    497 /*
    499  * Private helpers for the io module.
    500  */
    501 
    502 Py_off_t
    503 PyNumber_AsOff_t(PyObject *item, PyObject *err)
    504 {
    505     Py_off_t result;
    506     PyObject *runerr;
    507     PyObject *value = PyNumber_Index(item);
    508     if (value == NULL)
    509         return -1;
    510 
    511     /* We're done if PyLong_AsSsize_t() returns without error. */
    512     result = PyLong_AsOff_t(value);
    513     if (result != -1 || !(runerr = PyErr_Occurred()))
    514         goto finish;
    515 
    516     /* Error handling code -- only manage OverflowError differently */
    517     if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
    518         goto finish;
    519 
    520     PyErr_Clear();
    521     /* If no error-handling desired then the default clipping
    522        is sufficient.
    523      */
    524     if (!err) {
    525         assert(PyLong_Check(value));
    526         /* Whether or not it is less than or equal to
    527            zero is determined by the sign of ob_size
    528         */
    529         if (_PyLong_Sign(value) < 0)
    530             result = PY_OFF_T_MIN;
    531         else
    532             result = PY_OFF_T_MAX;
    533     }
    534     else {
    535         /* Otherwise replace the error with caller's error object. */
    536         PyErr_Format(err,
    537                      "cannot fit '%.200s' into an offset-sized integer",
    538                      item->ob_type->tp_name);
    539     }
    540 
    541  finish:
    542     Py_DECREF(value);
    543     return result;
    544 }
    545 
    546 
    547 _PyIO_State *
    548 _PyIO_get_module_state(void)
    549 {
    550     PyObject *mod = PyState_FindModule(&_PyIO_Module);
    551     _PyIO_State *state;
    552     if (mod == NULL || (state = IO_MOD_STATE(mod)) == NULL) {
    553         PyErr_SetString(PyExc_RuntimeError,
    554                         "could not find io module state "
    555                         "(interpreter shutdown?)");
    556         return NULL;
    557     }
    558     return state;
    559 }
    560 
    561 PyObject *
    562 _PyIO_get_locale_module(_PyIO_State *state)
    563 {
    564     PyObject *mod;
    565     if (state->locale_module != NULL) {
    566         assert(PyWeakref_CheckRef(state->locale_module));
    567         mod = PyWeakref_GET_OBJECT(state->locale_module);
    568         if (mod != Py_None) {
    569             Py_INCREF(mod);
    570             return mod;
    571         }
    572         Py_CLEAR(state->locale_module);
    573     }
    574     mod = PyImport_ImportModule("_bootlocale");
    575     if (mod == NULL)
    576         return NULL;
    577     state->locale_module = PyWeakref_NewRef(mod, NULL);
    578     if (state->locale_module == NULL) {
    579         Py_DECREF(mod);
    580         return NULL;
    581     }
    582     return mod;
    583 }
    584 
    585 
    586 static int
    587 iomodule_traverse(PyObject *mod, visitproc visit, void *arg) {
    588     _PyIO_State *state = IO_MOD_STATE(mod);
    589     if (!state->initialized)
    590         return 0;
    591     if (state->locale_module != NULL) {
    592         Py_VISIT(state->locale_module);
    593     }
    594     Py_VISIT(state->unsupported_operation);
    595     return 0;
    596 }
    597 
    598 
    599 static int
    600 iomodule_clear(PyObject *mod) {
    601     _PyIO_State *state = IO_MOD_STATE(mod);
    602     if (!state->initialized)
    603         return 0;
    604     if (state->locale_module != NULL)
    605         Py_CLEAR(state->locale_module);
    606     Py_CLEAR(state->unsupported_operation);
    607     return 0;
    608 }
    609 
    610 static void
    611 iomodule_free(PyObject *mod) {
    612     iomodule_clear(mod);
    613 }
    614 
    615 
    616 /*
    617  * Module definition
    618  */
    619 
    620 #include "clinic/_iomodule.c.h"
    621 
    622 static PyMethodDef module_methods[] = {
    623     _IO_OPEN_METHODDEF
    624     {NULL, NULL}
    625 };
    626 
    627 struct PyModuleDef _PyIO_Module = {
    628     PyModuleDef_HEAD_INIT,
    629     "io",
    630     module_doc,
    631     sizeof(_PyIO_State),
    632     module_methods,
    633     NULL,
    634     iomodule_traverse,
    635     iomodule_clear,
    636     (freefunc)iomodule_free,
    637 };
    638 
    639 PyMODINIT_FUNC
    640 PyInit__io(void)
    641 {
    642     PyObject *m = PyModule_Create(&_PyIO_Module);
    643     _PyIO_State *state = NULL;
    644     if (m == NULL)
    645         return NULL;
    646     state = IO_MOD_STATE(m);
    647     state->initialized = 0;
    648 
    649 #define ADD_TYPE(type, name) \
    650     if (PyType_Ready(type) < 0) \
    651         goto fail; \
    652     Py_INCREF(type); \
    653     if (PyModule_AddObject(m, name, (PyObject *)type) < 0) {  \
    654         Py_DECREF(type); \
    655         goto fail; \
    656     }
    657 
    658     /* DEFAULT_BUFFER_SIZE */
    659     if (PyModule_AddIntMacro(m, DEFAULT_BUFFER_SIZE) < 0)
    660         goto fail;
    661 
    662     /* UnsupportedOperation inherits from ValueError and OSError */
    663     state->unsupported_operation = PyObject_CallFunction(
    664         (PyObject *)&PyType_Type, "s(OO){}",
    665         "UnsupportedOperation", PyExc_OSError, PyExc_ValueError);
    666     if (state->unsupported_operation == NULL)
    667         goto fail;
    668     Py_INCREF(state->unsupported_operation);
    669     if (PyModule_AddObject(m, "UnsupportedOperation",
    670                            state->unsupported_operation) < 0)
    671         goto fail;
    672 
    673     /* BlockingIOError, for compatibility */
    674     Py_INCREF(PyExc_BlockingIOError);
    675     if (PyModule_AddObject(m, "BlockingIOError",
    676                            (PyObject *) PyExc_BlockingIOError) < 0)
    677         goto fail;
    678 
    679     /* Concrete base types of the IO ABCs.
    680        (the ABCs themselves are declared through inheritance in io.py)
    681     */
    682     ADD_TYPE(&PyIOBase_Type, "_IOBase");
    683     ADD_TYPE(&PyRawIOBase_Type, "_RawIOBase");
    684     ADD_TYPE(&PyBufferedIOBase_Type, "_BufferedIOBase");
    685     ADD_TYPE(&PyTextIOBase_Type, "_TextIOBase");
    686 
    687     /* Implementation of concrete IO objects. */
    688     /* FileIO */
    689     PyFileIO_Type.tp_base = &PyRawIOBase_Type;
    690     ADD_TYPE(&PyFileIO_Type, "FileIO");
    691 
    692     /* BytesIO */
    693     PyBytesIO_Type.tp_base = &PyBufferedIOBase_Type;
    694     ADD_TYPE(&PyBytesIO_Type, "BytesIO");
    695     if (PyType_Ready(&_PyBytesIOBuffer_Type) < 0)
    696         goto fail;
    697 
    698     /* StringIO */
    699     PyStringIO_Type.tp_base = &PyTextIOBase_Type;
    700     ADD_TYPE(&PyStringIO_Type, "StringIO");
    701 
    702 #ifdef MS_WINDOWS
    703     /* WindowsConsoleIO */
    704     PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type;
    705     ADD_TYPE(&PyWindowsConsoleIO_Type, "_WindowsConsoleIO");
    706 #endif
    707 
    708     /* BufferedReader */
    709     PyBufferedReader_Type.tp_base = &PyBufferedIOBase_Type;
    710     ADD_TYPE(&PyBufferedReader_Type, "BufferedReader");
    711 
    712     /* BufferedWriter */
    713     PyBufferedWriter_Type.tp_base = &PyBufferedIOBase_Type;
    714     ADD_TYPE(&PyBufferedWriter_Type, "BufferedWriter");
    715 
    716     /* BufferedRWPair */
    717     PyBufferedRWPair_Type.tp_base = &PyBufferedIOBase_Type;
    718     ADD_TYPE(&PyBufferedRWPair_Type, "BufferedRWPair");
    719 
    720     /* BufferedRandom */
    721     PyBufferedRandom_Type.tp_base = &PyBufferedIOBase_Type;
    722     ADD_TYPE(&PyBufferedRandom_Type, "BufferedRandom");
    723 
    724     /* TextIOWrapper */
    725     PyTextIOWrapper_Type.tp_base = &PyTextIOBase_Type;
    726     ADD_TYPE(&PyTextIOWrapper_Type, "TextIOWrapper");
    727 
    728     /* IncrementalNewlineDecoder */
    729     ADD_TYPE(&PyIncrementalNewlineDecoder_Type, "IncrementalNewlineDecoder");
    730 
    731     /* Interned strings */
    732 #define ADD_INTERNED(name) \
    733     if (!_PyIO_str_ ## name && \
    734         !(_PyIO_str_ ## name = PyUnicode_InternFromString(# name))) \
    735         goto fail;
    736 
    737     ADD_INTERNED(close)
    738     ADD_INTERNED(closed)
    739     ADD_INTERNED(decode)
    740     ADD_INTERNED(encode)
    741     ADD_INTERNED(fileno)
    742     ADD_INTERNED(flush)
    743     ADD_INTERNED(getstate)
    744     ADD_INTERNED(isatty)
    745     ADD_INTERNED(newlines)
    746     ADD_INTERNED(peek)
    747     ADD_INTERNED(read)
    748     ADD_INTERNED(read1)
    749     ADD_INTERNED(readable)
    750     ADD_INTERNED(readall)
    751     ADD_INTERNED(readinto)
    752     ADD_INTERNED(readline)
    753     ADD_INTERNED(reset)
    754     ADD_INTERNED(seek)
    755     ADD_INTERNED(seekable)
    756     ADD_INTERNED(setstate)
    757     ADD_INTERNED(tell)
    758     ADD_INTERNED(truncate)
    759     ADD_INTERNED(write)
    760     ADD_INTERNED(writable)
    761 
    762     if (!_PyIO_str_nl &&
    763         !(_PyIO_str_nl = PyUnicode_InternFromString("\n")))
    764         goto fail;
    765 
    766     if (!_PyIO_empty_str &&
    767         !(_PyIO_empty_str = PyUnicode_FromStringAndSize(NULL, 0)))
    768         goto fail;
    769     if (!_PyIO_empty_bytes &&
    770         !(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0)))
    771         goto fail;
    772 
    773     state->initialized = 1;
    774 
    775     return m;
    776 
    777   fail:
    778     Py_XDECREF(state->unsupported_operation);
    779     Py_DECREF(m);
    780     return NULL;
    781 }
    782