Coverage Report

Created: 2026-06-21 06:15

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
57.0k
{
79
57.0k
    PyObject *return_value = NULL;
80
57.0k
    Py_ssize_t size = -1;
81
82
57.0k
    if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
83
0
        goto exit;
84
0
    }
85
57.0k
    if (nargs < 1) {
86
0
        goto skip_optional;
87
0
    }
88
57.0k
    if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
89
0
        goto exit;
90
0
    }
91
57.0k
skip_optional:
92
57.0k
    Py_BEGIN_CRITICAL_SECTION(self);
93
57.0k
    return_value = _io_StringIO_read_impl((stringio *)self, size);
94
57.0k
    Py_END_CRITICAL_SECTION();
95
96
57.0k
exit:
97
57.0k
    return return_value;
98
57.0k
}
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
PyDoc_STRVAR(_io_StringIO_truncate__doc__,
139
"truncate($self, pos=None, /)\n"
140
"--\n"
141
"\n"
142
"Truncate size to pos.\n"
143
"\n"
144
"The pos argument defaults to the current file position, as\n"
145
"returned by tell().  The current file position is unchanged.\n"
146
"Returns the new absolute position.");
147
148
#define _IO_STRINGIO_TRUNCATE_METHODDEF    \
149
    {"truncate", _PyCFunction_CAST(_io_StringIO_truncate), METH_FASTCALL, _io_StringIO_truncate__doc__},
150
151
static PyObject *
152
_io_StringIO_truncate_impl(stringio *self, PyObject *pos);
153
154
static PyObject *
155
_io_StringIO_truncate(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
156
36.9k
{
157
36.9k
    PyObject *return_value = NULL;
158
36.9k
    PyObject *pos = Py_None;
159
160
36.9k
    if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
161
0
        goto exit;
162
0
    }
163
36.9k
    if (nargs < 1) {
164
36.9k
        goto skip_optional;
165
36.9k
    }
166
0
    pos = args[0];
167
36.9k
skip_optional:
168
36.9k
    Py_BEGIN_CRITICAL_SECTION(self);
169
36.9k
    return_value = _io_StringIO_truncate_impl((stringio *)self, pos);
170
36.9k
    Py_END_CRITICAL_SECTION();
171
172
36.9k
exit:
173
36.9k
    return return_value;
174
36.9k
}
175
176
PyDoc_STRVAR(_io_StringIO_seek__doc__,
177
"seek($self, pos, whence=0, /)\n"
178
"--\n"
179
"\n"
180
"Change stream position.\n"
181
"\n"
182
"Seek to character offset pos relative to position indicated by\n"
183
"whence:\n"
184
"    0  Start of stream (the default).  pos should be >= 0;\n"
185
"    1  Current position - pos must be 0;\n"
186
"    2  End of stream - pos must be 0.\n"
187
"Returns the new absolute position.");
188
189
#define _IO_STRINGIO_SEEK_METHODDEF    \
190
    {"seek", _PyCFunction_CAST(_io_StringIO_seek), METH_FASTCALL, _io_StringIO_seek__doc__},
191
192
static PyObject *
193
_io_StringIO_seek_impl(stringio *self, Py_ssize_t pos, int whence);
194
195
static PyObject *
196
_io_StringIO_seek(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
197
73.8k
{
198
73.8k
    PyObject *return_value = NULL;
199
73.8k
    Py_ssize_t pos;
200
73.8k
    int whence = 0;
201
202
73.8k
    if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
203
0
        goto exit;
204
0
    }
205
73.8k
    {
206
73.8k
        Py_ssize_t ival = -1;
207
73.8k
        PyObject *iobj = _PyNumber_Index(args[0]);
208
73.8k
        if (iobj != NULL) {
209
73.8k
            ival = PyLong_AsSsize_t(iobj);
210
73.8k
            Py_DECREF(iobj);
211
73.8k
        }
212
73.8k
        if (ival == -1 && PyErr_Occurred()) {
213
0
            goto exit;
214
0
        }
215
73.8k
        pos = ival;
216
73.8k
    }
217
73.8k
    if (nargs < 2) {
218
73.8k
        goto skip_optional;
219
73.8k
    }
220
0
    whence = PyLong_AsInt(args[1]);
221
0
    if (whence == -1 && PyErr_Occurred()) {
222
0
        goto exit;
223
0
    }
224
73.8k
skip_optional:
225
73.8k
    Py_BEGIN_CRITICAL_SECTION(self);
226
73.8k
    return_value = _io_StringIO_seek_impl((stringio *)self, pos, whence);
227
73.8k
    Py_END_CRITICAL_SECTION();
228
229
73.8k
exit:
230
73.8k
    return return_value;
231
73.8k
}
232
233
PyDoc_STRVAR(_io_StringIO_write__doc__,
234
"write($self, s, /)\n"
235
"--\n"
236
"\n"
237
"Write string to file.\n"
238
"\n"
239
"Returns the number of characters written, which is always equal to\n"
240
"the length of the string.");
241
242
#define _IO_STRINGIO_WRITE_METHODDEF    \
243
    {"write", (PyCFunction)_io_StringIO_write, METH_O, _io_StringIO_write__doc__},
