Coverage Report

Created: 2026-10-17 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
58.5k
{
79
58.5k
    PyObject *return_value = NULL;
80
58.5k
    Py_ssize_t size = -1;
81
82
58.5k
    if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
83
0
        goto exit;
84
0
    }
85
58.5k
    if (nargs < 1) {
86
0
        goto skip_optional;
87
0
    }
88
58.5k
    if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
89
0
        goto exit;
90
0
    }
91
58.5k
skip_optional:
92
58.5k
    Py_BEGIN_CRITICAL_SECTION(self);
93
58.5k
    return_value = _io_StringIO_read_impl((stringio *)self, size);
94
58.5k
    Py_END_CRITICAL_SECTION();
95
96
58.5k
exit:
97
58.5k
    return return_value;
98
58.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
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
38.0k
{
157
38.0k
    PyObject *return_value = NULL;
158
38.0k
    PyObject *pos = Py_None;
159
160
38.0k
    if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
161
0
        goto exit;
162
0
    }
163
38.0k
    if (nargs < 1) {
164
38.0k
        goto skip_optional;
165
38.0k
    }
166
0
    pos = args[0];
167
38.0k
skip_optional:
168
38.0k
    Py_BEGIN_CRITICAL_SECTION(self);
169
38.0k
    return_value = _io_StringIO_truncate_impl((stringio *)self, pos);
170
38.0k
    Py_END_CRITICAL_SECTION();
171
172
38.0k
exit:
173
38.0k
    return return_value;
174
38.0k
}
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
76.1k
{
198
76.1k
    PyObject *return_value = NULL;
199
76.1k
    Py_ssize_t pos;
200
76.1k
    int whence = 0;
201
202
76.1k
    if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
203
0
        goto exit;
204
0
    }
205
76.1k
    {
206
76.1k
        Py_ssize_t ival = -1;
207
76.1k
        PyObject *iobj = _PyNumber_Index(args[0]);
208
76.1k
        if (iobj != NULL) {
209
76.1k
            ival = PyLong_AsSsize_t(iobj);
210
76.1k
            Py_DECREF(iobj);
211
76.1k
        }
212
76.1k
        if (ival == -1 && PyErr_Occurred()) {
213
0
            goto exit;
214
0
        }
215
76.1k
        pos = ival;
216
76.1k
    }
217
76.1k
    if (nargs < 2) {
218
76.1k
        goto skip_optional;
219
76.1k
    }
220
0
    whence = PyLong_AsInt(args[1]);
221
0
    if (whence == -1 && PyErr_Occurred()) {
222
0
        goto exit;
223
0
    }
224
76.1k
skip_optional:
225
76.1k
    Py_BEGIN_CRITICAL_SECTION(self);
226
76.1k
    return_value = _io_StringIO_seek_impl((stringio *)self, pos, whence);
227
76.1k
    Py_END_CRITICAL_SECTION();
228
229
76.1k
exit:
230
76.1k
    return return_value;
