Coverage Report

Created: 2026-05-30 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Modules/_io/clinic/iobase.c.h
Line
Count
Source
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_runtime.h"     // _Py_SINGLETON()
7
#endif
8
#include "pycore_abstract.h"      // _Py_convert_optional_to_ssize_t()
9
#include "pycore_modsupport.h"    // _PyArg_UnpackKeywords()
10
11
PyDoc_STRVAR(_io__IOBase_seek__doc__,
12
"seek($self, offset, whence=os.SEEK_SET, /)\n"
13
"--\n"
14
"\n"
15
"Change the stream position to the given byte offset.\n"
16
"\n"
17
"  offset\n"
18
"    The stream position, relative to \'whence\'.\n"
19
"  whence\n"
20
"    The relative position to seek from.\n"
21
"\n"
22
"The offset is interpreted relative to the position indicated by\n"
23
"whence.  Values for whence are:\n"
24
"\n"
25
"* os.SEEK_SET or 0 -- start of stream (the default); offset should\n"
26
"  be zero or positive\n"
27
"* os.SEEK_CUR or 1 -- current stream position; offset may be\n"
28
"  negative\n"
29
"* os.SEEK_END or 2 -- end of stream; offset is usually negative\n"
30
"\n"
31
"Return the new absolute position.");
32
33
#define _IO__IOBASE_SEEK_METHODDEF    \
34
    {"seek", _PyCFunction_CAST(_io__IOBase_seek), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__IOBase_seek__doc__},
35
36
static PyObject *
37
_io__IOBase_seek_impl(PyObject *self, PyTypeObject *cls,
38
                      int Py_UNUSED(offset), int Py_UNUSED(whence));