244
245
static PyObject *
246
_io_StringIO_write_impl(stringio *self, PyObject *obj);
247
248
static PyObject *
249
_io_StringIO_write(PyObject *self, PyObject *obj)
250
61.5k
{
251
61.5k
    PyObject *return_value = NULL;
252
253
61.5k
    Py_BEGIN_CRITICAL_SECTION(self);
254
61.5k
    return_value = _io_StringIO_write_impl((stringio *)self, obj);
255
61.5k
    Py_END_CRITICAL_SECTION();
256
257
61.5k
    return return_value;
258
61.5k
}
259
260
PyDoc_STRVAR(_io_StringIO_close__doc__,
261
"close($self, /)\n"
262
"--\n"
263
"\n"
264
"Close the IO object.\n"
265
"\n"
266
"Attempting any further operation after the object is closed\n"
267
"will raise a ValueError.\n"
268
"\n"
269
"This method has no effect if the file is already closed.");
270
271
#define _IO_STRINGIO_CLOSE_METHODDEF    \
272
    {"close", (PyCFunction)_io_StringIO_close, METH_NOARGS, _io_StringIO_close__doc__},
273
274
static PyObject *
275
_io_StringIO_close_impl(stringio *self);
276
277
static PyObject *
278
_io_StringIO_close(PyObject *self, PyObject *Py_UNUSED(ignored))
279
0
{
280
0
    PyObject *return_value = NULL;
281
282
0
    Py_BEGIN_CRITICAL_SECTION(self);
283
0
    return_value = _io_StringIO_close_impl((stringio *)self);
284
0
    Py_END_CRITICAL_SECTION();
285
286
0
    return return_value;
287
0
}
288
289
PyDoc_STRVAR(_io_StringIO___init____doc__,
290
"StringIO(initial_value=\'\', newline=\'\\n\')\n"
291
"--\n"
292
"\n"
293
"Text I/O implementation using an in-memory buffer.\n"
294
"\n"
295
"The initial_value argument sets the value of object.  The newline\n"
296
"argument is like the one of TextIOWrapper\'s constructor.");
297
298
static int
299
_io_StringIO___init___impl(stringio *self, PyObject *value,
300
                           PyObject *newline_obj);
301
302
static int
303
_io_StringIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
304
31.7k
{
305
31.7k
    int return_value = -1;
306
31.7k
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
307
308
31.7k
    #define NUM_KEYWORDS 2
309
31.7k
    static struct {
310
31.7k
        PyGC_Head _this_is_not_used;
311
31.7k
        PyObject_VAR_HEAD
312
31.7k
        Py_hash_t ob_hash;
313
31.7k
        PyObject *ob_item[NUM_KEYWORDS];
314
31.7k
    } _kwtuple = {
315
31.7k
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
316
31.7k
        .ob_hash = -1,
317
31.7k
        .ob_item = { &_Py_ID(initial_value), &_Py_ID(newline), },
318
31.7k
    };
319
31.7k
    #undef NUM_KEYWORDS
320
31.7k
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
321
322
    #else  // !Py_BUILD_CORE
323
    #  define KWTUPLE NULL
324
    #endif  // !Py_BUILD_CORE
325
326
31.7k
    static const char * const _keywords[] = {"initial_value", "newline", NULL};
327
31.7k
    static _PyArg_Parser _parser = {
328
31.7k
        .keywords = _keywords,
329
31.7k
        .fname = "StringIO",
330
31.7k
        .kwtuple = KWTUPLE,
331
31.7k
    };
332
31.7k
    #undef KWTUPLE
333
31.7k
    PyObject *argsbuf[2];
334
31.7k
    PyObject * const *fastargs;
335
31.7k
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
336
31.7k
    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
337
31.7k
    PyObject *value = NULL;
338
31.7k
    PyObject *newline_obj = NULL;
339
340
31.7k
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
341
31.7k
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
342
31.7k
    if (!fastargs) {
343
0
        goto exit;
344
0
    }
345
31.7k
    if (!noptargs) {
346
0
        goto skip_optional_pos;
347
0
    }
348
31.7k
    if (fastargs[0]) {
349
15.8k
        value = fastargs[0];
350
15.8k
        if (!--noptargs) {
351
15.8k
            goto skip_optional_pos;
352
15.8k
        }
353
15.8k
    }
354
15.8k
    newline_obj = fastargs[1];
355
31.7k
skip_optional_pos:
356
31.7k
    return_value = _io_StringIO___init___impl((stringio *)self, value, newline_obj);
357
358
31.7k
exit:
359
31.7k
    return return_value;
360
31.7k
}
361
362
PyDoc_STRVAR(_io_StringIO_readable__doc__,
363
"readable($self, /)\n"
364
"--\n"
365
"\n"
366
"Returns True if the IO object can be read.");
367
368
#define _IO_STRINGIO_READABLE_METHODDEF    \
369
    {"readable", (PyCFunction)_io_StringIO_readable, METH_NOARGS, _io_StringIO_readable__doc__},
