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