Coverage Report

Created: 2025-07-11 06:59

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