Debian Codegraph @

Modules/_io/stringio.c

raw
1 #include "Python.h"
2 #include <stddef.h> // offsetof()
3 #include "pycore_object.h"
4 #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()
5 #include "_iomodule.h"
6
7 /* Implementation note: the buffer is always at least one character longer
8 than the enclosed string, for proper functioning of _PyIO_find_line_ending.
9 */
10
11 #define STATE_REALIZED 1
12 #define STATE_ACCUMULATING 2
13
14 /*[clinic input]
15 module _io
16 class _io.StringIO "stringio *" "clinic_state()->PyStringIO_Type"
17 [clinic start generated code]*/
18 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=2693eada0658d470]*/
19
20 typedef struct {
21 PyObject_HEAD
22 Py_UCS4 *buf;
23 Py_ssize_t pos;
24 Py_ssize_t string_size;
25 size_t buf_size;
26
27 /* The stringio object can be in two states: accumulating or realized.
28 In accumulating state, the internal buffer contains nothing and
29 the contents are given by the embedded _PyUnicodeWriter structure.
30 In realized state, the internal buffer is meaningful and the
31 _PyUnicodeWriter is destroyed.
32 */
33 int state;
34 PyUnicodeWriter *writer;
35
36 char ok; /* initialized? */
37 char closed;
38 char readuniversal;
39 char readtranslate;
40 PyObject *decoder;
41 PyObject *readnl;
42 PyObject *writenl;
43
44 PyObject *dict;
45 PyObject *weakreflist;
46 _PyIO_State *module_state;
47 } stringio;
48
49 #define stringio_CAST(op) ((stringio *)(op))
50
51 #define clinic_state() (find_io_state_by_def(Py_TYPE(self)))
52 #include "clinic/stringio.c.h"
53 #undef clinic_state
54
55 static int _io_StringIO___init__(PyObject *self, PyObject *args, PyObject *kwargs);
56
57 #define CHECK_INITIALIZED(self) \
58 if (self->ok <= 0) { \
59 PyErr_SetString(PyExc_ValueError, \
60 "I/O operation on uninitialized object"); \
61 return NULL; \
62 }
63
64 #define CHECK_CLOSED(self) \
65 if (self->closed) { \
66 PyErr_SetString(PyExc_ValueError, \
67 "I/O operation on closed file"); \
68 return NULL; \
69 }
70
71 #define ENSURE_REALIZED(self) \
72 if (realize(self) < 0) { \
73 return NULL; \
74 }
75
76
77 /* Internal routine for changing the size, in terms of characters, of the
78 buffer of StringIO objects. The caller should ensure that the 'size'
79 argument is non-negative. Returns 0 on success, -1 otherwise. */
80 static int
81 resize_buffer(stringio *self, size_t size)
82 {
83 /* Here, unsigned types are used to avoid dealing with signed integer
84 overflow, which is undefined in C. */
85 size_t alloc = self->buf_size;
86 Py_UCS4 *new_buf = NULL;
87
88 assert(self->buf != NULL);
89
90 /* Reserve one more char for line ending detection. */
91 size = size + 1;
92 /* For simplicity, stay in the range of the signed type. Anyway, Python
93 doesn't allow strings to be longer than this. */
94 if (size > PY_SSIZE_T_MAX)
95 goto overflow;
96
97 if (size < alloc / 2) {
98 /* Major downsize; resize down to exact size. */
99 alloc = size + 1;
100 }
101 else if (size < alloc) {
102 /* Within allocated size; quick exit */
103 return 0;
104 }
105 else if (size <= alloc * 1.125) {
106 /* Moderate upsize; overallocate similar to list_resize() */
107 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
108 }
109 else {
110 /* Major upsize; resize up to exact size */
111 alloc = size + 1;
112 }
113
114 if (alloc > PY_SIZE_MAX / sizeof(Py_UCS4))
115 goto overflow;
116 new_buf = (Py_UCS4 *)PyMem_Realloc(self->buf, alloc * sizeof(Py_UCS4));
117 if (new_buf == NULL) {
118 PyErr_NoMemory();
119 return -1;
120 }
121 self->buf_size = alloc;
122 self->buf = new_buf;
123
124 return 0;
125
126 overflow:
127 PyErr_SetString(PyExc_OverflowError,
128 "new buffer size too large");
129 return -1;
130 }
131
132 static PyObject *
133 make_intermediate(stringio *self)
134 {
135 PyObject *intermediate = PyUnicodeWriter_Finish(self->writer);
136 self->writer = NULL;
137 self->state = STATE_REALIZED;
138 if (intermediate == NULL)
139 return NULL;
140
141 self->writer = PyUnicodeWriter_Create(0);
142 if (self->writer == NULL) {
143 Py_DECREF(intermediate);
144 return NULL;
145 }
146 if (PyUnicodeWriter_WriteStr(self->writer, intermediate)) {
147 Py_DECREF(intermediate);
148 return NULL;
149 }
150 self->state = STATE_ACCUMULATING;
151 return intermediate;
152 }
153
154 static int
155 realize(stringio *self)
156 {
157 Py_ssize_t len;
158 PyObject *intermediate;
159
160 if (self->state == STATE_REALIZED)
161 return 0;
162 assert(self->state == STATE_ACCUMULATING);
163 self->state = STATE_REALIZED;
164
165 intermediate = PyUnicodeWriter_Finish(self->writer);
166 self->writer = NULL;
167 if (intermediate == NULL)
168 return -1;
169
170 /* Append the intermediate string to the internal buffer.
171 The length should be equal to the current cursor position.
172 */
173 len = PyUnicode_GET_LENGTH(intermediate);
174 if (resize_buffer(self, len) < 0) {
175 Py_DECREF(intermediate);
176 return -1;
177 }
178 if (!PyUnicode_AsUCS4(intermediate, self->buf, len, 0)) {
179 Py_DECREF(intermediate);
180 return -1;
181 }
182
183 Py_DECREF(intermediate);
184 return 0;
185 }
186
187 /* Internal routine for writing a whole PyUnicode object to the buffer of a
188 StringIO object. Returns 0 on success, or -1 on error. */
189 static Py_ssize_t
190 write_str(stringio *self, PyObject *obj)
191 {
192 Py_ssize_t len;
193 PyObject *decoded = NULL;
194
195 assert(self->buf != NULL);
196 assert(self->pos >= 0);
197
198 if (self->decoder != NULL) {
199 decoded = _PyIncrementalNewlineDecoder_decode(
200 self->decoder, obj, 1 /* always final */);
201 }
202 else {
203 decoded = Py_NewRef(obj);
204 }
205 if (self->writenl) {
206 PyObject *translated = PyUnicode_Replace(
207 decoded, _Py_LATIN1_CHR('\n'), self->writenl, -1);
208 Py_SETREF(decoded, translated);
209 }
210 if (decoded == NULL)
211 return -1;
212
213 assert(PyUnicode_Check(decoded));
214 len = PyUnicode_GET_LENGTH(decoded);
215 assert(len >= 0);
216
217 /* This overflow check is not strictly necessary. However, it avoids us to
218 deal with funky things like comparing an unsigned and a signed
219 integer. */
220 if (self->pos > PY_SSIZE_T_MAX - len) {
221 PyErr_SetString(PyExc_OverflowError,
222 "new position too large");
223 goto fail;
224 }
225
226 if (self->state == STATE_ACCUMULATING) {
227 if (self->string_size == self->pos) {
228 if (PyUnicodeWriter_WriteStr(self->writer, decoded))
229 goto fail;
230 goto success;
231 }
232 if (realize(self))
233 goto fail;
234 }
235
236 if (self->pos + len > self->string_size) {
237 if (resize_buffer(self, self->pos + len) < 0)
238 goto fail;
239 }
240
241 if (self->pos > self->string_size) {
242 /* In case of overseek, pad with null bytes the buffer region between
243 the end of stream and the current position.
244
245 0 lo string_size hi
246 | |<---used--->|<----------available----------->|
247 | | <--to pad-->|<---to write---> |
248 0 buf position
249
250 */
251 memset(self->buf + self->string_size, '\0',
252 (self->pos - self->string_size) * sizeof(Py_UCS4));
253 }
254
255 /* Copy the data to the internal buffer, overwriting some of the
256 existing data if self->pos < self->string_size. */
257 if (!PyUnicode_AsUCS4(decoded,
258 self->buf + self->pos,
259 self->buf_size - self->pos,
260 0))
261 goto fail;
262
263 success:
264 /* Set the new length of the internal string if it has changed. */
265 self->pos += len;
266 if (self->string_size < self->pos)
267 self->string_size = self->pos;
268
269 Py_DECREF(decoded);
270 return 0;
271
272 fail:
273 Py_XDECREF(decoded);
274 return -1;
275 }
276
277 /*[clinic input]
278 @critical_section
279 _io.StringIO.getvalue
280
281 Retrieve the entire contents of the object.
282 [clinic start generated code]*/
283
284 static PyObject *
285 _io_StringIO_getvalue_impl(stringio *self)
286 /*[clinic end generated code: output=27b6a7bfeaebce01 input=fb5dee06b8d467f3]*/
287 {
288 CHECK_INITIALIZED(self);
289 CHECK_CLOSED(self);
290 if (self->state == STATE_ACCUMULATING)
291 return make_intermediate(self);
292 return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, self->buf,
293 self->string_size);
294 }
295
296 /*[clinic input]
297 @critical_section
298 _io.StringIO.tell
299
300 Tell the current file position.
301 [clinic start generated code]*/
302
303 static PyObject *
304 _io_StringIO_tell_impl(stringio *self)
305 /*[clinic end generated code: output=2e87ac67b116c77b input=98a08f3e2dae3550]*/
306 {
307 CHECK_INITIALIZED(self);
308 CHECK_CLOSED(self);
309 return PyLong_FromSsize_t(self->pos);
310 }
311
312 /*[clinic input]
313 @critical_section
314 _io.StringIO.read
315 size: Py_ssize_t(accept={int, NoneType}) = -1
316 /
317
318 Read at most size characters, returned as a string.
319
320 If the argument is negative or omitted, read until EOF
321 is reached. Return an empty string at EOF.
322 [clinic start generated code]*/
323
324 static PyObject *
325 _io_StringIO_read_impl(stringio *self, Py_ssize_t size)
326 /*[clinic end generated code: output=ae8cf6002f71626c input=9fbef45d8aece8e7]*/
327 {
328 Py_ssize_t n;
329 Py_UCS4 *output;
330
331 CHECK_INITIALIZED(self);
332 CHECK_CLOSED(self);
333
334 /* adjust invalid sizes */
335 n = self->string_size - self->pos;
336 if (size < 0 || size > n) {
337 size = n;
338 if (size < 0)
339 size = 0;
340 }
341
342 /* Optimization for seek(0); read() */
343 if (self->state == STATE_ACCUMULATING && self->pos == 0 && size == n) {
344 PyObject *result = make_intermediate(self);
345 self->pos = self->string_size;
346 return result;
347 }
348
349 ENSURE_REALIZED(self);
350 output = self->buf + self->pos;
351 self->pos += size;
352 return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, size);
353 }
354
355 /* Internal helper, used by stringio_readline and stringio_iternext */
356 static PyObject *
357 _stringio_readline(stringio *self, Py_ssize_t limit)
358 {
359 Py_UCS4 *start, *end, old_char;
360 Py_ssize_t len, consumed;
361
362 /* In case of overseek, return the empty string */
363 if (self->pos >= self->string_size)
364 return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
365
366 start = self->buf + self->pos;
367 if (limit < 0 || limit > self->string_size - self->pos)
368 limit = self->string_size - self->pos;
369
370 end = start + limit;
371 old_char = *end;
372 *end = '\0';
373 len = _PyIO_find_line_ending(
374 self->readtranslate, self->readuniversal, self->readnl,
375 PyUnicode_4BYTE_KIND, (char*)start, (char*)end, &consumed);
376 *end = old_char;
377 /* If we haven't found any line ending, we just return everything
378 (`consumed` is ignored). */
379 if (len < 0)
380 len = limit;
381 self->pos += len;
382 return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, start, len);
383 }
384
385 /*[clinic input]
386 @critical_section
387 _io.StringIO.readline
388 size: Py_ssize_t(accept={int, NoneType}) = -1
389 /
390
391 Read until newline or EOF.
392
393 Returns an empty string if EOF is hit immediately.
394 [clinic start generated code]*/
395
396 static PyObject *
397 _io_StringIO_readline_impl(stringio *self, Py_ssize_t size)
398 /*[clinic end generated code: output=cabd6452f1b7e85d input=4d14b8495dea1d98]*/
399 {
400 CHECK_INITIALIZED(self);
401 CHECK_CLOSED(self);
402 ENSURE_REALIZED(self);
403
404 return _stringio_readline(self, size);
405 }
406
407 static PyObject *
408 stringio_iternext(PyObject *op)
409 {
410 PyObject *line;
411 stringio *self = stringio_CAST(op);
412
413 CHECK_INITIALIZED(self);
414 CHECK_CLOSED(self);
415 ENSURE_REALIZED(self);
416
417 if (Py_IS_TYPE(self, self->module_state->PyStringIO_Type)) {
418 /* Skip method call overhead for speed */
419 line = _stringio_readline(self, -1);
420 }
421 else {
422 /* XXX is subclassing StringIO really supported? */
423 line = PyObject_CallMethodNoArgs(op, &_Py_ID(readline));
424 if (line && !PyUnicode_Check(line)) {
425 PyErr_Format(PyExc_OSError,
426 "readline() should have returned a str object, "
427 "not '%.200s'", Py_TYPE(line)->tp_name);
428 Py_DECREF(line);
429 return NULL;
430 }
431 }
432
433 if (line == NULL)
434 return NULL;
435
436 if (PyUnicode_GET_LENGTH(line) == 0) {
437 /* Reached EOF */
438 Py_DECREF(line);
439 return NULL;
440 }
441
442 return line;
443 }
444
445 /*[clinic input]
446 @critical_section
447 _io.StringIO.truncate
448 pos as size: Py_ssize_t(accept={int, NoneType}, c_default="((stringio *)self)->pos") = None
449 /
450
451 Truncate size to pos.
452
453 The pos argument defaults to the current file position, as
454 returned by tell(). The current file position is unchanged.
455 Returns the new absolute position.
456 [clinic start generated code]*/
457
458 static PyObject *
459 _io_StringIO_truncate_impl(stringio *self, Py_ssize_t size)
460 /*[clinic end generated code: output=eb3aef8e06701365 input=fa8a6c98bb2ba780]*/
461 {
462 CHECK_INITIALIZED(self);
463 CHECK_CLOSED(self);
464
465 if (size < 0) {
466 PyErr_Format(PyExc_ValueError,
467 "Negative size value %zd", size);
468 return NULL;
469 }
470
471 if (size < self->string_size) {
472 ENSURE_REALIZED(self);
473 if (resize_buffer(self, size) < 0)
474 return NULL;
475 self->string_size = size;
476 }
477
478 return PyLong_FromSsize_t(size);
479 }
480
481 /*[clinic input]
482 @critical_section
483 _io.StringIO.seek
484 pos: Py_ssize_t
485 whence: int = 0
486 /
487
488 Change stream position.
489
490 Seek to character offset pos relative to position indicated by whence:
491 0 Start of stream (the default). pos should be >= 0;
492 1 Current position - pos must be 0;
493 2 End of stream - pos must be 0.
494 Returns the new absolute position.
495 [clinic start generated code]*/
496
497 static PyObject *
498 _io_StringIO_seek_impl(stringio *self, Py_ssize_t pos, int whence)
499 /*[clinic end generated code: output=e9e0ac9a8ae71c25 input=c75ced09343a00d7]*/
500 {
501 CHECK_INITIALIZED(self);
502 CHECK_CLOSED(self);
503
504 if (whence != 0 && whence != 1 && whence != 2) {
505 PyErr_Format(PyExc_ValueError,
506 "Invalid whence (%i, should be 0, 1 or 2)", whence);
507 return NULL;
508 }
509 else if (pos < 0 && whence == 0) {
510 PyErr_Format(PyExc_ValueError,
511 "Negative seek position %zd", pos);
512 return NULL;
513 }
514 else if (whence != 0 && pos != 0) {
515 PyErr_SetString(PyExc_OSError,
516 "Can't do nonzero cur-relative seeks");
517 return NULL;
518 }
519
520 /* whence = 0: offset relative to beginning of the string.
521 whence = 1: no change to current position.
522 whence = 2: change position to end of file. */
523 if (whence == 1) {
524 pos = self->pos;
525 }
526 else if (whence == 2) {
527 pos = self->string_size;
528 }
529
530 self->pos = pos;
531
532 return PyLong_FromSsize_t(self->pos);
533 }
534
535 /*[clinic input]
536 @critical_section
537 _io.StringIO.write
538 s as obj: object
539 /
540
541 Write string to file.
542
543 Returns the number of characters written, which is always equal to
544 the length of the string.
545 [clinic start generated code]*/
546
547 static PyObject *
548 _io_StringIO_write_impl(stringio *self, PyObject *obj)
549 /*[clinic end generated code: output=d53b1d841d7db288 input=1561272c0da4651f]*/
550 {
551 Py_ssize_t size;
552
553 CHECK_INITIALIZED(self);
554 if (!PyUnicode_Check(obj)) {
555 PyErr_Format(PyExc_TypeError, "string argument expected, got '%s'",
556 Py_TYPE(obj)->tp_name);
557 return NULL;
558 }
559 CHECK_CLOSED(self);
560 size = PyUnicode_GET_LENGTH(obj);
561
562 if (size > 0 && write_str(self, obj) < 0)
563 return NULL;
564
565 return PyLong_FromSsize_t(size);
566 }
567
568 /*[clinic input]
569 @critical_section
570 _io.StringIO.close
571
572 Close the IO object.
573
574 Attempting any further operation after the object is closed
575 will raise a ValueError.
576
577 This method has no effect if the file is already closed.
578 [clinic start generated code]*/
579
580 static PyObject *
581 _io_StringIO_close_impl(stringio *self)
582 /*[clinic end generated code: output=04399355cbe518f1 input=305d19aa29cc40b9]*/
583 {
584 self->closed = 1;
585 /* Free up some memory */
586 if (resize_buffer(self, 0) < 0)
587 return NULL;
588 PyUnicodeWriter_Discard(self->writer);
589 self->writer = NULL;
590 Py_CLEAR(self->readnl);
591 Py_CLEAR(self->writenl);
592 Py_CLEAR(self->decoder);
593 Py_RETURN_NONE;
594 }
595
596 static int
597 stringio_traverse(PyObject *op, visitproc visit, void *arg)
598 {
599 stringio *self = stringio_CAST(op);
600 Py_VISIT(Py_TYPE(self));
601 Py_VISIT(self->readnl);
602 Py_VISIT(self->writenl);
603 Py_VISIT(self->decoder);
604 Py_VISIT(self->dict);
605 return 0;
606 }
607
608 static int
609 stringio_clear(PyObject *op)
610 {
611 stringio *self = stringio_CAST(op);
612 Py_CLEAR(self->readnl);
613 Py_CLEAR(self->writenl);
614 Py_CLEAR(self->decoder);
615 Py_CLEAR(self->dict);
616 return 0;
617 }
618
619 static void
620 stringio_dealloc(PyObject *op)
621 {
622 stringio *self = stringio_CAST(op);
623 PyTypeObject *tp = Py_TYPE(self);
624 _PyObject_GC_UNTRACK(self);
625 self->ok = 0;
626 if (self->buf) {
627 PyMem_Free(self->buf);
628 self->buf = NULL;
629 }
630 PyUnicodeWriter_Discard(self->writer);
631 (void)stringio_clear(op);
632 FT_CLEAR_WEAKREFS(op, self->weakreflist);
633 tp->tp_free(self);
634 Py_DECREF(tp);
635 }
636
637 static PyObject *
638 stringio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
639 {
640 stringio *self;
641
642 assert(type != NULL && type->tp_alloc != NULL);
643 self = (stringio *)type->tp_alloc(type, 0);
644 if (self == NULL)
645 return NULL;
646
647 /* tp_alloc initializes all the fields to zero. So we don't have to
648 initialize them here. */
649
650 self->buf = (Py_UCS4 *)PyMem_Malloc(0);
651 if (self->buf == NULL) {
652 Py_DECREF(self);
653 return PyErr_NoMemory();
654 }
655
656 return (PyObject *)self;
657 }
658
659 /*[clinic input]
660 _io.StringIO.__init__
661 initial_value as value: object(c_default="NULL") = ''
662 newline as newline_obj: object(c_default="NULL") = '\n'
663
664 Text I/O implementation using an in-memory buffer.
665
666 The initial_value argument sets the value of object. The newline
667 argument is like the one of TextIOWrapper's constructor.
668 [clinic start generated code]*/
669
670 static int
671 _io_StringIO___init___impl(stringio *self, PyObject *value,
672 PyObject *newline_obj)
673 /*[clinic end generated code: output=a421ea023b22ef4e input=cee2d9181b2577a3]*/
674 {
675 const char *newline = "\n";
676 Py_ssize_t value_len;
677
678 /* Parse the newline argument. We only want to allow unicode objects or
679 None. */
680 if (newline_obj == Py_None) {
681 newline = NULL;
682 }
683 else if (newline_obj) {
684 if (!PyUnicode_Check(newline_obj)) {
685 PyErr_Format(PyExc_TypeError,
686 "newline must be str or None, not %.200s",
687 Py_TYPE(newline_obj)->tp_name);
688 return -1;
689 }
690 newline = PyUnicode_AsUTF8(newline_obj);
691 if (newline == NULL)
692 return -1;
693 }
694
695 if (newline && newline[0] != '\0'
696 && !(newline[0] == '\n' && newline[1] == '\0')
697 && !(newline[0] == '\r' && newline[1] == '\0')
698 && !(newline[0] == '\r' && newline[1] == '\n' && newline[2] == '\0')) {
699 PyErr_Format(PyExc_ValueError,
700 "illegal newline value: %R", newline_obj);
701 return -1;
702 }
703 if (value && value != Py_None && !PyUnicode_Check(value)) {
704 PyErr_Format(PyExc_TypeError,
705 "initial_value must be str or None, not %.200s",
706 Py_TYPE(value)->tp_name);
707 return -1;
708 }
709
710 self->ok = 0;
711
712 PyUnicodeWriter_Discard(self->writer);
713 self->writer = NULL;
714 Py_CLEAR(self->readnl);
715 Py_CLEAR(self->writenl);
716 Py_CLEAR(self->decoder);
717
718 assert((newline != NULL && newline_obj != Py_None) ||
719 (newline == NULL && newline_obj == Py_None));
720
721 if (newline) {
722 self->readnl = PyUnicode_FromString(newline);
723 if (self->readnl == NULL)
724 return -1;
725 }
726 self->readuniversal = (newline == NULL || newline[0] == '\0');
727 self->readtranslate = (newline == NULL);
728 /* If newline == "", we don't translate anything.
729 If newline == "\n" or newline == None, we translate to "\n", which is
730 a no-op.
731 (for newline == None, TextIOWrapper translates to os.linesep, but it
732 is pointless for StringIO)
733 */
734 if (newline != NULL && newline[0] == '\r') {
735 self->writenl = Py_NewRef(self->readnl);
736 }
737
738 _PyIO_State *module_state = find_io_state_by_def(Py_TYPE(self));
739 if (self->readuniversal) {
740 self->decoder = PyObject_CallFunctionObjArgs(
741 (PyObject *)module_state->PyIncrementalNewlineDecoder_Type,
742 Py_None, self->readtranslate ? Py_True : Py_False, NULL);
743 if (self->decoder == NULL)
744 return -1;
745 }
746
747 /* Now everything is set up, resize buffer to size of initial value,
748 and copy it */
749 self->string_size = 0;
750 if (value && value != Py_None)
751 value_len = PyUnicode_GetLength(value);
752 else
753 value_len = 0;
754 if (value_len > 0) {
755 /* This is a heuristic, for newline translation might change
756 the string length. */
757 if (resize_buffer(self, 0) < 0)
758 return -1;
759 self->state = STATE_REALIZED;
760 self->pos = 0;
761 if (write_str(self, value) < 0)
762 return -1;
763 }
764 else {
765 /* Empty stringio object, we can start by accumulating */
766 if (resize_buffer(self, 0) < 0)
767 return -1;
768 self->writer = PyUnicodeWriter_Create(0);
769 if (self->writer == NULL) {
770 return -1;
771 }
772 self->state = STATE_ACCUMULATING;
773 }
774 self->pos = 0;
775 self->module_state = module_state;
776 self->closed = 0;
777 self->ok = 1;
778 return 0;
779 }
780
781 /* Properties and pseudo-properties */
782
783 /*[clinic input]
784 @critical_section
785 _io.StringIO.readable
786
787 Returns True if the IO object can be read.
788 [clinic start generated code]*/
789
790 static PyObject *
791 _io_StringIO_readable_impl(stringio *self)
792 /*[clinic end generated code: output=b19d44dd8b1ceb99 input=6cd2ffd65a8e8763]*/
793 {
794 CHECK_INITIALIZED(self);
795 CHECK_CLOSED(self);
796 Py_RETURN_TRUE;
797 }
798
799 /*[clinic input]
800 @critical_section
801 _io.StringIO.writable
802
803 Returns True if the IO object can be written.
804 [clinic start generated code]*/
805
806 static PyObject *
807 _io_StringIO_writable_impl(stringio *self)
808 /*[clinic end generated code: output=13e4dd77187074ca input=1b3c63dbaa761c69]*/
809 {
810 CHECK_INITIALIZED(self);
811 CHECK_CLOSED(self);
812 Py_RETURN_TRUE;
813 }
814
815 /*[clinic input]
816 @critical_section
817 _io.StringIO.seekable
818
819 Returns True if the IO object can be seeked.
820 [clinic start generated code]*/
821
822 static PyObject *
823 _io_StringIO_seekable_impl(stringio *self)
824 /*[clinic end generated code: output=4d20b4641c756879 input=a820fad2cf085fc3]*/
825 {
826 CHECK_INITIALIZED(self);
827 CHECK_CLOSED(self);
828 Py_RETURN_TRUE;
829 }
830
831 /* Pickling support.
832
833 The implementation of __getstate__ is similar to the one for BytesIO,
834 except that we also save the newline parameter. For __setstate__ and unlike
835 BytesIO, we call __init__ to restore the object's state. Doing so allows us
836 to avoid decoding the complex newline state while keeping the object
837 representation compact.
838
839 See comment in bytesio.c regarding why only pickle protocols and onward are
840 supported.
841 */
842
843 /*[clinic input]
844 @critical_section
845 _io.StringIO.__getstate__
846
847 [clinic start generated code]*/
848
849 static PyObject *
850 _io_StringIO___getstate___impl(stringio *self)
851 /*[clinic end generated code: output=780be4a996410199 input=76f27255ef83bb92]*/
852 {
853 PyObject *initvalue = _io_StringIO_getvalue_impl(self);
854 PyObject *dict;
855 PyObject *state;
856
857 if (initvalue == NULL)
858 return NULL;
859 if (self->dict == NULL) {
860 dict = Py_NewRef(Py_None);
861 }
862 else {
863 dict = PyDict_Copy(self->dict);
864 if (dict == NULL) {
865 Py_DECREF(initvalue);
866 return NULL;
867 }
868 }
869
870 state = Py_BuildValue("(OOnN)", initvalue,
871 self->readnl ? self->readnl : Py_None,
872 self->pos, dict);
873 Py_DECREF(initvalue);
874 return state;
875 }
876
877 /*[clinic input]
878 @critical_section
879 _io.StringIO.__setstate__
880
881 state: object
882 /
883 [clinic start generated code]*/
884
885 static PyObject *
886 _io_StringIO___setstate___impl(stringio *self, PyObject *state)
887 /*[clinic end generated code: output=cb3962bc6d5c5609 input=8a27784b11b82e47]*/
888 {
889 PyObject *initarg;
890 PyObject *position_obj;
891 PyObject *dict;
892 Py_ssize_t pos;
893
894 assert(state != NULL);
895 CHECK_CLOSED(self);
896
897 /* We allow the state tuple to be longer than 4, because we may need
898 someday to extend the object's state without breaking
899 backward-compatibility. */
900 if (!PyTuple_Check(state) || PyTuple_GET_SIZE(state) < 4) {
901 PyErr_Format(PyExc_TypeError,
902 "%.200s.__setstate__ argument should be 4-tuple, got %.200s",
903 Py_TYPE(self)->tp_name, Py_TYPE(state)->tp_name);
904 return NULL;
905 }
906
907 /* Initialize the object's state. */
908 initarg = PyTuple_GetSlice(state, 0, 2);
909 if (initarg == NULL)
910 return NULL;
911 if (_io_StringIO___init__((PyObject *)self, initarg, NULL) < 0) {
912 Py_DECREF(initarg);
913 return NULL;
914 }
915 Py_DECREF(initarg);
916
917 /* Restore the buffer state. Even if __init__ did initialize the buffer,
918 we have to initialize it again since __init__ may translate the
919 newlines in the initial_value string. We clearly do not want that
920 because the string value in the state tuple has already been translated
921 once by __init__. So we do not take any chance and replace object's
922 buffer completely. */
923 {
924 PyObject *item = PyTuple_GET_ITEM(state, 0);
925 if (PyUnicode_Check(item)) {
926 Py_UCS4 *buf = PyUnicode_AsUCS4Copy(item);
927 if (buf == NULL)
928 return NULL;
929 Py_ssize_t bufsize = PyUnicode_GET_LENGTH(item);
930
931 if (resize_buffer(self, bufsize) < 0) {
932 PyMem_Free(buf);
933 return NULL;
934 }
935 memcpy(self->buf, buf, bufsize * sizeof(Py_UCS4));
936 PyMem_Free(buf);
937 self->string_size = bufsize;
938 }
939 else {
940 assert(item == Py_None);
941 self->string_size = 0;
942 }
943 }
944
945 /* Set carefully the position value. Alternatively, we could use the seek
946 method instead of modifying self->pos directly to better protect the
947 object internal state against erroneous (or malicious) inputs. */
948 position_obj = PyTuple_GET_ITEM(state, 2);
949 if (!PyLong_Check(position_obj)) {
950 PyErr_Format(PyExc_TypeError,
951 "third item of state must be an integer, got %.200s",
952 Py_TYPE(position_obj)->tp_name);
953 return NULL;
954 }
955 pos = PyLong_AsSsize_t(position_obj);
956 if (pos == -1 && PyErr_Occurred())
957 return NULL;
958 if (pos < 0) {
959 PyErr_SetString(PyExc_ValueError,
960 "position value cannot be negative");
961 return NULL;
962 }
963 self->pos = pos;
964
965 /* Set the dictionary of the instance variables. */
966 dict = PyTuple_GET_ITEM(state, 3);
967 if (dict != Py_None) {
968 if (!PyDict_Check(dict)) {
969 PyErr_Format(PyExc_TypeError,
970 "fourth item of state should be a dict, got a %.200s",
971 Py_TYPE(dict)->tp_name);
972 return NULL;
973 }
974 if (self->dict) {
975 /* Alternatively, we could replace the internal dictionary
976 completely. However, it seems more practical to just update it. */
977 if (PyDict_Update(self->dict, dict) < 0)
978 return NULL;
979 }
980 else {
981 self->dict = Py_NewRef(dict);
982 }
983 }
984
985 Py_RETURN_NONE;
986 }
987
988 /*[clinic input]
989 @critical_section
990 @getter
991 _io.StringIO.closed
992 [clinic start generated code]*/
993
994 static PyObject *
995 _io_StringIO_closed_get_impl(stringio *self)
996 /*[clinic end generated code: output=531ddca7954331d6 input=178d2ef24395fd49]*/
997 {
998 CHECK_INITIALIZED(self);
999 return PyBool_FromLong(self->closed);
1000 }
1001
1002 /*[clinic input]
1003 @critical_section
1004 @getter
1005 _io.StringIO.line_buffering
1006 [clinic start generated code]*/
1007
1008 static PyObject *
1009 _io_StringIO_line_buffering_get_impl(stringio *self)
1010 /*[clinic end generated code: output=360710e0112966ae input=6a7634e7f890745e]*/
1011 {
1012 CHECK_INITIALIZED(self);
1013 CHECK_CLOSED(self);
1014 Py_RETURN_FALSE;
1015 }
1016
1017 /*[clinic input]
1018 @critical_section
1019 @getter
1020 _io.StringIO.newlines
1021 [clinic start generated code]*/
1022
1023 static PyObject *
1024 _io_StringIO_newlines_get_impl(stringio *self)
1025 /*[clinic end generated code: output=35d7c0b66d7e0160 input=092a14586718244b]*/
1026 {
1027 CHECK_INITIALIZED(self);
1028 CHECK_CLOSED(self);
1029 if (self->decoder == NULL) {
1030 Py_RETURN_NONE;
1031 }
1032 return PyObject_GetAttr(self->decoder, &_Py_ID(newlines));
1033 }
1034
1035 static struct PyMethodDef stringio_methods[] = {
1036 _IO_STRINGIO_CLOSE_METHODDEF
1037 _IO_STRINGIO_GETVALUE_METHODDEF
1038 _IO_STRINGIO_READ_METHODDEF
1039 _IO_STRINGIO_READLINE_METHODDEF
1040 _IO_STRINGIO_TELL_METHODDEF
1041 _IO_STRINGIO_TRUNCATE_METHODDEF
1042 _IO_STRINGIO_SEEK_METHODDEF
1043 _IO_STRINGIO_WRITE_METHODDEF
1044
1045 _IO_STRINGIO_SEEKABLE_METHODDEF
1046 _IO_STRINGIO_READABLE_METHODDEF
1047 _IO_STRINGIO_WRITABLE_METHODDEF
1048
1049 _IO_STRINGIO___GETSTATE___METHODDEF
1050 _IO_STRINGIO___SETSTATE___METHODDEF
1051 {NULL, NULL} /* sentinel */
1052 };
1053
1054 static PyGetSetDef stringio_getset[] = {
1055 _IO_STRINGIO_CLOSED_GETSETDEF
1056 _IO_STRINGIO_NEWLINES_GETSETDEF
1057 /* (following comments straight off of the original Python wrapper:)
1058 XXX Cruft to support the TextIOWrapper API. This would only
1059 be meaningful if StringIO supported the buffer attribute.
1060 Hopefully, a better solution, than adding these pseudo-attributes,
1061 will be found.
1062 */
1063 _IO_STRINGIO_LINE_BUFFERING_GETSETDEF
1064 {NULL}
1065 };
1066
1067 static struct PyMemberDef stringio_members[] = {
1068 {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(stringio, weakreflist), Py_READONLY},
1069 {"__dictoffset__", Py_T_PYSSIZET, offsetof(stringio, dict), Py_READONLY},
1070 {NULL},
1071 };
1072
1073 static PyType_Slot stringio_slots[] = {
1074 {Py_tp_dealloc, stringio_dealloc},
1075 {Py_tp_doc, (void *)_io_StringIO___init____doc__},
1076 {Py_tp_traverse, stringio_traverse},
1077 {Py_tp_clear, stringio_clear},
1078 {Py_tp_iternext, stringio_iternext},
1079 {Py_tp_methods, stringio_methods},
1080 {Py_tp_members, stringio_members},
1081 {Py_tp_getset, stringio_getset},
1082 {Py_tp_init, _io_StringIO___init__},
1083 {Py_tp_new, stringio_new},
1084 {0, NULL},
1085 };
1086
1087 PyType_Spec stringio_spec = {
1088 .name = "_io.StringIO",
1089 .basicsize = sizeof(stringio),
1090 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
1091 Py_TPFLAGS_IMMUTABLETYPE),
1092 .slots = stringio_slots,
1093 };