Coverage Report

Created: 2026-12-01 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Modules/_io/clinic/stringio.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_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_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
11
#include "pycore_modsupport.h"    // _PyArg_CheckPositional()
12
13
PyDoc_STRVAR(_io_StringIO_getvalue__doc__,
14
"getvalue($self, /)\n"
15
"--\n"
16
"\n"
17
"Retrieve the entire contents of the object.");
18
19
#define _IO_STRINGIO_GETVALUE_METHODDEF    \
20
    {"getvalue", (PyCFunction)_io_StringIO_getvalue, METH_NOARGS, _io_StringIO_getvalue__doc__},
21
22
static PyObject *
23
_io_StringIO_getvalue_impl(stringio *self);
24
25
static PyObject *
26
_io_StringIO_getvalue(PyObject *self, PyObject *Py_UNUSED(ignored))
27
0
{
28
0
    PyObject *return_value = NULL;
29
30
0
    Py_BEGIN_CRITICAL_SECTION(self);
31
0
    return_value = _io_StringIO_getvalue_impl((stringio *)self);
32
0
    Py_END_CRITICAL_SECTION();
33
34
0
    return return_value;
35
0
}
36
37
PyDoc_STRVAR(_io_StringIO_tell__doc__,
38
"tell($self, /)\n"
39
"--\n"
40
"\n"
41
"Tell the current file position.");
42
43
#define _IO_STRINGIO_TELL_METHODDEF    \
44
    {"tell", (PyCFunction)_io_StringIO_tell, METH_NOARGS, _io_StringIO_tell__doc__},
45
46
static PyObject *
47
_io_StringIO_tell_impl(stringio *self);
48
49
static PyObject *
50
_io_StringIO_tell(PyObject *self, PyObject *Py_UNUSED(ignored))
51
0
{
52
0
    PyObject *return_value = NULL;
53
54
0
    Py_BEGIN_CRITICAL_SECTION(self);
55
0
    return_value = _io_StringIO_tell_impl((stringio *)self);
56
0
    Py_END_CRITICAL_SECTION();
57
58
0
    return return_value;
59
0
}
60
61
PyDoc_STRVAR(_io_StringIO_read__doc__,
62
"read($self, size=-1, /)\n"
63
"--\n"
64
"\n"
65
"Read at most size characters, returned as a string.\n"
66
"\n"
67
"If the argument is negative or omitted, read until EOF\n"
68
"is reached. Return an empty string at EOF.");
69
70
#define _IO_STRINGIO_READ_METHODDEF    \
71
    {"read", _PyCFunction_CAST(_io_StringIO_read), METH_FASTCALL, _io_StringIO_read__doc__},
72
73
static PyObject *
74
_io_StringIO_read_impl(stringio *self, Py_ssize_t size);
75
76
static PyObject *
77
_io_StringIO_read(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
78
61.5k
{
79
61.5k
    PyObject *return_value = NULL;
80
61.5k
    Py_ssize_t size = -1;
81
82
61.5k
    if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
83
0
        goto exit;
84
0
    }
85
61.5k
    if (nargs < 1) {
86
0
        goto skip_optional;
87
0
    }
88
61.5k
    if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
89
0
        goto exit;
90
0
    }
91
61.5k
skip_optional:
92
61.5k
    Py_BEGIN_CRITICAL_SECTION(self);
93
61.5k
    return_value = _io_StringIO_read_impl((stringio *)self, size);
94
61.5k
    Py_END_CRITICAL_SECTION();
95
96
61.5k
exit:
97
61.5k
    return return_value;
98
61.5k
}
99
100
PyDoc_STRVAR(_io_StringIO_readline__doc__,
101
"readline($self, size=-1, /)\n"
102
"--\n"
103
"\n"
104
"Read until newline or EOF.\n"
105
"\n"
106
"Returns an empty string if EOF is hit immediately.");
107
108
#define _IO_STRINGIO_READLINE_METHODDEF    \
109
    {"readline", _PyCFunction_CAST(_io_StringIO_readline), METH_FASTCALL, _io_StringIO_readline__doc__},
