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