Coverage Report

Created: 2025-07-11 06:24

/src/cpython/Modules/_io/clinic/stringio.c.h
Line
Count
Source (jump to first uncovered line)
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
54.8k
{
79
54.8k
    PyObject *return_value = NULL;
80
54.8k
    Py_ssize_t size = -1;
81
82
54.8k
    if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
83
0
        goto exit;
84
0
    }
85
54.8k
    if (nargs < 1) {
86
0
        goto skip_optional;
87
0
    }
88
54.8k
    if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
89
0
        goto exit;
90
0
    }
91
54.8k
skip_optional:
92
54.8k
    Py_BEGIN_CRITICAL_SECTION(self);
93
54.8k
    return_value = _io_StringIO_read_impl((stringio *)self, size);
94
54.8k
    Py_END_CRITICAL_SECTION();
95
96
54.8k
exit:
97
54.8k
    return return_value;
98
54.8k
}
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
34.7k
{
157
34.7k
    PyObject *return_value = NULL;
158
34.7k
    PyObject *pos = Py_None;
159
160
34.7k
    if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
161
0
        goto exit;
162
0
    }
163
34.7k
    if (nargs < 1) {
164
34.7k
        goto skip_optional;
165
34.7k
    }
166
0
    pos = args[0];
167
34.7k
skip_optional:
168
34.7k
    Py_BEGIN_CRITICAL_SECTION(self);
169
34.7k
    return_value = _io_StringIO_truncate_impl((stringio *)self, pos);
170
34.7k
    Py_END_CRITICAL_SECTION();
171
172
34.7k
exit:
173
34.7k
    return return_value;
174
34.7k
}
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
69.5k
{
197
69.5k
    PyObject *return_value = NULL;
198
69.5k
    Py_ssize_t pos;
199
69.5k
    int whence = 0;
200
201
69.5k
    if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
202
0
        goto exit;
203
0
    }
204
69.5k
    {
205
69.5k
        Py_ssize_t ival = -1;
206
69.5k
        PyObject *iobj = _PyNumber_Index(args[0]);
207
69.5k
        if (iobj != NULL) {
208
69.5k
            ival = PyLong_AsSsize_t(iobj);
209
69.5k
            Py_DECREF(iobj);
210
69.5k
        }
211
69.5k
        if (ival == -1 && PyErr_Occurred()) {
212
0
            goto exit;
213
0
        }
214
69.5k
        pos = ival;
215
69.5k
    }
216
69.5k
    if (nargs < 2) {
217
69.5k
        goto skip_optional;
218
69.5k
    }
219
0
    whence = PyLong_AsInt(args[1]);
220
0
    if (whence == -1 && PyErr_Occurred()) {
221
0
        goto exit;
222
0
    }
223
69.5k
skip_optional:
224
69.5k
    Py_BEGIN_CRITICAL_SECTION(self);
225
69.5k
    return_value = _io_StringIO_seek_impl((stringio *)self, pos, whence);
226
69.5k
    Py_END_CRITICAL_SECTION();
227
228
69.5k
exit:
229
69.5k
    return return_value;
230
69.5k
}
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
58.4k
{
250
58.4k
    PyObject *return_value = NULL;
251
252
58.4k
    Py_BEGIN_CRITICAL_SECTION(self);
253
58.4k
    return_value = _io_StringIO_write_impl((stringio *)self, obj);
254
58.4k
    Py_END_CRITICAL_SECTION();
255
256
58.4k
    return return_value;
257
58.4k
}
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
30.5k
{
304
30.5k
    int return_value = -1;
305
30.5k
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
306
307
30.5k
    #define NUM_KEYWORDS 2
308
30.5k
    static struct {
309
30.5k
        PyGC_Head _this_is_not_used;
310
30.5k
        PyObject_VAR_HEAD
311
30.5k
        Py_hash_t ob_hash;
312
30.5k
        PyObject *ob_item[NUM_KEYWORDS];
313
30.5k
    } _kwtuple = {
314
30.5k
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
315
30.5k
        .ob_hash = -1,
316
30.5k
        .ob_item = { &_Py_ID(initial_value), &_Py_ID(newline), },
317
30.5k
    };
318
30.5k
    #undef NUM_KEYWORDS
319
30.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
30.5k
    static const char * const _keywords[] = {"initial_value", "newline", NULL};
326
30.5k
    static _PyArg_Parser _parser = {
327
30.5k
        .keywords = _keywords,
328
30.5k
        .fname = "StringIO",
329
30.5k
        .kwtuple = KWTUPLE,
330
30.5k
    };
331
30.5k
    #undef KWTUPLE
332
30.5k
    PyObject *argsbuf[2];
333
30.5k
    PyObject * const *fastargs;
334
30.5k
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
335
30.5k
    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
336
30.5k
    PyObject *value = NULL;
337
30.5k
    PyObject *newline_obj = NULL;
338
339
30.5k
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
340
30.5k
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
341
30.5k
    if (!fastargs) {
342
0
        goto exit;
343
0
    }
344
30.5k
    if (!noptargs) {
345
0
        goto skip_optional_pos;
346
0
    }
347
30.5k
    if (fastargs[0]) {
348
15.2k
        value = fastargs[0];
349
15.2k
        if (!--noptargs) {
350
15.2k
            goto skip_optional_pos;
351
15.2k
        }
352
15.2k
    }
353
15.2k
    newline_obj = fastargs[1];
354
30.5k
skip_optional_pos:
355
30.5k
    return_value = _io_StringIO___init___impl((stringio *)self, value, newline_obj);
356
357
30.5k
exit:
358
30.5k
    return return_value;
359
30.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
34.7k
{
495
34.7k
    PyObject *return_value = NULL;
496
497
34.7k
    Py_BEGIN_CRITICAL_SECTION(self);
498
34.7k
    return_value = _io_StringIO_closed_get_impl((stringio *)self);
499
34.7k
    Py_END_CRITICAL_SECTION();
500
501
34.7k
    return return_value;
502
34.7k
}
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]*/