110
111
static PyObject *
112
_io_StringIO_readline_impl(stringio *self, Py_ssize_t size);
113
114
static PyObject *
115
_io_StringIO_readline(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
116
68
{
117
68
    PyObject *return_value = NULL;
118
68
    Py_ssize_t size = -1;
119
120
68
    if (!_PyArg_CheckPositional("readline", nargs, 0, 1)) {
121
0
        goto exit;
122
0
    }
123
68
    if (nargs < 1) {
124
68
        goto skip_optional;
125
68
    }
126
0
    if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
127
0
        goto exit;
128
0
    }
129
68
skip_optional:
130
68
    Py_BEGIN_CRITICAL_SECTION(self);
131
68
    return_value = _io_StringIO_readline_impl((stringio *)self, size);
132
68
    Py_END_CRITICAL_SECTION();
133
134
68
exit:
135
68
    return return_value;
136
68
}
137
138
static PyObject *
139
stringio_iternext_impl(stringio *self);
140
141
static PyObject *
142
stringio_iternext(PyObject *self)
143
30.6M
{
144
30.6M
    PyObject *return_value = NULL;
145
146
30.6M
    Py_BEGIN_CRITICAL_SECTION(self);
147
30.6M
    return_value = stringio_iternext_impl((stringio *)self);
148
30.6M
    Py_END_CRITICAL_SECTION();
149
150
30.6M
    return return_value;
151
30.6M
}
152
153
PyDoc_STRVAR(_io_StringIO_truncate__doc__,
154
"truncate($self, pos=None, /)\n"
155
"--\n"
156
"\n"
157
"Truncate size to pos.\n"
158
"\n"
159
"The pos argument defaults to the current file position, as\n"
160
"returned by tell().  The current file position is unchanged.\n"
161
"Returns the new absolute position.");
162
163
#define _IO_STRINGIO_TRUNCATE_METHODDEF    \
164
    {"truncate", _PyCFunction_CAST(_io_StringIO_truncate), METH_FASTCALL, _io_StringIO_truncate__doc__},
165
166
static PyObject *
167
_io_StringIO_truncate_impl(stringio *self, PyObject *pos);
168
169
static PyObject *
170
_io_StringIO_truncate(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
171
40.0k
{
172
40.0k
    PyObject *return_value = NULL;
173
40.0k
    PyObject *pos = Py_None;
174
175
40.0k
    if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
176
0
        goto exit;
177
0
    }
178
40.0k
    if (nargs < 1) {
179
40.0k
        goto skip_optional;
180
40.0k
    }
181
0
    pos = args[0];
182
40.0k
skip_optional:
183
40.0k
    Py_BEGIN_CRITICAL_SECTION(self);
184
40.0k
    return_value = _io_StringIO_truncate_impl((stringio *)self, pos);
185
40.0k
    Py_END_CRITICAL_SECTION();
186
187
40.0k
exit:
188
40.0k
    return return_value;
189
40.0k
}
190
191
PyDoc_STRVAR(_io_StringIO_seek__doc__,
192
"seek($self, pos, whence=0, /)\n"
193
"--\n"
194
"\n"
195
"Change stream position.\n"
196
"\n"
197
"Seek to character offset pos relative to position indicated by\n"
198
"whence:\n"
199
"    0  Start of stream (the default).  pos should be >= 0;\n"
200
"    1  Current position - pos must be 0;\n"
201
"    2  End of stream - pos must be 0.\n"
202
"Returns the new absolute position.");
203
204
#define _IO_STRINGIO_SEEK_METHODDEF    \
205
    {"seek", _PyCFunction_CAST(_io_StringIO_seek), METH_FASTCALL, _io_StringIO_seek__doc__},
