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