Debian Codegraph @

Modules/_io/textio.c

raw
1 /*
2 An implementation of Text I/O as defined by PEP 3116 - "New I/O"
3
4 Classes defined here: TextIOBase, IncrementalNewlineDecoder, TextIOWrapper.
5
6 Written by Amaury Forgeot d'Arc and Antoine Pitrou
7 */
8
9 #include "Python.h"
10 #include "pycore_call.h" // _PyObject_CallMethod()
11 #include "pycore_codecs.h" // _PyCodecInfo_GetIncrementalDecoder()
12 #include "pycore_fileutils.h" // _Py_GetLocaleEncoding()
13 #include "pycore_interp.h" // PyInterpreterState.fs_codec
14 #include "pycore_long.h" // _PyLong_GetZero()
15 #include "pycore_object.h" // _PyObject_GC_UNTRACK()
16 #include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
17 #include "pycore_pystate.h" // _PyInterpreterState_GET()
18 #include "pycore_unicodeobject.h" // _PyUnicode_AsASCIIString()
19 #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()
20
21 #include "_iomodule.h"
22
23 /*[clinic input]
24 module _io
25 class _io.IncrementalNewlineDecoder "nldecoder_object *" "clinic_state()->PyIncrementalNewlineDecoder_Type"
26 class _io.TextIOWrapper "textio *" "clinic_state()->TextIOWrapper_Type"
27 class _io._TextIOBase "PyObject *" "&PyTextIOBase_Type"
28 [clinic start generated code]*/
29 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=8b7f24fa13bfdd7f]*/
30
31 typedef struct nldecoder_object nldecoder_object;
32 typedef struct textio textio;
33
34 #define clinic_state() (find_io_state_by_def(Py_TYPE(self)))
35 #include "clinic/textio.c.h"
36 #undef clinic_state
37
38 /* TextIOBase */
39
40 PyDoc_STRVAR(textiobase_doc,
41 "Base class for text I/O.\n"
42 "\n"
43 "This class provides a character and line based interface to stream\n"
44 "I/O. There is no readinto method because Python's character strings\n"
45 "are immutable.\n"
46 );
47
48 static PyObject *
49 _unsupported(_PyIO_State *state, const char *message)
50 {
51 PyErr_SetString(state->unsupported_operation, message);
52 return NULL;
53 }
54
55 /*[clinic input]
56 _io._TextIOBase.detach
57 cls: defining_class
58 /
59
60 Separate the underlying buffer from the TextIOBase and return it.
61
62 After the underlying buffer has been detached, the TextIO is in
63 an unusable state.
64 [clinic start generated code]*/
65
66 static PyObject *
67 _io__TextIOBase_detach_impl(PyObject *self, PyTypeObject *cls)
68 /*[clinic end generated code: output=50915f40c609eaa4 input=8099c088abcb87d8]*/
69 {
70 _PyIO_State *state = get_io_state_by_cls(cls);
71 return _unsupported(state, "detach");
72 }
73
74 /*[clinic input]
75 _io._TextIOBase.read
76 cls: defining_class
77 size: int(unused=True) = -1
78 /
79
80 Read at most size characters from stream.
81
82 Read from underlying buffer until we have size characters or we hit
83 EOF. If size is negative or omitted, read until EOF.
84 [clinic start generated code]*/
85
86 static PyObject *
87 _io__TextIOBase_read_impl(PyObject *self, PyTypeObject *cls,
88 int Py_UNUSED(size))
89 /*[clinic end generated code: output=51a5178a309ce647 input=c9fd4cc1cf1b4614]*/
90 {
91 _PyIO_State *state = get_io_state_by_cls(cls);
92 return _unsupported(state, "read");
93 }
94
95 /*[clinic input]
96 _io._TextIOBase.readline
97 cls: defining_class
98 size: int(unused=True) = -1
99 /
100
101 Read until newline or EOF.
102
103 Return an empty string if EOF is hit immediately.
104 If size is specified, at most size characters will be read.
105 [clinic start generated code]*/
106
107 static PyObject *
108 _io__TextIOBase_readline_impl(PyObject *self, PyTypeObject *cls,
109 int Py_UNUSED(size))
110 /*[clinic end generated code: output=3f47d7966d6d074e input=42eafec94107fa27]*/
111 {
112 _PyIO_State *state = get_io_state_by_cls(cls);
113 return _unsupported(state, "readline");
114 }
115
116 /*[clinic input]
117 _io._TextIOBase.write
118 cls: defining_class
119 s: str(unused=True)
120 /
121
122 Write string s to stream.
123
124 Return the number of characters written
125 (which is always equal to the length of the string).
126 [clinic start generated code]*/
127
128 static PyObject *
129 _io__TextIOBase_write_impl(PyObject *self, PyTypeObject *cls,
130 const char *Py_UNUSED(s))
131 /*[clinic end generated code: output=18b28231460275de input=e9cabaa5f6732b07]*/
132 {
133 _PyIO_State *state = get_io_state_by_cls(cls);
134 return _unsupported(state, "write");
135 }
136
137 /*[clinic input]
138 @getter
139 _io._TextIOBase.encoding
140
141 Encoding of the text stream.
142
143 Subclasses should override.
144 [clinic start generated code]*/
145
146 static PyObject *
147 _io__TextIOBase_encoding_get_impl(PyObject *self)
148 /*[clinic end generated code: output=e0f5d8f548b92432 input=4736d7621dd38f43]*/
149 {
150 Py_RETURN_NONE;
151 }
152
153 /*[clinic input]
154 @getter
155 _io._TextIOBase.newlines
156
157 Line endings translated so far.
158
159 Only line endings translated during reading are considered.
160
161 Subclasses should override.
162 [clinic start generated code]*/
163
164 static PyObject *
165 _io__TextIOBase_newlines_get_impl(PyObject *self)
166 /*[clinic end generated code: output=46ec147fb9f00c2a input=a5b196d076af1164]*/
167 {
168 Py_RETURN_NONE;
169 }
170
171 /*[clinic input]
172 @getter
173 _io._TextIOBase.errors
174
175 The error setting of the decoder or encoder.
176
177 Subclasses should override.
178 [clinic start generated code]*/
179
180 static PyObject *
181 _io__TextIOBase_errors_get_impl(PyObject *self)
182 /*[clinic end generated code: output=c6623d6addcd087d input=974aa52d1db93a82]*/
183 {
184 Py_RETURN_NONE;
185 }
186
187
188 static PyMethodDef textiobase_methods[] = {
189 _IO__TEXTIOBASE_DETACH_METHODDEF
190 _IO__TEXTIOBASE_READ_METHODDEF
191 _IO__TEXTIOBASE_READLINE_METHODDEF
192 _IO__TEXTIOBASE_WRITE_METHODDEF
193 {NULL, NULL}
194 };
195
196 static PyGetSetDef textiobase_getset[] = {
197 _IO__TEXTIOBASE_ENCODING_GETSETDEF
198 _IO__TEXTIOBASE_NEWLINES_GETSETDEF
199 _IO__TEXTIOBASE_ERRORS_GETSETDEF
200 {NULL}
201 };
202
203 static PyType_Slot textiobase_slots[] = {
204 {Py_tp_doc, (void *)textiobase_doc},
205 {Py_tp_methods, textiobase_methods},
206 {Py_tp_getset, textiobase_getset},
207 {0, NULL},
208 };
209
210 /* Do not set Py_TPFLAGS_HAVE_GC so that tp_traverse and tp_clear are inherited */
211 PyType_Spec textiobase_spec = {
212 .name = "_io._TextIOBase",
213 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
214 Py_TPFLAGS_IMMUTABLETYPE),
215 .slots = textiobase_slots,
216 };
217
218 /* IncrementalNewlineDecoder */
219
220 struct nldecoder_object {
221 PyObject_HEAD
222 PyObject *decoder;
223 PyObject *errors;
224 unsigned int pendingcr: 1;
225 unsigned int translate: 1;
226 unsigned int seennl: 3;
227 };
228
229 #define nldecoder_object_CAST(op) ((nldecoder_object *)(op))
230
231 /*[clinic input]
232 _io.IncrementalNewlineDecoder.__init__
233 decoder: object
234 translate: bool
235 errors: object(c_default="NULL") = "strict"
236
237 Codec used when reading a file in universal newlines mode.
238
239 It wraps another incremental decoder, translating \r\n and \r into \n.
240 It also records the types of newlines encountered. When used with
241 translate=False, it ensures that the newline sequence is returned in
242 one piece. When used with decoder=None, it expects unicode strings as
243 decode input and translates newlines without first invoking an external
244 decoder.
245 [clinic start generated code]*/
246
247 static int
248 _io_IncrementalNewlineDecoder___init___impl(nldecoder_object *self,
249 PyObject *decoder, int translate,
250 PyObject *errors)
251 /*[clinic end generated code: output=fbd04d443e764ec2 input=ed547aa257616b0e]*/
252 {
253
254 if (errors == NULL) {
255 errors = &_Py_ID(strict);
256 }
257 else {
258 errors = Py_NewRef(errors);
259 }
260
261 Py_XSETREF(self->errors, errors);
262 Py_XSETREF(self->decoder, Py_NewRef(decoder));
263 self->translate = translate ? 1 : 0;
264 self->seennl = 0;
265 self->pendingcr = 0;
266
267 return 0;
268 }
269
270 static int
271 incrementalnewlinedecoder_traverse(PyObject *op, visitproc visit, void *arg)
272 {
273 nldecoder_object *self = nldecoder_object_CAST(op);
274 Py_VISIT(Py_TYPE(self));
275 Py_VISIT(self->decoder);
276 Py_VISIT(self->errors);
277 return 0;
278 }
279
280 static int
281 incrementalnewlinedecoder_clear(PyObject *op)
282 {
283 nldecoder_object *self = nldecoder_object_CAST(op);
284 Py_CLEAR(self->decoder);
285 Py_CLEAR(self->errors);
286 return 0;
287 }
288
289 static void
290 incrementalnewlinedecoder_dealloc(PyObject *op)
291 {
292 nldecoder_object *self = nldecoder_object_CAST(op);
293 PyTypeObject *tp = Py_TYPE(self);
294 _PyObject_GC_UNTRACK(self);
295 (void)incrementalnewlinedecoder_clear(op);
296 tp->tp_free(self);
297 Py_DECREF(tp);
298 }
299
300 static int
301 check_decoded(PyObject *decoded)
302 {
303 if (decoded == NULL)
304 return -1;
305 if (!PyUnicode_Check(decoded)) {
306 PyErr_Format(PyExc_TypeError,
307 "decoder should return a string result, not '%.200s'",
308 Py_TYPE(decoded)->tp_name);
309 Py_DECREF(decoded);
310 return -1;
311 }
312 return 0;
313 }
314
315 #define CHECK_INITIALIZED_DECODER(self) \
316 if (self->errors == NULL) { \
317 PyErr_SetString(PyExc_ValueError, \
318 "IncrementalNewlineDecoder.__init__() not called"); \
319 return NULL; \
320 }
321
322 #define SEEN_CR 1
323 #define SEEN_LF 2
324 #define SEEN_CRLF 4
325 #define SEEN_ALL (SEEN_CR | SEEN_LF | SEEN_CRLF)
326
327 PyObject *
328 _PyIncrementalNewlineDecoder_decode(PyObject *myself,
329 PyObject *input, int final)
330 {
331 PyObject *output;
332 Py_ssize_t output_len;
333 nldecoder_object *self = nldecoder_object_CAST(myself);
334
335 CHECK_INITIALIZED_DECODER(self);
336
337 /* decode input (with the eventual \r from a previous pass) */
338 if (self->decoder != Py_None) {
339 output = PyObject_CallMethodObjArgs(self->decoder,
340 &_Py_ID(decode), input, final ? Py_True : Py_False, NULL);
341 }
342 else {
343 output = Py_NewRef(input);
344 }
345
346 if (check_decoded(output) < 0)
347 return NULL;
348
349 output_len = PyUnicode_GET_LENGTH(output);
350 if (self->pendingcr && (final || output_len > 0)) {
351 /* Prefix output with CR */
352 int kind;
353 PyObject *modified;
354 char *out;
355
356 modified = PyUnicode_New(output_len + 1,
357 PyUnicode_MAX_CHAR_VALUE(output));
358 if (modified == NULL)
359 goto error;
360 kind = PyUnicode_KIND(modified);
361 out = PyUnicode_DATA(modified);
362 PyUnicode_WRITE(kind, out, 0, '\r');
363 memcpy(out + kind, PyUnicode_DATA(output), kind * output_len);
364 Py_SETREF(output, modified);
365 self->pendingcr = 0;
366 output_len++;
367 }
368
369 /* retain last \r even when not translating data:
370 * then readline() is sure to get \r\n in one pass
371 */
372 if (!final) {
373 if (output_len > 0
374 && PyUnicode_READ_CHAR(output, output_len - 1) == '\r')
375 {
376 PyObject *modified = PyUnicode_Substring(output, 0, output_len -1);
377 if (modified == NULL)
378 goto error;
379 Py_SETREF(output, modified);
380 self->pendingcr = 1;
381 }
382 }
383
384 /* Record which newlines are read and do newline translation if desired,
385 all in one pass. */
386 {
387 const void *in_str;
388 Py_ssize_t len;
389 int seennl = self->seennl;
390 int only_lf = 0;
391 int kind;
392
393 in_str = PyUnicode_DATA(output);
394 len = PyUnicode_GET_LENGTH(output);
395 kind = PyUnicode_KIND(output);
396
397 if (len == 0)
398 return output;
399
400 /* If, up to now, newlines are consistently \n, do a quick check
401 for the \r *byte* with the libc's optimized memchr.
402 */
403 if (seennl == SEEN_LF || seennl == 0) {
404 only_lf = (memchr(in_str, '\r', kind * len) == NULL);
405 }
406
407 if (only_lf) {
408 /* If not already seen, quick scan for a possible "\n" character.
409 (there's nothing else to be done, even when in translation mode)
410 */
411 if (seennl == 0 &&
412 memchr(in_str, '\n', kind * len) != NULL) {
413 if (kind == PyUnicode_1BYTE_KIND)
414 seennl |= SEEN_LF;
415 else {
416 Py_ssize_t i = 0;
417 for (;;) {
418 Py_UCS4 c;
419 /* Fast loop for non-control characters */
420 while (PyUnicode_READ(kind, in_str, i) > '\n')
421 i++;
422 c = PyUnicode_READ(kind, in_str, i++);
423 if (c == '\n') {
424 seennl |= SEEN_LF;
425 break;
426 }
427 if (i >= len)
428 break;
429 }
430 }
431 }
432 /* Finished: we have scanned for newlines, and none of them
433 need translating */
434 }
435 else if (!self->translate) {
436 Py_ssize_t i = 0;
437 /* We have already seen all newline types, no need to scan again */
438 if (seennl == SEEN_ALL)
439 goto endscan;
440 for (;;) {
441 Py_UCS4 c;
442 /* Fast loop for non-control characters */
443 while (PyUnicode_READ(kind, in_str, i) > '\r')
444 i++;
445 c = PyUnicode_READ(kind, in_str, i++);
446 if (c == '\n')
447 seennl |= SEEN_LF;
448 else if (c == '\r') {
449 if (PyUnicode_READ(kind, in_str, i) == '\n') {
450 seennl |= SEEN_CRLF;
451 i++;
452 }
453 else
454 seennl |= SEEN_CR;
455 }
456 if (i >= len)
457 break;
458 if (seennl == SEEN_ALL)
459 break;
460 }
461 endscan:
462 ;
463 }
464 else {
465 void *translated;
466 int kind = PyUnicode_KIND(output);
467 const void *in_str = PyUnicode_DATA(output);
468 Py_ssize_t in, out;
469 /* XXX: Previous in-place translation here is disabled as
470 resizing is not possible anymore */
471 /* We could try to optimize this so that we only do a copy
472 when there is something to translate. On the other hand,
473 we already know there is a \r byte, so chances are high
474 that something needs to be done. */
475 translated = PyMem_Malloc(kind * len);
476 if (translated == NULL) {
477 PyErr_NoMemory();
478 goto error;
479 }
480 in = out = 0;
481 for (;;) {
482 Py_UCS4 c;
483 /* Fast loop for non-control characters */
484 while ((c = PyUnicode_READ(kind, in_str, in++)) > '\r')
485 PyUnicode_WRITE(kind, translated, out++, c);
486 if (c == '\n') {
487 PyUnicode_WRITE(kind, translated, out++, c);
488 seennl |= SEEN_LF;
489 continue;
490 }
491 if (c == '\r') {
492 if (PyUnicode_READ(kind, in_str, in) == '\n') {
493 in++;
494 seennl |= SEEN_CRLF;
495 }
496 else
497 seennl |= SEEN_CR;
498 PyUnicode_WRITE(kind, translated, out++, '\n');
499 continue;
500 }
501 if (in > len)
502 break;
503 PyUnicode_WRITE(kind, translated, out++, c);
504 }
505 Py_DECREF(output);
506 output = PyUnicode_FromKindAndData(kind, translated, out);
507 PyMem_Free(translated);
508 if (!output)
509 return NULL;
510 }
511 self->seennl |= seennl;
512 }
513
514 return output;
515
516 error:
517 Py_DECREF(output);
518 return NULL;
519 }
520
521 /*[clinic input]
522 @critical_section
523 _io.IncrementalNewlineDecoder.decode
524 input: object
525 final: bool = False
526 [clinic start generated code]*/
527
528 static PyObject *
529 _io_IncrementalNewlineDecoder_decode_impl(nldecoder_object *self,
530 PyObject *input, int final)
531 /*[clinic end generated code: output=0d486755bb37a66e input=9475d16a73168504]*/
532 {
533 return _PyIncrementalNewlineDecoder_decode((PyObject *) self, input, final);
534 }
535
536 /*[clinic input]
537 @critical_section
538 _io.IncrementalNewlineDecoder.getstate
539 [clinic start generated code]*/
540
541 static PyObject *
542 _io_IncrementalNewlineDecoder_getstate_impl(nldecoder_object *self)
543 /*[clinic end generated code: output=f0d2c9c136f4e0d0 input=dc3e1f27aa850f12]*/
544 {
545 PyObject *buffer;
546 unsigned long long flag;
547
548 CHECK_INITIALIZED_DECODER(self);
549
550 if (self->decoder != Py_None) {
551 PyObject *state = PyObject_CallMethodNoArgs(self->decoder,
552 &_Py_ID(getstate));
553 if (state == NULL)
554 return NULL;
555 if (!PyTuple_Check(state)) {
556 PyErr_SetString(PyExc_TypeError,
557 "illegal decoder state");
558 Py_DECREF(state);
559 return NULL;
560 }
561 if (!PyArg_ParseTuple(state, "OK;illegal decoder state",
562 &buffer, &flag))
563 {
564 Py_DECREF(state);
565 return NULL;
566 }
567 Py_INCREF(buffer);
568 Py_DECREF(state);
569 }
570 else {
571 buffer = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
572 flag = 0;
573 }
574 flag <<= 1;
575 if (self->pendingcr)
576 flag |= 1;
577 return Py_BuildValue("NK", buffer, flag);
578 }
579
580 /*[clinic input]
581 @critical_section
582 _io.IncrementalNewlineDecoder.setstate
583 state: object
584 /
585 [clinic start generated code]*/
586
587 static PyObject *
588 _io_IncrementalNewlineDecoder_setstate_impl(nldecoder_object *self,
589 PyObject *state)
590 /*[clinic end generated code: output=09135cb6e78a1dc8 input=275fd3982d2b08cb]*/
591 {
592 PyObject *buffer;
593 unsigned long long flag;
594
595 CHECK_INITIALIZED_DECODER(self);
596
597 if (!PyTuple_Check(state)) {
598 PyErr_SetString(PyExc_TypeError, "state argument must be a tuple");
599 return NULL;
600 }
601 if (!PyArg_ParseTuple(state, "OK;setstate(): illegal state argument",
602 &buffer, &flag))
603 {
604 return NULL;
605 }
606
607 self->pendingcr = (int) (flag & 1);
608 flag >>= 1;
609
610 if (self->decoder != Py_None) {
611 return _PyObject_CallMethod(self->decoder, &_Py_ID(setstate),
612 "((OK))", buffer, flag);
613 }
614 else {
615 Py_RETURN_NONE;
616 }
617 }
618
619 /*[clinic input]
620 @critical_section
621 _io.IncrementalNewlineDecoder.reset
622 [clinic start generated code]*/
623
624 static PyObject *
625 _io_IncrementalNewlineDecoder_reset_impl(nldecoder_object *self)
626 /*[clinic end generated code: output=32fa40c7462aa8ff input=31bd8ae4e36cec83]*/
627 {
628 CHECK_INITIALIZED_DECODER(self);
629
630 self->seennl = 0;
631 self->pendingcr = 0;
632 if (self->decoder != Py_None)
633 return PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset));
634 else
635 Py_RETURN_NONE;
636 }
637
638 static PyObject *
639 incrementalnewlinedecoder_newlines_get(PyObject *op, void *Py_UNUSED(context))
640 {
641 nldecoder_object *self = nldecoder_object_CAST(op);
642 CHECK_INITIALIZED_DECODER(self);
643
644 switch (self->seennl) {
645 case SEEN_CR:
646 return PyUnicode_FromString("\r");
647 case SEEN_LF:
648 return PyUnicode_FromString("\n");
649 case SEEN_CRLF:
650 return PyUnicode_FromString("\r\n");
651 case SEEN_CR | SEEN_LF:
652 return Py_BuildValue("ss", "\r", "\n");
653 case SEEN_CR | SEEN_CRLF:
654 return Py_BuildValue("ss", "\r", "\r\n");
655 case SEEN_LF | SEEN_CRLF:
656 return Py_BuildValue("ss", "\n", "\r\n");
657 case SEEN_CR | SEEN_LF | SEEN_CRLF:
658 return Py_BuildValue("sss", "\r", "\n", "\r\n");
659 default:
660 Py_RETURN_NONE;
661 }
662
663 }
664
665 /* TextIOWrapper */
666
667 typedef PyObject *(*encodefunc_t)(PyObject *, PyObject *);
668
669 struct textio
670 {
671 PyObject_HEAD
672 int ok; /* initialized? */
673 int detached;
674 Py_ssize_t chunk_size;
675 PyObject *buffer;
676 PyObject *encoding;
677 PyObject *encoder;
678 PyObject *decoder;
679 PyObject *readnl;
680 PyObject *errors;
681 const char *writenl; /* ASCII-encoded; NULL stands for \n */
682 char line_buffering;
683 char write_through;
684 char readuniversal;
685 char readtranslate;
686 char writetranslate;
687 char seekable;
688 char has_read1;
689 char telling;
690 char finalizing;
691 /* Specialized encoding func (see below) */
692 encodefunc_t encodefunc;
693 /* Whether or not it's the start of the stream */
694 char encoding_start_of_stream;
695
696 /* Reads and writes are internally buffered in order to speed things up.
697 However, any read will first flush the write buffer if itsn't empty.
698
699 Please also note that text to be written is first encoded before being
700 buffered. This is necessary so that encoding errors are immediately
701 reported to the caller, but it unfortunately means that the
702 IncrementalEncoder (whose encode() method is always written in Python)
703 becomes a bottleneck for small writes.
704 */
705 PyObject *decoded_chars; /* buffer for text returned from decoder */
706 Py_ssize_t decoded_chars_used; /* offset into _decoded_chars for read() */
707 PyObject *pending_bytes; // data waiting to be written.
708 // ascii unicode, bytes, or list of them.
709 Py_ssize_t pending_bytes_count;
710
711 /* snapshot is either NULL, or a tuple (dec_flags, next_input) where
712 * dec_flags is the second (integer) item of the decoder state and
713 * next_input is the chunk of input bytes that comes next after the
714 * snapshot point. We use this to reconstruct decoder states in tell().
715 */
716 PyObject *snapshot;
717 /* Bytes-to-characters ratio for the current chunk. Serves as input for
718 the heuristic in tell(). */
719 double b2cratio;
720
721 /* Cache raw object if it's a FileIO object */
722 PyObject *raw;
723
724 PyObject *weakreflist;
725 PyObject *dict;
726
727 _PyIO_State *state;
728 };
729
730 #define textio_CAST(op) ((textio *)(op))
731
732 static void
733 textiowrapper_set_decoded_chars(textio *self, PyObject *chars);
734
735 /* A couple of specialized cases in order to bypass the slow incremental
736 encoding methods for the most popular encodings. */
737
738 static PyObject *
739 ascii_encode(PyObject *op, PyObject *text)
740 {
741 textio *self = textio_CAST(op);
742 return _PyUnicode_AsASCIIString(text, PyUnicode_AsUTF8(self->errors));
743 }
744
745 static PyObject *
746 utf16be_encode(PyObject *op, PyObject *text)
747 {
748 textio *self = textio_CAST(op);
749 return _PyUnicode_EncodeUTF16(text, PyUnicode_AsUTF8(self->errors), 1);
750 }
751
752 static PyObject *
753 utf16le_encode(PyObject *op, PyObject *text)
754 {
755 textio *self = textio_CAST(op);
756 return _PyUnicode_EncodeUTF16(text, PyUnicode_AsUTF8(self->errors), -1);
757 }
758
759 static PyObject *
760 utf16_encode(PyObject *op, PyObject *text)
761 {
762 textio *self = textio_CAST(op);
763 if (!self->encoding_start_of_stream) {
764 /* Skip the BOM and use native byte ordering */
765 #if PY_BIG_ENDIAN
766 return utf16be_encode(op, text);
767 #else
768 return utf16le_encode(op, text);
769 #endif
770 }
771 return _PyUnicode_EncodeUTF16(text, PyUnicode_AsUTF8(self->errors), 0);
772 }
773
774 static PyObject *
775 utf32be_encode(PyObject *op, PyObject *text)
776 {
777 textio *self = textio_CAST(op);
778 return _PyUnicode_EncodeUTF32(text, PyUnicode_AsUTF8(self->errors), 1);
779 }
780
781 static PyObject *
782 utf32le_encode(PyObject *op, PyObject *text)
783 {
784 textio *self = textio_CAST(op);
785 return _PyUnicode_EncodeUTF32(text, PyUnicode_AsUTF8(self->errors), -1);
786 }
787
788 static PyObject *
789 utf32_encode(PyObject *op, PyObject *text)
790 {
791 textio *self = textio_CAST(op);
792 if (!self->encoding_start_of_stream) {
793 /* Skip the BOM and use native byte ordering */
794 #if PY_BIG_ENDIAN
795 return utf32be_encode(op, text);
796 #else
797 return utf32le_encode(op, text);
798 #endif
799 }
800 return _PyUnicode_EncodeUTF32(text, PyUnicode_AsUTF8(self->errors), 0);
801 }
802
803 static PyObject *
804 utf8_encode(PyObject *op, PyObject *text)
805 {
806 textio *self = textio_CAST(op);
807 return _PyUnicode_AsUTF8String(text, PyUnicode_AsUTF8(self->errors));
808 }
809
810 static PyObject *
811 latin1_encode(PyObject *op, PyObject *text)
812 {
813 textio *self = textio_CAST(op);
814 return _PyUnicode_AsLatin1String(text, PyUnicode_AsUTF8(self->errors));
815 }
816
817 // Return true when encoding can be skipped when text is ascii.
818 static inline int
819 is_asciicompat_encoding(encodefunc_t f)
820 {
821 return f == ascii_encode || f == latin1_encode || f == utf8_encode;
822 }
823
824 /* Map normalized encoding names onto the specialized encoding funcs */
825
826 typedef struct {
827 const char *name;
828 encodefunc_t encodefunc;
829 } encodefuncentry;
830
831 static const encodefuncentry encodefuncs[] = {
832 {"ascii", ascii_encode},
833 {"iso8859-1", latin1_encode},
834 {"utf-8", utf8_encode},
835 {"utf-16-be", utf16be_encode},
836 {"utf-16-le", utf16le_encode},
837 {"utf-16", utf16_encode},
838 {"utf-32-be", utf32be_encode},
839 {"utf-32-le", utf32le_encode},
840 {"utf-32", utf32_encode},
841 {NULL, NULL}
842 };
843
844 static int
845 validate_newline(const char *newline)
846 {
847 if (newline && newline[0] != '\0'
848 && !(newline[0] == '\n' && newline[1] == '\0')
849 && !(newline[0] == '\r' && newline[1] == '\0')
850 && !(newline[0] == '\r' && newline[1] == '\n' && newline[2] == '\0')) {
851 PyErr_Format(PyExc_ValueError,
852 "illegal newline value: %s", newline);
853 return -1;
854 }
855 return 0;
856 }
857
858 static int
859 set_newline(textio *self, const char *newline)
860 {
861 PyObject *old = self->readnl;
862 if (newline == NULL) {
863 self->readnl = NULL;
864 }
865 else {
866 self->readnl = PyUnicode_FromString(newline);
867 if (self->readnl == NULL) {
868 self->readnl = old;
869 return -1;
870 }
871 }
872 self->readuniversal = (newline == NULL || newline[0] == '\0');
873 self->readtranslate = (newline == NULL);
874 self->writetranslate = (newline == NULL || newline[0] != '\0');
875 if (!self->readuniversal && self->readnl != NULL) {
876 // validate_newline() accepts only ASCII newlines.
877 assert(PyUnicode_KIND(self->readnl) == PyUnicode_1BYTE_KIND);
878 self->writenl = (const char *)PyUnicode_1BYTE_DATA(self->readnl);
879 if (strcmp(self->writenl, "\n") == 0) {
880 self->writenl = NULL;
881 }
882 }
883 else {
884 #ifdef MS_WINDOWS
885 self->writenl = "\r\n";
886 #else
887 self->writenl = NULL;
888 #endif
889 }
890 Py_XDECREF(old);
891 return 0;
892 }
893
894 static int
895 _textiowrapper_set_decoder(textio *self, PyObject *codec_info,
896 const char *errors)
897 {
898 PyObject *res;
899 int r;
900
901 res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(readable));
902 if (res == NULL)
903 return -1;
904
905 r = PyObject_IsTrue(res);
906 Py_DECREF(res);
907 if (r == -1)
908 return -1;
909
910 if (r != 1)
911 return 0;
912
913 Py_CLEAR(self->decoder);
914 self->decoder = _PyCodecInfo_GetIncrementalDecoder(codec_info, errors);
915 if (self->decoder == NULL)
916 return -1;
917
918 if (self->readuniversal) {
919 _PyIO_State *state = self->state;
920 PyObject *incrementalDecoder = PyObject_CallFunctionObjArgs(
921 (PyObject *)state->PyIncrementalNewlineDecoder_Type,
922 self->decoder, self->readtranslate ? Py_True : Py_False, NULL);
923 if (incrementalDecoder == NULL)
924 return -1;
925 Py_XSETREF(self->decoder, incrementalDecoder);
926 }
927
928 return 0;
929 }
930
931 static PyObject*
932 _textiowrapper_decode(_PyIO_State *state, PyObject *decoder, PyObject *bytes,
933 int eof)
934 {
935 PyObject *chars;
936
937 if (Py_IS_TYPE(decoder, state->PyIncrementalNewlineDecoder_Type))
938 chars = _PyIncrementalNewlineDecoder_decode(decoder, bytes, eof);
939 else
940 chars = PyObject_CallMethodObjArgs(decoder, &_Py_ID(decode), bytes,
941 eof ? Py_True : Py_False, NULL);
942
943 if (check_decoded(chars) < 0)
944 // check_decoded already decreases refcount
945 return NULL;
946
947 return chars;
948 }
949
950 static int
951 _textiowrapper_set_encoder(textio *self, PyObject *codec_info,
952 const char *errors)
953 {
954 PyObject *res;
955 int r;
956
957 res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(writable));
958 if (res == NULL)
959 return -1;
960
961 r = PyObject_IsTrue(res);
962 Py_DECREF(res);
963 if (r == -1)
964 return -1;
965
966 if (r != 1)
967 return 0;
968
969 Py_CLEAR(self->encoder);
970 self->encodefunc = NULL;
971 self->encoder = _PyCodecInfo_GetIncrementalEncoder(codec_info, errors);
972 if (self->encoder == NULL)
973 return -1;
974
975 /* Get the normalized named of the codec */
976 if (PyObject_GetOptionalAttr(codec_info, &_Py_ID(name), &res) < 0) {
977 return -1;
978 }
979 if (res != NULL && PyUnicode_Check(res)) {
980 const encodefuncentry *e = encodefuncs;
981 while (e->name != NULL) {
982 if (_PyUnicode_EqualToASCIIString(res, e->name)) {
983 self->encodefunc = e->encodefunc;
984 break;
985 }
986 e++;
987 }
988 }
989 Py_XDECREF(res);
990
991 return 0;
992 }
993
994 static int
995 _textiowrapper_fix_encoder_state(textio *self)
996 {
997 if (!self->seekable || !self->encoder) {
998 return 0;
999 }
1000
1001 self->encoding_start_of_stream = 1;
1002
1003 PyObject *cookieObj = PyObject_CallMethodNoArgs(
1004 self->buffer, &_Py_ID(tell));
1005 if (cookieObj == NULL) {
1006 return -1;
1007 }
1008
1009 int cmp = PyObject_RichCompareBool(cookieObj, _PyLong_GetZero(), Py_EQ);
1010 Py_DECREF(cookieObj);
1011 if (cmp < 0) {
1012 return -1;
1013 }
1014
1015 if (cmp == 0) {
1016 self->encoding_start_of_stream = 0;
1017 PyObject *res = PyObject_CallMethodOneArg(
1018 self->encoder, &_Py_ID(setstate), _PyLong_GetZero());
1019 if (res == NULL) {
1020 return -1;
1021 }
1022 Py_DECREF(res);
1023 }
1024
1025 return 0;
1026 }
1027
1028 static int
1029 io_check_errors(PyObject *errors)
1030 {
1031 assert(errors != NULL && errors != Py_None);
1032
1033 PyInterpreterState *interp = _PyInterpreterState_GET();
1034 #ifndef Py_DEBUG
1035 /* In release mode, only check in development mode (-X dev) */
1036 if (!_PyInterpreterState_GetConfig(interp)->dev_mode) {
1037 return 0;
1038 }
1039 #else
1040 /* Always check in debug mode */
1041 #endif
1042
1043 /* Avoid calling PyCodec_LookupError() before the codec registry is ready:
1044 before_PyUnicode_InitEncodings() is called. */
1045 if (!interp->unicode.fs_codec.encoding) {
1046 return 0;
1047 }
1048
1049 const char *name = _PyUnicode_AsUTF8NoNUL(errors);
1050 if (name == NULL) {
1051 return -1;
1052 }
1053 PyObject *handler = PyCodec_LookupError(name);
1054 if (handler != NULL) {
1055 Py_DECREF(handler);
1056 return 0;
1057 }
1058 return -1;
1059 }
1060
1061
1062
1063 /*[clinic input]
1064 @critical_section
1065 _io.TextIOWrapper.__init__
1066 buffer: object
1067 encoding: str(accept={str, NoneType}) = None
1068 errors: object = None
1069 newline: str(accept={str, NoneType}) = None
1070 line_buffering: bool = False
1071 write_through: bool = False
1072
1073 Character and line based layer over a BufferedIOBase object, buffer.
1074
1075 encoding gives the name of the encoding that the stream will be
1076 decoded or encoded with. It defaults to locale.getencoding().
1077
1078 errors determines the strictness of encoding and decoding (see
1079 help(codecs.Codec) or the documentation for codecs.register) and
1080 defaults to "strict".
1081
1082 newline controls how line endings are handled. It can be None, '',
1083 '\n', '\r', and '\r\n'. It works as follows:
1084
1085 * On input, if newline is None, universal newlines mode is
1086 enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
1087 these are translated into '\n' before being returned to the
1088 caller. If it is '', universal newline mode is enabled, but line
1089 endings are returned to the caller untranslated. If it has any of
1090 the other legal values, input lines are only terminated by the given
1091 string, and the line ending is returned to the caller untranslated.
1092
1093 * On output, if newline is None, any '\n' characters written are
1094 translated to the system default line separator, os.linesep. If
1095 newline is '' or '\n', no translation takes place. If newline is any
1096 of the other legal values, any '\n' characters written are translated
1097 to the given string.
1098
1099 If line_buffering is True, a call to flush is implied when a call to
1100 write contains a newline character.
1101 [clinic start generated code]*/
1102
1103 static int
1104 _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
1105 const char *encoding, PyObject *errors,
1106 const char *newline, int line_buffering,
1107 int write_through)
1108 /*[clinic end generated code: output=72267c0c01032ed2 input=0f077220214c40a4]*/
1109 {
1110 PyObject *raw, *codec_info = NULL;
1111 PyObject *res;
1112 int r;
1113
1114 self->ok = 0;
1115 self->detached = 0;
1116
1117 if (encoding == NULL) {
1118 PyInterpreterState *interp = _PyInterpreterState_GET();
1119 if (_PyInterpreterState_GetConfig(interp)->warn_default_encoding) {
1120 if (PyErr_WarnEx(PyExc_EncodingWarning,
1121 "'encoding' argument not specified", 1)) {
1122 return -1;
1123 }
1124 }
1125 }
1126
1127 if (errors == Py_None) {
1128 errors = &_Py_ID(strict);
1129 }
1130 else if (!PyUnicode_Check(errors)) {
1131 // Check 'errors' argument here because Argument Clinic doesn't support
1132 // 'str(accept={str, NoneType})' converter.
1133 PyErr_Format(
1134 PyExc_TypeError,
1135 "TextIOWrapper() argument 'errors' must be str or None, not %.50s",
1136 Py_TYPE(errors)->tp_name);
1137 return -1;
1138 }
1139 else if (io_check_errors(errors)) {
1140 return -1;
1141 }
1142 const char *errors_str = _PyUnicode_AsUTF8NoNUL(errors);
1143 if (errors_str == NULL) {
1144 return -1;
1145 }
1146
1147 if (validate_newline(newline) < 0) {
1148 return -1;
1149 }
1150
1151 Py_CLEAR(self->buffer);
1152 Py_CLEAR(self->encoding);
1153 Py_CLEAR(self->encoder);
1154 Py_CLEAR(self->decoder);
1155 Py_CLEAR(self->readnl);
1156 Py_CLEAR(self->decoded_chars);
1157 Py_CLEAR(self->pending_bytes);
1158 Py_CLEAR(self->snapshot);
1159 Py_CLEAR(self->errors);
1160 Py_CLEAR(self->raw);
1161 self->decoded_chars_used = 0;
1162 self->pending_bytes_count = 0;
1163 self->encodefunc = NULL;
1164 self->b2cratio = 0.0;
1165
1166 if (encoding == NULL && _PyRuntime.preconfig.utf8_mode) {
1167 _Py_DECLARE_STR(utf_8, "utf-8");
1168 self->encoding = &_Py_STR(utf_8);
1169 }
1170 else if (encoding == NULL || (strcmp(encoding, "locale") == 0)) {
1171 self->encoding = _Py_GetLocaleEncodingObject();
1172 if (self->encoding == NULL) {
1173 goto error;
1174 }
1175 assert(PyUnicode_Check(self->encoding));
1176 }
1177
1178 if (self->encoding != NULL) {
1179 encoding = PyUnicode_AsUTF8(self->encoding);
1180 if (encoding == NULL)
1181 goto error;
1182 }
1183 else if (encoding != NULL) {
1184 self->encoding = PyUnicode_FromString(encoding);
1185 if (self->encoding == NULL)
1186 goto error;
1187 }
1188 else {
1189 PyErr_SetString(PyExc_OSError,
1190 "could not determine default encoding");
1191 goto error;
1192 }
1193
1194 /* Check we have been asked for a real text encoding */
1195 codec_info = _PyCodec_LookupTextEncoding(encoding, NULL);
1196 if (codec_info == NULL) {
1197 Py_CLEAR(self->encoding);
1198 goto error;
1199 }
1200
1201 /* XXX: Failures beyond this point have the potential to leak elements
1202 * of the partially constructed object (like self->encoding)
1203 */
1204
1205 self->errors = Py_NewRef(errors);
1206 self->chunk_size = 8192;
1207 self->line_buffering = line_buffering;
1208 self->write_through = write_through;
1209 if (set_newline(self, newline) < 0) {
1210 goto error;
1211 }
1212
1213 self->buffer = Py_NewRef(buffer);
1214
1215 /* Build the decoder object */
1216 _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
1217 self->state = state;
1218 if (_textiowrapper_set_decoder(self, codec_info, errors_str) != 0)
1219 goto error;
1220
1221 /* Build the encoder object */
1222 if (_textiowrapper_set_encoder(self, codec_info, errors_str) != 0)
1223 goto error;
1224
1225 /* Finished sorting out the codec details */
1226 Py_CLEAR(codec_info);
1227
1228 if (Py_IS_TYPE(buffer, state->PyBufferedReader_Type) ||
1229 Py_IS_TYPE(buffer, state->PyBufferedWriter_Type) ||
1230 Py_IS_TYPE(buffer, state->PyBufferedRandom_Type))
1231 {
1232 if (PyObject_GetOptionalAttr(buffer, &_Py_ID(raw), &raw) < 0)
1233 goto error;
1234 /* Cache the raw FileIO object to speed up 'closed' checks */
1235 if (raw != NULL) {
1236 if (Py_IS_TYPE(raw, state->PyFileIO_Type))
1237 self->raw = raw;
1238 else
1239 Py_DECREF(raw);
1240 }
1241 }
1242
1243 res = PyObject_CallMethodNoArgs(buffer, &_Py_ID(seekable));
1244 if (res == NULL)
1245 goto error;
1246 r = PyObject_IsTrue(res);
1247 Py_DECREF(res);
1248 if (r < 0)
1249 goto error;
1250 self->seekable = self->telling = r;
1251
1252 r = PyObject_HasAttrWithError(buffer, &_Py_ID(read1));
1253 if (r < 0) {
1254 goto error;
1255 }
1256 self->has_read1 = r;
1257
1258 self->encoding_start_of_stream = 0;
1259 if (_textiowrapper_fix_encoder_state(self) < 0) {
1260 goto error;
1261 }
1262
1263 self->ok = 1;
1264 return 0;
1265
1266 error:
1267 Py_XDECREF(codec_info);
1268 return -1;
1269 }
1270
1271 /* Return *default_value* if ob is None, 0 if ob is false, 1 if ob is true,
1272 * -1 on error.
1273 */
1274 static int
1275 convert_optional_bool(PyObject *obj, int default_value)
1276 {
1277 long v;
1278 if (obj == Py_None) {
1279 v = default_value;
1280 }
1281 else {
1282 v = PyLong_AsLong(obj);
1283 if (v == -1 && PyErr_Occurred())
1284 return -1;
1285 }
1286 return v != 0;
1287 }
1288
1289 static int
1290 textiowrapper_change_encoding(textio *self, PyObject *encoding,
1291 PyObject *errors, int newline_changed)
1292 {
1293 /* Use existing settings where new settings are not specified */
1294 if (encoding == Py_None && errors == Py_None && !newline_changed) {
1295 return 0; // no change
1296 }
1297
1298 if (encoding == Py_None) {
1299 encoding = self->encoding;
1300 if (errors == Py_None) {
1301 errors = self->errors;
1302 }
1303 Py_INCREF(encoding);
1304 }
1305 else {
1306 if (_PyUnicode_EqualToASCIIString(encoding, "locale")) {
1307 encoding = _Py_GetLocaleEncodingObject();
1308 if (encoding == NULL) {
1309 return -1;
1310 }
1311 } else {
1312 Py_INCREF(encoding);
1313 }
1314 if (errors == Py_None) {
1315 errors = &_Py_ID(strict);
1316 }
1317 }
1318 Py_INCREF(errors);
1319
1320 const char *c_encoding = PyUnicode_AsUTF8(encoding);
1321 if (c_encoding == NULL) {
1322 Py_DECREF(encoding);
1323 Py_DECREF(errors);
1324 return -1;
1325 }
1326 const char *c_errors = PyUnicode_AsUTF8(errors);
1327 if (c_errors == NULL) {
1328 Py_DECREF(encoding);
1329 Py_DECREF(errors);
1330 return -1;
1331 }
1332
1333 // Create new encoder & decoder
1334 PyObject *codec_info = _PyCodec_LookupTextEncoding(c_encoding, NULL);
1335 if (codec_info == NULL) {
1336 Py_DECREF(encoding);
1337 Py_DECREF(errors);
1338 return -1;
1339 }
1340 if (_textiowrapper_set_decoder(self, codec_info, c_errors) != 0 ||
1341 _textiowrapper_set_encoder(self, codec_info, c_errors) != 0) {
1342 Py_DECREF(codec_info);
1343 Py_DECREF(encoding);
1344 Py_DECREF(errors);
1345 return -1;
1346 }
1347 Py_DECREF(codec_info);
1348
1349 Py_SETREF(self->encoding, encoding);
1350 Py_SETREF(self->errors, errors);
1351
1352 return _textiowrapper_fix_encoder_state(self);
1353 }
1354
1355 /*[clinic input]
1356 @critical_section
1357 _io.TextIOWrapper.reconfigure
1358 *
1359 encoding: object = None
1360 errors: object = None
1361 newline as newline_obj: object(c_default="NULL") = None
1362 line_buffering as line_buffering_obj: object = None
1363 write_through as write_through_obj: object = None
1364
1365 Reconfigure the text stream with new parameters.
1366
1367 This also does an implicit stream flush.
1368
1369 [clinic start generated code]*/
1370
1371 static PyObject *
1372 _io_TextIOWrapper_reconfigure_impl(textio *self, PyObject *encoding,
1373 PyObject *errors, PyObject *newline_obj,
1374 PyObject *line_buffering_obj,
1375 PyObject *write_through_obj)
1376 /*[clinic end generated code: output=52b812ff4b3d4b0f input=dc3bd35ebda702a7]*/
1377 {
1378 int line_buffering;
1379 int write_through;
1380 const char *newline = NULL;
1381
1382 if (encoding != Py_None && !PyUnicode_Check(encoding)) {
1383 PyErr_Format(PyExc_TypeError,
1384 "reconfigure() argument 'encoding' must be str or None, not %s",
1385 Py_TYPE(encoding)->tp_name);
1386 return NULL;
1387 }
1388 if (errors != Py_None && !PyUnicode_Check(errors)) {
1389 PyErr_Format(PyExc_TypeError,
1390 "reconfigure() argument 'errors' must be str or None, not %s",
1391 Py_TYPE(errors)->tp_name);
1392 return NULL;
1393 }
1394 if (newline_obj != NULL && newline_obj != Py_None &&
1395 !PyUnicode_Check(newline_obj))
1396 {
1397 PyErr_Format(PyExc_TypeError,
1398 "reconfigure() argument 'newline' must be str or None, not %s",
1399 Py_TYPE(newline_obj)->tp_name);
1400 return NULL;
1401 }
1402 /* Check if something is in the read buffer */
1403 if (self->decoded_chars != NULL) {
1404 if (encoding != Py_None || errors != Py_None || newline_obj != NULL) {
1405 _unsupported(self->state,
1406 "It is not possible to set the encoding or newline "
1407 "of stream after the first read");
1408 return NULL;
1409 }
1410 }
1411
1412 if (newline_obj != NULL && newline_obj != Py_None) {
1413 newline = PyUnicode_AsUTF8(newline_obj);
1414 if (newline == NULL || validate_newline(newline) < 0) {
1415 return NULL;
1416 }
1417 }
1418
1419 line_buffering = convert_optional_bool(line_buffering_obj,
1420 self->line_buffering);
1421 if (line_buffering < 0) {
1422 return NULL;
1423 }
1424 write_through = convert_optional_bool(write_through_obj,
1425 self->write_through);
1426 if (write_through < 0) {
1427 return NULL;
1428 }
1429
1430 if (_PyFile_Flush((PyObject *)self) < 0) {
1431 return NULL;
1432 }
1433 self->b2cratio = 0;
1434
1435 if (newline_obj != NULL && set_newline(self, newline) < 0) {
1436 return NULL;
1437 }
1438
1439 if (textiowrapper_change_encoding(
1440 self, encoding, errors, newline_obj != NULL) < 0) {
1441 return NULL;
1442 }
1443
1444 self->line_buffering = line_buffering;
1445 self->write_through = write_through;
1446 Py_RETURN_NONE;
1447 }
1448
1449 static int
1450 textiowrapper_clear(PyObject *op)
1451 {
1452 textio *self = textio_CAST(op);
1453 self->ok = 0;
1454 Py_CLEAR(self->buffer);
1455 Py_CLEAR(self->encoding);
1456 Py_CLEAR(self->encoder);
1457 Py_CLEAR(self->decoder);
1458 Py_CLEAR(self->readnl);
1459 Py_CLEAR(self->decoded_chars);
1460 Py_CLEAR(self->pending_bytes);
1461 Py_CLEAR(self->snapshot);
1462 Py_CLEAR(self->errors);
1463 Py_CLEAR(self->raw);
1464
1465 Py_CLEAR(self->dict);
1466 return 0;
1467 }
1468
1469 static void
1470 textiowrapper_dealloc(PyObject *op)
1471 {
1472 textio *self = textio_CAST(op);
1473 PyTypeObject *tp = Py_TYPE(self);
1474 self->finalizing = 1;
1475 if (_PyIOBase_finalize(op) < 0)
1476 return;
1477 self->ok = 0;
1478 _PyObject_GC_UNTRACK(self);
1479 FT_CLEAR_WEAKREFS(op, self->weakreflist);
1480 (void)textiowrapper_clear(op);
1481 tp->tp_free(self);
1482 Py_DECREF(tp);
1483 }
1484
1485 static int
1486 textiowrapper_traverse(PyObject *op, visitproc visit, void *arg)
1487 {
1488 textio *self = textio_CAST(op);
1489 Py_VISIT(Py_TYPE(self));
1490 Py_VISIT(self->buffer);
1491 Py_VISIT(self->encoding);
1492 Py_VISIT(self->encoder);
1493 Py_VISIT(self->decoder);
1494 Py_VISIT(self->readnl);
1495 Py_VISIT(self->decoded_chars);
1496 Py_VISIT(self->pending_bytes);
1497 Py_VISIT(self->snapshot);
1498 Py_VISIT(self->errors);
1499 Py_VISIT(self->raw);
1500
1501 Py_VISIT(self->dict);
1502 return 0;
1503 }
1504
1505 static PyObject *
1506 _io_TextIOWrapper_closed_get_impl(textio *self);
1507
1508 /* This macro takes some shortcuts to make the common case faster. */
1509 #define CHECK_CLOSED(self) \
1510 do { \
1511 int r; \
1512 PyObject *_res; \
1513 if (Py_IS_TYPE(self, self->state->PyTextIOWrapper_Type)) { \
1514 if (self->raw != NULL) \
1515 r = _PyFileIO_closed(self->raw); \
1516 else { \
1517 _res = _io_TextIOWrapper_closed_get_impl(self); \
1518 if (_res == NULL) \
1519 return NULL; \
1520 r = PyObject_IsTrue(_res); \
1521 Py_DECREF(_res); \
1522 if (r < 0) \
1523 return NULL; \
1524 } \
1525 if (r > 0) { \
1526 PyErr_SetString(PyExc_ValueError, \
1527 "I/O operation on closed file."); \
1528 return NULL; \
1529 } \
1530 } \
1531 else if (_PyIOBase_check_closed((PyObject *)self, Py_True) == NULL) \
1532 return NULL; \
1533 } while (0)
1534
1535 #define CHECK_INITIALIZED(self) \
1536 if (self->ok <= 0) { \
1537 PyErr_SetString(PyExc_ValueError, \
1538 "I/O operation on uninitialized object"); \
1539 return NULL; \
1540 }
1541
1542 #define CHECK_ATTACHED(self) \
1543 CHECK_INITIALIZED(self); \
1544 if (self->detached) { \
1545 PyErr_SetString(PyExc_ValueError, \
1546 "underlying buffer has been detached"); \
1547 return NULL; \
1548 }
1549
1550 #define CHECK_ATTACHED_INT(self) \
1551 if (self->ok <= 0) { \
1552 PyErr_SetString(PyExc_ValueError, \
1553 "I/O operation on uninitialized object"); \
1554 return -1; \
1555 } else if (self->detached) { \
1556 PyErr_SetString(PyExc_ValueError, \
1557 "underlying buffer has been detached"); \
1558 return -1; \
1559 }
1560
1561
1562 /*[clinic input]
1563 @critical_section
1564 _io.TextIOWrapper.detach
1565 [clinic start generated code]*/
1566
1567 static PyObject *
1568 _io_TextIOWrapper_detach_impl(textio *self)
1569 /*[clinic end generated code: output=7ba3715cd032d5f2 input=c908a3b4ef203b0f]*/
1570 {
1571 PyObject *buffer;
1572 CHECK_ATTACHED(self);
1573 if (_PyFile_Flush((PyObject *)self) < 0) {
1574 return NULL;
1575 }
1576 buffer = self->buffer;
1577 self->buffer = NULL;
1578 self->detached = 1;
1579 return buffer;
1580 }
1581
1582 /* Flush the internal write buffer. This doesn't explicitly flush the
1583 underlying buffered object, though. */
1584 static int
1585 _textiowrapper_writeflush(textio *self)
1586 {
1587 _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
1588
1589 if (self->pending_bytes == NULL)
1590 return 0;
1591
1592 PyObject *pending = self->pending_bytes;
1593 PyObject *b;
1594
1595 if (PyBytes_Check(pending)) {
1596 b = Py_NewRef(pending);
1597 }
1598 else if (PyUnicode_Check(pending)) {
1599 assert(PyUnicode_IS_ASCII(pending));
1600 assert(PyUnicode_GET_LENGTH(pending) == self->pending_bytes_count);
1601 b = PyBytes_FromStringAndSize(
1602 PyUnicode_DATA(pending), PyUnicode_GET_LENGTH(pending));
1603 if (b == NULL) {
1604 return -1;
1605 }
1606 }
1607 else {
1608 assert(PyList_Check(pending));
1609 b = PyBytes_FromStringAndSize(NULL, self->pending_bytes_count);
1610 if (b == NULL) {
1611 return -1;
1612 }
1613
1614 char *buf = PyBytes_AsString(b);
1615 Py_ssize_t pos = 0;
1616
1617 for (Py_ssize_t i = 0; i < PyList_GET_SIZE(pending); i++) {
1618 PyObject *obj = PyList_GET_ITEM(pending, i);
1619 char *src;
1620 Py_ssize_t len;
1621 if (PyUnicode_Check(obj)) {
1622 assert(PyUnicode_IS_ASCII(obj));
1623 src = PyUnicode_DATA(obj);
1624 len = PyUnicode_GET_LENGTH(obj);
1625 }
1626 else {
1627 assert(PyBytes_Check(obj));
1628 if (PyBytes_AsStringAndSize(obj, &src, &len) < 0) {
1629 Py_DECREF(b);
1630 return -1;
1631 }
1632 }
1633 memcpy(buf + pos, src, len);
1634 pos += len;
1635 }
1636 assert(pos == self->pending_bytes_count);
1637 }
1638
1639 self->pending_bytes_count = 0;
1640 self->pending_bytes = NULL;
1641 Py_DECREF(pending);
1642
1643 PyObject *ret;
1644 do {
1645 ret = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(write), b);
1646 } while (ret == NULL && _PyIO_trap_eintr());
1647 Py_DECREF(b);
1648 // NOTE: We cleared buffer but we don't know how many bytes are actually written
1649 // when an error occurred.
1650 if (ret == NULL)
1651 return -1;
1652 Py_DECREF(ret);
1653 return 0;
1654 }
1655
1656 /*[clinic input]
1657 @critical_section
1658 _io.TextIOWrapper.write
1659 text: unicode
1660 /
1661 [clinic start generated code]*/
1662
1663 static PyObject *
1664 _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
1665 /*[clinic end generated code: output=d2deb0d50771fcec input=73ec95c5c4a3489c]*/
1666 {
1667 PyObject *ret;
1668 PyObject *b;
1669 Py_ssize_t textlen;
1670 int haslf = 0;
1671 int needflush = 0, text_needflush = 0;
1672
1673 CHECK_ATTACHED(self);
1674 CHECK_CLOSED(self);
1675
1676 if (self->encoder == NULL) {
1677 return _unsupported(self->state, "not writable");
1678 }
1679
1680 Py_INCREF(text);
1681
1682 textlen = PyUnicode_GET_LENGTH(text);
1683
1684 if ((self->writetranslate && self->writenl != NULL) || self->line_buffering)
1685 if (PyUnicode_FindChar(text, '\n', 0, PyUnicode_GET_LENGTH(text), 1) != -1)
1686 haslf = 1;
1687
1688 if (haslf && self->writetranslate && self->writenl != NULL) {
1689 PyObject *newtext = _PyObject_CallMethod(text, &_Py_ID(replace),
1690 "ss", "\n", self->writenl);
1691 Py_DECREF(text);
1692 if (newtext == NULL)
1693 return NULL;
1694 text = newtext;
1695 }
1696
1697 if (self->write_through)
1698 text_needflush = 1;
1699 if (self->line_buffering &&
1700 (haslf ||
1701 PyUnicode_FindChar(text, '\r', 0, PyUnicode_GET_LENGTH(text), 1) != -1))
1702 needflush = 1;
1703
1704 /* XXX What if we were just reading? */
1705 if (self->encodefunc != NULL) {
1706 if (PyUnicode_IS_ASCII(text) &&
1707 // See bpo-43260
1708 PyUnicode_GET_LENGTH(text) <= self->chunk_size &&
1709 is_asciicompat_encoding(self->encodefunc)) {
1710 b = Py_NewRef(text);
1711 }
1712 else {
1713 b = (*self->encodefunc)((PyObject *) self, text);
1714 }
1715 self->encoding_start_of_stream = 0;
1716 }
1717 else {
1718 b = PyObject_CallMethodOneArg(self->encoder, &_Py_ID(encode), text);
1719 }
1720
1721 Py_DECREF(text);
1722 if (b == NULL)
1723 return NULL;
1724 if (b != text && !PyBytes_Check(b)) {
1725 PyErr_Format(PyExc_TypeError,
1726 "encoder should return a bytes object, not '%.200s'",
1727 Py_TYPE(b)->tp_name);
1728 Py_DECREF(b);
1729 return NULL;
1730 }
1731
1732 Py_ssize_t bytes_len;
1733 if (b == text) {
1734 bytes_len = PyUnicode_GET_LENGTH(b);
1735 }
1736 else {
1737 bytes_len = PyBytes_GET_SIZE(b);
1738 }
1739
1740 // We should avoid concatenating huge data.
1741 // Flush the buffer before adding b to the buffer if b is not small.
1742 // https://github.com/python/cpython/issues/87426
1743 if (bytes_len >= self->chunk_size) {
1744 // _textiowrapper_writeflush() calls buffer.write().
1745 // self->pending_bytes can be appended during buffer->write()
1746 // or other thread.
1747 // We need to loop until buffer becomes empty.
1748 // https://github.com/python/cpython/issues/118138
1749 // https://github.com/python/cpython/issues/119506
1750 while (self->pending_bytes != NULL) {
1751 if (_textiowrapper_writeflush(self) < 0) {
1752 Py_DECREF(b);
1753 return NULL;
1754 }
1755 }
1756 }
1757
1758 if (self->pending_bytes == NULL) {
1759 assert(self->pending_bytes_count == 0);
1760 self->pending_bytes = b;
1761 }
1762 else if (!PyList_CheckExact(self->pending_bytes)) {
1763 PyObject *list = PyList_New(2);
1764 if (list == NULL) {
1765 Py_DECREF(b);
1766 return NULL;
1767 }
1768 // Since Python 3.12, allocating GC object won't trigger GC and release
1769 // GIL. See https://github.com/python/cpython/issues/97922
1770 assert(!PyList_CheckExact(self->pending_bytes));
1771 PyList_SET_ITEM(list, 0, self->pending_bytes);
1772 PyList_SET_ITEM(list, 1, b);
1773 self->pending_bytes = list;
1774 }
1775 else {
1776 if (PyList_Append(self->pending_bytes, b) < 0) {
1777 Py_DECREF(b);
1778 return NULL;
1779 }
1780 Py_DECREF(b);
1781 }
1782
1783 self->pending_bytes_count += bytes_len;
1784 if (self->pending_bytes_count >= self->chunk_size || needflush ||
1785 text_needflush) {
1786 if (_textiowrapper_writeflush(self) < 0)
1787 return NULL;
1788 }
1789
1790 if (needflush) {
1791 if (_PyFile_Flush(self->buffer) < 0) {
1792 return NULL;
1793 }
1794 }
1795
1796 if (self->snapshot != NULL) {
1797 textiowrapper_set_decoded_chars(self, NULL);
1798 Py_CLEAR(self->snapshot);
1799 }
1800
1801 if (self->decoder) {
1802 ret = PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset));
1803 if (ret == NULL)
1804 return NULL;
1805 Py_DECREF(ret);
1806 }
1807
1808 return PyLong_FromSsize_t(textlen);
1809 }
1810
1811 /* Steal a reference to chars and store it in the decoded_char buffer;
1812 */
1813 static void
1814 textiowrapper_set_decoded_chars(textio *self, PyObject *chars)
1815 {
1816 Py_XSETREF(self->decoded_chars, chars);
1817 self->decoded_chars_used = 0;
1818 }
1819
1820 static PyObject *
1821 textiowrapper_get_decoded_chars(textio *self, Py_ssize_t n)
1822 {
1823 PyObject *chars;
1824 Py_ssize_t avail;
1825
1826 if (self->decoded_chars == NULL)
1827 return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
1828
1829 avail = (PyUnicode_GET_LENGTH(self->decoded_chars)
1830 - self->decoded_chars_used);
1831
1832 assert(avail >= 0);
1833
1834 if (n < 0 || n > avail)
1835 n = avail;
1836
1837 if (self->decoded_chars_used > 0 || n < avail) {
1838 chars = PyUnicode_Substring(self->decoded_chars,
1839 self->decoded_chars_used,
1840 self->decoded_chars_used + n);
1841 if (chars == NULL)
1842 return NULL;
1843 }
1844 else {
1845 chars = Py_NewRef(self->decoded_chars);
1846 }
1847
1848 self->decoded_chars_used += n;
1849 return chars;
1850 }
1851
1852 /* Read and decode the next chunk of data from the BufferedReader.
1853 */
1854 static int
1855 textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
1856 {
1857 PyObject *dec_buffer = NULL;
1858 PyObject *dec_flags = NULL;
1859 PyObject *input_chunk = NULL;
1860 Py_buffer input_chunk_buf;
1861 PyObject *decoded_chars, *chunk_size;
1862 Py_ssize_t nbytes, nchars;
1863 int eof;
1864
1865 /* The return value is True unless EOF was reached. The decoded string is
1866 * placed in self._decoded_chars (replacing its previous value). The
1867 * entire input chunk is sent to the decoder, though some of it may remain
1868 * buffered in the decoder, yet to be converted.
1869 */
1870
1871 if (self->decoder == NULL) {
1872 _unsupported(self->state, "not readable");
1873 return -1;
1874 }
1875
1876 if (self->telling) {
1877 /* To prepare for tell(), we need to snapshot a point in the file
1878 * where the decoder's input buffer is empty.
1879 */
1880 PyObject *state = PyObject_CallMethodNoArgs(self->decoder,
1881 &_Py_ID(getstate));
1882 if (state == NULL)
1883 return -1;
1884 /* Given this, we know there was a valid snapshot point
1885 * len(dec_buffer) bytes ago with decoder state (b'', dec_flags).
1886 */
1887 if (!PyTuple_Check(state)) {
1888 PyErr_SetString(PyExc_TypeError,
1889 "illegal decoder state");
1890 Py_DECREF(state);
1891 return -1;
1892 }
1893 if (!PyArg_ParseTuple(state,
1894 "OO;illegal decoder state", &dec_buffer, &dec_flags))
1895 {
1896 Py_DECREF(state);
1897 return -1;
1898 }
1899
1900 if (!PyBytes_Check(dec_buffer)) {
1901 PyErr_Format(PyExc_TypeError,
1902 "illegal decoder state: the first item should be a "
1903 "bytes object, not '%.200s'",
1904 Py_TYPE(dec_buffer)->tp_name);
1905 Py_DECREF(state);
1906 return -1;
1907 }
1908 Py_INCREF(dec_buffer);
1909 Py_INCREF(dec_flags);
1910 Py_DECREF(state);
1911 }
1912
1913 /* Read a chunk, decode it, and put the result in self._decoded_chars. */
1914 if (size_hint > 0) {
1915 size_hint = (Py_ssize_t)(Py_MAX(self->b2cratio, 1.0) * size_hint);
1916 }
1917 chunk_size = PyLong_FromSsize_t(Py_MAX(self->chunk_size, size_hint));
1918 if (chunk_size == NULL)
1919 goto fail;
1920
1921 input_chunk = PyObject_CallMethodOneArg(self->buffer,
1922 (self->has_read1 ? &_Py_ID(read1): &_Py_ID(read)),
1923 chunk_size);
1924 Py_DECREF(chunk_size);
1925 if (input_chunk == NULL)
1926 goto fail;
1927
1928 if (PyObject_GetBuffer(input_chunk, &input_chunk_buf, 0) != 0) {
1929 PyErr_Format(PyExc_TypeError,
1930 "underlying %s() should have returned a bytes-like object, "
1931 "not '%.200s'", (self->has_read1 ? "read1": "read"),
1932 Py_TYPE(input_chunk)->tp_name);
1933 goto fail;
1934 }
1935
1936 nbytes = input_chunk_buf.len;
1937 eof = (nbytes == 0);
1938
1939 decoded_chars = _textiowrapper_decode(self->state, self->decoder,
1940 input_chunk, eof);
1941 PyBuffer_Release(&input_chunk_buf);
1942 if (decoded_chars == NULL)
1943 goto fail;
1944
1945 textiowrapper_set_decoded_chars(self, decoded_chars);
1946 nchars = PyUnicode_GET_LENGTH(decoded_chars);
1947 if (nchars > 0)
1948 self->b2cratio = (double) nbytes / nchars;
1949 else
1950 self->b2cratio = 0.0;
1951 if (nchars > 0)
1952 eof = 0;
1953
1954 if (self->telling) {
1955 /* At the snapshot point, len(dec_buffer) bytes before the read, the
1956 * next input to be decoded is dec_buffer + input_chunk.
1957 */
1958 PyObject *next_input = dec_buffer;
1959 PyBytes_Concat(&next_input, input_chunk);
1960 dec_buffer = NULL; /* Reference lost to PyBytes_Concat */
1961 if (next_input == NULL) {
1962 goto fail;
1963 }
1964 PyObject *snapshot = Py_BuildValue("NN", dec_flags, next_input);
1965 if (snapshot == NULL) {
1966 dec_flags = NULL;
1967 goto fail;
1968 }
1969 Py_XSETREF(self->snapshot, snapshot);
1970 }
1971 Py_DECREF(input_chunk);
1972
1973 return (eof == 0);
1974
1975 fail:
1976 Py_XDECREF(dec_buffer);
1977 Py_XDECREF(dec_flags);
1978 Py_XDECREF(input_chunk);
1979 return -1;
1980 }
1981
1982 /*[clinic input]
1983 @critical_section
1984 _io.TextIOWrapper.read
1985 size as n: Py_ssize_t(accept={int, NoneType}) = -1
1986 /
1987 [clinic start generated code]*/
1988
1989 static PyObject *
1990 _io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n)
1991 /*[clinic end generated code: output=7e651ce6cc6a25a6 input=67d14c5661121377]*/
1992 {
1993 PyObject *result = NULL, *chunks = NULL;
1994
1995 CHECK_ATTACHED(self);
1996 CHECK_CLOSED(self);
1997
1998 if (self->decoder == NULL) {
1999 return _unsupported(self->state, "not readable");
2000 }
2001
2002 if (_textiowrapper_writeflush(self) < 0)
2003 return NULL;
2004
2005 if (n < 0) {
2006 /* Read everything */
2007 PyObject *bytes = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(read));
2008 PyObject *decoded;
2009 if (bytes == NULL)
2010 goto fail;
2011
2012 if (bytes == Py_None){
2013 Py_DECREF(bytes);
2014 PyErr_SetString(PyExc_BlockingIOError, "Read returned None.");
2015 return NULL;
2016 }
2017
2018 _PyIO_State *state = self->state;
2019 if (Py_IS_TYPE(self->decoder, state->PyIncrementalNewlineDecoder_Type))
2020 decoded = _PyIncrementalNewlineDecoder_decode(self->decoder,
2021 bytes, 1);
2022 else
2023 decoded = PyObject_CallMethodObjArgs(
2024 self->decoder, &_Py_ID(decode), bytes, Py_True, NULL);
2025 Py_DECREF(bytes);
2026 if (check_decoded(decoded) < 0)
2027 goto fail;
2028
2029 result = textiowrapper_get_decoded_chars(self, -1);
2030
2031 if (result == NULL) {
2032 Py_DECREF(decoded);
2033 return NULL;
2034 }
2035
2036 PyUnicode_AppendAndDel(&result, decoded);
2037 if (result == NULL)
2038 goto fail;
2039
2040 if (self->snapshot != NULL) {
2041 textiowrapper_set_decoded_chars(self, NULL);
2042 Py_CLEAR(self->snapshot);
2043 }
2044 return result;
2045 }
2046 else {
2047 int res = 1;
2048 Py_ssize_t remaining = n;
2049
2050 result = textiowrapper_get_decoded_chars(self, n);
2051 if (result == NULL)
2052 goto fail;
2053 remaining -= PyUnicode_GET_LENGTH(result);
2054
2055 /* Keep reading chunks until we have n characters to return */
2056 while (remaining > 0) {
2057 res = textiowrapper_read_chunk(self, remaining);
2058 if (res < 0) {
2059 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
2060 when EINTR occurs so we needn't do it ourselves. */
2061 if (_PyIO_trap_eintr()) {
2062 continue;
2063 }
2064 goto fail;
2065 }
2066 if (res == 0) /* EOF */
2067 break;
2068 if (chunks == NULL) {
2069 chunks = PyList_New(0);
2070 if (chunks == NULL)
2071 goto fail;
2072 }
2073 if (PyUnicode_GET_LENGTH(result) > 0 &&
2074 PyList_Append(chunks, result) < 0)
2075 goto fail;
2076 Py_DECREF(result);
2077 result = textiowrapper_get_decoded_chars(self, remaining);
2078 if (result == NULL)
2079 goto fail;
2080 remaining -= PyUnicode_GET_LENGTH(result);
2081 }
2082 if (chunks != NULL) {
2083 if (result != NULL && PyList_Append(chunks, result) < 0)
2084 goto fail;
2085 _Py_DECLARE_STR(empty, "");
2086 Py_XSETREF(result, PyUnicode_Join(&_Py_STR(empty), chunks));
2087 if (result == NULL)
2088 goto fail;
2089 Py_CLEAR(chunks);
2090 }
2091 return result;
2092 }
2093 fail:
2094 Py_XDECREF(result);
2095 Py_XDECREF(chunks);
2096 return NULL;
2097 }
2098
2099
2100 /* NOTE: `end` must point to the real end of the Py_UCS4 storage,
2101 that is to the NUL character. Otherwise the function will produce
2102 incorrect results. */
2103 static const char *
2104 find_control_char(int kind, const char *s, const char *end, Py_UCS4 ch)
2105 {
2106 if (kind == PyUnicode_1BYTE_KIND) {
2107 assert(ch < 256);
2108 return (char *) memchr((const void *) s, (char) ch, end - s);
2109 }
2110 for (;;) {
2111 while (PyUnicode_READ(kind, s, 0) > ch)
2112 s += kind;
2113 if (PyUnicode_READ(kind, s, 0) == ch)
2114 return s;
2115 if (s == end)
2116 return NULL;
2117 s += kind;
2118 }
2119 }
2120
2121 Py_ssize_t
2122 _PyIO_find_line_ending(
2123 int translated, int universal, PyObject *readnl,
2124 int kind, const char *start, const char *end, Py_ssize_t *consumed)
2125 {
2126 Py_ssize_t len = (end - start)/kind;
2127
2128 if (translated) {
2129 /* Newlines are already translated, only search for \n */
2130 const char *pos = find_control_char(kind, start, end, '\n');
2131 if (pos != NULL)
2132 return (pos - start)/kind + 1;
2133 else {
2134 *consumed = len;
2135 return -1;
2136 }
2137 }
2138 else if (universal) {
2139 /* Universal newline search. Find any of \r, \r\n, \n
2140 * The decoder ensures that \r\n are not split in two pieces
2141 */
2142 const char *s = start;
2143 for (;;) {
2144 Py_UCS4 ch;
2145 /* Fast path for non-control chars. The loop always ends
2146 since the Unicode string is NUL-terminated. */
2147 while (PyUnicode_READ(kind, s, 0) > '\r')
2148 s += kind;
2149 if (s >= end) {
2150 *consumed = len;
2151 return -1;
2152 }
2153 ch = PyUnicode_READ(kind, s, 0);
2154 s += kind;
2155 if (ch == '\n')
2156 return (s - start)/kind;
2157 if (ch == '\r') {
2158 if (PyUnicode_READ(kind, s, 0) == '\n')
2159 return (s - start)/kind + 1;
2160 else
2161 return (s - start)/kind;
2162 }
2163 }
2164 }
2165 else {
2166 /* Non-universal mode. */
2167 Py_ssize_t readnl_len = PyUnicode_GET_LENGTH(readnl);
2168 const Py_UCS1 *nl = PyUnicode_1BYTE_DATA(readnl);
2169 /* Assume that readnl is an ASCII character. */
2170 assert(PyUnicode_KIND(readnl) == PyUnicode_1BYTE_KIND);
2171 if (readnl_len == 1) {
2172 const char *pos = find_control_char(kind, start, end, nl[0]);
2173 if (pos != NULL)
2174 return (pos - start)/kind + 1;
2175 *consumed = len;
2176 return -1;
2177 }
2178 else {
2179 const char *s = start;
2180 const char *e = end - (readnl_len - 1)*kind;
2181 const char *pos;
2182 if (e < s)
2183 e = s;
2184 while (s < e) {
2185 Py_ssize_t i;
2186 const char *pos = find_control_char(kind, s, end, nl[0]);
2187 if (pos == NULL || pos >= e)
2188 break;
2189 for (i = 1; i < readnl_len; i++) {
2190 if (PyUnicode_READ(kind, pos, i) != nl[i])
2191 break;
2192 }
2193 if (i == readnl_len)
2194 return (pos - start)/kind + readnl_len;
2195 s = pos + kind;
2196 }
2197 pos = find_control_char(kind, e, end, nl[0]);
2198 if (pos == NULL)
2199 *consumed = len;
2200 else
2201 *consumed = (pos - start)/kind;
2202 return -1;
2203 }
2204 }
2205 }
2206
2207 static PyObject *
2208 _textiowrapper_readline(textio *self, Py_ssize_t limit)
2209 {
2210 PyObject *line = NULL, *chunks = NULL, *remaining = NULL;
2211 Py_ssize_t start, endpos, chunked, offset_to_buffer;
2212 int res;
2213
2214 CHECK_CLOSED(self);
2215
2216 if (_textiowrapper_writeflush(self) < 0)
2217 return NULL;
2218
2219 chunked = 0;
2220
2221 while (1) {
2222 const char *ptr;
2223 Py_ssize_t line_len;
2224 int kind;
2225 Py_ssize_t consumed = 0;
2226
2227 /* First, get some data if necessary */
2228 res = 1;
2229 while (!self->decoded_chars ||
2230 !PyUnicode_GET_LENGTH(self->decoded_chars)) {
2231 res = textiowrapper_read_chunk(self, 0);
2232 if (res < 0) {
2233 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
2234 when EINTR occurs so we needn't do it ourselves. */
2235 if (_PyIO_trap_eintr()) {
2236 continue;
2237 }
2238 goto error;
2239 }
2240 if (res == 0)
2241 break;
2242 }
2243 if (res == 0) {
2244 /* end of file */
2245 textiowrapper_set_decoded_chars(self, NULL);
2246 Py_CLEAR(self->snapshot);
2247 start = endpos = offset_to_buffer = 0;
2248 break;
2249 }
2250
2251 if (remaining == NULL) {
2252 line = Py_NewRef(self->decoded_chars);
2253 start = self->decoded_chars_used;
2254 offset_to_buffer = 0;
2255 }
2256 else {
2257 assert(self->decoded_chars_used == 0);
2258 line = PyUnicode_Concat(remaining, self->decoded_chars);
2259 start = 0;
2260 offset_to_buffer = PyUnicode_GET_LENGTH(remaining);
2261 Py_CLEAR(remaining);
2262 if (line == NULL)
2263 goto error;
2264 }
2265
2266 ptr = PyUnicode_DATA(line);
2267 line_len = PyUnicode_GET_LENGTH(line);
2268 kind = PyUnicode_KIND(line);
2269
2270 endpos = _PyIO_find_line_ending(
2271 self->readtranslate, self->readuniversal, self->readnl,
2272 kind,
2273 ptr + kind * start,
2274 ptr + kind * line_len,
2275 &consumed);
2276 if (endpos >= 0) {
2277 endpos += start;
2278 if (limit >= 0 && (endpos - start) + chunked >= limit)
2279 endpos = start + limit - chunked;
2280 break;
2281 }
2282
2283 /* We can put aside up to `endpos` */
2284 endpos = consumed + start;
2285 if (limit >= 0 && (endpos - start) + chunked >= limit) {
2286 /* Didn't find line ending, but reached length limit */
2287 endpos = start + limit - chunked;
2288 break;
2289 }
2290
2291 if (endpos > start) {
2292 /* No line ending seen yet - put aside current data */
2293 PyObject *s;
2294 if (chunks == NULL) {
2295 chunks = PyList_New(0);
2296 if (chunks == NULL)
2297 goto error;
2298 }
2299 s = PyUnicode_Substring(line, start, endpos);
2300 if (s == NULL)
2301 goto error;
2302 if (PyList_Append(chunks, s) < 0) {
2303 Py_DECREF(s);
2304 goto error;
2305 }
2306 chunked += PyUnicode_GET_LENGTH(s);
2307 Py_DECREF(s);
2308 }
2309 /* There may be some remaining bytes we'll have to prepend to the
2310 next chunk of data */
2311 if (endpos < line_len) {
2312 remaining = PyUnicode_Substring(line, endpos, line_len);
2313 if (remaining == NULL)
2314 goto error;
2315 }
2316 Py_CLEAR(line);
2317 /* We have consumed the buffer */
2318 textiowrapper_set_decoded_chars(self, NULL);
2319 }
2320
2321 if (line != NULL) {
2322 /* Our line ends in the current buffer */
2323 self->decoded_chars_used = endpos - offset_to_buffer;
2324 if (start > 0 || endpos < PyUnicode_GET_LENGTH(line)) {
2325 PyObject *s = PyUnicode_Substring(line, start, endpos);
2326 Py_CLEAR(line);
2327 if (s == NULL)
2328 goto error;
2329 line = s;
2330 }
2331 }
2332 if (remaining != NULL) {
2333 if (chunks == NULL) {
2334 chunks = PyList_New(0);
2335 if (chunks == NULL)
2336 goto error;
2337 }
2338 if (PyList_Append(chunks, remaining) < 0)
2339 goto error;
2340 Py_CLEAR(remaining);
2341 }
2342 if (chunks != NULL) {
2343 if (line != NULL) {
2344 if (PyList_Append(chunks, line) < 0)
2345 goto error;
2346 Py_DECREF(line);
2347 }
2348 line = PyUnicode_Join(&_Py_STR(empty), chunks);
2349 if (line == NULL)
2350 goto error;
2351 Py_CLEAR(chunks);
2352 }
2353 if (line == NULL) {
2354 line = &_Py_STR(empty);
2355 }
2356
2357 return line;
2358
2359 error:
2360 Py_XDECREF(chunks);
2361 Py_XDECREF(remaining);
2362 Py_XDECREF(line);
2363 return NULL;
2364 }
2365
2366 /*[clinic input]
2367 @critical_section
2368 _io.TextIOWrapper.readline
2369 size: Py_ssize_t = -1
2370 /
2371 [clinic start generated code]*/
2372
2373 static PyObject *
2374 _io_TextIOWrapper_readline_impl(textio *self, Py_ssize_t size)
2375 /*[clinic end generated code: output=344afa98804e8b25 input=b65bab871dc3ddba]*/
2376 {
2377 CHECK_ATTACHED(self);
2378 return _textiowrapper_readline(self, size);
2379 }
2380
2381 /* Seek and Tell */
2382
2383 typedef struct {
2384 Py_off_t start_pos;
2385 int dec_flags;
2386 int bytes_to_feed;
2387 int chars_to_skip;
2388 char need_eof;
2389 } cookie_type;
2390
2391 /*
2392 To speed up cookie packing/unpacking, we store the fields in a temporary
2393 string and call _PyLong_FromByteArray() or _PyLong_AsByteArray (resp.).
2394 The following macros define at which offsets in the intermediary byte
2395 string the various CookieStruct fields will be stored.
2396 */
2397
2398 #define COOKIE_BUF_LEN (sizeof(Py_off_t) + 3 * sizeof(int) + sizeof(char))
2399
2400 #if PY_BIG_ENDIAN
2401 /* We want the least significant byte of start_pos to also be the least
2402 significant byte of the cookie, which means that in big-endian mode we
2403 must copy the fields in reverse order. */
2404
2405 # define OFF_START_POS (sizeof(char) + 3 * sizeof(int))
2406 # define OFF_DEC_FLAGS (sizeof(char) + 2 * sizeof(int))
2407 # define OFF_BYTES_TO_FEED (sizeof(char) + sizeof(int))
2408 # define OFF_CHARS_TO_SKIP (sizeof(char))
2409 # define OFF_NEED_EOF 0
2410
2411 #else
2412 /* Little-endian mode: the least significant byte of start_pos will
2413 naturally end up the least significant byte of the cookie. */
2414
2415 # define OFF_START_POS 0
2416 # define OFF_DEC_FLAGS (sizeof(Py_off_t))
2417 # define OFF_BYTES_TO_FEED (sizeof(Py_off_t) + sizeof(int))
2418 # define OFF_CHARS_TO_SKIP (sizeof(Py_off_t) + 2 * sizeof(int))
2419 # define OFF_NEED_EOF (sizeof(Py_off_t) + 3 * sizeof(int))
2420
2421 #endif
2422
2423 static int
2424 textiowrapper_parse_cookie(cookie_type *cookie, PyObject *cookieObj)
2425 {
2426 unsigned char buffer[COOKIE_BUF_LEN];
2427 PyLongObject *cookieLong = (PyLongObject *)PyNumber_Long(cookieObj);
2428 if (cookieLong == NULL)
2429 return -1;
2430
2431 if (_PyLong_AsByteArray(cookieLong, buffer, sizeof(buffer),
2432 PY_LITTLE_ENDIAN, 0, 1) < 0) {
2433 Py_DECREF(cookieLong);
2434 return -1;
2435 }
2436 Py_DECREF(cookieLong);
2437
2438 memcpy(&cookie->start_pos, buffer + OFF_START_POS, sizeof(cookie->start_pos));
2439 memcpy(&cookie->dec_flags, buffer + OFF_DEC_FLAGS, sizeof(cookie->dec_flags));
2440 memcpy(&cookie->bytes_to_feed, buffer + OFF_BYTES_TO_FEED, sizeof(cookie->bytes_to_feed));
2441 memcpy(&cookie->chars_to_skip, buffer + OFF_CHARS_TO_SKIP, sizeof(cookie->chars_to_skip));
2442 memcpy(&cookie->need_eof, buffer + OFF_NEED_EOF, sizeof(cookie->need_eof));
2443
2444 return 0;
2445 }
2446
2447 static PyObject *
2448 textiowrapper_build_cookie(cookie_type *cookie)
2449 {
2450 unsigned char buffer[COOKIE_BUF_LEN];
2451
2452 memcpy(buffer + OFF_START_POS, &cookie->start_pos, sizeof(cookie->start_pos));
2453 memcpy(buffer + OFF_DEC_FLAGS, &cookie->dec_flags, sizeof(cookie->dec_flags));
2454 memcpy(buffer + OFF_BYTES_TO_FEED, &cookie->bytes_to_feed, sizeof(cookie->bytes_to_feed));
2455 memcpy(buffer + OFF_CHARS_TO_SKIP, &cookie->chars_to_skip, sizeof(cookie->chars_to_skip));
2456 memcpy(buffer + OFF_NEED_EOF, &cookie->need_eof, sizeof(cookie->need_eof));
2457
2458 return _PyLong_FromByteArray(buffer, sizeof(buffer),
2459 PY_LITTLE_ENDIAN, 0);
2460 }
2461
2462 static int
2463 _textiowrapper_decoder_setstate(textio *self, cookie_type *cookie)
2464 {
2465 PyObject *res;
2466 /* When seeking to the start of the stream, we call decoder.reset()
2467 rather than decoder.getstate().
2468 This is for a few decoders such as utf-16 for which the state value
2469 at start is not (b"", 0) but e.g. (b"", 2) (meaning, in the case of
2470 utf-16, that we are expecting a BOM).
2471 */
2472 if (cookie->start_pos == 0 && cookie->dec_flags == 0) {
2473 res = PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset));
2474 }
2475 else {
2476 res = _PyObject_CallMethod(self->decoder, &_Py_ID(setstate),
2477 "((yi))", "", cookie->dec_flags);
2478 }
2479 if (res == NULL) {
2480 return -1;
2481 }
2482 Py_DECREF(res);
2483 return 0;
2484 }
2485
2486 static int
2487 _textiowrapper_encoder_reset(textio *self, int start_of_stream)
2488 {
2489 PyObject *res;
2490 if (start_of_stream) {
2491 res = PyObject_CallMethodNoArgs(self->encoder, &_Py_ID(reset));
2492 self->encoding_start_of_stream = 1;
2493 }
2494 else {
2495 res = PyObject_CallMethodOneArg(self->encoder, &_Py_ID(setstate),
2496 _PyLong_GetZero());
2497 self->encoding_start_of_stream = 0;
2498 }
2499 if (res == NULL)
2500 return -1;
2501 Py_DECREF(res);
2502 return 0;
2503 }
2504
2505 static int
2506 _textiowrapper_encoder_setstate(textio *self, cookie_type *cookie)
2507 {
2508 /* Same as _textiowrapper_decoder_setstate() above. */
2509 return _textiowrapper_encoder_reset(
2510 self, cookie->start_pos == 0 && cookie->dec_flags == 0);
2511 }
2512
2513 /*[clinic input]
2514 @critical_section
2515 _io.TextIOWrapper.seek
2516 cookie as cookieObj: object
2517 Zero or an opaque number returned by tell().
2518 whence: int(c_default='0') = os.SEEK_SET
2519 The relative position to seek from.
2520 /
2521
2522 Set the stream position, and return the new stream position.
2523
2524 Four operations are supported, given by the following argument
2525 combinations:
2526
2527 - seek(0, SEEK_SET): Rewind to the start of the stream.
2528 - seek(cookie, SEEK_SET): Restore a previous position;
2529 'cookie' must be a number returned by tell().
2530 - seek(0, SEEK_END): Fast-forward to the end of the stream.
2531 - seek(0, SEEK_CUR): Leave the current stream position unchanged.
2532
2533 Any other argument combinations are invalid,
2534 and may raise exceptions.
2535 [clinic start generated code]*/
2536
2537 static PyObject *
2538 _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
2539 /*[clinic end generated code: output=0a15679764e2d04d input=4bea78698be23d7e]*/
2540 {
2541 PyObject *posobj;
2542 cookie_type cookie;
2543 PyObject *res;
2544 int cmp;
2545 PyObject *snapshot;
2546
2547 CHECK_ATTACHED(self);
2548 CHECK_CLOSED(self);
2549
2550 Py_INCREF(cookieObj);
2551
2552 if (!self->seekable) {
2553 _unsupported(self->state, "underlying stream is not seekable");
2554 goto fail;
2555 }
2556
2557 PyObject *zero = _PyLong_GetZero(); // borrowed reference
2558
2559 switch (whence) {
2560 case SEEK_CUR:
2561 /* seek relative to current position */
2562 cmp = PyObject_RichCompareBool(cookieObj, zero, Py_EQ);
2563 if (cmp < 0)
2564 goto fail;
2565
2566 if (cmp == 0) {
2567 _unsupported(self->state, "can't do nonzero cur-relative seeks");
2568 goto fail;
2569 }
2570
2571 /* Seeking to the current position should attempt to
2572 * sync the underlying buffer with the current position.
2573 */
2574 Py_DECREF(cookieObj);
2575 cookieObj = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(tell));
2576 if (cookieObj == NULL)
2577 goto fail;
2578 break;
2579
2580 case SEEK_END:
2581 /* seek relative to end of file */
2582 cmp = PyObject_RichCompareBool(cookieObj, zero, Py_EQ);
2583 if (cmp < 0)
2584 goto fail;
2585
2586 if (cmp == 0) {
2587 _unsupported(self->state, "can't do nonzero end-relative seeks");
2588 goto fail;
2589 }
2590
2591 if (_PyFile_Flush((PyObject *)self) < 0) {
2592 goto fail;
2593 }
2594
2595 textiowrapper_set_decoded_chars(self, NULL);
2596 Py_CLEAR(self->snapshot);
2597 if (self->decoder) {
2598 res = PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset));
2599 if (res == NULL)
2600 goto fail;
2601 Py_DECREF(res);
2602 }
2603
2604 res = _PyObject_CallMethod(self->buffer, &_Py_ID(seek), "ii", 0, 2);
2605 Py_CLEAR(cookieObj);
2606 if (res == NULL)
2607 goto fail;
2608 if (self->encoder) {
2609 /* If seek() == 0, we are at the start of stream, otherwise not */
2610 cmp = PyObject_RichCompareBool(res, zero, Py_EQ);
2611 if (cmp < 0 || _textiowrapper_encoder_reset(self, cmp)) {
2612 Py_DECREF(res);
2613 goto fail;
2614 }
2615 }
2616 return res;
2617
2618 case SEEK_SET:
2619 break;
2620
2621 default:
2622 PyErr_Format(PyExc_ValueError,
2623 "invalid whence (%d, should be %d, %d or %d)", whence,
2624 SEEK_SET, SEEK_CUR, SEEK_END);
2625 goto fail;
2626 }
2627
2628 cmp = PyObject_RichCompareBool(cookieObj, zero, Py_LT);
2629 if (cmp < 0)
2630 goto fail;
2631
2632 if (cmp == 1) {
2633 PyErr_Format(PyExc_ValueError,
2634 "negative seek position %R", cookieObj);
2635 goto fail;
2636 }
2637
2638 if (_PyFile_Flush((PyObject *)self) < 0) {
2639 goto fail;
2640 }
2641
2642 /* The strategy of seek() is to go back to the safe start point
2643 * and replay the effect of read(chars_to_skip) from there.
2644 */
2645 if (textiowrapper_parse_cookie(&cookie, cookieObj) < 0)
2646 goto fail;
2647
2648 /* Seek back to the safe start point. */
2649 posobj = PyLong_FromOff_t(cookie.start_pos);
2650 if (posobj == NULL)
2651 goto fail;
2652 res = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(seek), posobj);
2653 Py_DECREF(posobj);
2654 if (res == NULL)
2655 goto fail;
2656 Py_DECREF(res);
2657
2658 textiowrapper_set_decoded_chars(self, NULL);
2659 Py_CLEAR(self->snapshot);
2660
2661 /* Restore the decoder to its state from the safe start point. */
2662 if (self->decoder) {
2663 if (_textiowrapper_decoder_setstate(self, &cookie) < 0)
2664 goto fail;
2665 }
2666
2667 if (cookie.chars_to_skip) {
2668 /* Just like _read_chunk, feed the decoder and save a snapshot. */
2669 PyObject *input_chunk = _PyObject_CallMethod(self->buffer, &_Py_ID(read),
2670 "i", cookie.bytes_to_feed);
2671 PyObject *decoded;
2672
2673 if (input_chunk == NULL)
2674 goto fail;
2675
2676 if (!PyBytes_Check(input_chunk)) {
2677 PyErr_Format(PyExc_TypeError,
2678 "underlying read() should have returned a bytes "
2679 "object, not '%.200s'",
2680 Py_TYPE(input_chunk)->tp_name);
2681 Py_DECREF(input_chunk);
2682 goto fail;
2683 }
2684
2685 snapshot = Py_BuildValue("iN", cookie.dec_flags, input_chunk);
2686 if (snapshot == NULL) {
2687 goto fail;
2688 }
2689 Py_XSETREF(self->snapshot, snapshot);
2690
2691 decoded = PyObject_CallMethodObjArgs(self->decoder, &_Py_ID(decode),
2692 input_chunk, cookie.need_eof ? Py_True : Py_False, NULL);
2693
2694 if (check_decoded(decoded) < 0)
2695 goto fail;
2696
2697 textiowrapper_set_decoded_chars(self, decoded);
2698
2699 /* Skip chars_to_skip of the decoded characters. */
2700 if (PyUnicode_GetLength(self->decoded_chars) < cookie.chars_to_skip) {
2701 PyErr_SetString(PyExc_OSError, "can't restore logical file position");
2702 goto fail;
2703 }
2704 self->decoded_chars_used = cookie.chars_to_skip;
2705 }
2706 else {
2707 snapshot = Py_BuildValue("iy", cookie.dec_flags, "");
2708 if (snapshot == NULL)
2709 goto fail;
2710 Py_XSETREF(self->snapshot, snapshot);
2711 }
2712
2713 /* Finally, reset the encoder (merely useful for proper BOM handling) */
2714 if (self->encoder) {
2715 if (_textiowrapper_encoder_setstate(self, &cookie) < 0)
2716 goto fail;
2717 }
2718 return cookieObj;
2719 fail:
2720 Py_XDECREF(cookieObj);
2721 return NULL;
2722
2723 }
2724
2725 /*[clinic input]
2726 @critical_section
2727 _io.TextIOWrapper.tell
2728
2729 Return the stream position as an opaque number.
2730
2731 The return value of tell() can be given as input to seek(), to
2732 restore a previous stream position.
2733 [clinic start generated code]*/
2734
2735 static PyObject *
2736 _io_TextIOWrapper_tell_impl(textio *self)
2737 /*[clinic end generated code: output=4f168c08bf34ad5f input=aeece020f747fd92]*/
2738 {
2739 PyObject *res;
2740 PyObject *posobj = NULL;
2741 cookie_type cookie = {0,0,0,0,0};
2742 PyObject *next_input;
2743 Py_ssize_t chars_to_skip, chars_decoded;
2744 Py_ssize_t skip_bytes, skip_back;
2745 PyObject *saved_state = NULL;
2746 const char *input, *input_end;
2747 Py_ssize_t dec_buffer_len;
2748 int dec_flags;
2749
2750 CHECK_ATTACHED(self);
2751 CHECK_CLOSED(self);
2752
2753 if (!self->seekable) {
2754 _unsupported(self->state, "underlying stream is not seekable");
2755 goto fail;
2756 }
2757 if (!self->telling) {
2758 PyErr_SetString(PyExc_OSError,
2759 "telling position disabled by next() call");
2760 goto fail;
2761 }
2762
2763 if (_textiowrapper_writeflush(self) < 0)
2764 return NULL;
2765 if (_PyFile_Flush((PyObject *)self) < 0) {
2766 goto fail;
2767 }
2768
2769 posobj = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(tell));
2770 if (posobj == NULL)
2771 goto fail;
2772
2773 if (self->decoder == NULL || self->snapshot == NULL) {
2774 assert (self->decoded_chars == NULL || PyUnicode_GetLength(self->decoded_chars) == 0);
2775 return posobj;
2776 }
2777
2778 #if defined(HAVE_LARGEFILE_SUPPORT)
2779 cookie.start_pos = PyLong_AsLongLong(posobj);
2780 #else
2781 cookie.start_pos = PyLong_AsLong(posobj);
2782 #endif
2783 Py_DECREF(posobj);
2784 if (PyErr_Occurred())
2785 goto fail;
2786
2787 /* Skip backward to the snapshot point (see _read_chunk). */
2788 assert(PyTuple_Check(self->snapshot));
2789 if (!PyArg_ParseTuple(self->snapshot, "iO", &cookie.dec_flags, &next_input))
2790 goto fail;
2791
2792 assert (PyBytes_Check(next_input));
2793
2794 cookie.start_pos -= PyBytes_GET_SIZE(next_input);
2795
2796 /* How many decoded characters have been used up since the snapshot? */
2797 if (self->decoded_chars_used == 0) {
2798 /* We haven't moved from the snapshot point. */
2799 return textiowrapper_build_cookie(&cookie);
2800 }
2801
2802 chars_to_skip = self->decoded_chars_used;
2803
2804 /* Decoder state will be restored at the end */
2805 saved_state = PyObject_CallMethodNoArgs(self->decoder,
2806 &_Py_ID(getstate));
2807 if (saved_state == NULL)
2808 goto fail;
2809
2810 #define DECODER_GETSTATE() do { \
2811 PyObject *dec_buffer; \
2812 PyObject *_state = PyObject_CallMethodNoArgs(self->decoder, \
2813 &_Py_ID(getstate)); \
2814 if (_state == NULL) \
2815 goto fail; \
2816 if (!PyTuple_Check(_state)) { \
2817 PyErr_SetString(PyExc_TypeError, \
2818 "illegal decoder state"); \
2819 Py_DECREF(_state); \
2820 goto fail; \
2821 } \
2822 if (!PyArg_ParseTuple(_state, "Oi;illegal decoder state", \
2823 &dec_buffer, &dec_flags)) \
2824 { \
2825 Py_DECREF(_state); \
2826 goto fail; \
2827 } \
2828 if (!PyBytes_Check(dec_buffer)) { \
2829 PyErr_Format(PyExc_TypeError, \
2830 "illegal decoder state: the first item should be a " \
2831 "bytes object, not '%.200s'", \
2832 Py_TYPE(dec_buffer)->tp_name); \
2833 Py_DECREF(_state); \
2834 goto fail; \
2835 } \
2836 dec_buffer_len = PyBytes_GET_SIZE(dec_buffer); \
2837 Py_DECREF(_state); \
2838 } while (0)
2839
2840 #define DECODER_DECODE(start, len, res) do { \
2841 PyObject *_decoded = _PyObject_CallMethod( \
2842 self->decoder, &_Py_ID(decode), "y#", start, len); \
2843 if (check_decoded(_decoded) < 0) \
2844 goto fail; \
2845 res = PyUnicode_GET_LENGTH(_decoded); \
2846 Py_DECREF(_decoded); \
2847 } while (0)
2848
2849 /* Fast search for an acceptable start point, close to our
2850 current pos */
2851 skip_bytes = (Py_ssize_t) (self->b2cratio * chars_to_skip);
2852 skip_back = 1;
2853 assert(skip_bytes <= PyBytes_GET_SIZE(next_input));
2854 input = PyBytes_AS_STRING(next_input);
2855 while (skip_bytes > 0) {
2856 /* Decode up to temptative start point */
2857 if (_textiowrapper_decoder_setstate(self, &cookie) < 0)
2858 goto fail;
2859 DECODER_DECODE(input, skip_bytes, chars_decoded);
2860 if (chars_decoded <= chars_to_skip) {
2861 DECODER_GETSTATE();
2862 if (dec_buffer_len == 0) {
2863 /* Before pos and no bytes buffered in decoder => OK */
2864 cookie.dec_flags = dec_flags;
2865 chars_to_skip -= chars_decoded;
2866 break;
2867 }
2868 /* Skip back by buffered amount and reset heuristic */
2869 skip_bytes -= dec_buffer_len;
2870 skip_back = 1;
2871 }
2872 else {
2873 /* We're too far ahead, skip back a bit */
2874 skip_bytes -= skip_back;
2875 skip_back *= 2;
2876 }
2877 }
2878 if (skip_bytes <= 0) {
2879 skip_bytes = 0;
2880 if (_textiowrapper_decoder_setstate(self, &cookie) < 0)
2881 goto fail;
2882 }
2883
2884 /* Note our initial start point. */
2885 cookie.start_pos += skip_bytes;
2886 cookie.chars_to_skip = Py_SAFE_DOWNCAST(chars_to_skip, Py_ssize_t, int);
2887 if (chars_to_skip == 0)
2888 goto finally;
2889
2890 /* We should be close to the desired position. Now feed the decoder one
2891 * byte at a time until we reach the `chars_to_skip` target.
2892 * As we go, note the nearest "safe start point" before the current
2893 * location (a point where the decoder has nothing buffered, so seek()
2894 * can safely start from there and advance to this location).
2895 */
2896 chars_decoded = 0;
2897 input = PyBytes_AS_STRING(next_input);
2898 input_end = input + PyBytes_GET_SIZE(next_input);
2899 input += skip_bytes;
2900 while (input < input_end) {
2901 Py_ssize_t n;
2902
2903 DECODER_DECODE(input, (Py_ssize_t)1, n);
2904 /* We got n chars for 1 byte */
2905 chars_decoded += n;
2906 cookie.bytes_to_feed += 1;
2907 DECODER_GETSTATE();
2908
2909 if (dec_buffer_len == 0 && chars_decoded <= chars_to_skip) {
2910 /* Decoder buffer is empty, so this is a safe start point. */
2911 cookie.start_pos += cookie.bytes_to_feed;
2912 chars_to_skip -= chars_decoded;
2913 cookie.dec_flags = dec_flags;
2914 cookie.bytes_to_feed = 0;
2915 chars_decoded = 0;
2916 }
2917 if (chars_decoded >= chars_to_skip)
2918 break;
2919 input++;
2920 }
2921 if (input == input_end) {
2922 /* We didn't get enough decoded data; signal EOF to get more. */
2923 PyObject *decoded = _PyObject_CallMethod(
2924 self->decoder, &_Py_ID(decode), "yO", "", /* final = */ Py_True);
2925 if (check_decoded(decoded) < 0)
2926 goto fail;
2927 chars_decoded += PyUnicode_GET_LENGTH(decoded);
2928 Py_DECREF(decoded);
2929 cookie.need_eof = 1;
2930
2931 if (chars_decoded < chars_to_skip) {
2932 PyErr_SetString(PyExc_OSError,
2933 "can't reconstruct logical file position");
2934 goto fail;
2935 }
2936 }
2937
2938 finally:
2939 res = PyObject_CallMethodOneArg(
2940 self->decoder, &_Py_ID(setstate), saved_state);
2941 Py_DECREF(saved_state);
2942 if (res == NULL)
2943 return NULL;
2944 Py_DECREF(res);
2945
2946 /* The returned cookie corresponds to the last safe start point. */
2947 cookie.chars_to_skip = Py_SAFE_DOWNCAST(chars_to_skip, Py_ssize_t, int);
2948 return textiowrapper_build_cookie(&cookie);
2949
2950 fail:
2951 if (saved_state) {
2952 PyObject *exc = PyErr_GetRaisedException();
2953 res = PyObject_CallMethodOneArg(
2954 self->decoder, &_Py_ID(setstate), saved_state);
2955 _PyErr_ChainExceptions1(exc);
2956 Py_DECREF(saved_state);
2957 Py_XDECREF(res);
2958 }
2959 return NULL;
2960 }
2961
2962 /*[clinic input]
2963 @critical_section
2964 _io.TextIOWrapper.truncate
2965 pos: object = None
2966 /
2967 [clinic start generated code]*/
2968
2969 static PyObject *
2970 _io_TextIOWrapper_truncate_impl(textio *self, PyObject *pos)
2971 /*[clinic end generated code: output=90ec2afb9bb7745f input=8bddb320834c93ee]*/
2972 {
2973 CHECK_ATTACHED(self)
2974
2975 if (_PyFile_Flush((PyObject *)self) < 0) {
2976 return NULL;
2977 }
2978
2979 return PyObject_CallMethodOneArg(self->buffer, &_Py_ID(truncate), pos);
2980 }
2981
2982 static PyObject *
2983 textiowrapper_repr(PyObject *op)
2984 {
2985 PyObject *nameobj, *modeobj, *res, *s;
2986 int status;
2987 textio *self = textio_CAST(op);
2988 const char *type_name = Py_TYPE(self)->tp_name;
2989
2990 CHECK_INITIALIZED(self);
2991
2992 res = PyUnicode_FromFormat("<%.100s", type_name);
2993 if (res == NULL)
2994 return NULL;
2995
2996 status = Py_ReprEnter(op);
2997 if (status != 0) {
2998 if (status > 0) {
2999 PyErr_Format(PyExc_RuntimeError,
3000 "reentrant call inside %.100s.__repr__",
3001 type_name);
3002 }
3003 goto error;
3004 }
3005 if (PyObject_GetOptionalAttr(op, &_Py_ID(name), &nameobj) < 0) {
3006 if (!PyErr_ExceptionMatches(PyExc_ValueError)) {
3007 goto error;
3008 }
3009 /* Ignore ValueError raised if the underlying stream was detached */
3010 PyErr_Clear();
3011 }
3012 if (nameobj != NULL) {
3013 s = PyUnicode_FromFormat(" name=%R", nameobj);
3014 Py_DECREF(nameobj);
3015 if (s == NULL)
3016 goto error;
3017 PyUnicode_AppendAndDel(&res, s);
3018 if (res == NULL)
3019 goto error;
3020 }
3021 if (PyObject_GetOptionalAttr(op, &_Py_ID(mode), &modeobj) < 0) {
3022 goto error;
3023 }
3024 if (modeobj != NULL) {
3025 s = PyUnicode_FromFormat(" mode=%R", modeobj);
3026 Py_DECREF(modeobj);
3027 if (s == NULL)
3028 goto error;
3029 PyUnicode_AppendAndDel(&res, s);
3030 if (res == NULL)
3031 goto error;
3032 }
3033 s = PyUnicode_FromFormat("%U encoding=%R>",
3034 res, self->encoding);
3035 Py_DECREF(res);
3036 if (status == 0) {
3037 Py_ReprLeave(op);
3038 }
3039 return s;
3040
3041 error:
3042 Py_XDECREF(res);
3043 if (status == 0) {
3044 Py_ReprLeave(op);
3045 }
3046 return NULL;
3047 }
3048
3049
3050 /* Inquiries */
3051
3052 /*[clinic input]
3053 @critical_section
3054 _io.TextIOWrapper.fileno
3055 [clinic start generated code]*/
3056
3057 static PyObject *
3058 _io_TextIOWrapper_fileno_impl(textio *self)
3059 /*[clinic end generated code: output=21490a4c3da13e6c input=515e1196aceb97ab]*/
3060 {
3061 CHECK_ATTACHED(self);
3062 return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(fileno));
3063 }
3064
3065 /*[clinic input]
3066 @critical_section
3067 _io.TextIOWrapper.seekable
3068 [clinic start generated code]*/
3069
3070 static PyObject *
3071 _io_TextIOWrapper_seekable_impl(textio *self)
3072 /*[clinic end generated code: output=ab223dbbcffc0f00 input=71c4c092736c549b]*/
3073 {
3074 CHECK_ATTACHED(self);
3075 return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(seekable));
3076 }
3077
3078 /*[clinic input]
3079 @critical_section
3080 _io.TextIOWrapper.readable
3081 [clinic start generated code]*/
3082
3083 static PyObject *
3084 _io_TextIOWrapper_readable_impl(textio *self)
3085 /*[clinic end generated code: output=72ff7ba289a8a91b input=80438d1f01b0a89b]*/
3086 {
3087 CHECK_ATTACHED(self);
3088 return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(readable));
3089 }
3090
3091 /*[clinic input]
3092 @critical_section
3093 _io.TextIOWrapper.writable
3094 [clinic start generated code]*/
3095
3096 static PyObject *
3097 _io_TextIOWrapper_writable_impl(textio *self)
3098 /*[clinic end generated code: output=a728c71790d03200 input=9d6c22befb0c340a]*/
3099 {
3100 CHECK_ATTACHED(self);
3101 return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(writable));
3102 }
3103
3104 /*[clinic input]
3105 @critical_section
3106 _io.TextIOWrapper.isatty
3107 [clinic start generated code]*/
3108
3109 static PyObject *
3110 _io_TextIOWrapper_isatty_impl(textio *self)
3111 /*[clinic end generated code: output=12be1a35bace882e input=7f83ff04d4d1733d]*/
3112 {
3113 CHECK_ATTACHED(self);
3114 return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(isatty));
3115 }
3116
3117 /*[clinic input]
3118 @critical_section
3119 _io.TextIOWrapper.flush
3120 [clinic start generated code]*/
3121
3122 static PyObject *
3123 _io_TextIOWrapper_flush_impl(textio *self)
3124 /*[clinic end generated code: output=59de9165f9c2e4d2 input=3ac3bf521bfed59d]*/
3125 {
3126 CHECK_ATTACHED(self);
3127 CHECK_CLOSED(self);
3128 self->telling = self->seekable;
3129 if (_textiowrapper_writeflush(self) < 0)
3130 return NULL;
3131 return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(flush));
3132 }
3133
3134 /*[clinic input]
3135 @critical_section
3136 _io.TextIOWrapper.close
3137 [clinic start generated code]*/
3138
3139 static PyObject *
3140 _io_TextIOWrapper_close_impl(textio *self)
3141 /*[clinic end generated code: output=056ccf8b4876e4f4 input=8e12d7079d5ac5c1]*/
3142 {
3143 PyObject *res;
3144 int r;
3145 CHECK_ATTACHED(self);
3146
3147 res = _io_TextIOWrapper_closed_get_impl(self);
3148 if (res == NULL)
3149 return NULL;
3150 r = PyObject_IsTrue(res);
3151 Py_DECREF(res);
3152 if (r < 0)
3153 return NULL;
3154
3155 if (r > 0) {
3156 Py_RETURN_NONE; /* stream already closed */
3157 }
3158 if (self->detached) {
3159 Py_RETURN_NONE; /* gh-142594 null pointer issue */
3160 }
3161 else {
3162 PyObject *exc = NULL;
3163 if (self->finalizing) {
3164 res = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(_dealloc_warn),
3165 (PyObject *)self);
3166 if (res) {
3167 Py_DECREF(res);
3168 }
3169 else {
3170 PyErr_Clear();
3171 }
3172 }
3173 if (_PyFile_Flush((PyObject *)self) < 0) {
3174 exc = PyErr_GetRaisedException();
3175 }
3176
3177 res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(close));
3178 if (exc != NULL) {
3179 _PyErr_ChainExceptions1(exc);
3180 Py_CLEAR(res);
3181 }
3182 return res;
3183 }
3184 }
3185
3186 static PyObject *
3187 textiowrapper_iternext_lock_held(PyObject *op)
3188 {
3189 _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op);
3190 PyObject *line;
3191 textio *self = textio_CAST(op);
3192
3193 CHECK_ATTACHED(self);
3194
3195 self->telling = 0;
3196 if (Py_IS_TYPE(self, self->state->PyTextIOWrapper_Type)) {
3197 /* Skip method call overhead for speed */
3198 line = _textiowrapper_readline(self, -1);
3199 }
3200 else {
3201 line = PyObject_CallMethodNoArgs(op, &_Py_ID(readline));
3202 if (line && !PyUnicode_Check(line)) {
3203 PyErr_Format(PyExc_OSError,
3204 "readline() should have returned a str object, "
3205 "not '%.200s'", Py_TYPE(line)->tp_name);
3206 Py_DECREF(line);
3207 return NULL;
3208 }
3209 }
3210
3211 if (line == NULL)
3212 return NULL;
3213
3214 if (PyUnicode_GET_LENGTH(line) == 0) {
3215 /* Reached EOF or would have blocked */
3216 Py_DECREF(line);
3217 Py_CLEAR(self->snapshot);
3218 self->telling = self->seekable;
3219 return NULL;
3220 }
3221
3222 return line;
3223 }
3224
3225 static PyObject *
3226 textiowrapper_iternext(PyObject *op)
3227 {
3228 PyObject *result;
3229 Py_BEGIN_CRITICAL_SECTION(op);
3230 result = textiowrapper_iternext_lock_held(op);
3231 Py_END_CRITICAL_SECTION();
3232 return result;
3233 }
3234
3235 /*[clinic input]
3236 @critical_section
3237 @getter
3238 _io.TextIOWrapper.name
3239 [clinic start generated code]*/
3240
3241 static PyObject *
3242 _io_TextIOWrapper_name_get_impl(textio *self)
3243 /*[clinic end generated code: output=8c2f1d6d8756af40 input=26ecec9b39e30e07]*/
3244 {
3245 CHECK_ATTACHED(self);
3246 return PyObject_GetAttr(self->buffer, &_Py_ID(name));
3247 }
3248
3249 /*[clinic input]
3250 @critical_section
3251 @getter
3252 _io.TextIOWrapper.closed
3253 [clinic start generated code]*/
3254
3255 static PyObject *
3256 _io_TextIOWrapper_closed_get_impl(textio *self)
3257 /*[clinic end generated code: output=b49b68f443a85e3c input=7dfcf43f63c7003d]*/
3258 {
3259 CHECK_ATTACHED(self);
3260 return PyObject_GetAttr(self->buffer, &_Py_ID(closed));
3261 }
3262
3263 /*[clinic input]
3264 @critical_section
3265 @getter
3266 _io.TextIOWrapper.newlines
3267 [clinic start generated code]*/
3268
3269 static PyObject *
3270 _io_TextIOWrapper_newlines_get_impl(textio *self)
3271 /*[clinic end generated code: output=53aa03ac35573180 input=610df647e514b3e8]*/
3272 {
3273 PyObject *res;
3274 CHECK_ATTACHED(self);
3275 if (self->decoder == NULL ||
3276 PyObject_GetOptionalAttr(self->decoder, &_Py_ID(newlines), &res) == 0)
3277 {
3278 Py_RETURN_NONE;
3279 }
3280 return res;
3281 }
3282
3283 /*[clinic input]
3284 @critical_section
3285 @getter
3286 _io.TextIOWrapper.errors
3287 [clinic start generated code]*/
3288
3289 static PyObject *
3290 _io_TextIOWrapper_errors_get_impl(textio *self)
3291 /*[clinic end generated code: output=dca3a3ef21b09484 input=b45f983e6d43c4d8]*/
3292 {
3293 CHECK_INITIALIZED(self);
3294 return Py_NewRef(self->errors);
3295 }
3296
3297 /*[clinic input]
3298 @critical_section
3299 @getter
3300 _io.TextIOWrapper._CHUNK_SIZE
3301 [clinic start generated code]*/
3302
3303 static PyObject *
3304 _io_TextIOWrapper__CHUNK_SIZE_get_impl(textio *self)
3305 /*[clinic end generated code: output=039925cd2df375bc input=e9715b0e06ff0fa6]*/
3306 {
3307 CHECK_ATTACHED(self);
3308 return PyLong_FromSsize_t(self->chunk_size);
3309 }
3310
3311 /*[clinic input]
3312 @critical_section
3313 @setter
3314 _io.TextIOWrapper._CHUNK_SIZE
3315 [clinic start generated code]*/
3316
3317 static int
3318 _io_TextIOWrapper__CHUNK_SIZE_set_impl(textio *self, PyObject *value)
3319 /*[clinic end generated code: output=edb86d2db660a5ab input=32fc99861db02a0a]*/
3320 {
3321 Py_ssize_t n;
3322 CHECK_ATTACHED_INT(self);
3323 if (value == NULL) {
3324 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
3325 return -1;
3326 }
3327 n = PyNumber_AsSsize_t(value, PyExc_ValueError);
3328 if (n == -1 && PyErr_Occurred())
3329 return -1;
3330 if (n <= 0) {
3331 PyErr_SetString(PyExc_ValueError,
3332 "a strictly positive integer is required");
3333 return -1;
3334 }
3335 self->chunk_size = n;
3336 return 0;
3337 }
3338
3339 static PyMethodDef incrementalnewlinedecoder_methods[] = {
3340 _IO_INCREMENTALNEWLINEDECODER_DECODE_METHODDEF
3341 _IO_INCREMENTALNEWLINEDECODER_GETSTATE_METHODDEF
3342 _IO_INCREMENTALNEWLINEDECODER_SETSTATE_METHODDEF
3343 _IO_INCREMENTALNEWLINEDECODER_RESET_METHODDEF
3344 {NULL}
3345 };
3346
3347 static PyGetSetDef incrementalnewlinedecoder_getset[] = {
3348 {"newlines", incrementalnewlinedecoder_newlines_get, NULL, NULL},
3349 {NULL}
3350 };
3351
3352 static PyType_Slot nldecoder_slots[] = {
3353 {Py_tp_dealloc, incrementalnewlinedecoder_dealloc},
3354 {Py_tp_doc, (void *)_io_IncrementalNewlineDecoder___init____doc__},
3355 {Py_tp_methods, incrementalnewlinedecoder_methods},
3356 {Py_tp_getset, incrementalnewlinedecoder_getset},
3357 {Py_tp_traverse, incrementalnewlinedecoder_traverse},
3358 {Py_tp_clear, incrementalnewlinedecoder_clear},
3359 {Py_tp_init, _io_IncrementalNewlineDecoder___init__},
3360 {0, NULL},
3361 };
3362
3363 PyType_Spec nldecoder_spec = {
3364 .name = "_io.IncrementalNewlineDecoder",
3365 .basicsize = sizeof(nldecoder_object),
3366 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
3367 Py_TPFLAGS_IMMUTABLETYPE),
3368 .slots = nldecoder_slots,
3369 };
3370
3371
3372 static PyMethodDef textiowrapper_methods[] = {
3373 _IO_TEXTIOWRAPPER_DETACH_METHODDEF
3374 _IO_TEXTIOWRAPPER_RECONFIGURE_METHODDEF
3375 _IO_TEXTIOWRAPPER_WRITE_METHODDEF
3376 _IO_TEXTIOWRAPPER_READ_METHODDEF
3377 _IO_TEXTIOWRAPPER_READLINE_METHODDEF
3378 _IO_TEXTIOWRAPPER_FLUSH_METHODDEF
3379 _IO_TEXTIOWRAPPER_CLOSE_METHODDEF
3380
3381 _IO_TEXTIOWRAPPER_FILENO_METHODDEF
3382 _IO_TEXTIOWRAPPER_SEEKABLE_METHODDEF
3383 _IO_TEXTIOWRAPPER_READABLE_METHODDEF
3384 _IO_TEXTIOWRAPPER_WRITABLE_METHODDEF
3385 _IO_TEXTIOWRAPPER_ISATTY_METHODDEF
3386
3387 _IO_TEXTIOWRAPPER_SEEK_METHODDEF
3388 _IO_TEXTIOWRAPPER_TELL_METHODDEF
3389 _IO_TEXTIOWRAPPER_TRUNCATE_METHODDEF
3390
3391 {"__getstate__", _PyIOBase_cannot_pickle, METH_NOARGS},
3392 {NULL, NULL}
3393 };
3394
3395 static PyMemberDef textiowrapper_members[] = {
3396 {"encoding", _Py_T_OBJECT, offsetof(textio, encoding), Py_READONLY},
3397 {"buffer", _Py_T_OBJECT, offsetof(textio, buffer), Py_READONLY},
3398 {"line_buffering", Py_T_BOOL, offsetof(textio, line_buffering), Py_READONLY},
3399 {"write_through", Py_T_BOOL, offsetof(textio, write_through), Py_READONLY},
3400 {"_finalizing", Py_T_BOOL, offsetof(textio, finalizing), 0},
3401 {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(textio, weakreflist), Py_READONLY},
3402 {"__dictoffset__", Py_T_PYSSIZET, offsetof(textio, dict), Py_READONLY},
3403 {NULL}
3404 };
3405
3406 static PyGetSetDef textiowrapper_getset[] = {
3407 _IO_TEXTIOWRAPPER_NAME_GETSETDEF
3408 _IO_TEXTIOWRAPPER_CLOSED_GETSETDEF
3409 _IO_TEXTIOWRAPPER_NEWLINES_GETSETDEF
3410 _IO_TEXTIOWRAPPER_ERRORS_GETSETDEF
3411 _IO_TEXTIOWRAPPER__CHUNK_SIZE_GETSETDEF
3412 {NULL}
3413 };
3414
3415 PyType_Slot textiowrapper_slots[] = {
3416 {Py_tp_dealloc, textiowrapper_dealloc},
3417 {Py_tp_repr, textiowrapper_repr},
3418 {Py_tp_doc, (void *)_io_TextIOWrapper___init____doc__},
3419 {Py_tp_traverse, textiowrapper_traverse},
3420 {Py_tp_clear, textiowrapper_clear},
3421 {Py_tp_iternext, textiowrapper_iternext},
3422 {Py_tp_methods, textiowrapper_methods},
3423 {Py_tp_members, textiowrapper_members},
3424 {Py_tp_getset, textiowrapper_getset},
3425 {Py_tp_init, _io_TextIOWrapper___init__},
3426 {0, NULL},
3427 };
3428
3429 PyType_Spec textiowrapper_spec = {
3430 .name = "_io.TextIOWrapper",
3431 .basicsize = sizeof(textio),
3432 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
3433 Py_TPFLAGS_IMMUTABLETYPE),
3434 .slots = textiowrapper_slots,
3435 };