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