206
207
static PyObject *
208
_io_StringIO_seek_impl(stringio *self, Py_ssize_t pos, int whence);
209
210
static PyObject *
211
_io_StringIO_seek(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
212
80.0k
{
213
80.0k
    PyObject *return_value = NULL;
214
80.0k
    Py_ssize_t pos;
215
80.0k
    int whence = 0;
216
217
80.0k
    if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
218
0
        goto exit;
219
0
    }
220
80.0k
    {
221
80.0k
        Py_ssize_t ival = -1;
222
80.0k
        PyObject *iobj = _PyNumber_Index(args[0]);
223
80.0k
        if (iobj != NULL) {
224
80.0k
            ival = PyLong_AsSsize_t(iobj);
225
80.0k
            Py_DECREF(iobj);
226
80.0k
        }
227
80.0k
        if (ival == -1 && PyErr_Occurred()) {
228
0
            goto exit;
229
0
        }
230
80.0k
        pos = ival;
231
80.0k
    }
232
80.0k
    if (nargs < 2) {
233
80.0k
        goto skip_optional;
234
80.0k
    }
235
0
    whence = PyLong_AsInt(args[1]);
236
0
    if (whence == -1 && PyErr_Occurred()) {
237
0
        goto exit;
238
0
    }
239
80.0k
skip_optional:
240
80.0k
    Py_BEGIN_CRITICAL_SECTION(self);
241
80.0k
    return_value = _io_StringIO_seek_impl((stringio *)self, pos, whence);
242
80.0k
    Py_END_CRITICAL_SECTION();
243
244
80.0k
exit:
245
80.0k
    return return_value;
246
80.0k
}
247
248
PyDoc_STRVAR(_io_StringIO_write__doc__,
249
"write($self, s, /)\n"
250
"--\n"
251
"\n"
252
"Write string to file.\n"
253
"\n"
254
"Returns the number of characters written, which is always equal to\n"
255
"the length of the string.");
256
257
#define _IO_STRINGIO_WRITE_METHODDEF    \
258
    {"write", (PyCFunction)_io_StringIO_write, METH_O, _io_StringIO_write__doc__},
259
260
static PyObject *
261
_io_StringIO_write_impl(stringio *self, PyObject *obj);
262
263
static PyObject *
264
_io_StringIO_write(PyObject *self, PyObject *obj)
265
67.4k
{
266
67.4k
    PyObject *return_value = NULL;
267
268
67.4k
    Py_BEGIN_CRITICAL_SECTION(self);
269
67.4k
    return_value = _io_StringIO_write_impl((stringio *)self, obj);
270
67.4k
    Py_END_CRITICAL_SECTION();
271
272
67.4k
    return return_value;
273
67.4k
}
274
275
PyDoc_STRVAR(_io_StringIO_close__doc__,
276
"close($self, /)\n"
277
"--\n"
278
"\n"
279
"Close the IO object.\n"
280
"\n"
281
"Attempting any further operation after the object is closed\n"
282
"will raise a ValueError.\n"
283
"\n"
284
"This method has no effect if the file is already closed.");
285
286
#define _IO_STRINGIO_CLOSE_METHODDEF    \
287
    {"close", (PyCFunction)_io_StringIO_close, METH_NOARGS, _io_StringIO_close__doc__},
288
289
static PyObject *
290
_io_StringIO_close_impl(stringio *self);
291
292
static PyObject *
293
_io_StringIO_close(PyObject *self, PyObject *Py_UNUSED(ignored))
294
0
{
295
0
    PyObject *return_value = NULL;
296
297
0
    Py_BEGIN_CRITICAL_SECTION(self);
298
0
    return_value = _io_StringIO_close_impl((stringio *)self);
299
0
    Py_END_CRITICAL_SECTION();
300
301
0
    return return_value;
302
0
}
303
304
PyDoc_STRVAR(_io_StringIO___init____doc__,
305
"StringIO(initial_value=\'\', newline=\'\\n\')\n"
306
"--\n"
307
"\n"
308
"Text I/O implementation using an in-memory buffer.\n"
309
"\n"
310
"The initial_value argument sets the value of object.  The newline\n"
311
"argument is like the one of TextIOWrapper\'s constructor.");
312
313
static int
314
_io_StringIO___init___impl(stringio *self, PyObject *value,
315
                           PyObject *newline_obj);
