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