Debian Codegraph @

Modules/_io/clinic/stringio.c.h

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