Coverage Report

Created: 2026-11-20 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython3/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
0
{
79
0
    PyObject *return_value = NULL;
80
0
    Py_ssize_t size = -1;
81
82
0
    if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
83
0
        goto exit;
84
0
    }
85
0
    if (nargs < 1) {
86
0
        goto skip_optional;
87
0
    }
88
0
    if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
89
0
        goto exit;
90
0
    }
91
0
skip_optional:
92
0
    Py_BEGIN_CRITICAL_SECTION(self);
93
0
    return_value = _io_StringIO_read_impl((stringio *)self, size);
94
0
    Py_END_CRITICAL_SECTION();
95
96
0
exit:
97
0
    return return_value;
98
0
}
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
0
{
117
0
    PyObject *return_value = NULL;
118
0
    Py_ssize_t size = -1;
119
120
0
    if (!_PyArg_CheckPositional("readline", nargs, 0, 1)) {
121
0
        goto exit;
122
0
    }
123
0
    if (nargs < 1) {
124
0
        goto skip_optional;
125
0
    }
126
0
    if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
127
0
        goto exit;
128
0
    }
129
0
skip_optional:
130
0
    Py_BEGIN_CRITICAL_SECTION(self);
131
0
    return_value = _io_StringIO_readline_impl((stringio *)self, size);
132
0
    Py_END_CRITICAL_SECTION();
133
134
0
exit:
135
0
    return return_value;
136
0
}
137
138
static PyObject *
139
stringio_iternext_impl(stringio *self);
140
141
static PyObject *
142
stringio_iternext(PyObject *self)
143
0
{
144
0
    PyObject *return_value = NULL;
145
146
0
    Py_BEGIN_CRITICAL_SECTION(self);
147
0
    return_value = stringio_iternext_impl((stringio *)self);
148
0
    Py_END_CRITICAL_SECTION();
149
150
0
    return return_value;
151
0
}
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
0
{
172
0
    PyObject *return_value = NULL;
173
0
    PyObject *pos = Py_None;
174
175
0
    if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
176
0
        goto exit;
177
0
    }
178
0
    if (nargs < 1) {
179
0
        goto skip_optional;
180
0
    }
181
0
    pos = args[0];
182
0
skip_optional:
183
0
    Py_BEGIN_CRITICAL_SECTION(self);
184
0
    return_value = _io_StringIO_truncate_impl((stringio *)self, pos);
185
0
    Py_END_CRITICAL_SECTION();
186
187
0
exit:
188
0
    return return_value;
189
0
}
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
0
{
213
0
    PyObject *return_value = NULL;
214
0
    Py_ssize_t pos;
215
0
    int whence = 0;
216
217
0
    if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
218
0
        goto exit;
219
0
    }
220
0
    {
221
0
        Py_ssize_t ival = -1;
222
0
        PyObject *iobj = _PyNumber_Index(args[0]);
223
0
        if (iobj != NULL) {
224
0
            ival = PyLong_AsSsize_t(iobj);
225
0
            Py_DECREF(iobj);
226
0
        }
227
0
        if (ival == -1 && PyErr_Occurred()) {
228
0
            goto exit;
229
0
        }
230
0
        pos = ival;
231
0
    }
232
0
    if (nargs < 2) {
233
0
        goto skip_optional;
234
0
    }
235
0
    whence = PyLong_AsInt(args[1]);
236
0
    if (whence == -1 && PyErr_Occurred()) {
237
0
        goto exit;
238
0
    }
239
0
skip_optional:
240
0
    Py_BEGIN_CRITICAL_SECTION(self);
241
0
    return_value = _io_StringIO_seek_impl((stringio *)self, pos, whence);
242
0
    Py_END_CRITICAL_SECTION();
243
244
0
exit:
245
0
    return return_value;
246
0
}
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
0
{
266
0
    PyObject *return_value = NULL;
267
268
0
    Py_BEGIN_CRITICAL_SECTION(self);
269
0
    return_value = _io_StringIO_write_impl((stringio *)self, obj);
270
0
    Py_END_CRITICAL_SECTION();
271
272
0
    return return_value;
273
0
}
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
0
{
320
0
    int return_value = -1;
321
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
322
323
0
    #define NUM_KEYWORDS 2
324
0
    static struct {
325
0
        PyGC_Head _this_is_not_used;
326
0
        PyObject_VAR_HEAD
327
0
        Py_hash_t ob_hash;
328
0
        PyObject *ob_item[NUM_KEYWORDS];
329
0
    } _kwtuple = {
330
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
331
0
        .ob_hash = -1,
332
0
        .ob_item = { &_Py_ID(initial_value), &_Py_ID(newline), },
333
0
    };
334
0
    #undef NUM_KEYWORDS
335
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
336
337
    #else  // !Py_BUILD_CORE
338
    #  define KWTUPLE NULL
339
    #endif  // !Py_BUILD_CORE
340
341
0
    static const char * const _keywords[] = {"initial_value", "newline", NULL};
342
0
    static _PyArg_Parser _parser = {
343
0
        .keywords = _keywords,
344
0
        .fname = "StringIO",
345
0
        .kwtuple = KWTUPLE,
346
0
    };
347
0
    #undef KWTUPLE
348
0
    PyObject *argsbuf[2];
349
0
    PyObject * const *fastargs;
350
0
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
351
0
    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
352
0
    PyObject *value = NULL;
353
0
    PyObject *newline_obj = NULL;
354
355
0
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
356
0
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
357
0
    if (!fastargs) {
358
0
        goto exit;
359
0
    }
360
0
    if (!noptargs) {
361
0
        goto skip_optional_pos;
362
0
    }
363
0
    if (fastargs[0]) {
364
0
        value = fastargs[0];
365
0
        if (!--noptargs) {
366
0
            goto skip_optional_pos;
367
0
        }
368
0
    }
369
0
    newline_obj = fastargs[1];
370
0
skip_optional_pos:
371
0
    return_value = _io_StringIO___init___impl((stringio *)self, value, newline_obj);
372
373
0
exit:
374
0
    return return_value;
375
0
}
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
0
{
501
0
    PyObject *return_value = NULL;
502
0
    int _return_value;
503
504
0
    Py_BEGIN_CRITICAL_SECTION(self);
505
0
    _return_value = _io_StringIO_closed_get_impl((stringio *)self);
506
0
    Py_END_CRITICAL_SECTION();
507
0
    if ((_return_value == -1) && PyErr_Occurred()) {
508
0
        goto exit;
509
0
    }
510
0
    return_value = PyBool_FromLong((long)_return_value);
511
512
0
exit:
513
0
    return return_value;
514
0
}
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]*/