Branch data Line data Source code
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 : :
10 : :
11 : : PyDoc_STRVAR(_io_StringIO_getvalue__doc__,
12 : : "getvalue($self, /)\n"
13 : : "--\n"
14 : : "\n"
15 : : "Retrieve the entire contents of the object.");
16 : :
17 : : #define _IO_STRINGIO_GETVALUE_METHODDEF \
18 : : {"getvalue", (PyCFunction)_io_StringIO_getvalue, METH_NOARGS, _io_StringIO_getvalue__doc__},
19 : :
20 : : static PyObject *
21 : : _io_StringIO_getvalue_impl(stringio *self);
22 : :
23 : : static PyObject *
24 : 65 : _io_StringIO_getvalue(stringio *self, PyObject *Py_UNUSED(ignored))
25 : : {
26 : 65 : return _io_StringIO_getvalue_impl(self);
27 : : }
28 : :
29 : : PyDoc_STRVAR(_io_StringIO_tell__doc__,
30 : : "tell($self, /)\n"
31 : : "--\n"
32 : : "\n"
33 : : "Tell the current file position.");
34 : :
35 : : #define _IO_STRINGIO_TELL_METHODDEF \
36 : : {"tell", (PyCFunction)_io_StringIO_tell, METH_NOARGS, _io_StringIO_tell__doc__},
37 : :
38 : : static PyObject *
39 : : _io_StringIO_tell_impl(stringio *self);
40 : :
41 : : static PyObject *
42 : 0 : _io_StringIO_tell(stringio *self, PyObject *Py_UNUSED(ignored))
43 : : {
44 : 0 : return _io_StringIO_tell_impl(self);
45 : : }
46 : :
47 : : PyDoc_STRVAR(_io_StringIO_read__doc__,
48 : : "read($self, size=-1, /)\n"
49 : : "--\n"
50 : : "\n"
51 : : "Read at most size characters, returned as a string.\n"
52 : : "\n"
53 : : "If the argument is negative or omitted, read until EOF\n"
54 : : "is reached. Return an empty string at EOF.");
55 : :
56 : : #define _IO_STRINGIO_READ_METHODDEF \
57 : : {"read", _PyCFunction_CAST(_io_StringIO_read), METH_FASTCALL, _io_StringIO_read__doc__},
58 : :
59 : : static PyObject *
60 : : _io_StringIO_read_impl(stringio *self, Py_ssize_t size);
61 : :
62 : : static PyObject *
63 : 0 : _io_StringIO_read(stringio *self, PyObject *const *args, Py_ssize_t nargs)
64 : : {
65 : 0 : PyObject *return_value = NULL;
66 : 0 : Py_ssize_t size = -1;
67 : :
68 [ # # # # : 0 : if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
# # ]
69 : 0 : goto exit;
70 : : }
71 [ # # ]: 0 : if (nargs < 1) {
72 : 0 : goto skip_optional;
73 : : }
74 [ # # ]: 0 : if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
75 : 0 : goto exit;
76 : : }
77 : 0 : skip_optional:
78 : 0 : return_value = _io_StringIO_read_impl(self, size);
79 : :
80 : 0 : exit:
81 : 0 : return return_value;
82 : : }
83 : :
84 : : PyDoc_STRVAR(_io_StringIO_readline__doc__,
85 : : "readline($self, size=-1, /)\n"
86 : : "--\n"
87 : : "\n"
88 : : "Read until newline or EOF.\n"
89 : : "\n"
90 : : "Returns an empty string if EOF is hit immediately.");
91 : :
92 : : #define _IO_STRINGIO_READLINE_METHODDEF \
93 : : {"readline", _PyCFunction_CAST(_io_StringIO_readline), METH_FASTCALL, _io_StringIO_readline__doc__},
94 : :
95 : : static PyObject *
96 : : _io_StringIO_readline_impl(stringio *self, Py_ssize_t size);
97 : :
98 : : static PyObject *
99 : 0 : _io_StringIO_readline(stringio *self, PyObject *const *args, Py_ssize_t nargs)
100 : : {
101 : 0 : PyObject *return_value = NULL;
102 : 0 : Py_ssize_t size = -1;
103 : :
104 [ # # # # : 0 : if (!_PyArg_CheckPositional("readline", nargs, 0, 1)) {
# # ]
105 : 0 : goto exit;
106 : : }
107 [ # # ]: 0 : if (nargs < 1) {
108 : 0 : goto skip_optional;
109 : : }
110 [ # # ]: 0 : if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
111 : 0 : goto exit;
112 : : }
113 : 0 : skip_optional:
114 : 0 : return_value = _io_StringIO_readline_impl(self, size);
115 : :
116 : 0 : exit:
117 : 0 : return return_value;
118 : : }
119 : :
120 : : PyDoc_STRVAR(_io_StringIO_truncate__doc__,
121 : : "truncate($self, pos=None, /)\n"
122 : : "--\n"
123 : : "\n"
124 : : "Truncate size to pos.\n"
125 : : "\n"
126 : : "The pos argument defaults to the current file position, as\n"
127 : : "returned by tell(). The current file position is unchanged.\n"
128 : : "Returns the new absolute position.");
129 : :
130 : : #define _IO_STRINGIO_TRUNCATE_METHODDEF \
131 : : {"truncate", _PyCFunction_CAST(_io_StringIO_truncate), METH_FASTCALL, _io_StringIO_truncate__doc__},
132 : :
133 : : static PyObject *
134 : : _io_StringIO_truncate_impl(stringio *self, Py_ssize_t size);
135 : :
136 : : static PyObject *
137 : 56 : _io_StringIO_truncate(stringio *self, PyObject *const *args, Py_ssize_t nargs)
138 : : {
139 : 56 : PyObject *return_value = NULL;
140 : 56 : Py_ssize_t size = self->pos;
141 : :
142 [ + - - + : 56 : if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
- - ]
143 : 0 : goto exit;
144 : : }
145 [ + - ]: 56 : if (nargs < 1) {
146 : 56 : goto skip_optional;
147 : : }
148 [ # # ]: 0 : if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
149 : 0 : goto exit;
150 : : }
151 : 0 : skip_optional:
152 : 56 : return_value = _io_StringIO_truncate_impl(self, size);
153 : :
154 : 56 : exit:
155 : 56 : return return_value;
156 : : }
157 : :
158 : : PyDoc_STRVAR(_io_StringIO_seek__doc__,
159 : : "seek($self, pos, whence=0, /)\n"
160 : : "--\n"
161 : : "\n"
162 : : "Change stream position.\n"
163 : : "\n"
164 : : "Seek to character offset pos relative to position indicated by whence:\n"
165 : : " 0 Start of stream (the default). pos should be >= 0;\n"
166 : : " 1 Current position - pos must be 0;\n"
167 : : " 2 End of stream - pos must be 0.\n"
168 : : "Returns the new absolute position.");
169 : :
170 : : #define _IO_STRINGIO_SEEK_METHODDEF \
171 : : {"seek", _PyCFunction_CAST(_io_StringIO_seek), METH_FASTCALL, _io_StringIO_seek__doc__},
172 : :
173 : : static PyObject *
174 : : _io_StringIO_seek_impl(stringio *self, Py_ssize_t pos, int whence);
175 : :
176 : : static PyObject *
177 : 56 : _io_StringIO_seek(stringio *self, PyObject *const *args, Py_ssize_t nargs)
178 : : {
179 : 56 : PyObject *return_value = NULL;
180 : : Py_ssize_t pos;
181 : 56 : int whence = 0;
182 : :
183 [ + - - + : 56 : if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
- - ]
184 : 0 : goto exit;
185 : : }
186 : : {
187 : 56 : Py_ssize_t ival = -1;
188 : 56 : PyObject *iobj = _PyNumber_Index(args[0]);
189 [ + - ]: 56 : if (iobj != NULL) {
190 : 56 : ival = PyLong_AsSsize_t(iobj);
191 : 56 : Py_DECREF(iobj);
192 : : }
193 [ - + - - ]: 56 : if (ival == -1 && PyErr_Occurred()) {
194 : 0 : goto exit;
195 : : }
196 : 56 : pos = ival;
197 : : }
198 [ + - ]: 56 : if (nargs < 2) {
199 : 56 : goto skip_optional;
200 : : }
201 : 0 : whence = _PyLong_AsInt(args[1]);
202 [ # # # # ]: 0 : if (whence == -1 && PyErr_Occurred()) {
203 : 0 : goto exit;
204 : : }
205 : 0 : skip_optional:
206 : 56 : return_value = _io_StringIO_seek_impl(self, pos, whence);
207 : :
208 : 56 : exit:
209 : 56 : return return_value;
210 : : }
211 : :
212 : : PyDoc_STRVAR(_io_StringIO_write__doc__,
213 : : "write($self, s, /)\n"
214 : : "--\n"
215 : : "\n"
216 : : "Write string to file.\n"
217 : : "\n"
218 : : "Returns the number of characters written, which is always equal to\n"
219 : : "the length of the string.");
220 : :
221 : : #define _IO_STRINGIO_WRITE_METHODDEF \
222 : : {"write", (PyCFunction)_io_StringIO_write, METH_O, _io_StringIO_write__doc__},
223 : :
224 : : PyDoc_STRVAR(_io_StringIO_close__doc__,
225 : : "close($self, /)\n"
226 : : "--\n"
227 : : "\n"
228 : : "Close the IO object.\n"
229 : : "\n"
230 : : "Attempting any further operation after the object is closed\n"
231 : : "will raise a ValueError.\n"
232 : : "\n"
233 : : "This method has no effect if the file is already closed.");
234 : :
235 : : #define _IO_STRINGIO_CLOSE_METHODDEF \
236 : : {"close", (PyCFunction)_io_StringIO_close, METH_NOARGS, _io_StringIO_close__doc__},
237 : :
238 : : static PyObject *
239 : : _io_StringIO_close_impl(stringio *self);
240 : :
241 : : static PyObject *
242 : 1 : _io_StringIO_close(stringio *self, PyObject *Py_UNUSED(ignored))
243 : : {
244 : 1 : return _io_StringIO_close_impl(self);
245 : : }
246 : :
247 : : PyDoc_STRVAR(_io_StringIO___init____doc__,
248 : : "StringIO(initial_value=\'\', newline=\'\\n\')\n"
249 : : "--\n"
250 : : "\n"
251 : : "Text I/O implementation using an in-memory buffer.\n"
252 : : "\n"
253 : : "The initial_value argument sets the value of object. The newline\n"
254 : : "argument is like the one of TextIOWrapper\'s constructor.");
255 : :
256 : : static int
257 : : _io_StringIO___init___impl(stringio *self, PyObject *value,
258 : : PyObject *newline_obj);
259 : :
260 : : static int
261 : 11 : _io_StringIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
262 : : {
263 : 11 : int return_value = -1;
264 : : #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
265 : :
266 : : #define NUM_KEYWORDS 2
267 : : static struct {
268 : : PyGC_Head _this_is_not_used;
269 : : PyObject_VAR_HEAD
270 : : PyObject *ob_item[NUM_KEYWORDS];
271 : : } _kwtuple = {
272 : : .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
273 : : .ob_item = { &_Py_ID(initial_value), &_Py_ID(newline), },
274 : : };
275 : : #undef NUM_KEYWORDS
276 : : #define KWTUPLE (&_kwtuple.ob_base.ob_base)
277 : :
278 : : #else // !Py_BUILD_CORE
279 : : # define KWTUPLE NULL
280 : : #endif // !Py_BUILD_CORE
281 : :
282 : : static const char * const _keywords[] = {"initial_value", "newline", NULL};
283 : : static _PyArg_Parser _parser = {
284 : : .keywords = _keywords,
285 : : .fname = "StringIO",
286 : : .kwtuple = KWTUPLE,
287 : : };
288 : : #undef KWTUPLE
289 : : PyObject *argsbuf[2];
290 : : PyObject * const *fastargs;
291 : 11 : Py_ssize_t nargs = PyTuple_GET_SIZE(args);
292 [ - + ]: 11 : Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
293 : 11 : PyObject *value = NULL;
294 : 11 : PyObject *newline_obj = NULL;
295 : :
296 [ + - + - : 11 : fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf);
+ - + - ]
297 [ - + ]: 11 : if (!fastargs) {
298 : 0 : goto exit;
299 : : }
300 [ + - ]: 11 : if (!noptargs) {
301 : 11 : goto skip_optional_pos;
302 : : }
303 [ # # ]: 0 : if (fastargs[0]) {
304 : 0 : value = fastargs[0];
305 [ # # ]: 0 : if (!--noptargs) {
306 : 0 : goto skip_optional_pos;
307 : : }
308 : : }
309 : 0 : newline_obj = fastargs[1];
310 : 11 : skip_optional_pos:
311 : 11 : return_value = _io_StringIO___init___impl((stringio *)self, value, newline_obj);
312 : :
313 : 11 : exit:
314 : 11 : return return_value;
315 : : }
316 : :
317 : : PyDoc_STRVAR(_io_StringIO_readable__doc__,
318 : : "readable($self, /)\n"
319 : : "--\n"
320 : : "\n"
321 : : "Returns True if the IO object can be read.");
322 : :
323 : : #define _IO_STRINGIO_READABLE_METHODDEF \
324 : : {"readable", (PyCFunction)_io_StringIO_readable, METH_NOARGS, _io_StringIO_readable__doc__},
325 : :
326 : : static PyObject *
327 : : _io_StringIO_readable_impl(stringio *self);
328 : :
329 : : static PyObject *
330 : 0 : _io_StringIO_readable(stringio *self, PyObject *Py_UNUSED(ignored))
331 : : {
332 : 0 : return _io_StringIO_readable_impl(self);
333 : : }
334 : :
335 : : PyDoc_STRVAR(_io_StringIO_writable__doc__,
336 : : "writable($self, /)\n"
337 : : "--\n"
338 : : "\n"
339 : : "Returns True if the IO object can be written.");
340 : :
341 : : #define _IO_STRINGIO_WRITABLE_METHODDEF \
342 : : {"writable", (PyCFunction)_io_StringIO_writable, METH_NOARGS, _io_StringIO_writable__doc__},
343 : :
344 : : static PyObject *
345 : : _io_StringIO_writable_impl(stringio *self);
346 : :
347 : : static PyObject *
348 : 0 : _io_StringIO_writable(stringio *self, PyObject *Py_UNUSED(ignored))
349 : : {
350 : 0 : return _io_StringIO_writable_impl(self);
351 : : }
352 : :
353 : : PyDoc_STRVAR(_io_StringIO_seekable__doc__,
354 : : "seekable($self, /)\n"
355 : : "--\n"
356 : : "\n"
357 : : "Returns True if the IO object can be seeked.");
358 : :
359 : : #define _IO_STRINGIO_SEEKABLE_METHODDEF \
360 : : {"seekable", (PyCFunction)_io_StringIO_seekable, METH_NOARGS, _io_StringIO_seekable__doc__},
361 : :
362 : : static PyObject *
363 : : _io_StringIO_seekable_impl(stringio *self);
364 : :
365 : : static PyObject *
366 : 0 : _io_StringIO_seekable(stringio *self, PyObject *Py_UNUSED(ignored))
367 : : {
368 : 0 : return _io_StringIO_seekable_impl(self);
369 : : }
370 : : /*[clinic end generated code: output=533f20ae9b773126 input=a9049054013a1b77]*/
|