1  /*[clinic input]
       2  preserve
       3  [clinic start generated code]*/
       4  
       5  #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
       6  #  include "pycore_gc.h"            // PyGC_Head
       7  #  include "pycore_runtime.h"       // _Py_ID()
       8  #endif
       9  
      10  
      11  PyDoc_STRVAR(_io_FileIO_close__doc__,
      12  "close($self, /)\n"
      13  "--\n"
      14  "\n"
      15  "Close the file.\n"
      16  "\n"
      17  "A closed file cannot be used for further I/O operations.  close() may be\n"
      18  "called more than once without error.");
      19  
      20  #define _IO_FILEIO_CLOSE_METHODDEF    \
      21      {"close", _PyCFunction_CAST(_io_FileIO_close), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_FileIO_close__doc__},
      22  
      23  static PyObject *
      24  _io_FileIO_close_impl(fileio *self, PyTypeObject *cls);
      25  
      26  static PyObject *
      27  _io_FileIO_close(fileio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
      28  {
      29      if (nargs) {
      30          PyErr_SetString(PyExc_TypeError, "close() takes no arguments");
      31          return NULL;
      32      }
      33      return _io_FileIO_close_impl(self, cls);
      34  }
      35  
      36  PyDoc_STRVAR(_io_FileIO___init____doc__,
      37  "FileIO(file, mode=\'r\', closefd=True, opener=None)\n"
      38  "--\n"
      39  "\n"
      40  "Open a file.\n"
      41  "\n"
      42  "The mode can be \'r\' (default), \'w\', \'x\' or \'a\' for reading,\n"
      43  "writing, exclusive creation or appending.  The file will be created if it\n"
      44  "doesn\'t exist when opened for writing or appending; it will be truncated\n"
      45  "when opened for writing.  A FileExistsError will be raised if it already\n"
      46  "exists when opened for creating. Opening a file for creating implies\n"
      47  "writing so this mode behaves in a similar way to \'w\'.Add a \'+\' to the mode\n"
      48  "to allow simultaneous reading and writing. A custom opener can be used by\n"
      49  "passing a callable as *opener*. The underlying file descriptor for the file\n"
      50  "object is then obtained by calling opener with (*name*, *flags*).\n"
      51  "*opener* must return an open file descriptor (passing os.open as *opener*\n"
      52  "results in functionality similar to passing None).");
      53  
      54  static int
      55  _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
      56                           int closefd, PyObject *opener);
      57  
      58  static int
      59  _io_FileIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
      60  {
      61      int return_value = -1;
      62      #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
      63  
      64      #define NUM_KEYWORDS 4
      65      static struct {
      66          PyGC_Head _this_is_not_used;
      67          PyObject_VAR_HEAD
      68          PyObject *ob_item[NUM_KEYWORDS];
      69      } _kwtuple = {
      70          .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
      71          .ob_item = { &_Py_ID(file), &_Py_ID(mode), &_Py_ID(closefd), &_Py_ID(opener), },
      72      };
      73      #undef NUM_KEYWORDS
      74      #define KWTUPLE (&_kwtuple.ob_base.ob_base)
      75  
      76      #else  // !Py_BUILD_CORE
      77      #  define KWTUPLE NULL
      78      #endif  // !Py_BUILD_CORE
      79  
      80      static const char * const _keywords[] = {"file", "mode", "closefd", "opener", NULL};
      81      static _PyArg_Parser _parser = {
      82          .keywords = _keywords,
      83          .fname = "FileIO",
      84          .kwtuple = KWTUPLE,
      85      };
      86      #undef KWTUPLE
      87      PyObject *argsbuf[4];
      88      PyObject * const *fastargs;
      89      Py_ssize_t nargs = PyTuple_GET_SIZE(args);
      90      Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
      91      PyObject *nameobj;
      92      const char *mode = "r";
      93      int closefd = 1;
      94      PyObject *opener = Py_None;
      95  
      96      fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 4, 0, argsbuf);
      97      if (!fastargs) {
      98          goto exit;
      99      }
     100      nameobj = fastargs[0];
     101      if (!noptargs) {
     102          goto skip_optional_pos;
     103      }
     104      if (fastargs[1]) {
     105          if (!PyUnicode_Check(fastargs[1])) {
     106              _PyArg_BadArgument("FileIO", "argument 'mode'", "str", fastargs[1]);
     107              goto exit;
     108          }
     109          Py_ssize_t mode_length;
     110          mode = PyUnicode_AsUTF8AndSize(fastargs[1], &mode_length);
     111          if (mode == NULL) {
     112              goto exit;
     113          }
     114          if (strlen(mode) != (size_t)mode_length) {
     115              PyErr_SetString(PyExc_ValueError, "embedded null character");
     116              goto exit;
     117          }
     118          if (!--noptargs) {
     119              goto skip_optional_pos;
     120          }
     121      }
     122      if (fastargs[2]) {
     123          closefd = PyObject_IsTrue(fastargs[2]);
     124          if (closefd < 0) {
     125              goto exit;
     126          }
     127          if (!--noptargs) {
     128              goto skip_optional_pos;
     129          }
     130      }
     131      opener = fastargs[3];
     132  skip_optional_pos:
     133      return_value = _io_FileIO___init___impl((fileio *)self, nameobj, mode, closefd, opener);
     134  
     135  exit:
     136      return return_value;
     137  }
     138  
     139  PyDoc_STRVAR(_io_FileIO_fileno__doc__,
     140  "fileno($self, /)\n"
     141  "--\n"
     142  "\n"
     143  "Return the underlying file descriptor (an integer).");
     144  
     145  #define _IO_FILEIO_FILENO_METHODDEF    \
     146      {"fileno", (PyCFunction)_io_FileIO_fileno, METH_NOARGS, _io_FileIO_fileno__doc__},
     147  
     148  static PyObject *
     149  _io_FileIO_fileno_impl(fileio *self);
     150  
     151  static PyObject *
     152  _io_FileIO_fileno(fileio *self, PyObject *Py_UNUSED(ignored))
     153  {
     154      return _io_FileIO_fileno_impl(self);
     155  }
     156  
     157  PyDoc_STRVAR(_io_FileIO_readable__doc__,
     158  "readable($self, /)\n"
     159  "--\n"
     160  "\n"
     161  "True if file was opened in a read mode.");
     162  
     163  #define _IO_FILEIO_READABLE_METHODDEF    \
     164      {"readable", (PyCFunction)_io_FileIO_readable, METH_NOARGS, _io_FileIO_readable__doc__},
     165  
     166  static PyObject *
     167  _io_FileIO_readable_impl(fileio *self);
     168  
     169  static PyObject *
     170  _io_FileIO_readable(fileio *self, PyObject *Py_UNUSED(ignored))
     171  {
     172      return _io_FileIO_readable_impl(self);
     173  }
     174  
     175  PyDoc_STRVAR(_io_FileIO_writable__doc__,
     176  "writable($self, /)\n"
     177  "--\n"
     178  "\n"
     179  "True if file was opened in a write mode.");
     180  
     181  #define _IO_FILEIO_WRITABLE_METHODDEF    \
     182      {"writable", (PyCFunction)_io_FileIO_writable, METH_NOARGS, _io_FileIO_writable__doc__},
     183  
     184  static PyObject *
     185  _io_FileIO_writable_impl(fileio *self);
     186  
     187  static PyObject *
     188  _io_FileIO_writable(fileio *self, PyObject *Py_UNUSED(ignored))
     189  {
     190      return _io_FileIO_writable_impl(self);
     191  }
     192  
     193  PyDoc_STRVAR(_io_FileIO_seekable__doc__,
     194  "seekable($self, /)\n"
     195  "--\n"
     196  "\n"
     197  "True if file supports random-access.");
     198  
     199  #define _IO_FILEIO_SEEKABLE_METHODDEF    \
     200      {"seekable", (PyCFunction)_io_FileIO_seekable, METH_NOARGS, _io_FileIO_seekable__doc__},
     201  
     202  static PyObject *
     203  _io_FileIO_seekable_impl(fileio *self);
     204  
     205  static PyObject *
     206  _io_FileIO_seekable(fileio *self, PyObject *Py_UNUSED(ignored))
     207  {
     208      return _io_FileIO_seekable_impl(self);
     209  }
     210  
     211  PyDoc_STRVAR(_io_FileIO_readinto__doc__,
     212  "readinto($self, buffer, /)\n"
     213  "--\n"
     214  "\n"
     215  "Same as RawIOBase.readinto().");
     216  
     217  #define _IO_FILEIO_READINTO_METHODDEF    \
     218      {"readinto", _PyCFunction_CAST(_io_FileIO_readinto), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_FileIO_readinto__doc__},
     219  
     220  static PyObject *
     221  _io_FileIO_readinto_impl(fileio *self, PyTypeObject *cls, Py_buffer *buffer);
     222  
     223  static PyObject *
     224  _io_FileIO_readinto(fileio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
     225  {
     226      PyObject *return_value = NULL;
     227      #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
     228      #  define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
     229      #else
     230      #  define KWTUPLE NULL
     231      #endif
     232  
     233      static const char * const _keywords[] = {"", NULL};
     234      static _PyArg_Parser _parser = {
     235          .keywords = _keywords,
     236          .fname = "readinto",
     237          .kwtuple = KWTUPLE,
     238      };
     239      #undef KWTUPLE
     240      PyObject *argsbuf[1];
     241      Py_buffer buffer = {NULL, NULL};
     242  
     243      args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
     244      if (!args) {
     245          goto exit;
     246      }
     247      if (PyObject_GetBuffer(args[0], &buffer, PyBUF_WRITABLE) < 0) {
     248          PyErr_Clear();
     249          _PyArg_BadArgument("readinto", "argument 1", "read-write bytes-like object", args[0]);
     250          goto exit;
     251      }
     252      if (!PyBuffer_IsContiguous(&buffer, 'C')) {
     253          _PyArg_BadArgument("readinto", "argument 1", "contiguous buffer", args[0]);
     254          goto exit;
     255      }
     256      return_value = _io_FileIO_readinto_impl(self, cls, &buffer);
     257  
     258  exit:
     259      /* Cleanup for buffer */
     260      if (buffer.obj) {
     261         PyBuffer_Release(&buffer);
     262      }
     263  
     264      return return_value;
     265  }
     266  
     267  PyDoc_STRVAR(_io_FileIO_readall__doc__,
     268  "readall($self, /)\n"
     269  "--\n"
     270  "\n"
     271  "Read all data from the file, returned as bytes.\n"
     272  "\n"
     273  "In non-blocking mode, returns as much as is immediately available,\n"
     274  "or None if no data is available.  Return an empty bytes object at EOF.");
     275  
     276  #define _IO_FILEIO_READALL_METHODDEF    \
     277      {"readall", (PyCFunction)_io_FileIO_readall, METH_NOARGS, _io_FileIO_readall__doc__},
     278  
     279  static PyObject *
     280  _io_FileIO_readall_impl(fileio *self);
     281  
     282  static PyObject *
     283  _io_FileIO_readall(fileio *self, PyObject *Py_UNUSED(ignored))
     284  {
     285      return _io_FileIO_readall_impl(self);
     286  }
     287  
     288  PyDoc_STRVAR(_io_FileIO_read__doc__,
     289  "read($self, size=-1, /)\n"
     290  "--\n"
     291  "\n"
     292  "Read at most size bytes, returned as bytes.\n"
     293  "\n"
     294  "Only makes one system call, so less data may be returned than requested.\n"
     295  "In non-blocking mode, returns None if no data is available.\n"
     296  "Return an empty bytes object at EOF.");
     297  
     298  #define _IO_FILEIO_READ_METHODDEF    \
     299      {"read", _PyCFunction_CAST(_io_FileIO_read), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_FileIO_read__doc__},
     300  
     301  static PyObject *
     302  _io_FileIO_read_impl(fileio *self, PyTypeObject *cls, Py_ssize_t size);
     303  
     304  static PyObject *
     305  _io_FileIO_read(fileio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
     306  {
     307      PyObject *return_value = NULL;
     308      #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
     309      #  define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
     310      #else
     311      #  define KWTUPLE NULL
     312      #endif
     313  
     314      static const char * const _keywords[] = {"", NULL};
     315      static _PyArg_Parser _parser = {
     316          .keywords = _keywords,
     317          .fname = "read",
     318          .kwtuple = KWTUPLE,
     319      };
     320      #undef KWTUPLE
     321      PyObject *argsbuf[1];
     322      Py_ssize_t size = -1;
     323  
     324      args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
     325      if (!args) {
     326          goto exit;
     327      }
     328      if (nargs < 1) {
     329          goto skip_optional_posonly;
     330      }
     331      if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
     332          goto exit;
     333      }
     334  skip_optional_posonly:
     335      return_value = _io_FileIO_read_impl(self, cls, size);
     336  
     337  exit:
     338      return return_value;
     339  }
     340  
     341  PyDoc_STRVAR(_io_FileIO_write__doc__,
     342  "write($self, b, /)\n"
     343  "--\n"
     344  "\n"
     345  "Write buffer b to file, return number of bytes written.\n"
     346  "\n"
     347  "Only makes one system call, so not all of the data may be written.\n"
     348  "The number of bytes actually written is returned.  In non-blocking mode,\n"
     349  "returns None if the write would block.");
     350  
     351  #define _IO_FILEIO_WRITE_METHODDEF    \
     352      {"write", _PyCFunction_CAST(_io_FileIO_write), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_FileIO_write__doc__},
     353  
     354  static PyObject *
     355  _io_FileIO_write_impl(fileio *self, PyTypeObject *cls, Py_buffer *b);
     356  
     357  static PyObject *
     358  _io_FileIO_write(fileio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
     359  {
     360      PyObject *return_value = NULL;
     361      #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
     362      #  define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
     363      #else
     364      #  define KWTUPLE NULL
     365      #endif
     366  
     367      static const char * const _keywords[] = {"", NULL};
     368      static _PyArg_Parser _parser = {
     369          .keywords = _keywords,
     370          .fname = "write",
     371          .kwtuple = KWTUPLE,
     372      };
     373      #undef KWTUPLE
     374      PyObject *argsbuf[1];
     375      Py_buffer b = {NULL, NULL};
     376  
     377      args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
     378      if (!args) {
     379          goto exit;
     380      }
     381      if (PyObject_GetBuffer(args[0], &b, PyBUF_SIMPLE) != 0) {
     382          goto exit;
     383      }
     384      if (!PyBuffer_IsContiguous(&b, 'C')) {
     385          _PyArg_BadArgument("write", "argument 1", "contiguous buffer", args[0]);
     386          goto exit;
     387      }
     388      return_value = _io_FileIO_write_impl(self, cls, &b);
     389  
     390  exit:
     391      /* Cleanup for b */
     392      if (b.obj) {
     393         PyBuffer_Release(&b);
     394      }
     395  
     396      return return_value;
     397  }
     398  
     399  PyDoc_STRVAR(_io_FileIO_seek__doc__,
     400  "seek($self, pos, whence=0, /)\n"
     401  "--\n"
     402  "\n"
     403  "Move to new file position and return the file position.\n"
     404  "\n"
     405  "Argument offset is a byte count.  Optional argument whence defaults to\n"
     406  "SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values\n"
     407  "are SEEK_CUR or 1 (move relative to current position, positive or negative),\n"
     408  "and SEEK_END or 2 (move relative to end of file, usually negative, although\n"
     409  "many platforms allow seeking beyond the end of a file).\n"
     410  "\n"
     411  "Note that not all file objects are seekable.");
     412  
     413  #define _IO_FILEIO_SEEK_METHODDEF    \
     414      {"seek", _PyCFunction_CAST(_io_FileIO_seek), METH_FASTCALL, _io_FileIO_seek__doc__},
     415  
     416  static PyObject *
     417  _io_FileIO_seek_impl(fileio *self, PyObject *pos, int whence);
     418  
     419  static PyObject *
     420  _io_FileIO_seek(fileio *self, PyObject *const *args, Py_ssize_t nargs)
     421  {
     422      PyObject *return_value = NULL;
     423      PyObject *pos;
     424      int whence = 0;
     425  
     426      if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
     427          goto exit;
     428      }
     429      pos = args[0];
     430      if (nargs < 2) {
     431          goto skip_optional;
     432      }
     433      whence = _PyLong_AsInt(args[1]);
     434      if (whence == -1 && PyErr_Occurred()) {
     435          goto exit;
     436      }
     437  skip_optional:
     438      return_value = _io_FileIO_seek_impl(self, pos, whence);
     439  
     440  exit:
     441      return return_value;
     442  }
     443  
     444  PyDoc_STRVAR(_io_FileIO_tell__doc__,
     445  "tell($self, /)\n"
     446  "--\n"
     447  "\n"
     448  "Current file position.\n"
     449  "\n"
     450  "Can raise OSError for non seekable files.");
     451  
     452  #define _IO_FILEIO_TELL_METHODDEF    \
     453      {"tell", (PyCFunction)_io_FileIO_tell, METH_NOARGS, _io_FileIO_tell__doc__},
     454  
     455  static PyObject *
     456  _io_FileIO_tell_impl(fileio *self);
     457  
     458  static PyObject *
     459  _io_FileIO_tell(fileio *self, PyObject *Py_UNUSED(ignored))
     460  {
     461      return _io_FileIO_tell_impl(self);
     462  }
     463  
     464  #if defined(HAVE_FTRUNCATE)
     465  
     466  PyDoc_STRVAR(_io_FileIO_truncate__doc__,
     467  "truncate($self, size=None, /)\n"
     468  "--\n"
     469  "\n"
     470  "Truncate the file to at most size bytes and return the truncated size.\n"
     471  "\n"
     472  "Size defaults to the current file position, as returned by tell().\n"
     473  "The current file position is changed to the value of size.");
     474  
     475  #define _IO_FILEIO_TRUNCATE_METHODDEF    \
     476      {"truncate", _PyCFunction_CAST(_io_FileIO_truncate), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_FileIO_truncate__doc__},
     477  
     478  static PyObject *
     479  _io_FileIO_truncate_impl(fileio *self, PyTypeObject *cls, PyObject *posobj);
     480  
     481  static PyObject *
     482  _io_FileIO_truncate(fileio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
     483  {
     484      PyObject *return_value = NULL;
     485      #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
     486      #  define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
     487      #else
     488      #  define KWTUPLE NULL
     489      #endif
     490  
     491      static const char * const _keywords[] = {"", NULL};
     492      static _PyArg_Parser _parser = {
     493          .keywords = _keywords,
     494          .fname = "truncate",
     495          .kwtuple = KWTUPLE,
     496      };
     497      #undef KWTUPLE
     498      PyObject *argsbuf[1];
     499      PyObject *posobj = Py_None;
     500  
     501      args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
     502      if (!args) {
     503          goto exit;
     504      }
     505      if (nargs < 1) {
     506          goto skip_optional_posonly;
     507      }
     508      posobj = args[0];
     509  skip_optional_posonly:
     510      return_value = _io_FileIO_truncate_impl(self, cls, posobj);
     511  
     512  exit:
     513      return return_value;
     514  }
     515  
     516  #endif /* defined(HAVE_FTRUNCATE) */
     517  
     518  PyDoc_STRVAR(_io_FileIO_isatty__doc__,
     519  "isatty($self, /)\n"
     520  "--\n"
     521  "\n"
     522  "True if the file is connected to a TTY device.");
     523  
     524  #define _IO_FILEIO_ISATTY_METHODDEF    \
     525      {"isatty", (PyCFunction)_io_FileIO_isatty, METH_NOARGS, _io_FileIO_isatty__doc__},
     526  
     527  static PyObject *
     528  _io_FileIO_isatty_impl(fileio *self);
     529  
     530  static PyObject *
     531  _io_FileIO_isatty(fileio *self, PyObject *Py_UNUSED(ignored))
     532  {
     533      return _io_FileIO_isatty_impl(self);
     534  }
     535  
     536  #ifndef _IO_FILEIO_TRUNCATE_METHODDEF
     537      #define _IO_FILEIO_TRUNCATE_METHODDEF
     538  #endif /* !defined(_IO_FILEIO_TRUNCATE_METHODDEF) */
     539  /*[clinic end generated code: output=bef47b31b644996a input=a9049054013a1b77]*/