370
371
static PyObject *
372
_io_StringIO_readable_impl(stringio *self);
373
374
static PyObject *
375
_io_StringIO_readable(PyObject *self, PyObject *Py_UNUSED(ignored))
376
0
{
377
0
    PyObject *return_value = NULL;
378
379
0
    Py_BEGIN_CRITICAL_SECTION(self);
380
0
    return_value = _io_StringIO_readable_impl((stringio *)self);
381
0
    Py_END_CRITICAL_SECTION();
382
383
0
    return return_value;
384
0
}
385
386
PyDoc_STRVAR(_io_StringIO_writable__doc__,
387
"writable($self, /)\n"
388
"--\n"
389
"\n"
390
"Returns True if the IO object can be written.");
391
392
#define _IO_STRINGIO_WRITABLE_METHODDEF    \
393
    {"writable", (PyCFunction)_io_StringIO_writable, METH_NOARGS, _io_StringIO_writable__doc__},
394
395
static PyObject *
396
_io_StringIO_writable_impl(stringio *self);
397
398
static PyObject *
399
_io_StringIO_writable(PyObject *self, PyObject *Py_UNUSED(ignored))
400
0
{
401
0
    PyObject *return_value = NULL;
402
403
0
    Py_BEGIN_CRITICAL_SECTION(self);
404
0
    return_value = _io_StringIO_writable_impl((stringio *)self);
405
0
    Py_END_CRITICAL_SECTION();
406
407
0
    return return_value;
408
0
}
409
410
PyDoc_STRVAR(_io_StringIO_seekable__doc__,
411
"seekable($self, /)\n"
412
"--\n"
413
"\n"
414
"Returns True if the IO object can be seeked.");
415
416
#define _IO_STRINGIO_SEEKABLE_METHODDEF    \
417
    {"seekable", (PyCFunction)_io_StringIO_seekable, METH_NOARGS, _io_StringIO_seekable__doc__},
418
419
static PyObject *
420
_io_StringIO_seekable_impl(stringio *self);
421
422
static PyObject *
423
_io_StringIO_seekable(PyObject *self, PyObject *Py_UNUSED(ignored))
424
0
{
425
0
    PyObject *return_value = NULL;
426
427
0
    Py_BEGIN_CRITICAL_SECTION(self);
428
0
    return_value = _io_StringIO_seekable_impl((stringio *)self);
429
0
    Py_END_CRITICAL_SECTION();
430
431
0
    return return_value;
432
0
}
433
434
PyDoc_STRVAR(_io_StringIO___getstate____doc__,
435
"__getstate__($self, /)\n"
436
"--\n"
437
"\n");
438
439
#define _IO_STRINGIO___GETSTATE___METHODDEF    \
440
    {"__getstate__", (PyCFunction)_io_StringIO___getstate__, METH_NOARGS, _io_StringIO___getstate____doc__},
441
442
static PyObject *
443
_io_StringIO___getstate___impl(stringio *self);
444
445
static PyObject *
446
_io_StringIO___getstate__(PyObject *self, PyObject *Py_UNUSED(ignored))
447
0
{
448
0
    PyObject *return_value = NULL;
449
450
0
    Py_BEGIN_CRITICAL_SECTION(self);
451
0
    return_value = _io_StringIO___getstate___impl((stringio *)self);
452
0
    Py_END_CRITICAL_SECTION();
453
454
0
    return return_value;
455
0
}
456
457
PyDoc_STRVAR(_io_StringIO___setstate____doc__,
458
"__setstate__($self, state, /)\n"
459
"--\n"
460
"\n");
461
462
#define _IO_STRINGIO___SETSTATE___METHODDEF    \
463
    {"__setstate__", (PyCFunction)_io_StringIO___setstate__, METH_O, _io_StringIO___setstate____doc__},
