Home | History | Annotate | Download | only in _io
      1 /*
      2     An implementation of the I/O abstract base classes hierarchy
      3     as defined by PEP 3116 - "New I/O"
      4 
      5     Classes defined here: IOBase, RawIOBase.
      6 
      7     Written by Amaury Forgeot d'Arc and Antoine Pitrou
      8 */
      9 
     10 
     11 #define PY_SSIZE_T_CLEAN
     12 #include "Python.h"
     13 #include "structmember.h"
     14 #include "_iomodule.h"
     15 
     16 /*[clinic input]
     17 module _io
     18 class _io._IOBase "PyObject *" "&PyIOBase_Type"
     19 class _io._RawIOBase "PyObject *" "&PyRawIOBase_Type"
     20 [clinic start generated code]*/
     21 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=d29a4d076c2b211c]*/
     22 
     23 /*
     24  * IOBase class, an abstract class
     25  */
     26 
     27 typedef struct {
     28     PyObject_HEAD
     29 
     30     PyObject *dict;
     31     PyObject *weakreflist;
     32 } iobase;
     33 
     34 PyDoc_STRVAR(iobase_doc,
     35     "The abstract base class for all I/O classes, acting on streams of\n"
     36     "bytes. There is no public constructor.\n"
     37     "\n"
     38     "This class provides dummy implementations for many methods that\n"
     39     "derived classes can override selectively; the default implementations\n"
     40     "represent a file that cannot be read, written or seeked.\n"
     41     "\n"
     42     "Even though IOBase does not declare read, readinto, or write because\n"
     43     "their signatures will vary, implementations and clients should\n"
     44     "consider those methods part of the interface. Also, implementations\n"
     45     "may raise UnsupportedOperation when operations they do not support are\n"
     46     "called.\n"
     47     "\n"
     48     "The basic type used for binary data read from or written to a file is\n"
     49     "bytes. Other bytes-like objects are accepted as method arguments too.\n"
     50     "In some cases (such as readinto), a writable object is required. Text\n"
     51     "I/O classes work with str data.\n"
     52     "\n"
     53     "Note that calling any method (except additional calls to close(),\n"
     54     "which are ignored) on a closed stream should raise a ValueError.\n"
     55     "\n"
     56     "IOBase (and its subclasses) support the iterator protocol, meaning\n"
     57     "that an IOBase object can be iterated over yielding the lines in a\n"
     58     "stream.\n"
     59     "\n"
     60     "IOBase also supports the :keyword:`with` statement. In this example,\n"
     61     "fp is closed after the suite of the with statement is complete:\n"
     62     "\n"
     63     "with open('spam.txt', 'r') as fp:\n"
     64     "    fp.write('Spam and eggs!')\n");
     65 
     66 /* Use this macro whenever you want to check the internal `closed` status
     67    of the IOBase object rather than the virtual `closed` attribute as returned
     68    by whatever subclass. */
     69 
     70 _Py_IDENTIFIER(__IOBase_closed);
     71 _Py_IDENTIFIER(read);
     72 
     73 
     74 /* Internal methods */
     75 static PyObject *
     76 iobase_unsupported(const char *message)
     77 {
     78     _PyIO_State *state = IO_STATE();
     79     if (state != NULL)
     80         PyErr_SetString(state->unsupported_operation, message);
     81     return NULL;
     82 }
     83 
     84 /* Positioning */
     85 
     86 PyDoc_STRVAR(iobase_seek_doc,
     87     "Change stream position.\n"
     88     "\n"
     89     "Change the stream position to the given byte offset. The offset is\n"
     90     "interpreted relative to the position indicated by whence.  Values\n"
     91     "for whence are:\n"
     92     "\n"
     93     "* 0 -- start of stream (the default); offset should be zero or positive\n"
     94     "* 1 -- current stream position; offset may be negative\n"
     95     "* 2 -- end of stream; offset is usually negative\n"
     96     "\n"
     97     "Return the new absolute position.");
     98 
     99 static PyObject *
    100 iobase_seek(PyObject *self, PyObject *args)
    101 {
    102     return iobase_unsupported("seek");
    103 }
    104 
    105 /*[clinic input]
    106 _io._IOBase.tell
    107 
    108 Return current stream position.
    109 [clinic start generated code]*/
    110 
    111 static PyObject *
    112 _io__IOBase_tell_impl(PyObject *self)
    113 /*[clinic end generated code: output=89a1c0807935abe2 input=04e615fec128801f]*/
    114 {
    115     _Py_IDENTIFIER(seek);
    116 
    117     return _PyObject_CallMethodId(self, &PyId_seek, "ii", 0, 1);
    118 }
    119 
    120 PyDoc_STRVAR(iobase_truncate_doc,
    121     "Truncate file to size bytes.\n"
    122     "\n"
    123     "File pointer is left unchanged.  Size defaults to the current IO\n"
    124     "position as reported by tell().  Returns the new size.");
    125 
    126 static PyObject *
    127 iobase_truncate(PyObject *self, PyObject *args)
    128 {
    129     return iobase_unsupported("truncate");
    130 }
    131 
    132 static int
    133 iobase_is_closed(PyObject *self)
    134 {
    135     PyObject *res;
    136     int ret;
    137     /* This gets the derived attribute, which is *not* __IOBase_closed
    138        in most cases! */
    139     ret = _PyObject_LookupAttrId(self, &PyId___IOBase_closed, &res);
    140     Py_XDECREF(res);
    141     return ret;
    142 }
    143 
    144 /* Flush and close methods */
    145 
    146 /*[clinic input]
    147 _io._IOBase.flush
    148 
    149 Flush write buffers, if applicable.
    150 
    151 This is not implemented for read-only and non-blocking streams.
    152 [clinic start generated code]*/
    153 
    154 static PyObject *
    155 _io__IOBase_flush_impl(PyObject *self)
    156 /*[clinic end generated code: output=7cef4b4d54656a3b input=773be121abe270aa]*/
    157 {
    158     /* XXX Should this return the number of bytes written??? */
    159     int closed = iobase_is_closed(self);
    160 
    161     if (!closed) {
    162         Py_RETURN_NONE;
    163     }
    164     if (closed > 0) {
    165         PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
    166     }
    167     return NULL;
    168 }
    169 
    170 static PyObject *
    171 iobase_closed_get(PyObject *self, void *context)
    172 {
    173     int closed = iobase_is_closed(self);
    174     if (closed < 0) {
    175         return NULL;
    176     }
    177     return PyBool_FromLong(closed);
    178 }
    179 
    180 static int
    181 iobase_check_closed(PyObject *self)
    182 {
    183     PyObject *res;
    184     int closed;
    185     /* This gets the derived attribute, which is *not* __IOBase_closed
    186        in most cases! */
    187     closed = _PyObject_LookupAttr(self, _PyIO_str_closed, &res);
    188     if (closed > 0) {
    189         closed = PyObject_IsTrue(res);
    190         Py_DECREF(res);
    191         if (closed > 0) {
    192             PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
    193             return -1;
    194         }
    195     }
    196     return closed;
    197 }
    198 
    199 PyObject *
    200 _PyIOBase_check_closed(PyObject *self, PyObject *args)
    201 {
    202     if (iobase_check_closed(self)) {
    203         return NULL;
    204     }
    205     if (args == Py_True) {
    206         return Py_None;
    207     }
    208     Py_RETURN_NONE;
    209 }
    210 
    211 /* XXX: IOBase thinks it has to maintain its own internal state in
    212    `__IOBase_closed` and call flush() by itself, but it is redundant with
    213    whatever behaviour a non-trivial derived class will implement. */
    214 
    215 /*[clinic input]
    216 _io._IOBase.close
    217 
    218 Flush and close the IO object.
    219 
    220 This method has no effect if the file is already closed.
    221 [clinic start generated code]*/
    222 
    223 static PyObject *
    224 _io__IOBase_close_impl(PyObject *self)
    225 /*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/
    226 {
    227     PyObject *res, *exc, *val, *tb;
    228     int rc, closed = iobase_is_closed(self);
    229 
    230     if (closed < 0) {
    231         return NULL;
    232     }
    233     if (closed) {
    234         Py_RETURN_NONE;
    235     }
    236 
    237     res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);
    238 
    239     PyErr_Fetch(&exc, &val, &tb);
    240     rc = _PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True);
    241     _PyErr_ChainExceptions(exc, val, tb);
    242     if (rc < 0) {
    243         Py_CLEAR(res);
    244     }
    245 
    246     if (res == NULL)
    247         return NULL;
    248 
    249     Py_DECREF(res);
    250     Py_RETURN_NONE;
    251 }
    252 
    253 /* Finalization and garbage collection support */
    254 
    255 static void
    256 iobase_finalize(PyObject *self)
    257 {
    258     PyObject *res;
    259     PyObject *error_type, *error_value, *error_traceback;
    260     int closed;
    261     _Py_IDENTIFIER(_finalizing);
    262 
    263     /* Save the current exception, if any. */
    264     PyErr_Fetch(&error_type, &error_value, &error_traceback);
    265 
    266     /* If `closed` doesn't exist or can't be evaluated as bool, then the
    267        object is probably in an unusable state, so ignore. */
    268     if (_PyObject_LookupAttr(self, _PyIO_str_closed, &res) <= 0) {
    269         PyErr_Clear();
    270         closed = -1;
    271     }
    272     else {
    273         closed = PyObject_IsTrue(res);
    274         Py_DECREF(res);
    275         if (closed == -1)
    276             PyErr_Clear();
    277     }
    278     if (closed == 0) {
    279         /* Signal close() that it was called as part of the object
    280            finalization process. */
    281         if (_PyObject_SetAttrId(self, &PyId__finalizing, Py_True))
    282             PyErr_Clear();
    283         res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_close,
    284                                           NULL);
    285         /* Silencing I/O errors is bad, but printing spurious tracebacks is
    286            equally as bad, and potentially more frequent (because of
    287            shutdown issues). */
    288         if (res == NULL)
    289             PyErr_Clear();
    290         else
    291             Py_DECREF(res);
    292     }
    293 
    294     /* Restore the saved exception. */
    295     PyErr_Restore(error_type, error_value, error_traceback);
    296 }
    297 
    298 int
    299 _PyIOBase_finalize(PyObject *self)
    300 {
    301     int is_zombie;
    302 
    303     /* If _PyIOBase_finalize() is called from a destructor, we need to
    304        resurrect the object as calling close() can invoke arbitrary code. */
    305     is_zombie = (Py_REFCNT(self) == 0);
    306     if (is_zombie)
    307         return PyObject_CallFinalizerFromDealloc(self);
    308     else {
    309         PyObject_CallFinalizer(self);
    310         return 0;
    311     }
    312 }
    313 
    314 static int
    315 iobase_traverse(iobase *self, visitproc visit, void *arg)
    316 {
    317     Py_VISIT(self->dict);
    318     return 0;
    319 }
    320 
    321 static int
    322 iobase_clear(iobase *self)
    323 {
    324     Py_CLEAR(self->dict);
    325     return 0;
    326 }
    327 
    328 /* Destructor */
    329 
    330 static void
    331 iobase_dealloc(iobase *self)
    332 {
    333     /* NOTE: since IOBaseObject has its own dict, Python-defined attributes
    334        are still available here for close() to use.
    335        However, if the derived class declares a __slots__, those slots are
    336        already gone.
    337     */
    338     if (_PyIOBase_finalize((PyObject *) self) < 0) {
    339         /* When called from a heap type's dealloc, the type will be
    340            decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */
    341         if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE))
    342             Py_INCREF(Py_TYPE(self));
    343         return;
    344     }
    345     _PyObject_GC_UNTRACK(self);
    346     if (self->weakreflist != NULL)
    347         PyObject_ClearWeakRefs((PyObject *) self);
    348     Py_CLEAR(self->dict);
    349     Py_TYPE(self)->tp_free((PyObject *) self);
    350 }
    351 
    352 /* Inquiry methods */
    353 
    354 /*[clinic input]
    355 _io._IOBase.seekable
    356 
    357 Return whether object supports random access.
    358 
    359 If False, seek(), tell() and truncate() will raise OSError.
    360 This method may need to do a test seek().
    361 [clinic start generated code]*/
    362 
    363 static PyObject *
    364 _io__IOBase_seekable_impl(PyObject *self)
    365 /*[clinic end generated code: output=4c24c67f5f32a43d input=b976622f7fdf3063]*/
    366 {
    367     Py_RETURN_FALSE;
    368 }
    369 
    370 PyObject *
    371 _PyIOBase_check_seekable(PyObject *self, PyObject *args)
    372 {
    373     PyObject *res  = PyObject_CallMethodObjArgs(self, _PyIO_str_seekable, NULL);
    374     if (res == NULL)
    375         return NULL;
    376     if (res != Py_True) {
    377         Py_CLEAR(res);
    378         iobase_unsupported("File or stream is not seekable.");
    379         return NULL;
    380     }
    381     if (args == Py_True) {
    382         Py_DECREF(res);
    383     }
    384     return res;
    385 }
    386 
    387 /*[clinic input]
    388 _io._IOBase.readable
    389 
    390 Return whether object was opened for reading.
    391 
    392 If False, read() will raise OSError.
    393 [clinic start generated code]*/
    394 
    395 static PyObject *
    396 _io__IOBase_readable_impl(PyObject *self)
    397 /*[clinic end generated code: output=e48089250686388b input=285b3b866a0ec35f]*/
    398 {
    399     Py_RETURN_FALSE;
    400 }
    401 
    402 /* May be called with any object */
    403 PyObject *
    404 _PyIOBase_check_readable(PyObject *self, PyObject *args)
    405 {
    406     PyObject *res  = PyObject_CallMethodObjArgs(self, _PyIO_str_readable, NULL);
    407     if (res == NULL)
    408         return NULL;
    409     if (res != Py_True) {
    410         Py_CLEAR(res);
    411         iobase_unsupported("File or stream is not readable.");
    412         return NULL;
    413     }
    414     if (args == Py_True) {
    415         Py_DECREF(res);
    416     }
    417     return res;
    418 }
    419 
    420 /*[clinic input]
    421 _io._IOBase.writable
    422 
    423 Return whether object was opened for writing.
    424 
    425 If False, write() will raise OSError.
    426 [clinic start generated code]*/
    427 
    428 static PyObject *
    429 _io__IOBase_writable_impl(PyObject *self)
    430 /*[clinic end generated code: output=406001d0985be14f input=9dcac18a013a05b5]*/
    431 {
    432     Py_RETURN_FALSE;
    433 }
    434 
    435 /* May be called with any object */
    436 PyObject *
    437 _PyIOBase_check_writable(PyObject *self, PyObject *args)
    438 {
    439     PyObject *res  = PyObject_CallMethodObjArgs(self, _PyIO_str_writable, NULL);
    440     if (res == NULL)
    441         return NULL;
    442     if (res != Py_True) {
    443         Py_CLEAR(res);
    444         iobase_unsupported("File or stream is not writable.");
    445         return NULL;
    446     }
    447     if (args == Py_True) {
    448         Py_DECREF(res);
    449     }
    450     return res;
    451 }
    452 
    453 /* Context manager */
    454 
    455 static PyObject *
    456 iobase_enter(PyObject *self, PyObject *args)
    457 {
    458     if (iobase_check_closed(self))
    459         return NULL;
    460 
    461     Py_INCREF(self);
    462     return self;
    463 }
    464 
    465 static PyObject *
    466 iobase_exit(PyObject *self, PyObject *args)
    467 {
    468     return PyObject_CallMethodObjArgs(self, _PyIO_str_close, NULL);
    469 }
    470 
    471 /* Lower-level APIs */
    472 
    473 /* XXX Should these be present even if unimplemented? */
    474 
    475 /*[clinic input]
    476 _io._IOBase.fileno
    477 
    478 Returns underlying file descriptor if one exists.
    479 
    480 OSError is raised if the IO object does not use a file descriptor.
    481 [clinic start generated code]*/
    482 
    483 static PyObject *
    484 _io__IOBase_fileno_impl(PyObject *self)
    485 /*[clinic end generated code: output=7cc0973f0f5f3b73 input=4e37028947dc1cc8]*/
    486 {
    487     return iobase_unsupported("fileno");
    488 }
    489 
    490 /*[clinic input]
    491 _io._IOBase.isatty
    492 
    493 Return whether this is an 'interactive' stream.
    494 
    495 Return False if it can't be determined.
    496 [clinic start generated code]*/
    497 
    498 static PyObject *
    499 _io__IOBase_isatty_impl(PyObject *self)
    500 /*[clinic end generated code: output=60cab77cede41cdd input=9ef76530d368458b]*/
    501 {
    502     if (iobase_check_closed(self))
    503         return NULL;
    504     Py_RETURN_FALSE;
    505 }
    506 
    507 /* Readline(s) and writelines */
    508 
    509 /*[clinic input]
    510 _io._IOBase.readline
    511     size as limit: Py_ssize_t(accept={int, NoneType}) = -1
    512     /
    513 
    514 Read and return a line from the stream.
    515 
    516 If size is specified, at most size bytes will be read.
    517 
    518 The line terminator is always b'\n' for binary files; for text
    519 files, the newlines argument to open can be used to select the line
    520 terminator(s) recognized.
    521 [clinic start generated code]*/
    522 
    523 static PyObject *
    524 _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit)
    525 /*[clinic end generated code: output=4479f79b58187840 input=d0c596794e877bff]*/
    526 {
    527     /* For backwards compatibility, a (slowish) readline(). */
    528 
    529     PyObject *peek, *buffer, *result;
    530     Py_ssize_t old_size = -1;
    531 
    532     if (_PyObject_LookupAttr(self, _PyIO_str_peek, &peek) < 0) {
    533         return NULL;
    534     }
    535 
    536     buffer = PyByteArray_FromStringAndSize(NULL, 0);
    537     if (buffer == NULL) {
    538         Py_XDECREF(peek);
    539         return NULL;
    540     }
    541 
    542     while (limit < 0 || PyByteArray_GET_SIZE(buffer) < limit) {
    543         Py_ssize_t nreadahead = 1;
    544         PyObject *b;
    545 
    546         if (peek != NULL) {
    547             PyObject *readahead = PyObject_CallFunctionObjArgs(peek, _PyLong_One, NULL);
    548             if (readahead == NULL) {
    549                 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
    550                    when EINTR occurs so we needn't do it ourselves. */
    551                 if (_PyIO_trap_eintr()) {
    552                     continue;
    553                 }
    554                 goto fail;
    555             }
    556             if (!PyBytes_Check(readahead)) {
    557                 PyErr_Format(PyExc_OSError,
    558                              "peek() should have returned a bytes object, "
    559                              "not '%.200s'", Py_TYPE(readahead)->tp_name);
    560                 Py_DECREF(readahead);
    561                 goto fail;
    562             }
    563             if (PyBytes_GET_SIZE(readahead) > 0) {
    564                 Py_ssize_t n = 0;
    565                 const char *buf = PyBytes_AS_STRING(readahead);
    566                 if (limit >= 0) {
    567                     do {
    568                         if (n >= PyBytes_GET_SIZE(readahead) || n >= limit)
    569                             break;
    570                         if (buf[n++] == '\n')
    571                             break;
    572                     } while (1);
    573                 }
    574                 else {
    575                     do {
    576                         if (n >= PyBytes_GET_SIZE(readahead))
    577                             break;
    578                         if (buf[n++] == '\n')
    579                             break;
    580                     } while (1);
    581                 }
    582                 nreadahead = n;
    583             }
    584             Py_DECREF(readahead);
    585         }
    586 
    587         b = _PyObject_CallMethodId(self, &PyId_read, "n", nreadahead);
    588         if (b == NULL) {
    589             /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
    590                when EINTR occurs so we needn't do it ourselves. */
    591             if (_PyIO_trap_eintr()) {
    592                 continue;
    593             }
    594             goto fail;
    595         }
    596         if (!PyBytes_Check(b)) {
    597             PyErr_Format(PyExc_OSError,
    598                          "read() should have returned a bytes object, "
    599                          "not '%.200s'", Py_TYPE(b)->tp_name);
    600             Py_DECREF(b);
    601             goto fail;
    602         }
    603         if (PyBytes_GET_SIZE(b) == 0) {
    604             Py_DECREF(b);
    605             break;
    606         }
    607 
    608         old_size = PyByteArray_GET_SIZE(buffer);
    609         if (PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)) < 0) {
    610             Py_DECREF(b);
    611             goto fail;
    612         }
    613         memcpy(PyByteArray_AS_STRING(buffer) + old_size,
    614                PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b));
    615 
    616         Py_DECREF(b);
    617 
    618         if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n')
    619             break;
    620     }
    621 
    622     result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer),
    623                                        PyByteArray_GET_SIZE(buffer));
    624     Py_XDECREF(peek);
    625     Py_DECREF(buffer);
    626     return result;
    627   fail:
    628     Py_XDECREF(peek);
    629     Py_DECREF(buffer);
    630     return NULL;
    631 }
    632 
    633 static PyObject *
    634 iobase_iter(PyObject *self)
    635 {
    636     if (iobase_check_closed(self))
    637         return NULL;
    638 
    639     Py_INCREF(self);
    640     return self;
    641 }
    642 
    643 static PyObject *
    644 iobase_iternext(PyObject *self)
    645 {
    646     PyObject *line = PyObject_CallMethodObjArgs(self, _PyIO_str_readline, NULL);
    647 
    648     if (line == NULL)
    649         return NULL;
    650 
    651     if (PyObject_Size(line) <= 0) {
    652         /* Error or empty */
    653         Py_DECREF(line);
    654         return NULL;
    655     }
    656 
    657     return line;
    658 }
    659 
    660 /*[clinic input]
    661 _io._IOBase.readlines
    662     hint: Py_ssize_t(accept={int, NoneType}) = -1
    663     /
    664 
    665 Return a list of lines from the stream.
    666 
    667 hint can be specified to control the number of lines read: no more
    668 lines will be read if the total size (in bytes/characters) of all
    669 lines so far exceeds hint.
    670 [clinic start generated code]*/
    671 
    672 static PyObject *
    673 _io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint)
    674 /*[clinic end generated code: output=2f50421677fa3dea input=9400c786ea9dc416]*/
    675 {
    676     Py_ssize_t length = 0;
    677     PyObject *result, *it = NULL;
    678 
    679     result = PyList_New(0);
    680     if (result == NULL)
    681         return NULL;
    682 
    683     if (hint <= 0) {
    684         /* XXX special-casing this made sense in the Python version in order
    685            to remove the bytecode interpretation overhead, but it could
    686            probably be removed here. */
    687         _Py_IDENTIFIER(extend);
    688         PyObject *ret = _PyObject_CallMethodIdObjArgs(result, &PyId_extend,
    689                                                       self, NULL);
    690 
    691         if (ret == NULL) {
    692             goto error;
    693         }
    694         Py_DECREF(ret);
    695         return result;
    696     }
    697 
    698     it = PyObject_GetIter(self);
    699     if (it == NULL) {
    700         goto error;
    701     }
    702 
    703     while (1) {
    704         Py_ssize_t line_length;
    705         PyObject *line = PyIter_Next(it);
    706         if (line == NULL) {
    707             if (PyErr_Occurred()) {
    708                 goto error;
    709             }
    710             else
    711                 break; /* StopIteration raised */
    712         }
    713 
    714         if (PyList_Append(result, line) < 0) {
    715             Py_DECREF(line);
    716             goto error;
    717         }
    718         line_length = PyObject_Size(line);
    719         Py_DECREF(line);
    720         if (line_length < 0) {
    721             goto error;
    722         }
    723         if (line_length > hint - length)
    724             break;
    725         length += line_length;
    726     }
    727 
    728     Py_DECREF(it);
    729     return result;
    730 
    731  error:
    732     Py_XDECREF(it);
    733     Py_DECREF(result);
    734     return NULL;
    735 }
    736 
    737 /*[clinic input]
    738 _io._IOBase.writelines
    739     lines: object
    740     /
    741 [clinic start generated code]*/
    742 
    743 static PyObject *
    744 _io__IOBase_writelines(PyObject *self, PyObject *lines)
    745 /*[clinic end generated code: output=976eb0a9b60a6628 input=432e729a8450b3cb]*/
    746 {
    747     PyObject *iter, *res;
    748 
    749     if (iobase_check_closed(self))
    750         return NULL;
    751 
    752     iter = PyObject_GetIter(lines);
    753     if (iter == NULL)
    754         return NULL;
    755 
    756     while (1) {
    757         PyObject *line = PyIter_Next(iter);
    758         if (line == NULL) {
    759             if (PyErr_Occurred()) {
    760                 Py_DECREF(iter);
    761                 return NULL;
    762             }
    763             else
    764                 break; /* Stop Iteration */
    765         }
    766 
    767         res = NULL;
    768         do {
    769             res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL);
    770         } while (res == NULL && _PyIO_trap_eintr());
    771         Py_DECREF(line);
    772         if (res == NULL) {
    773             Py_DECREF(iter);
    774             return NULL;
    775         }
    776         Py_DECREF(res);
    777     }
    778     Py_DECREF(iter);
    779     Py_RETURN_NONE;
    780 }
    781 
    782 #include "clinic/iobase.c.h"
    783 
    784 static PyMethodDef iobase_methods[] = {
    785     {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc},
    786     _IO__IOBASE_TELL_METHODDEF
    787     {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc},
    788     _IO__IOBASE_FLUSH_METHODDEF
    789     _IO__IOBASE_CLOSE_METHODDEF
    790 
    791     _IO__IOBASE_SEEKABLE_METHODDEF
    792     _IO__IOBASE_READABLE_METHODDEF
    793     _IO__IOBASE_WRITABLE_METHODDEF
    794 
    795     {"_checkClosed",   _PyIOBase_check_closed, METH_NOARGS},
    796     {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS},
    797     {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS},
    798     {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS},
    799 
    800     _IO__IOBASE_FILENO_METHODDEF
    801     _IO__IOBASE_ISATTY_METHODDEF
    802 
    803     {"__enter__", iobase_enter, METH_NOARGS},
    804     {"__exit__", iobase_exit, METH_VARARGS},
    805 
    806     _IO__IOBASE_READLINE_METHODDEF
    807     _IO__IOBASE_READLINES_METHODDEF
    808     _IO__IOBASE_WRITELINES_METHODDEF
    809 
    810     {NULL, NULL}
    811 };
    812 
    813 static PyGetSetDef iobase_getset[] = {
    814     {"__dict__", PyObject_GenericGetDict, NULL, NULL},
    815     {"closed", (getter)iobase_closed_get, NULL, NULL},
    816     {NULL}
    817 };
    818 
    819 
    820 PyTypeObject PyIOBase_Type = {
    821     PyVarObject_HEAD_INIT(NULL, 0)
    822     "_io._IOBase",              /*tp_name*/
    823     sizeof(iobase),             /*tp_basicsize*/
    824     0,                          /*tp_itemsize*/
    825     (destructor)iobase_dealloc, /*tp_dealloc*/
    826     0,                          /*tp_print*/
    827     0,                          /*tp_getattr*/
    828     0,                          /*tp_setattr*/
    829     0,                          /*tp_compare */
    830     0,                          /*tp_repr*/
    831     0,                          /*tp_as_number*/
    832     0,                          /*tp_as_sequence*/
    833     0,                          /*tp_as_mapping*/
    834     0,                          /*tp_hash */
    835     0,                          /*tp_call*/
    836     0,                          /*tp_str*/
    837     0,                          /*tp_getattro*/
    838     0,                          /*tp_setattro*/
    839     0,                          /*tp_as_buffer*/
    840     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
    841         | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE,   /*tp_flags*/
    842     iobase_doc,                 /* tp_doc */
    843     (traverseproc)iobase_traverse, /* tp_traverse */
    844     (inquiry)iobase_clear,      /* tp_clear */
    845     0,                          /* tp_richcompare */
    846     offsetof(iobase, weakreflist), /* tp_weaklistoffset */
    847     iobase_iter,                /* tp_iter */
    848     iobase_iternext,            /* tp_iternext */
    849     iobase_methods,             /* tp_methods */
    850     0,                          /* tp_members */
    851     iobase_getset,              /* tp_getset */
    852     0,                          /* tp_base */
    853     0,                          /* tp_dict */
    854     0,                          /* tp_descr_get */
    855     0,                          /* tp_descr_set */
    856     offsetof(iobase, dict),     /* tp_dictoffset */
    857     0,                          /* tp_init */
    858     0,                          /* tp_alloc */
    859     PyType_GenericNew,          /* tp_new */
    860     0,                          /* tp_free */
    861     0,                          /* tp_is_gc */
    862     0,                          /* tp_bases */
    863     0,                          /* tp_mro */
    864     0,                          /* tp_cache */
    865     0,                          /* tp_subclasses */
    866     0,                          /* tp_weaklist */
    867     0,                          /* tp_del */
    868     0,                          /* tp_version_tag */
    869     iobase_finalize,            /* tp_finalize */
    870 };
    871 
    872 
    873 /*
    874  * RawIOBase class, Inherits from IOBase.
    875  */
    876 PyDoc_STRVAR(rawiobase_doc,
    877              "Base class for raw binary I/O.");
    878 
    879 /*
    880  * The read() method is implemented by calling readinto(); derived classes
    881  * that want to support read() only need to implement readinto() as a
    882  * primitive operation.  In general, readinto() can be more efficient than
    883  * read().
    884  *
    885  * (It would be tempting to also provide an implementation of readinto() in
    886  * terms of read(), in case the latter is a more suitable primitive operation,
    887  * but that would lead to nasty recursion in case a subclass doesn't implement
    888  * either.)
    889 */
    890 
    891 /*[clinic input]
    892 _io._RawIOBase.read
    893     size as n: Py_ssize_t = -1
    894     /
    895 [clinic start generated code]*/
    896 
    897 static PyObject *
    898 _io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n)
    899 /*[clinic end generated code: output=6cdeb731e3c9f13c input=b6d0dcf6417d1374]*/
    900 {
    901     PyObject *b, *res;
    902 
    903     if (n < 0) {
    904         _Py_IDENTIFIER(readall);
    905 
    906         return _PyObject_CallMethodId(self, &PyId_readall, NULL);
    907     }
    908 
    909     /* TODO: allocate a bytes object directly instead and manually construct
    910        a writable memoryview pointing to it. */
    911     b = PyByteArray_FromStringAndSize(NULL, n);
    912     if (b == NULL)
    913         return NULL;
    914 
    915     res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL);
    916     if (res == NULL || res == Py_None) {
    917         Py_DECREF(b);
    918         return res;
    919     }
    920 
    921     n = PyNumber_AsSsize_t(res, PyExc_ValueError);
    922     Py_DECREF(res);
    923     if (n == -1 && PyErr_Occurred()) {
    924         Py_DECREF(b);
    925         return NULL;
    926     }
    927 
    928     res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);
    929     Py_DECREF(b);
    930     return res;
    931 }
    932 
    933 
    934 /*[clinic input]
    935 _io._RawIOBase.readall
    936 
    937 Read until EOF, using multiple read() call.
    938 [clinic start generated code]*/
    939 
    940 static PyObject *
    941 _io__RawIOBase_readall_impl(PyObject *self)
    942 /*[clinic end generated code: output=1987b9ce929425a0 input=688874141213622a]*/
    943 {
    944     int r;
    945     PyObject *chunks = PyList_New(0);
    946     PyObject *result;
    947 
    948     if (chunks == NULL)
    949         return NULL;
    950 
    951     while (1) {
    952         PyObject *data = _PyObject_CallMethodId(self, &PyId_read,
    953                                                 "i", DEFAULT_BUFFER_SIZE);
    954         if (!data) {
    955             /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
    956                when EINTR occurs so we needn't do it ourselves. */
    957             if (_PyIO_trap_eintr()) {
    958                 continue;
    959             }
    960             Py_DECREF(chunks);
    961             return NULL;
    962         }
    963         if (data == Py_None) {
    964             if (PyList_GET_SIZE(chunks) == 0) {
    965                 Py_DECREF(chunks);
    966                 return data;
    967             }
    968             Py_DECREF(data);
    969             break;
    970         }
    971         if (!PyBytes_Check(data)) {
    972             Py_DECREF(chunks);
    973             Py_DECREF(data);
    974             PyErr_SetString(PyExc_TypeError, "read() should return bytes");
    975             return NULL;
    976         }
    977         if (PyBytes_GET_SIZE(data) == 0) {
    978             /* EOF */
    979             Py_DECREF(data);
    980             break;
    981         }
    982         r = PyList_Append(chunks, data);
    983         Py_DECREF(data);
    984         if (r < 0) {
    985             Py_DECREF(chunks);
    986             return NULL;
    987         }
    988     }
    989     result = _PyBytes_Join(_PyIO_empty_bytes, chunks);
    990     Py_DECREF(chunks);
    991     return result;
    992 }
    993 
    994 static PyObject *
    995 rawiobase_readinto(PyObject *self, PyObject *args)
    996 {
    997     PyErr_SetNone(PyExc_NotImplementedError);
    998     return NULL;
    999 }
   1000 
   1001 static PyObject *
   1002 rawiobase_write(PyObject *self, PyObject *args)
   1003 {
   1004     PyErr_SetNone(PyExc_NotImplementedError);
   1005     return NULL;
   1006 }
   1007 
   1008 static PyMethodDef rawiobase_methods[] = {
   1009     _IO__RAWIOBASE_READ_METHODDEF
   1010     _IO__RAWIOBASE_READALL_METHODDEF
   1011     {"readinto", rawiobase_readinto, METH_VARARGS},
   1012     {"write", rawiobase_write, METH_VARARGS},
   1013     {NULL, NULL}
   1014 };
   1015 
   1016 PyTypeObject PyRawIOBase_Type = {
   1017     PyVarObject_HEAD_INIT(NULL, 0)
   1018     "_io._RawIOBase",                /*tp_name*/
   1019     0,                          /*tp_basicsize*/
   1020     0,                          /*tp_itemsize*/
   1021     0,                          /*tp_dealloc*/
   1022     0,                          /*tp_print*/
   1023     0,                          /*tp_getattr*/
   1024     0,                          /*tp_setattr*/
   1025     0,                          /*tp_compare */
   1026     0,                          /*tp_repr*/
   1027     0,                          /*tp_as_number*/
   1028     0,                          /*tp_as_sequence*/
   1029     0,                          /*tp_as_mapping*/
   1030     0,                          /*tp_hash */
   1031     0,                          /*tp_call*/
   1032     0,                          /*tp_str*/
   1033     0,                          /*tp_getattro*/
   1034     0,                          /*tp_setattro*/
   1035     0,                          /*tp_as_buffer*/
   1036     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE,  /*tp_flags*/
   1037     rawiobase_doc,              /* tp_doc */
   1038     0,                          /* tp_traverse */
   1039     0,                          /* tp_clear */
   1040     0,                          /* tp_richcompare */
   1041     0,                          /* tp_weaklistoffset */
   1042     0,                          /* tp_iter */
   1043     0,                          /* tp_iternext */
   1044     rawiobase_methods,          /* tp_methods */
   1045     0,                          /* tp_members */
   1046     0,                          /* tp_getset */
   1047     &PyIOBase_Type,             /* tp_base */
   1048     0,                          /* tp_dict */
   1049     0,                          /* tp_descr_get */
   1050     0,                          /* tp_descr_set */
   1051     0,                          /* tp_dictoffset */
   1052     0,                          /* tp_init */
   1053     0,                          /* tp_alloc */
   1054     0,                          /* tp_new */
   1055     0,                          /* tp_free */
   1056     0,                          /* tp_is_gc */
   1057     0,                          /* tp_bases */
   1058     0,                          /* tp_mro */
   1059     0,                          /* tp_cache */
   1060     0,                          /* tp_subclasses */
   1061     0,                          /* tp_weaklist */
   1062     0,                          /* tp_del */
   1063     0,                          /* tp_version_tag */
   1064     0,                          /* tp_finalize */
   1065 };
   1066