Coverage Report

Created: 2025-11-30 06:38

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