316
317
static int
318
_io_StringIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
319
40.1k
{
320
40.1k
    int return_value = -1;
321
40.1k
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
322
323
40.1k
    #define NUM_KEYWORDS 2
324
40.1k
    static struct {
325
40.1k
        PyGC_Head _this_is_not_used;
326
40.1k
        PyObject_VAR_HEAD
327
40.1k
        Py_hash_t ob_hash;
328
40.1k
        PyObject *ob_item[NUM_KEYWORDS];
329
40.1k
    } _kwtuple = {
330
40.1k
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
331
40.1k
        .ob_hash = -1,
332
40.1k
        .ob_item = { &_Py_ID(initial_value), &_Py_ID(newline), },
333
40.1k
    };
334
40.1k
    #undef NUM_KEYWORDS
335
40.1k
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
336
337
    #else  // !Py_BUILD_CORE
338
    #  define KWTUPLE NULL
339
    #endif  // !Py_BUILD_CORE
340
341
40.1k
    static const char * const _keywords[] = {"initial_value", "newline", NULL};
342
40.1k
    static _PyArg_Parser _parser = {
343
40.1k
        .keywords = _keywords,
344
40.1k
        .fname = "StringIO",
345
40.1k
        .kwtuple = KWTUPLE,
346
40.1k
    };
347
40.1k
    #undef KWTUPLE
348
40.1k
    PyObject *argsbuf[2];
349
40.1k
    PyObject * const *fastargs;
350
40.1k
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
351
40.1k
    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
352
40.1k
    PyObject *value = NULL;
353
40.1k
    PyObject *newline_obj = NULL;
354
355
40.1k
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
356
40.1k
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
357
40.1k
    if (!fastargs) {
358
0
        goto exit;
359
0
    }
360
40.1k
    if (!noptargs) {
361
0
        goto skip_optional_pos;
362
0
    }
363
40.1k
    if (fastargs[0]) {
364
23.3k
        value = fastargs[0];
365
23.3k
        if (!--noptargs) {
366
23.3k
            goto skip_optional_pos;
367
23.3k
        }
368
23.3k
    }
369
16.7k
    newline_obj = fastargs[1];
370
40.1k
skip_optional_pos:
371
40.1k
    return_value = _io_StringIO___init___impl((stringio *)self, value, newline_obj);
372
373
40.1k
exit:
374
40.1k
    return return_value;
375
40.1k
}
376
377
PyDoc_STRVAR(_io_StringIO_readable__doc__,
378
"readable($self, /)\n"
379
"--\n"
380
"\n"
381
"Returns True if the IO object can be read.");
382
383
#define _IO_STRINGIO_READABLE_METHODDEF    \
384
    {"readable", (PyCFunction)_io_StringIO_readable, METH_NOARGS, _io_StringIO_readable__doc__},
385
386
static PyObject *
387
_io_StringIO_readable_impl(stringio *self);
388
389
static PyObject *
390
_io_StringIO_readable(PyObject *self, PyObject *Py_UNUSED(ignored))
391
0
{
392
0
    PyObject *return_value = NULL;
393
394
0
    Py_BEGIN_CRITICAL_SECTION(self);
395
0
    return_value = _io_StringIO_readable_impl((stringio *)self);
396
0
    Py_END_CRITICAL_SECTION();
397
398
0
    return return_value;
399
0
}
400
401
PyDoc_STRVAR(_io_StringIO_writable__doc__,
402
"writable($self, /)\n"
403
"--\n"
404
"\n"
405
"Returns True if the IO object can be written.");
406
407
#define _IO_STRINGIO_WRITABLE_METHODDEF    \
408
    {"writable", (PyCFunction)_io_StringIO_writable, METH_NOARGS, _io_StringIO_writable__doc__},
409
410
static PyObject *
411
_io_StringIO_writable_impl(stringio *self);
412
413
static PyObject *
414
_io_StringIO_writable(PyObject *self, PyObject *Py_UNUSED(ignored))
415
0
{
416
0
    PyObject *return_value = NULL;
417
418
0
    Py_BEGIN_CRITICAL_SECTION(self);
419
0
    return_value = _io_StringIO_writable_impl((stringio *)self);
420
0
    Py_END_CRITICAL_SECTION();
421
422
0
    return return_value;
423
0
}
424
425
PyDoc_STRVAR(_io_StringIO_seekable__doc__,
426
"seekable($self, /)\n"
427
"--\n"
428
"\n"
429
"Returns True if the IO object can be seeked.");
430
431
#define _IO_STRINGIO_SEEKABLE_METHODDEF    \
432
    {"seekable", (PyCFunction)_io_StringIO_seekable, METH_NOARGS, _io_StringIO_seekable__doc__},
433
434
static PyObject *
435
_io_StringIO_seekable_impl(stringio *self);
436
437
static PyObject *
438
_io_StringIO_seekable(PyObject *self, PyObject *Py_UNUSED(ignored))
439
0
{
440
0
    PyObject *return_value = NULL;
441
442
0
    Py_BEGIN_CRITICAL_SECTION(self);
443
0
    return_value = _io_StringIO_seekable_impl((stringio *)self);
444
0
    Py_END_CRITICAL_SECTION();
445
446
0
    return return_value;
447
0
}
448
449
PyDoc_STRVAR(_io_StringIO___getstate____doc__,
450
"__getstate__($self, /)\n"
451
"--\n"
452
"\n");
453
454
#define _IO_STRINGIO___GETSTATE___METHODDEF    \
455
    {"__getstate__", (PyCFunction)_io_StringIO___getstate__, METH_NOARGS, _io_StringIO___getstate____doc__},