39
40
static PyObject *
41
_io__IOBase_seek(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
42
5
{
43
5
    PyObject *return_value = NULL;
44
5
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
45
5
    #  define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
46
    #else
47
    #  define KWTUPLE NULL
48
    #endif
49
50
5
    static const char * const _keywords[] = {"", "", NULL};
51
5
    static _PyArg_Parser _parser = {
52
5
        .keywords = _keywords,
53
5
        .fname = "seek",
54
5
        .kwtuple = KWTUPLE,
55
5
    };
56
5
    #undef KWTUPLE
57
5
    PyObject *argsbuf[2];
58
5
    int offset;
59
5
    int whence = 0;
60
61
5
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
62
5
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
63
5
    if (!args) {
64
0
        goto exit;
65
0
    }
66
5
    offset = PyLong_AsInt(args[0]);
67
5
    if (offset == -1 && PyErr_Occurred()) {
68
0
        goto exit;
69
0
    }
70
5
    if (nargs < 2) {
71
0
        goto skip_optional_posonly;
72
0
    }
73
5
    whence = PyLong_AsInt(args[1]);
74
5
    if (whence == -1 && PyErr_Occurred()) {
75
0
        goto exit;
76
0
    }
77
5
skip_optional_posonly:
78
5
    return_value = _io__IOBase_seek_impl(self, cls, offset, whence);
79
80
5
exit:
81
5
    return return_value;
82
5
}
83
84
PyDoc_STRVAR(_io__IOBase_tell__doc__,
85
"tell($self, /)\n"
86
"--\n"
87
"\n"
88
"Return current stream position.");
89
90
#define _IO__IOBASE_TELL_METHODDEF    \
91
    {"tell", (PyCFunction)_io__IOBase_tell, METH_NOARGS, _io__IOBase_tell__doc__},
92
93
static PyObject *
94
_io__IOBase_tell_impl(PyObject *self);
95
96
static PyObject *
97
_io__IOBase_tell(PyObject *self, PyObject *Py_UNUSED(ignored))
98
1.86M
{
99
1.86M
    return _io__IOBase_tell_impl(self);
100
1.86M
}
101
102
PyDoc_STRVAR(_io__IOBase_truncate__doc__,
103
"truncate($self, size=None, /)\n"
104
"--\n"
105
"\n"
106
"Truncate file to size bytes.\n"
107
"\n"
108
"File pointer is left unchanged.  Size defaults to the current IO\n"
109
"position as reported by tell().  Return the new size.");
110
111
#define _IO__IOBASE_TRUNCATE_METHODDEF    \
112
    {"truncate", _PyCFunction_CAST(_io__IOBase_truncate), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__IOBase_truncate__doc__},
113
114
static PyObject *
115
_io__IOBase_truncate_impl(PyObject *self, PyTypeObject *cls,
116
                          PyObject *Py_UNUSED(size));
117
118
static PyObject *
119
_io__IOBase_truncate(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
120
0
{
121
0
    PyObject *return_value = NULL;
122
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
123
0
    #  define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
124
    #else
125
    #  define KWTUPLE NULL
126
    #endif
127
128
0
    static const char * const _keywords[] = {"", NULL};
129
0
    static _PyArg_Parser _parser = {
130
0
        .keywords = _keywords,
131
0
        .fname = "truncate",
132
0
        .kwtuple = KWTUPLE,
133
0
    };
134
0
    #undef KWTUPLE
135
0
    PyObject *argsbuf[1];
136
0
    PyObject *size = Py_None;
137
138
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
139
0
            /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
140
0
    if (!args) {
141
0
        goto exit;
142
0
    }
143
0
    if (nargs < 1) {
144
0
        goto skip_optional_posonly;
145
0
    }
146
0
    size = args[0];
147
0
skip_optional_posonly:
148
0
    return_value = _io__IOBase_truncate_impl(self, cls, size);
149
150
0
exit:
151
0
    return return_value;
152
0
}
153
154
PyDoc_STRVAR(_io__IOBase_flush__doc__,
155
"flush($self, /)\n"
156
"--\n"
157
"\n"
158
"Flush write buffers, if applicable.\n"
159
"\n"
160
"This is not implemented for read-only and non-blocking streams.");
161
162
#define _IO__IOBASE_FLUSH_METHODDEF    \
163
    {"flush", (PyCFunction)_io__IOBase_flush, METH_NOARGS, _io__IOBase_flush__doc__},
164
165
static PyObject *
166
_io__IOBase_flush_impl(PyObject *self);
167
168
static PyObject *
169
_io__IOBase_flush(PyObject *self, PyObject *Py_UNUSED(ignored))
170
1.95M
{
171
1.95M
    return _io__IOBase_flush_impl(self);
172
1.95M
}
173
174
PyDoc_STRVAR(_io__IOBase_close__doc__,
175
"close($self, /)\n"
176
"--\n"
177
"\n"
178
"Flush and close the IO object.\n"
179
"\n"
180
"This method has no effect if the file is already closed.");
181
182
#define _IO__IOBASE_CLOSE_METHODDEF    \
183
    {"close", (PyCFunction)_io__IOBase_close, METH_NOARGS, _io__IOBase_close__doc__},
184
185
static PyObject *
186
_io__IOBase_close_impl(PyObject *self);
187
188
static PyObject *
189
_io__IOBase_close(PyObject *self, PyObject *Py_UNUSED(ignored))
190
73.6k
{
191
73.6k
    return _io__IOBase_close_impl(self);
192
73.6k
}
193
194
PyDoc_STRVAR(_io__IOBase_seekable__doc__,
195
"seekable($self, /)\n"
196
"--\n"
197
"\n"
198
"Return whether object supports random access.\n"
199
"\n"
200
"If False, seek(), tell() and truncate() will raise OSError.\n"
201
"This method may need to do a test seek().");
202
203
#define _IO__IOBASE_SEEKABLE_METHODDEF    \
204
    {"seekable", (PyCFunction)_io__IOBase_seekable, METH_NOARGS, _io__IOBase_seekable__doc__},
205
206
static PyObject *
207
_io__IOBase_seekable_impl(PyObject *self);
208
209
static PyObject *
210
_io__IOBase_seekable(PyObject *self, PyObject *Py_UNUSED(ignored))
211
0
{
212
0
    return _io__IOBase_seekable_impl(self);
213
0
}
214
215
PyDoc_STRVAR(_io__IOBase_readable__doc__,
216
"readable($self, /)\n"
217
"--\n"
218
"\n"
219
"Return whether object was opened for reading.\n"
220
"\n"
221
"If False, read() will raise OSError.");
222
223
#define _IO__IOBASE_READABLE_METHODDEF    \
224
    {"readable", (PyCFunction)_io__IOBase_readable, METH_NOARGS, _io__IOBase_readable__doc__},
225
226
static PyObject *
227
_io__IOBase_readable_impl(PyObject *self);
228
229
static PyObject *
230
_io__IOBase_readable(PyObject *self, PyObject *Py_UNUSED(ignored))
231
75
{
232
75
    return _io__IOBase_readable_impl(self);
233
75
}
234
235
PyDoc_STRVAR(_io__IOBase_writable__doc__,
236
"writable($self, /)\n"
237
"--\n"
238
"\n"
239
"Return whether object was opened for writing.\n"
240
"\n"
241
"If False, write() will raise OSError.");
242
243
#define _IO__IOBASE_WRITABLE_METHODDEF    \
244
    {"writable", (PyCFunction)_io__IOBase_writable, METH_NOARGS, _io__IOBase_writable__doc__},
245
246
static PyObject *
247
_io__IOBase_writable_impl(PyObject *self);
248
249
static PyObject *
250
_io__IOBase_writable(PyObject *self, PyObject *Py_UNUSED(ignored))
251
55
{
252
55
    return _io__IOBase_writable_impl(self);
253
55
}
254
255
PyDoc_STRVAR(_io__IOBase_fileno__doc__,
256
"fileno($self, /)\n"
257
"--\n"
258
"\n"
259
"Return underlying file descriptor if one exists.\n"
260
"\n"
261
"Raise OSError if the IO object does not use a file descriptor.");
262
263
#define _IO__IOBASE_FILENO_METHODDEF    \
264
    {"fileno", _PyCFunction_CAST(_io__IOBase_fileno), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io__IOBase_fileno__doc__},
265
266
static PyObject *
267
_io__IOBase_fileno_impl(PyObject *self, PyTypeObject *cls);
268
269
static PyObject *
270
_io__IOBase_fileno(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
271
0
{
272
0
    if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
273
0
        PyErr_SetString(PyExc_TypeError, "fileno() takes no arguments");
274
0
        return NULL;
275
0
    }
276
0
    return _io__IOBase_fileno_impl(self, cls);
277
0
}
278
279
PyDoc_STRVAR(_io__IOBase_isatty__doc__,
280
"isatty($self, /)\n"
281
"--\n"
282
"\n"
283
"Return whether this is an \'interactive\' stream.\n"
284
"\n"
285
"Return False if it can\'t be determined.");
286
287
#define _IO__IOBASE_ISATTY_METHODDEF    \
288
    {"isatty", (PyCFunction)_io__IOBase_isatty, METH_NOARGS, _io__IOBase_isatty__doc__},
289
290
static PyObject *
291
_io__IOBase_isatty_impl(PyObject *self);
292
293
static PyObject *
294
_io__IOBase_isatty(PyObject *self, PyObject *Py_UNUSED(ignored))
295
0
{
296
0
    return _io__IOBase_isatty_impl(self);
297
0
}
298
299
PyDoc_STRVAR(_io__IOBase_readline__doc__,
300
"readline($self, size=-1, /)\n"
301
"--\n"
302
"\n"
303
"Read and return a line from the stream.\n"
304
"\n"
305
"If size is specified, at most size bytes will be read.\n"
306
"\n"
307
"The line terminator is always b\'\\n\' for binary files; for text\n"
308
"files, the newlines argument to open can be used to select the line\n"
309
"terminator(s) recognized.");
310
311
#define _IO__IOBASE_READLINE_METHODDEF    \
312
    {"readline", _PyCFunction_CAST(_io__IOBase_readline), METH_FASTCALL, _io__IOBase_readline__doc__},
313
314
static PyObject *
315
_io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit);
316
317
static PyObject *
318
_io__IOBase_readline(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
319
0
{
320
0
    PyObject *return_value = NULL;
321
0
    Py_ssize_t limit = -1;
322
323
0
    if (!_PyArg_CheckPositional("readline", nargs, 0, 1)) {
324
0
        goto exit;
325
0
    }
326
0
    if (nargs < 1) {
327
0
        goto skip_optional;
328
0
    }
329
0
    if (!_Py_convert_optional_to_ssize_t(args[0], &limit)) {
330
0
        goto exit;
331
0
    }
332
0
skip_optional:
333
0
    return_value = _io__IOBase_readline_impl(self, limit);
334
335
0
exit:
336
0
    return return_value;
337
0
}
338
339
PyDoc_STRVAR(_io__IOBase_readlines__doc__,
340
"readlines($self, hint=-1, /)\n"
341
"--\n"
342
"\n"
343
"Return a list of lines from the stream.\n"
344
"\n"
345
"hint can be specified to control the number of lines read: no more\n"
346
"lines will be read if the total size (in bytes/characters) of all\n"
347
"lines so far exceeds hint.");
348
349
#define _IO__IOBASE_READLINES_METHODDEF    \
350
    {"readlines", _PyCFunction_CAST(_io__IOBase_readlines), METH_FASTCALL, _io__IOBase_readlines__doc__},
351
352
static PyObject *
353
_io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint);
354
355
static PyObject *
356
_io__IOBase_readlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
357
37.5k
{
358
37.5k
    PyObject *return_value = NULL;
359
37.5k
    Py_ssize_t hint = -1;
360
361
37.5k
    if (!_PyArg_CheckPositional("readlines", nargs, 0, 1)) {
362
0
        goto exit;
363
0
    }
364
37.5k
    if (nargs < 1) {
365
37.5k
        goto skip_optional;
366
37.5k
    }
367
0
    if (!_Py_convert_optional_to_ssize_t(args[0], &hint)) {
368
0
        goto exit;
369
0
    }
370
37.5k
skip_optional:
371
37.5k
    return_value = _io__IOBase_readlines_impl(self, hint);
372
373
37.5k
exit:
374
37.5k
    return return_value;
375
37.5k
}
376
377
PyDoc_STRVAR(_io__IOBase_writelines__doc__,
378
"writelines($self, lines, /)\n"
379
"--\n"
380
"\n"
381
"Write a list of lines to stream.\n"
382
"\n"
383
"Line separators are not added, so it is usual for each of the\n"
384
"lines provided to have a line separator at the end.");
385
386
#define _IO__IOBASE_WRITELINES_METHODDEF    \
387
    {"writelines", (PyCFunction)_io__IOBase_writelines, METH_O, _io__IOBase_writelines__doc__},
388
389
PyDoc_STRVAR(_io__RawIOBase_read__doc__,
390
"read($self, size=-1, /)\n"
391
"--\n"
392
"\n");
393
394
#define _IO__RAWIOBASE_READ_METHODDEF    \
395
    {"read", _PyCFunction_CAST(_io__RawIOBase_read), METH_FASTCALL, _io__RawIOBase_read__doc__},
396
397
static PyObject *
398
_io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n);
399
400
static PyObject *
401
_io__RawIOBase_read(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
402
0
{
403
0
    PyObject *return_value = NULL;
404
0
    Py_ssize_t n = -1;
405
406
0
    if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
407
0
        goto exit;
408
0
    }
409
0
    if (nargs < 1) {
410
0
        goto skip_optional;
411
0
    }
412
0
    {
413
0
        Py_ssize_t ival = -1;
414
0
        PyObject *iobj = _PyNumber_Index(args[0]);
415
0
        if (iobj != NULL) {
416
0
            ival = PyLong_AsSsize_t(iobj);
417
0
            Py_DECREF(iobj);
418
0
        }
419
0
        if (ival == -1 && PyErr_Occurred()) {
420
0
            goto exit;
421
0
        }
422
0
        n = ival;
423
0
    }
424
0
skip_optional:
425
0
    return_value = _io__RawIOBase_read_impl(self, n);
426
427
0
exit:
428
0
    return return_value;
429
0
}
430
431
PyDoc_STRVAR(_io__RawIOBase_readall__doc__,
432
"readall($self, /)\n"
433
"--\n"
434
"\n"
435
"Read until EOF, using multiple read() call.");
436
437
#define _IO__RAWIOBASE_READALL_METHODDEF    \
438
    {"readall", (PyCFunction)_io__RawIOBase_readall, METH_NOARGS, _io__RawIOBase_readall__doc__},
439
440
static PyObject *
441
_io__RawIOBase_readall_impl(PyObject *self);
442
443
static PyObject *
444
_io__RawIOBase_readall(PyObject *self, PyObject *Py_UNUSED(ignored))
445
0
{
446
0
    return _io__RawIOBase_readall_impl(self);
447
0
}
448
/*[clinic end generated code: output=28c06bb6db32c096 input=a9049054013a1b77]*/