464
465
static PyObject *
466
_io_StringIO___setstate___impl(stringio *self, PyObject *state);
467
468
static PyObject *
469
_io_StringIO___setstate__(PyObject *self, PyObject *state)
470
0
{
471
0
    PyObject *return_value = NULL;
472
473
0
    Py_BEGIN_CRITICAL_SECTION(self);
474
0
    return_value = _io_StringIO___setstate___impl((stringio *)self, state);
475
0
    Py_END_CRITICAL_SECTION();
476
477
0
    return return_value;
478
0
}
479
480
#if !defined(_io_StringIO_closed_DOCSTR)
481
#  define _io_StringIO_closed_DOCSTR NULL
482
#endif
483
#if defined(_IO_STRINGIO_CLOSED_GETSETDEF)
484
#  undef _IO_STRINGIO_CLOSED_GETSETDEF
485
#  define _IO_STRINGIO_CLOSED_GETSETDEF {"closed", (getter)_io_StringIO_closed_get, (setter)_io_StringIO_closed_set, _io_StringIO_closed_DOCSTR},
486
#else
487
#  define _IO_STRINGIO_CLOSED_GETSETDEF {"closed", (getter)_io_StringIO_closed_get, NULL, _io_StringIO_closed_DOCSTR},
488
#endif
489
490
static PyObject *
491
_io_StringIO_closed_get_impl(stringio *self);
492
493
static PyObject *
494
_io_StringIO_closed_get(PyObject *self, void *Py_UNUSED(context))
495
36.9k
{
496
36.9k
    PyObject *return_value = NULL;
497
498
36.9k
    Py_BEGIN_CRITICAL_SECTION(self);
499
36.9k
    return_value = _io_StringIO_closed_get_impl((stringio *)self);
500
36.9k
    Py_END_CRITICAL_SECTION();
501
502
36.9k
    return return_value;
503
36.9k
}
504
505
#if !defined(_io_StringIO_line_buffering_DOCSTR)
506
#  define _io_StringIO_line_buffering_DOCSTR NULL
507
#endif
508
#if defined(_IO_STRINGIO_LINE_BUFFERING_GETSETDEF)
509
#  undef _IO_STRINGIO_LINE_BUFFERING_GETSETDEF
510
#  define _IO_STRINGIO_LINE_BUFFERING_GETSETDEF {"line_buffering", (getter)_io_StringIO_line_buffering_get, (setter)_io_StringIO_line_buffering_set, _io_StringIO_line_buffering_DOCSTR},
511
#else
512
#  define _IO_STRINGIO_LINE_BUFFERING_GETSETDEF {"line_buffering", (getter)_io_StringIO_line_buffering_get, NULL, _io_StringIO_line_buffering_DOCSTR},
513
#endif
514
515
static PyObject *
516
_io_StringIO_line_buffering_get_impl(stringio *self);
517
518
static PyObject *
519
_io_StringIO_line_buffering_get(PyObject *self, void *Py_UNUSED(context))
520
0
{
521
0
    PyObject *return_value = NULL;
522
523
0
    Py_BEGIN_CRITICAL_SECTION(self);
524
0
    return_value = _io_StringIO_line_buffering_get_impl((stringio *)self);
525
0
    Py_END_CRITICAL_SECTION();
526
527
0
    return return_value;
528
0
}
529
530
#if !defined(_io_StringIO_newlines_DOCSTR)
531
#  define _io_StringIO_newlines_DOCSTR NULL
532
#endif
533
#if defined(_IO_STRINGIO_NEWLINES_GETSETDEF)
534
#  undef _IO_STRINGIO_NEWLINES_GETSETDEF
535
#  define _IO_STRINGIO_NEWLINES_GETSETDEF {"newlines", (getter)_io_StringIO_newlines_get, (setter)_io_StringIO_newlines_set, _io_StringIO_newlines_DOCSTR},
536
#else
537
#  define _IO_STRINGIO_NEWLINES_GETSETDEF {"newlines", (getter)_io_StringIO_newlines_get, NULL, _io_StringIO_newlines_DOCSTR},
538
#endif
539
540
static PyObject *
541
_io_StringIO_newlines_get_impl(stringio *self);
542
543
static PyObject *
544
_io_StringIO_newlines_get(PyObject *self, void *Py_UNUSED(context))
545
0
{
546
0
    PyObject *return_value = NULL;
547
548
0
    Py_BEGIN_CRITICAL_SECTION(self);
549
0
    return_value = _io_StringIO_newlines_get_impl((stringio *)self);
550
0
    Py_END_CRITICAL_SECTION();
551
552
0
    return return_value;
553
0
}
554
/*[clinic end generated code: output=730c34b2a6c0500b input=a9049054013a1b77]*/