231
76.1k
}
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
63.9k
{
251
63.9k
    PyObject *return_value = NULL;
252
253
63.9k
    Py_BEGIN_CRITICAL_SECTION(self);
254
63.9k
    return_value = _io_StringIO_write_impl((stringio *)self, obj);
255
63.9k
    Py_END_CRITICAL_SECTION();
256
257
63.9k
    return return_value;
258
63.9k
}
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
37.7k
{
305
37.7k
    int return_value = -1;
306
37.7k
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
307
308
37.7k
    #define NUM_KEYWORDS 2
309
37.7k
    static struct {
310
37.7k
        PyGC_Head _this_is_not_used;
311
37.7k
        PyObject_VAR_HEAD
312
37.7k
        Py_hash_t ob_hash;
313
37.7k
        PyObject *ob_item[NUM_KEYWORDS];
314
37.7k
    } _kwtuple = {
315
37.7k
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
316
37.7k
        .ob_hash = -1,
317
37.7k
        .ob_item = { &_Py_ID(initial_value), &_Py_ID(newline), },
318
37.7k
    };
319
37.7k
    #undef NUM_KEYWORDS
320
37.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
37.7k
    static const char * const _keywords[] = {"initial_value", "newline", NULL};
327
37.7k
    static _PyArg_Parser _parser = {
328
37.7k
        .keywords = _keywords,
329
37.7k
        .fname = "StringIO",
330
37.7k
        .kwtuple = KWTUPLE,
331
37.7k
    };
332
37.7k
    #undef KWTUPLE
333
37.7k
    PyObject *argsbuf[2];
334
37.7k
    PyObject * const *fastargs;
335
37.7k
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
336
37.7k
    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
337
37.7k
    PyObject *value = NULL;
338
37.7k
    PyObject *newline_obj = NULL;
339
340
37.7k
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
341
37.7k
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
342
37.7k
    if (!fastargs) {
343
0
        goto exit;
344
0
    }
345
37.7k
    if (!noptargs) {
346
0
        goto skip_optional_pos;
347
0
    }
348
37.7k
    if (fastargs[0]) {
349
21.7k
        value = fastargs[0];
350
21.7k
        if (!--noptargs) {
351
21.7k
            goto skip_optional_pos;
352
21.7k
        }
353
21.7k
    }
354
15.9k
    newline_obj = fastargs[1];
355
37.7k
skip_optional_pos:
356
37.7k
    return_value = _io_StringIO___init___impl((stringio *)self, value, newline_obj);
357
358
37.7k
exit:
359
37.7k
    return return_value;
360
37.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
static int
481
_io_StringIO_closed_get_impl(stringio *self);
482
483
static PyObject *
484
_io_StringIO_closed_get(PyObject *self, void *Py_UNUSED(context))
485
43.8k
{
486
43.8k
    PyObject *return_value = NULL;
487
43.8k
    int _return_value;
488
489
43.8k
    Py_BEGIN_CRITICAL_SECTION(self);
490
43.8k
    _return_value = _io_StringIO_closed_get_impl((stringio *)self);
491
43.8k
    Py_END_CRITICAL_SECTION();
492
43.8k
    if ((_return_value == -1) && PyErr_Occurred()) {
493
0
        goto exit;
494
0
    }
495
43.8k
    return_value = PyBool_FromLong((long)_return_value);
496
497
43.8k
exit:
498
43.8k
    return return_value;
499
43.8k
}
500
501
static int
502
_io_StringIO_line_buffering_get_impl(stringio *self);
503
504
static PyObject *
505
_io_StringIO_line_buffering_get(PyObject *self, void *Py_UNUSED(context))
506
0
{
507
0
    PyObject *return_value = NULL;
508
0
    int _return_value;
509
510
0
    Py_BEGIN_CRITICAL_SECTION(self);
511
0
    _return_value = _io_StringIO_line_buffering_get_impl((stringio *)self);
512
0
    Py_END_CRITICAL_SECTION();
513
0
    if ((_return_value == -1) && PyErr_Occurred()) {
514
0
        goto exit;
515
0
    }
516
0
    return_value = PyBool_FromLong((long)_return_value);
517
518
0
exit:
519
0
    return return_value;
520
0
}
521
522
static PyObject *
523
_io_StringIO_newlines_get_impl(stringio *self);
524
525
static PyObject *
526
_io_StringIO_newlines_get(PyObject *self, void *Py_UNUSED(context))
527
0
{
528
0
    PyObject *return_value = NULL;
529
530
0
    Py_BEGIN_CRITICAL_SECTION(self);
531
0
    return_value = _io_StringIO_newlines_get_impl((stringio *)self);
532
0
    Py_END_CRITICAL_SECTION();
533
534
0
    return return_value;
535
0
}
536
#define _IO_STRINGIO_CLOSED_GETSETDEF {"closed", (getter)_io_StringIO_closed_get, (setter)NULL, NULL},
537
538
#define _IO_STRINGIO_LINE_BUFFERING_GETSETDEF {"line_buffering", (getter)_io_StringIO_line_buffering_get, (setter)NULL, NULL},
539
540
#define _IO_STRINGIO_NEWLINES_GETSETDEF {"newlines", (getter)_io_StringIO_newlines_get, (setter)NULL, NULL},
541
542
/*[clinic end generated code: output=4100afa5e4f295d7 input=a9049054013a1b77]*/