456
457
static PyObject *
458
_io_StringIO___getstate___impl(stringio *self);
459
460
static PyObject *
461
_io_StringIO___getstate__(PyObject *self, PyObject *Py_UNUSED(ignored))
462
0
{
463
0
    PyObject *return_value = NULL;
464
465
0
    Py_BEGIN_CRITICAL_SECTION(self);
466
0
    return_value = _io_StringIO___getstate___impl((stringio *)self);
467
0
    Py_END_CRITICAL_SECTION();
468
469
0
    return return_value;
470
0
}
471
472
PyDoc_STRVAR(_io_StringIO___setstate____doc__,
473
"__setstate__($self, state, /)\n"
474
"--\n"
475
"\n");
476
477
#define _IO_STRINGIO___SETSTATE___METHODDEF    \
478
    {"__setstate__", (PyCFunction)_io_StringIO___setstate__, METH_O, _io_StringIO___setstate____doc__},
479
480
static PyObject *
481
_io_StringIO___setstate___impl(stringio *self, PyObject *state);
482
483
static PyObject *
484
_io_StringIO___setstate__(PyObject *self, PyObject *state)
485
0
{
486
0
    PyObject *return_value = NULL;
487
488
0
    Py_BEGIN_CRITICAL_SECTION(self);
489
0
    return_value = _io_StringIO___setstate___impl((stringio *)self, state);
490
0
    Py_END_CRITICAL_SECTION();
491
492
0
    return return_value;
493
0
}
494
495
static int
496
_io_StringIO_closed_get_impl(stringio *self);
497
498
static PyObject *
499
_io_StringIO_closed_get(PyObject *self, void *Py_UNUSED(context))
500
46.6k
{
501
46.6k
    PyObject *return_value = NULL;
502
46.6k
    int _return_value;
503
504
46.6k
    Py_BEGIN_CRITICAL_SECTION(self);
505
46.6k
    _return_value = _io_StringIO_closed_get_impl((stringio *)self);
506
46.6k
    Py_END_CRITICAL_SECTION();
507
46.6k
    if ((_return_value == -1) && PyErr_Occurred()) {
508
0
        goto exit;
509
0
    }
510
46.6k
    return_value = PyBool_FromLong((long)_return_value);
511
512
46.6k
exit:
513
46.6k
    return return_value;
514
46.6k
}
515
516
static int
517
_io_StringIO_line_buffering_get_impl(stringio *self);
518
519
static PyObject *
520
_io_StringIO_line_buffering_get(PyObject *self, void *Py_UNUSED(context))
521
0
{
522
0
    PyObject *return_value = NULL;
523
0
    int _return_value;
524
525
0
    Py_BEGIN_CRITICAL_SECTION(self);
526
0
    _return_value = _io_StringIO_line_buffering_get_impl((stringio *)self);
527
0
    Py_END_CRITICAL_SECTION();
528
0
    if ((_return_value == -1) && PyErr_Occurred()) {
529
0
        goto exit;
530
0
    }
531
0
    return_value = PyBool_FromLong((long)_return_value);
532
533
0
exit:
534
0
    return return_value;
535
0
}
536
537
static PyObject *
538
_io_StringIO_newlines_get_impl(stringio *self);
539
540
static PyObject *
541
_io_StringIO_newlines_get(PyObject *self, void *Py_UNUSED(context))
542
0
{
543
0
    PyObject *return_value = NULL;
544
545
0
    Py_BEGIN_CRITICAL_SECTION(self);
546
0
    return_value = _io_StringIO_newlines_get_impl((stringio *)self);
547
0
    Py_END_CRITICAL_SECTION();
548
549
0
    return return_value;
550
0
}
551
#define _IO_STRINGIO_CLOSED_GETSETDEF {"closed", (getter)_io_StringIO_closed_get, (setter)NULL, NULL},
552
553
#define _IO_STRINGIO_LINE_BUFFERING_GETSETDEF {"line_buffering", (getter)_io_StringIO_line_buffering_get, (setter)NULL, NULL},
554
555
#define _IO_STRINGIO_NEWLINES_GETSETDEF {"newlines", (getter)_io_StringIO_newlines_get, (setter)NULL, NULL},
556
557
/*[clinic end generated code: output=38a0385529d9cbef input=a9049054013a1b77]*/