Bug Summary

File:./Modules/_io/textio.c
Location:line 1464, column 9
Description:Access to field 'ob_refcnt' results in a dereference of a null pointer (loaded from variable 'dec_buffer')

Annotated Source Code

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#define PY_SSIZE_T_CLEAN
10#include "Python.h"
11#include "structmember.h"
12#include "_iomodule.h"
13
14/* TextIOBase */
15
16PyDoc_STRVAR(textiobase_doc,static char textiobase_doc[] = "Base class for text I/O.\n" "\n"
"This class provides a character and line based interface to stream\n"
"I/O. There is no readinto method because Python's character strings\n"
"are immutable. There is no public constructor.\n"
17 "Base class for text I/O.\n"static char textiobase_doc[] = "Base class for text I/O.\n" "\n"
"This class provides a character and line based interface to stream\n"
"I/O. There is no readinto method because Python's character strings\n"
"are immutable. There is no public constructor.\n"
18 "\n"static char textiobase_doc[] = "Base class for text I/O.\n" "\n"
"This class provides a character and line based interface to stream\n"
"I/O. There is no readinto method because Python's character strings\n"
"are immutable. There is no public constructor.\n"
19 "This class provides a character and line based interface to stream\n"static char textiobase_doc[] = "Base class for text I/O.\n" "\n"
"This class provides a character and line based interface to stream\n"
"I/O. There is no readinto method because Python's character strings\n"
"are immutable. There is no public constructor.\n"
20 "I/O. There is no readinto method because Python's character strings\n"static char textiobase_doc[] = "Base class for text I/O.\n" "\n"
"This class provides a character and line based interface to stream\n"
"I/O. There is no readinto method because Python's character strings\n"
"are immutable. There is no public constructor.\n"
21 "are immutable. There is no public constructor.\n"static char textiobase_doc[] = "Base class for text I/O.\n" "\n"
"This class provides a character and line based interface to stream\n"
"I/O. There is no readinto method because Python's character strings\n"
"are immutable. There is no public constructor.\n"
22 )static char textiobase_doc[] = "Base class for text I/O.\n" "\n"
"This class provides a character and line based interface to stream\n"
"I/O. There is no readinto method because Python's character strings\n"
"are immutable. There is no public constructor.\n"
;
23
24static PyObject *
25_unsupported(const char *message)
26{
27 PyErr_SetString(IO_STATE((_PyIO_State *)PyModule_GetState(PyState_FindModule(&_PyIO_Module
)))
->unsupported_operation, message);
28 return NULL((void*)0);
29}
30
31PyDoc_STRVAR(textiobase_detach_doc,static char textiobase_detach_doc[] = "Separate the underlying buffer from the TextIOBase and return it.\n"
"\n" "After the underlying buffer has been detached, the TextIO is in an\n"
"unusable state.\n"
32 "Separate the underlying buffer from the TextIOBase and return it.\n"static char textiobase_detach_doc[] = "Separate the underlying buffer from the TextIOBase and return it.\n"
"\n" "After the underlying buffer has been detached, the TextIO is in an\n"
"unusable state.\n"
33 "\n"static char textiobase_detach_doc[] = "Separate the underlying buffer from the TextIOBase and return it.\n"
"\n" "After the underlying buffer has been detached, the TextIO is in an\n"
"unusable state.\n"
34 "After the underlying buffer has been detached, the TextIO is in an\n"static char textiobase_detach_doc[] = "Separate the underlying buffer from the TextIOBase and return it.\n"
"\n" "After the underlying buffer has been detached, the TextIO is in an\n"
"unusable state.\n"
35 "unusable state.\n"static char textiobase_detach_doc[] = "Separate the underlying buffer from the TextIOBase and return it.\n"
"\n" "After the underlying buffer has been detached, the TextIO is in an\n"
"unusable state.\n"
36 )static char textiobase_detach_doc[] = "Separate the underlying buffer from the TextIOBase and return it.\n"
"\n" "After the underlying buffer has been detached, the TextIO is in an\n"
"unusable state.\n"
;
37
38static PyObject *
39textiobase_detach(PyObject *self)
40{
41 return _unsupported("detach");
42}
43
44PyDoc_STRVAR(textiobase_read_doc,static char textiobase_read_doc[] = "Read at most n characters from stream.\n"
"\n" "Read from underlying buffer until we have n characters or we hit EOF.\n"
"If n is negative or omitted, read until EOF.\n"
45 "Read at most n characters from stream.\n"static char textiobase_read_doc[] = "Read at most n characters from stream.\n"
"\n" "Read from underlying buffer until we have n characters or we hit EOF.\n"
"If n is negative or omitted, read until EOF.\n"
46 "\n"static char textiobase_read_doc[] = "Read at most n characters from stream.\n"
"\n" "Read from underlying buffer until we have n characters or we hit EOF.\n"
"If n is negative or omitted, read until EOF.\n"
47 "Read from underlying buffer until we have n characters or we hit EOF.\n"static char textiobase_read_doc[] = "Read at most n characters from stream.\n"
"\n" "Read from underlying buffer until we have n characters or we hit EOF.\n"
"If n is negative or omitted, read until EOF.\n"
48 "If n is negative or omitted, read until EOF.\n"static char textiobase_read_doc[] = "Read at most n characters from stream.\n"
"\n" "Read from underlying buffer until we have n characters or we hit EOF.\n"
"If n is negative or omitted, read until EOF.\n"
49 )static char textiobase_read_doc[] = "Read at most n characters from stream.\n"
"\n" "Read from underlying buffer until we have n characters or we hit EOF.\n"
"If n is negative or omitted, read until EOF.\n"
;
50
51static PyObject *
52textiobase_read(PyObject *self, PyObject *args)
53{
54 return _unsupported("read");
55}
56
57PyDoc_STRVAR(textiobase_readline_doc,static char textiobase_readline_doc[] = "Read until newline or EOF.\n"
"\n" "Returns an empty string if EOF is hit immediately.\n"
58 "Read until newline or EOF.\n"static char textiobase_readline_doc[] = "Read until newline or EOF.\n"
"\n" "Returns an empty string if EOF is hit immediately.\n"
59 "\n"static char textiobase_readline_doc[] = "Read until newline or EOF.\n"
"\n" "Returns an empty string if EOF is hit immediately.\n"
60 "Returns an empty string if EOF is hit immediately.\n"static char textiobase_readline_doc[] = "Read until newline or EOF.\n"
"\n" "Returns an empty string if EOF is hit immediately.\n"
61 )static char textiobase_readline_doc[] = "Read until newline or EOF.\n"
"\n" "Returns an empty string if EOF is hit immediately.\n"
;
62
63static PyObject *
64textiobase_readline(PyObject *self, PyObject *args)
65{
66 return _unsupported("readline");
67}
68
69PyDoc_STRVAR(textiobase_write_doc,static char textiobase_write_doc[] = "Write string to stream.\n"
"Returns the number of characters written (which is always equal to\n"
"the length of the string).\n"
70 "Write string to stream.\n"static char textiobase_write_doc[] = "Write string to stream.\n"
"Returns the number of characters written (which is always equal to\n"
"the length of the string).\n"
71 "Returns the number of characters written (which is always equal to\n"static char textiobase_write_doc[] = "Write string to stream.\n"
"Returns the number of characters written (which is always equal to\n"
"the length of the string).\n"
72 "the length of the string).\n"static char textiobase_write_doc[] = "Write string to stream.\n"
"Returns the number of characters written (which is always equal to\n"
"the length of the string).\n"
73 )static char textiobase_write_doc[] = "Write string to stream.\n"
"Returns the number of characters written (which is always equal to\n"
"the length of the string).\n"
;
74
75static PyObject *
76textiobase_write(PyObject *self, PyObject *args)
77{
78 return _unsupported("write");
79}
80
81PyDoc_STRVAR(textiobase_encoding_doc,static char textiobase_encoding_doc[] = "Encoding of the text stream.\n"
"\n" "Subclasses should override.\n"
82 "Encoding of the text stream.\n"static char textiobase_encoding_doc[] = "Encoding of the text stream.\n"
"\n" "Subclasses should override.\n"
83 "\n"static char textiobase_encoding_doc[] = "Encoding of the text stream.\n"
"\n" "Subclasses should override.\n"
84 "Subclasses should override.\n"static char textiobase_encoding_doc[] = "Encoding of the text stream.\n"
"\n" "Subclasses should override.\n"
85 )static char textiobase_encoding_doc[] = "Encoding of the text stream.\n"
"\n" "Subclasses should override.\n"
;
86
87static PyObject *
88textiobase_encoding_get(PyObject *self, void *context)
89{
90 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
91}
92
93PyDoc_STRVAR(textiobase_newlines_doc,static char textiobase_newlines_doc[] = "Line endings translated so far.\n"
"\n" "Only line endings translated during reading are considered.\n"
"\n" "Subclasses should override.\n"
94 "Line endings translated so far.\n"static char textiobase_newlines_doc[] = "Line endings translated so far.\n"
"\n" "Only line endings translated during reading are considered.\n"
"\n" "Subclasses should override.\n"
95 "\n"static char textiobase_newlines_doc[] = "Line endings translated so far.\n"
"\n" "Only line endings translated during reading are considered.\n"
"\n" "Subclasses should override.\n"
96 "Only line endings translated during reading are considered.\n"static char textiobase_newlines_doc[] = "Line endings translated so far.\n"
"\n" "Only line endings translated during reading are considered.\n"
"\n" "Subclasses should override.\n"
97 "\n"static char textiobase_newlines_doc[] = "Line endings translated so far.\n"
"\n" "Only line endings translated during reading are considered.\n"
"\n" "Subclasses should override.\n"
98 "Subclasses should override.\n"static char textiobase_newlines_doc[] = "Line endings translated so far.\n"
"\n" "Only line endings translated during reading are considered.\n"
"\n" "Subclasses should override.\n"
99 )static char textiobase_newlines_doc[] = "Line endings translated so far.\n"
"\n" "Only line endings translated during reading are considered.\n"
"\n" "Subclasses should override.\n"
;
100
101static PyObject *
102textiobase_newlines_get(PyObject *self, void *context)
103{
104 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
105}
106
107PyDoc_STRVAR(textiobase_errors_doc,static char textiobase_errors_doc[] = "The error setting of the decoder or encoder.\n"
"\n" "Subclasses should override.\n"
108 "The error setting of the decoder or encoder.\n"static char textiobase_errors_doc[] = "The error setting of the decoder or encoder.\n"
"\n" "Subclasses should override.\n"
109 "\n"static char textiobase_errors_doc[] = "The error setting of the decoder or encoder.\n"
"\n" "Subclasses should override.\n"
110 "Subclasses should override.\n"static char textiobase_errors_doc[] = "The error setting of the decoder or encoder.\n"
"\n" "Subclasses should override.\n"
111 )static char textiobase_errors_doc[] = "The error setting of the decoder or encoder.\n"
"\n" "Subclasses should override.\n"
;
112
113static PyObject *
114textiobase_errors_get(PyObject *self, void *context)
115{
116 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
117}
118
119
120static PyMethodDef textiobase_methods[] = {
121 {"detach", (PyCFunction)textiobase_detach, METH_NOARGS0x0004, textiobase_detach_doc},
122 {"read", textiobase_read, METH_VARARGS0x0001, textiobase_read_doc},
123 {"readline", textiobase_readline, METH_VARARGS0x0001, textiobase_readline_doc},
124 {"write", textiobase_write, METH_VARARGS0x0001, textiobase_write_doc},
125 {NULL((void*)0), NULL((void*)0)}
126};
127
128static PyGetSetDef textiobase_getset[] = {
129 {"encoding", (getter)textiobase_encoding_get, NULL((void*)0), textiobase_encoding_doc},
130 {"newlines", (getter)textiobase_newlines_get, NULL((void*)0), textiobase_newlines_doc},
131 {"errors", (getter)textiobase_errors_get, NULL((void*)0), textiobase_errors_doc},
132 {NULL((void*)0)}
133};
134
135PyTypeObject PyTextIOBase_Type = {
136 PyVarObject_HEAD_INIT(NULL, 0){ { 0, 0, 1, ((void*)0) }, 0 },
137 "_io._TextIOBase", /*tp_name*/
138 0, /*tp_basicsize*/
139 0, /*tp_itemsize*/
140 0, /*tp_dealloc*/
141 0, /*tp_print*/
142 0, /*tp_getattr*/
143 0, /*tp_setattr*/
144 0, /*tp_compare */
145 0, /*tp_repr*/
146 0, /*tp_as_number*/
147 0, /*tp_as_sequence*/
148 0, /*tp_as_mapping*/
149 0, /*tp_hash */
150 0, /*tp_call*/
151 0, /*tp_str*/
152 0, /*tp_getattro*/
153 0, /*tp_setattro*/
154 0, /*tp_as_buffer*/
155 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_BASETYPE(1L<<10), /*tp_flags*/
156 textiobase_doc, /* tp_doc */
157 0, /* tp_traverse */
158 0, /* tp_clear */
159 0, /* tp_richcompare */
160 0, /* tp_weaklistoffset */
161 0, /* tp_iter */
162 0, /* tp_iternext */
163 textiobase_methods, /* tp_methods */
164 0, /* tp_members */
165 textiobase_getset, /* tp_getset */
166 &PyIOBase_Type, /* tp_base */
167 0, /* tp_dict */
168 0, /* tp_descr_get */
169 0, /* tp_descr_set */
170 0, /* tp_dictoffset */
171 0, /* tp_init */
172 0, /* tp_alloc */
173 0, /* tp_new */
174};
175
176
177/* IncrementalNewlineDecoder */
178
179PyDoc_STRVAR(incrementalnewlinedecoder_doc,static char incrementalnewlinedecoder_doc[] = "Codec used when reading a file in universal newlines mode. It wraps\n"
"another incremental decoder, translating \\r\\n and \\r into \\n. It also\n"
"records the types of newlines encountered. When used with\n"
"translate=False, it ensures that the newline sequence is returned in\n"
"one piece. When used with decoder=None, it expects unicode strings as\n"
"decode input and translates newlines without first invoking an external\n"
"decoder.\n"
180 "Codec used when reading a file in universal newlines mode. It wraps\n"static char incrementalnewlinedecoder_doc[] = "Codec used when reading a file in universal newlines mode. It wraps\n"
"another incremental decoder, translating \\r\\n and \\r into \\n. It also\n"
"records the types of newlines encountered. When used with\n"
"translate=False, it ensures that the newline sequence is returned in\n"
"one piece. When used with decoder=None, it expects unicode strings as\n"
"decode input and translates newlines without first invoking an external\n"
"decoder.\n"
181 "another incremental decoder, translating \\r\\n and \\r into \\n. It also\n"static char incrementalnewlinedecoder_doc[] = "Codec used when reading a file in universal newlines mode. It wraps\n"
"another incremental decoder, translating \\r\\n and \\r into \\n. It also\n"
"records the types of newlines encountered. When used with\n"
"translate=False, it ensures that the newline sequence is returned in\n"
"one piece. When used with decoder=None, it expects unicode strings as\n"
"decode input and translates newlines without first invoking an external\n"
"decoder.\n"
182 "records the types of newlines encountered. When used with\n"static char incrementalnewlinedecoder_doc[] = "Codec used when reading a file in universal newlines mode. It wraps\n"
"another incremental decoder, translating \\r\\n and \\r into \\n. It also\n"
"records the types of newlines encountered. When used with\n"
"translate=False, it ensures that the newline sequence is returned in\n"
"one piece. When used with decoder=None, it expects unicode strings as\n"
"decode input and translates newlines without first invoking an external\n"
"decoder.\n"
183 "translate=False, it ensures that the newline sequence is returned in\n"static char incrementalnewlinedecoder_doc[] = "Codec used when reading a file in universal newlines mode. It wraps\n"
"another incremental decoder, translating \\r\\n and \\r into \\n. It also\n"
"records the types of newlines encountered. When used with\n"
"translate=False, it ensures that the newline sequence is returned in\n"
"one piece. When used with decoder=None, it expects unicode strings as\n"
"decode input and translates newlines without first invoking an external\n"
"decoder.\n"
184 "one piece. When used with decoder=None, it expects unicode strings as\n"static char incrementalnewlinedecoder_doc[] = "Codec used when reading a file in universal newlines mode. It wraps\n"
"another incremental decoder, translating \\r\\n and \\r into \\n. It also\n"
"records the types of newlines encountered. When used with\n"
"translate=False, it ensures that the newline sequence is returned in\n"
"one piece. When used with decoder=None, it expects unicode strings as\n"
"decode input and translates newlines without first invoking an external\n"
"decoder.\n"
185 "decode input and translates newlines without first invoking an external\n"static char incrementalnewlinedecoder_doc[] = "Codec used when reading a file in universal newlines mode. It wraps\n"
"another incremental decoder, translating \\r\\n and \\r into \\n. It also\n"
"records the types of newlines encountered. When used with\n"
"translate=False, it ensures that the newline sequence is returned in\n"
"one piece. When used with decoder=None, it expects unicode strings as\n"
"decode input and translates newlines without first invoking an external\n"
"decoder.\n"
186 "decoder.\n"static char incrementalnewlinedecoder_doc[] = "Codec used when reading a file in universal newlines mode. It wraps\n"
"another incremental decoder, translating \\r\\n and \\r into \\n. It also\n"
"records the types of newlines encountered. When used with\n"
"translate=False, it ensures that the newline sequence is returned in\n"
"one piece. When used with decoder=None, it expects unicode strings as\n"
"decode input and translates newlines without first invoking an external\n"
"decoder.\n"
187 )static char incrementalnewlinedecoder_doc[] = "Codec used when reading a file in universal newlines mode. It wraps\n"
"another incremental decoder, translating \\r\\n and \\r into \\n. It also\n"
"records the types of newlines encountered. When used with\n"
"translate=False, it ensures that the newline sequence is returned in\n"
"one piece. When used with decoder=None, it expects unicode strings as\n"
"decode input and translates newlines without first invoking an external\n"
"decoder.\n"
;
188
189typedef struct {
190 PyObject_HEADPyObject ob_base;
191 PyObject *decoder;
192 PyObject *errors;
193 signed int pendingcr: 1;
194 signed int translate: 1;
195 unsigned int seennl: 3;
196} nldecoder_object;
197
198static int
199incrementalnewlinedecoder_init(nldecoder_object *self,
200 PyObject *args, PyObject *kwds)
201{
202 PyObject *decoder;
203 int translate;
204 PyObject *errors = NULL((void*)0);
205 char *kwlist[] = {"decoder", "translate", "errors", NULL((void*)0)};
206
207 if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "Oi|O:IncrementalNewlineDecoder",
208 kwlist, &decoder, &translate, &errors))
209 return -1;
210
211 self->decoder = decoder;
212 Py_INCREF(decoder)( _Py_RefTotal++ , ((PyObject*)(decoder))->ob_refcnt++);
213
214 if (errors == NULL((void*)0)) {
215 self->errors = PyUnicode_FromStringPyUnicodeUCS2_FromString("strict");
216 if (self->errors == NULL((void*)0))
217 return -1;
218 }
219 else {
220 Py_INCREF(errors)( _Py_RefTotal++ , ((PyObject*)(errors))->ob_refcnt++);
221 self->errors = errors;
222 }
223
224 self->translate = translate;
225 self->seennl = 0;
226 self->pendingcr = 0;
227
228 return 0;
229}
230
231static void
232incrementalnewlinedecoder_dealloc(nldecoder_object *self)
233{
234 Py_CLEAR(self->decoder)do { if (self->decoder) { PyObject *_py_tmp = (PyObject *)
(self->decoder); (self->decoder) = ((void*)0); do { if (
_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0)
{ if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 234, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
235 Py_CLEAR(self->errors)do { if (self->errors) { PyObject *_py_tmp = (PyObject *)(
self->errors); (self->errors) = ((void*)0); do { if (_Py_RefTotal
-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0) { if (((PyObject
*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 235, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
236 Py_TYPE(self)(((PyObject*)(self))->ob_type)->tp_free((PyObject *)self);
237}
238
239#define SEEN_CR1 1
240#define SEEN_LF2 2
241#define SEEN_CRLF4 4
242#define SEEN_ALL(1 | 2 | 4) (SEEN_CR1 | SEEN_LF2 | SEEN_CRLF4)
243
244PyObject *
245_PyIncrementalNewlineDecoder_decode(PyObject *_self,
246 PyObject *input, int final)
247{
248 PyObject *output;
249 Py_ssize_t output_len;
250 nldecoder_object *self = (nldecoder_object *) _self;
251
252 if (self->decoder == NULL((void*)0)) {
253 PyErr_SetString(PyExc_ValueError,
254 "IncrementalNewlineDecoder.__init__ not called");
255 return NULL((void*)0);
256 }
257
258 /* decode input (with the eventual \r from a previous pass) */
259 if (self->decoder != Py_None(&_Py_NoneStruct)) {
260 output = PyObject_CallMethodObjArgs(self->decoder,
261 _PyIO_str_decode, input, final ? Py_True((PyObject *) &_Py_TrueStruct) : Py_False((PyObject *) &_Py_FalseStruct), NULL((void*)0));
262 }
263 else {
264 output = input;
265 Py_INCREF(output)( _Py_RefTotal++ , ((PyObject*)(output))->ob_refcnt++);
266 }
267
268 if (output == NULL((void*)0))
269 return NULL((void*)0);
270
271 if (!PyUnicode_Check(output)((((((PyObject*)(output))->ob_type))->tp_flags & ((
1L<<28))) != 0)
) {
272 PyErr_SetString(PyExc_TypeError,
273 "decoder should return a string result");
274 goto error;
275 }
276
277 output_len = PyUnicode_GET_SIZE(output)((__builtin_expect(!(((((((PyObject*)(output))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 277, "PyUnicode_Check(output)") :
(void)0),(((PyUnicodeObject *)(output))->length))
;
278 if (self->pendingcr && (final || output_len > 0)) {
279 Py_UNICODE *out;
280 PyObject *modified = PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(NULL((void*)0), output_len + 1);
281 if (modified == NULL((void*)0))
282 goto error;
283 out = PyUnicode_AS_UNICODE(modified)((__builtin_expect(!(((((((PyObject*)(modified))->ob_type)
)->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 283, "PyUnicode_Check(modified)"
) : (void)0),(((PyUnicodeObject *)(modified))->str))
;
284 out[0] = '\r';
285 memcpy(out + 1, PyUnicode_AS_UNICODE(output),((__builtin_object_size (out + 1, 0) != (size_t) -1) ? __builtin___memcpy_chk
(out + 1, ((__builtin_expect(!(((((((PyObject*)(output))->
ob_type))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 285, "PyUnicode_Check(output)"
) : (void)0),(((PyUnicodeObject *)(output))->str)), output_len
* sizeof(Py_UNICODE), __builtin_object_size (out + 1, 0)) : __inline_memcpy_chk
(out + 1, ((__builtin_expect(!(((((((PyObject*)(output))->
ob_type))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 285, "PyUnicode_Check(output)"
) : (void)0),(((PyUnicodeObject *)(output))->str)), output_len
* sizeof(Py_UNICODE)))
286 output_len * sizeof(Py_UNICODE))((__builtin_object_size (out + 1, 0) != (size_t) -1) ? __builtin___memcpy_chk
(out + 1, ((__builtin_expect(!(((((((PyObject*)(output))->
ob_type))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 285, "PyUnicode_Check(output)"
) : (void)0),(((PyUnicodeObject *)(output))->str)), output_len
* sizeof(Py_UNICODE), __builtin_object_size (out + 1, 0)) : __inline_memcpy_chk
(out + 1, ((__builtin_expect(!(((((((PyObject*)(output))->
ob_type))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 285, "PyUnicode_Check(output)"
) : (void)0),(((PyUnicodeObject *)(output))->str)), output_len
* sizeof(Py_UNICODE)))
;
287 Py_DECREF(output)do { if (_Py_RefTotal-- , --((PyObject*)(output))->ob_refcnt
!= 0) { if (((PyObject*)output)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 287, (PyObject *)(output)); } else
_Py_Dealloc((PyObject *)(output)); } while (0)
;
288 output = modified;
289 self->pendingcr = 0;
290 output_len++;
291 }
292
293 /* retain last \r even when not translating data:
294 * then readline() is sure to get \r\n in one pass
295 */
296 if (!final) {
297 if (output_len > 0
298 && PyUnicode_AS_UNICODE(output)((__builtin_expect(!(((((((PyObject*)(output))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 298, "PyUnicode_Check(output)") :
(void)0),(((PyUnicodeObject *)(output))->str))
[output_len - 1] == '\r') {
299
300 if (Py_REFCNT(output)(((PyObject*)(output))->ob_refcnt) == 1) {
301 if (PyUnicode_ResizePyUnicodeUCS2_Resize(&output, output_len - 1) < 0)
302 goto error;
303 }
304 else {
305 PyObject *modified = PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(
306 PyUnicode_AS_UNICODE(output)((__builtin_expect(!(((((((PyObject*)(output))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 306, "PyUnicode_Check(output)") :
(void)0),(((PyUnicodeObject *)(output))->str))
,
307 output_len - 1);
308 if (modified == NULL((void*)0))
309 goto error;
310 Py_DECREF(output)do { if (_Py_RefTotal-- , --((PyObject*)(output))->ob_refcnt
!= 0) { if (((PyObject*)output)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 310, (PyObject *)(output)); } else
_Py_Dealloc((PyObject *)(output)); } while (0)
;
311 output = modified;
312 }
313 self->pendingcr = 1;
314 }
315 }
316
317 /* Record which newlines are read and do newline translation if desired,
318 all in one pass. */
319 {
320 Py_UNICODE *in_str;
321 Py_ssize_t len;
322 int seennl = self->seennl;
323 int only_lf = 0;
324
325 in_str = PyUnicode_AS_UNICODE(output)((__builtin_expect(!(((((((PyObject*)(output))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 325, "PyUnicode_Check(output)") :
(void)0),(((PyUnicodeObject *)(output))->str))
;
326 len = PyUnicode_GET_SIZE(output)((__builtin_expect(!(((((((PyObject*)(output))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 326, "PyUnicode_Check(output)") :
(void)0),(((PyUnicodeObject *)(output))->length))
;
327
328 if (len == 0)
329 return output;
330
331 /* If, up to now, newlines are consistently \n, do a quick check
332 for the \r *byte* with the libc's optimized memchr.
333 */
334 if (seennl == SEEN_LF2 || seennl == 0) {
335 only_lf = (memchr(in_str, '\r', len * sizeof(Py_UNICODE)) == NULL((void*)0));
336 }
337
338 if (only_lf) {
339 /* If not already seen, quick scan for a possible "\n" character.
340 (there's nothing else to be done, even when in translation mode)
341 */
342 if (seennl == 0 &&
343 memchr(in_str, '\n', len * sizeof(Py_UNICODE)) != NULL((void*)0)) {
344 Py_UNICODE *s, *end;
345 s = in_str;
346 end = in_str + len;
347 for (;;) {
348 Py_UNICODE c;
349 /* Fast loop for non-control characters */
350 while (*s > '\n')
351 s++;
352 c = *s++;
353 if (c == '\n') {
354 seennl |= SEEN_LF2;
355 break;
356 }
357 if (s > end)
358 break;
359 }
360 }
361 /* Finished: we have scanned for newlines, and none of them
362 need translating */
363 }
364 else if (!self->translate) {
365 Py_UNICODE *s, *end;
366 /* We have already seen all newline types, no need to scan again */
367 if (seennl == SEEN_ALL(1 | 2 | 4))
368 goto endscan;
369 s = in_str;
370 end = in_str + len;
371 for (;;) {
372 Py_UNICODE c;
373 /* Fast loop for non-control characters */
374 while (*s > '\r')
375 s++;
376 c = *s++;
377 if (c == '\n')
378 seennl |= SEEN_LF2;
379 else if (c == '\r') {
380 if (*s == '\n') {
381 seennl |= SEEN_CRLF4;
382 s++;
383 }
384 else
385 seennl |= SEEN_CR1;
386 }
387 if (s > end)
388 break;
389 if (seennl == SEEN_ALL(1 | 2 | 4))
390 break;
391 }
392 endscan:
393 ;
394 }
395 else {
396 PyObject *translated = NULL((void*)0);
397 Py_UNICODE *out_str;
398 Py_UNICODE *in, *out, *end;
399 if (Py_REFCNT(output)(((PyObject*)(output))->ob_refcnt) != 1) {
400 /* We could try to optimize this so that we only do a copy
401 when there is something to translate. On the other hand,
402 most decoders should only output non-shared strings, i.e.
403 translation is done in place. */
404 translated = PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(NULL((void*)0), len);
405 if (translated == NULL((void*)0))
406 goto error;
407 assert(Py_REFCNT(translated) == 1)(__builtin_expect(!((((PyObject*)(translated))->ob_refcnt)
== 1), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c",
407, "Py_REFCNT(translated) == 1") : (void)0)
;
408 memcpy(PyUnicode_AS_UNICODE(translated),((__builtin_object_size (((__builtin_expect(!(((((((PyObject*
)(translated))->ob_type))->tp_flags & ((1L<<28
))) != 0)), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c"
, 408, "PyUnicode_Check(translated)") : (void)0),(((PyUnicodeObject
*)(translated))->str)), 0) != (size_t) -1) ? __builtin___memcpy_chk
(((__builtin_expect(!(((((((PyObject*)(translated))->ob_type
))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 408, "PyUnicode_Check(translated)"
) : (void)0),(((PyUnicodeObject *)(translated))->str)), ((
__builtin_expect(!(((((((PyObject*)(output))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 409, "PyUnicode_Check(output)") :
(void)0),(((PyUnicodeObject *)(output))->str)), len * sizeof
(Py_UNICODE), __builtin_object_size (((__builtin_expect(!((((
(((PyObject*)(translated))->ob_type))->tp_flags & (
(1L<<28))) != 0)), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c"
, 408, "PyUnicode_Check(translated)") : (void)0),(((PyUnicodeObject
*)(translated))->str)), 0)) : __inline_memcpy_chk (((__builtin_expect
(!(((((((PyObject*)(translated))->ob_type))->tp_flags &
((1L<<28))) != 0)), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c"
, 408, "PyUnicode_Check(translated)") : (void)0),(((PyUnicodeObject
*)(translated))->str)), ((__builtin_expect(!(((((((PyObject
*)(output))->ob_type))->tp_flags & ((1L<<28))
) != 0)), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c"
, 409, "PyUnicode_Check(output)") : (void)0),(((PyUnicodeObject
*)(output))->str)), len * sizeof(Py_UNICODE)))
409 PyUnicode_AS_UNICODE(output),((__builtin_object_size (((__builtin_expect(!(((((((PyObject*
)(translated))->ob_type))->tp_flags & ((1L<<28
))) != 0)), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c"
, 408, "PyUnicode_Check(translated)") : (void)0),(((PyUnicodeObject
*)(translated))->str)), 0) != (size_t) -1) ? __builtin___memcpy_chk
(((__builtin_expect(!(((((((PyObject*)(translated))->ob_type
))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 408, "PyUnicode_Check(translated)"
) : (void)0),(((PyUnicodeObject *)(translated))->str)), ((
__builtin_expect(!(((((((PyObject*)(output))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 409, "PyUnicode_Check(output)") :
(void)0),(((PyUnicodeObject *)(output))->str)), len * sizeof
(Py_UNICODE), __builtin_object_size (((__builtin_expect(!((((
(((PyObject*)(translated))->ob_type))->tp_flags & (
(1L<<28))) != 0)), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c"
, 408, "PyUnicode_Check(translated)") : (void)0),(((PyUnicodeObject
*)(translated))->str)), 0)) : __inline_memcpy_chk (((__builtin_expect
(!(((((((PyObject*)(translated))->ob_type))->tp_flags &
((1L<<28))) != 0)), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c"
, 408, "PyUnicode_Check(translated)") : (void)0),(((PyUnicodeObject
*)(translated))->str)), ((__builtin_expect(!(((((((PyObject
*)(output))->ob_type))->tp_flags & ((1L<<28))
) != 0)), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c"
, 409, "PyUnicode_Check(output)") : (void)0),(((PyUnicodeObject
*)(output))->str)), len * sizeof(Py_UNICODE)))
410 len * sizeof(Py_UNICODE))((__builtin_object_size (((__builtin_expect(!(((((((PyObject*
)(translated))->ob_type))->tp_flags & ((1L<<28
))) != 0)), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c"
, 408, "PyUnicode_Check(translated)") : (void)0),(((PyUnicodeObject
*)(translated))->str)), 0) != (size_t) -1) ? __builtin___memcpy_chk
(((__builtin_expect(!(((((((PyObject*)(translated))->ob_type
))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 408, "PyUnicode_Check(translated)"
) : (void)0),(((PyUnicodeObject *)(translated))->str)), ((
__builtin_expect(!(((((((PyObject*)(output))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 409, "PyUnicode_Check(output)") :
(void)0),(((PyUnicodeObject *)(output))->str)), len * sizeof
(Py_UNICODE), __builtin_object_size (((__builtin_expect(!((((
(((PyObject*)(translated))->ob_type))->tp_flags & (
(1L<<28))) != 0)), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c"
, 408, "PyUnicode_Check(translated)") : (void)0),(((PyUnicodeObject
*)(translated))->str)), 0)) : __inline_memcpy_chk (((__builtin_expect
(!(((((((PyObject*)(translated))->ob_type))->tp_flags &
((1L<<28))) != 0)), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c"
, 408, "PyUnicode_Check(translated)") : (void)0),(((PyUnicodeObject
*)(translated))->str)), ((__builtin_expect(!(((((((PyObject
*)(output))->ob_type))->tp_flags & ((1L<<28))
) != 0)), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c"
, 409, "PyUnicode_Check(output)") : (void)0),(((PyUnicodeObject
*)(output))->str)), len * sizeof(Py_UNICODE)))
;
411 }
412 else {
413 translated = output;
414 }
415 out_str = PyUnicode_AS_UNICODE(translated)((__builtin_expect(!(((((((PyObject*)(translated))->ob_type
))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 415, "PyUnicode_Check(translated)"
) : (void)0),(((PyUnicodeObject *)(translated))->str))
;
416 in = in_str;
417 out = out_str;
418 end = in_str + len;
419 for (;;) {
420 Py_UNICODE c;
421 /* Fast loop for non-control characters */
422 while ((c = *in++) > '\r')
423 *out++ = c;
424 if (c == '\n') {
425 *out++ = c;
426 seennl |= SEEN_LF2;
427 continue;
428 }
429 if (c == '\r') {
430 if (*in == '\n') {
431 in++;
432 seennl |= SEEN_CRLF4;
433 }
434 else
435 seennl |= SEEN_CR1;
436 *out++ = '\n';
437 continue;
438 }
439 if (in > end)
440 break;
441 *out++ = c;
442 }
443 if (translated != output) {
444 Py_DECREF(output)do { if (_Py_RefTotal-- , --((PyObject*)(output))->ob_refcnt
!= 0) { if (((PyObject*)output)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 444, (PyObject *)(output)); } else
_Py_Dealloc((PyObject *)(output)); } while (0)
;
445 output = translated;
446 }
447 if (out - out_str != len) {
448 if (PyUnicode_ResizePyUnicodeUCS2_Resize(&output, out - out_str) < 0)
449 goto error;
450 }
451 }
452 self->seennl |= seennl;
453 }
454
455 return output;
456
457 error:
458 Py_DECREF(output)do { if (_Py_RefTotal-- , --((PyObject*)(output))->ob_refcnt
!= 0) { if (((PyObject*)output)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 458, (PyObject *)(output)); } else
_Py_Dealloc((PyObject *)(output)); } while (0)
;
459 return NULL((void*)0);
460}
461
462static PyObject *
463incrementalnewlinedecoder_decode(nldecoder_object *self,
464 PyObject *args, PyObject *kwds)
465{
466 char *kwlist[] = {"input", "final", NULL((void*)0)};
467 PyObject *input;
468 int final = 0;
469
470 if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "O|i:IncrementalNewlineDecoder",
471 kwlist, &input, &final))
472 return NULL((void*)0);
473 return _PyIncrementalNewlineDecoder_decode((PyObject *) self, input, final);
474}
475
476static PyObject *
477incrementalnewlinedecoder_getstate(nldecoder_object *self, PyObject *args)
478{
479 PyObject *buffer;
480 unsigned PY_LONG_LONGlong long flag;
481
482 if (self->decoder != Py_None(&_Py_NoneStruct)) {
483 PyObject *state = PyObject_CallMethodObjArgs(self->decoder,
484 _PyIO_str_getstate, NULL((void*)0));
485 if (state == NULL((void*)0))
486 return NULL((void*)0);
487 if (!PyArg_Parse_PyArg_Parse_SizeT(state, "(OK)", &buffer, &flag)) {
488 Py_DECREF(state)do { if (_Py_RefTotal-- , --((PyObject*)(state))->ob_refcnt
!= 0) { if (((PyObject*)state)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 488, (PyObject *)(state)); } else _Py_Dealloc
((PyObject *)(state)); } while (0)
;
489 return NULL((void*)0);
490 }
491 Py_INCREF(buffer)( _Py_RefTotal++ , ((PyObject*)(buffer))->ob_refcnt++);
492 Py_DECREF(state)do { if (_Py_RefTotal-- , --((PyObject*)(state))->ob_refcnt
!= 0) { if (((PyObject*)state)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 492, (PyObject *)(state)); } else _Py_Dealloc
((PyObject *)(state)); } while (0)
;
493 }
494 else {
495 buffer = PyBytes_FromString("");
496 flag = 0;
497 }
498 flag <<= 1;
499 if (self->pendingcr)
500 flag |= 1;
501 return Py_BuildValue_Py_BuildValue_SizeT("NK", buffer, flag);
502}
503
504static PyObject *
505incrementalnewlinedecoder_setstate(nldecoder_object *self, PyObject *state)
506{
507 PyObject *buffer;
508 unsigned PY_LONG_LONGlong long flag;
509
510 if (!PyArg_Parse_PyArg_Parse_SizeT(state, "(OK)", &buffer, &flag))
511 return NULL((void*)0);
512
513 self->pendingcr = (int) flag & 1;
514 flag >>= 1;
515
516 if (self->decoder != Py_None(&_Py_NoneStruct))
517 return PyObject_CallMethod_PyObject_CallMethod_SizeT(self->decoder,
518 "setstate", "((OK))", buffer, flag);
519 else
520 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
521}
522
523static PyObject *
524incrementalnewlinedecoder_reset(nldecoder_object *self, PyObject *args)
525{
526 self->seennl = 0;
527 self->pendingcr = 0;
528 if (self->decoder != Py_None(&_Py_NoneStruct))
529 return PyObject_CallMethodObjArgs(self->decoder, _PyIO_str_reset, NULL((void*)0));
530 else
531 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
532}
533
534static PyObject *
535incrementalnewlinedecoder_newlines_get(nldecoder_object *self, void *context)
536{
537 switch (self->seennl) {
538 case SEEN_CR1:
539 return PyUnicode_FromStringPyUnicodeUCS2_FromString("\r");
540 case SEEN_LF2:
541 return PyUnicode_FromStringPyUnicodeUCS2_FromString("\n");
542 case SEEN_CRLF4:
543 return PyUnicode_FromStringPyUnicodeUCS2_FromString("\r\n");
544 case SEEN_CR1 | SEEN_LF2:
545 return Py_BuildValue_Py_BuildValue_SizeT("ss", "\r", "\n");
546 case SEEN_CR1 | SEEN_CRLF4:
547 return Py_BuildValue_Py_BuildValue_SizeT("ss", "\r", "\r\n");
548 case SEEN_LF2 | SEEN_CRLF4:
549 return Py_BuildValue_Py_BuildValue_SizeT("ss", "\n", "\r\n");
550 case SEEN_CR1 | SEEN_LF2 | SEEN_CRLF4:
551 return Py_BuildValue_Py_BuildValue_SizeT("sss", "\r", "\n", "\r\n");
552 default:
553 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
554 }
555
556}
557
558
559static PyMethodDef incrementalnewlinedecoder_methods[] = {
560 {"decode", (PyCFunction)incrementalnewlinedecoder_decode, METH_VARARGS0x0001|METH_KEYWORDS0x0002},
561 {"getstate", (PyCFunction)incrementalnewlinedecoder_getstate, METH_NOARGS0x0004},
562 {"setstate", (PyCFunction)incrementalnewlinedecoder_setstate, METH_O0x0008},
563 {"reset", (PyCFunction)incrementalnewlinedecoder_reset, METH_NOARGS0x0004},
564 {NULL((void*)0)}
565};
566
567static PyGetSetDef incrementalnewlinedecoder_getset[] = {
568 {"newlines", (getter)incrementalnewlinedecoder_newlines_get, NULL((void*)0), NULL((void*)0)},
569 {NULL((void*)0)}
570};
571
572PyTypeObject PyIncrementalNewlineDecoder_Type = {
573 PyVarObject_HEAD_INIT(NULL, 0){ { 0, 0, 1, ((void*)0) }, 0 },
574 "_io.IncrementalNewlineDecoder", /*tp_name*/
575 sizeof(nldecoder_object), /*tp_basicsize*/
576 0, /*tp_itemsize*/
577 (destructor)incrementalnewlinedecoder_dealloc, /*tp_dealloc*/
578 0, /*tp_print*/
579 0, /*tp_getattr*/
580 0, /*tp_setattr*/
581 0, /*tp_compare */
582 0, /*tp_repr*/
583 0, /*tp_as_number*/
584 0, /*tp_as_sequence*/
585 0, /*tp_as_mapping*/
586 0, /*tp_hash */
587 0, /*tp_call*/
588 0, /*tp_str*/
589 0, /*tp_getattro*/
590 0, /*tp_setattro*/
591 0, /*tp_as_buffer*/
592 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_BASETYPE(1L<<10), /*tp_flags*/
593 incrementalnewlinedecoder_doc, /* tp_doc */
594 0, /* tp_traverse */
595 0, /* tp_clear */
596 0, /* tp_richcompare */
597 0, /*tp_weaklistoffset*/
598 0, /* tp_iter */
599 0, /* tp_iternext */
600 incrementalnewlinedecoder_methods, /* tp_methods */
601 0, /* tp_members */
602 incrementalnewlinedecoder_getset, /* tp_getset */
603 0, /* tp_base */
604 0, /* tp_dict */
605 0, /* tp_descr_get */
606 0, /* tp_descr_set */
607 0, /* tp_dictoffset */
608 (initproc)incrementalnewlinedecoder_init, /* tp_init */
609 0, /* tp_alloc */
610 PyType_GenericNew, /* tp_new */
611};
612
613
614/* TextIOWrapper */
615
616PyDoc_STRVAR(textiowrapper_doc,static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
617 "Character and line based layer over a BufferedIOBase object, buffer.\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
618 "\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
619 "encoding gives the name of the encoding that the stream will be\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
620 "decoded or encoded with. It defaults to locale.getpreferredencoding.\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
621 "\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
622 "errors determines the strictness of encoding and decoding (see the\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
623 "codecs.register) and defaults to \"strict\".\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
624 "\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
625 "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
626 "handling of line endings. If it is None, universal newlines is\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
627 "enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
628 "or '\\r\\n' are translated to '\\n' before being returned to the\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
629 "caller. Conversely, on output, '\\n' is translated to the system\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
630 "default line seperator, os.linesep. If newline is any other of its\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
631 "legal values, that newline becomes the newline when the file is read\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
632 "and it is returned untranslated. On output, '\\n' is converted to the\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
633 "newline.\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
634 "\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
635 "If line_buffering is True, a call to flush is implied when a call to\n"static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
636 "write contains a newline character."static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
637 )static char textiowrapper_doc[] = "Character and line based layer over a BufferedIOBase object, buffer.\n"
"\n" "encoding gives the name of the encoding that the stream will be\n"
"decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
"\n" "errors determines the strictness of encoding and decoding (see the\n"
"codecs.register) and defaults to \"strict\".\n" "\n" "newline can be None, '', '\\n', '\\r', or '\\r\\n'. It controls the\n"
"handling of line endings. If it is None, universal newlines is\n"
"enabled. With this enabled, on input, the lines endings '\\n', '\\r',\n"
"or '\\r\\n' are translated to '\\n' before being returned to the\n"
"caller. Conversely, on output, '\\n' is translated to the system\n"
"default line seperator, os.linesep. If newline is any other of its\n"
"legal values, that newline becomes the newline when the file is read\n"
"and it is returned untranslated. On output, '\\n' is converted to the\n"
"newline.\n" "\n" "If line_buffering is True, a call to flush is implied when a call to\n"
"write contains a newline character."
;
638
639typedef PyObject *
640 (*encodefunc_t)(PyObject *, PyObject *);
641
642typedef struct
643{
644 PyObject_HEADPyObject ob_base;
645 int ok; /* initialized? */
646 int detached;
647 Py_ssize_t chunk_size;
648 PyObject *buffer;
649 PyObject *encoding;
650 PyObject *encoder;
651 PyObject *decoder;
652 PyObject *readnl;
653 PyObject *errors;
654 const char *writenl; /* utf-8 encoded, NULL stands for \n */
655 char line_buffering;
656 char readuniversal;
657 char readtranslate;
658 char writetranslate;
659 char seekable;
660 char telling;
661 char deallocating;
662 /* Specialized encoding func (see below) */
663 encodefunc_t encodefunc;
664 /* Whether or not it's the start of the stream */
665 char encoding_start_of_stream;
666
667 /* Reads and writes are internally buffered in order to speed things up.
668 However, any read will first flush the write buffer if itsn't empty.
669
670 Please also note that text to be written is first encoded before being
671 buffered. This is necessary so that encoding errors are immediately
672 reported to the caller, but it unfortunately means that the
673 IncrementalEncoder (whose encode() method is always written in Python)
674 becomes a bottleneck for small writes.
675 */
676 PyObject *decoded_chars; /* buffer for text returned from decoder */
677 Py_ssize_t decoded_chars_used; /* offset into _decoded_chars for read() */
678 PyObject *pending_bytes; /* list of bytes objects waiting to be
679 written, or NULL */
680 Py_ssize_t pending_bytes_count;
681 PyObject *snapshot;
682 /* snapshot is either None, or a tuple (dec_flags, next_input) where
683 * dec_flags is the second (integer) item of the decoder state and
684 * next_input is the chunk of input bytes that comes next after the
685 * snapshot point. We use this to reconstruct decoder states in tell().
686 */
687
688 /* Cache raw object if it's a FileIO object */
689 PyObject *raw;
690
691 PyObject *weakreflist;
692 PyObject *dict;
693} textio;
694
695
696/* A couple of specialized cases in order to bypass the slow incremental
697 encoding methods for the most popular encodings. */
698
699static PyObject *
700ascii_encode(textio *self, PyObject *text)
701{
702 return PyUnicode_EncodeASCIIPyUnicodeUCS2_EncodeASCII(PyUnicode_AS_UNICODE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 702, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->str))
,
703 PyUnicode_GET_SIZE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 703, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->length))
,
704 PyBytes_AS_STRING(self->errors)((__builtin_expect(!(((((((PyObject*)(self->errors))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 704, "PyBytes_Check(self->errors)"
) : (void)0), (((PyBytesObject *)(self->errors))->ob_sval
))
);
705}
706
707static PyObject *
708utf16be_encode(textio *self, PyObject *text)
709{
710 return PyUnicode_EncodeUTF16PyUnicodeUCS2_EncodeUTF16(PyUnicode_AS_UNICODE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 710, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->str))
,
711 PyUnicode_GET_SIZE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 711, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->length))
,
712 PyBytes_AS_STRING(self->errors)((__builtin_expect(!(((((((PyObject*)(self->errors))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 712, "PyBytes_Check(self->errors)"
) : (void)0), (((PyBytesObject *)(self->errors))->ob_sval
))
, 1);
713}
714
715static PyObject *
716utf16le_encode(textio *self, PyObject *text)
717{
718 return PyUnicode_EncodeUTF16PyUnicodeUCS2_EncodeUTF16(PyUnicode_AS_UNICODE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 718, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->str))
,
719 PyUnicode_GET_SIZE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 719, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->length))
,
720 PyBytes_AS_STRING(self->errors)((__builtin_expect(!(((((((PyObject*)(self->errors))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 720, "PyBytes_Check(self->errors)"
) : (void)0), (((PyBytesObject *)(self->errors))->ob_sval
))
, -1);
721}
722
723static PyObject *
724utf16_encode(textio *self, PyObject *text)
725{
726 if (!self->encoding_start_of_stream) {
727 /* Skip the BOM and use native byte ordering */
728#if defined(WORDS_BIGENDIAN)
729 return utf16be_encode(self, text);
730#else
731 return utf16le_encode(self, text);
732#endif
733 }
734 return PyUnicode_EncodeUTF16PyUnicodeUCS2_EncodeUTF16(PyUnicode_AS_UNICODE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 734, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->str))
,
735 PyUnicode_GET_SIZE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 735, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->length))
,
736 PyBytes_AS_STRING(self->errors)((__builtin_expect(!(((((((PyObject*)(self->errors))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 736, "PyBytes_Check(self->errors)"
) : (void)0), (((PyBytesObject *)(self->errors))->ob_sval
))
, 0);
737}
738
739static PyObject *
740utf32be_encode(textio *self, PyObject *text)
741{
742 return PyUnicode_EncodeUTF32PyUnicodeUCS2_EncodeUTF32(PyUnicode_AS_UNICODE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 742, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->str))
,
743 PyUnicode_GET_SIZE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 743, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->length))
,
744 PyBytes_AS_STRING(self->errors)((__builtin_expect(!(((((((PyObject*)(self->errors))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 744, "PyBytes_Check(self->errors)"
) : (void)0), (((PyBytesObject *)(self->errors))->ob_sval
))
, 1);
745}
746
747static PyObject *
748utf32le_encode(textio *self, PyObject *text)
749{
750 return PyUnicode_EncodeUTF32PyUnicodeUCS2_EncodeUTF32(PyUnicode_AS_UNICODE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 750, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->str))
,
751 PyUnicode_GET_SIZE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 751, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->length))
,
752 PyBytes_AS_STRING(self->errors)((__builtin_expect(!(((((((PyObject*)(self->errors))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 752, "PyBytes_Check(self->errors)"
) : (void)0), (((PyBytesObject *)(self->errors))->ob_sval
))
, -1);
753}
754
755static PyObject *
756utf32_encode(textio *self, PyObject *text)
757{
758 if (!self->encoding_start_of_stream) {
759 /* Skip the BOM and use native byte ordering */
760#if defined(WORDS_BIGENDIAN)
761 return utf32be_encode(self, text);
762#else
763 return utf32le_encode(self, text);
764#endif
765 }
766 return PyUnicode_EncodeUTF32PyUnicodeUCS2_EncodeUTF32(PyUnicode_AS_UNICODE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 766, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->str))
,
767 PyUnicode_GET_SIZE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 767, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->length))
,
768 PyBytes_AS_STRING(self->errors)((__builtin_expect(!(((((((PyObject*)(self->errors))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 768, "PyBytes_Check(self->errors)"
) : (void)0), (((PyBytesObject *)(self->errors))->ob_sval
))
, 0);
769}
770
771static PyObject *
772utf8_encode(textio *self, PyObject *text)
773{
774 return PyUnicode_EncodeUTF8PyUnicodeUCS2_EncodeUTF8(PyUnicode_AS_UNICODE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 774, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->str))
,
775 PyUnicode_GET_SIZE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 775, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->length))
,
776 PyBytes_AS_STRING(self->errors)((__builtin_expect(!(((((((PyObject*)(self->errors))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 776, "PyBytes_Check(self->errors)"
) : (void)0), (((PyBytesObject *)(self->errors))->ob_sval
))
);
777}
778
779static PyObject *
780latin1_encode(textio *self, PyObject *text)
781{
782 return PyUnicode_EncodeLatin1PyUnicodeUCS2_EncodeLatin1(PyUnicode_AS_UNICODE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 782, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->str))
,
783 PyUnicode_GET_SIZE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 783, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->length))
,
784 PyBytes_AS_STRING(self->errors)((__builtin_expect(!(((((((PyObject*)(self->errors))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 784, "PyBytes_Check(self->errors)"
) : (void)0), (((PyBytesObject *)(self->errors))->ob_sval
))
);
785}
786
787/* Map normalized encoding names onto the specialized encoding funcs */
788
789typedef struct {
790 const char *name;
791 encodefunc_t encodefunc;
792} encodefuncentry;
793
794static encodefuncentry encodefuncs[] = {
795 {"ascii", (encodefunc_t) ascii_encode},
796 {"iso8859-1", (encodefunc_t) latin1_encode},
797 {"utf-8", (encodefunc_t) utf8_encode},
798 {"utf-16-be", (encodefunc_t) utf16be_encode},
799 {"utf-16-le", (encodefunc_t) utf16le_encode},
800 {"utf-16", (encodefunc_t) utf16_encode},
801 {"utf-32-be", (encodefunc_t) utf32be_encode},
802 {"utf-32-le", (encodefunc_t) utf32le_encode},
803 {"utf-32", (encodefunc_t) utf32_encode},
804 {NULL((void*)0), NULL((void*)0)}
805};
806
807
808static int
809textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
810{
811 char *kwlist[] = {"buffer", "encoding", "errors",
812 "newline", "line_buffering",
813 NULL((void*)0)};
814 PyObject *buffer, *raw;
815 char *encoding = NULL((void*)0);
816 char *errors = NULL((void*)0);
817 char *newline = NULL((void*)0);
818 int line_buffering = 0;
819 _PyIO_State *state = IO_STATE((_PyIO_State *)PyModule_GetState(PyState_FindModule(&_PyIO_Module
)))
;
820
821 PyObject *res;
822 int r;
823
824 self->ok = 0;
825 self->detached = 0;
826 if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "O|zzzi:fileio",
827 kwlist, &buffer, &encoding, &errors,
828 &newline, &line_buffering))
829 return -1;
830
831 if (newline && newline[0] != '\0'
832 && !(newline[0] == '\n' && newline[1] == '\0')
833 && !(newline[0] == '\r' && newline[1] == '\0')
834 && !(newline[0] == '\r' && newline[1] == '\n' && newline[2] == '\0')) {
835 PyErr_Format(PyExc_ValueError,
836 "illegal newline value: %s", newline);
837 return -1;
838 }
839
840 Py_CLEAR(self->buffer)do { if (self->buffer) { PyObject *_py_tmp = (PyObject *)(
self->buffer); (self->buffer) = ((void*)0); do { if (_Py_RefTotal
-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0) { if (((PyObject
*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 840, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
841 Py_CLEAR(self->encoding)do { if (self->encoding) { PyObject *_py_tmp = (PyObject *
)(self->encoding); (self->encoding) = ((void*)0); do { if
(_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0
) { if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 841, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
842 Py_CLEAR(self->encoder)do { if (self->encoder) { PyObject *_py_tmp = (PyObject *)
(self->encoder); (self->encoder) = ((void*)0); do { if (
_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0)
{ if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 842, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
843 Py_CLEAR(self->decoder)do { if (self->decoder) { PyObject *_py_tmp = (PyObject *)
(self->decoder); (self->decoder) = ((void*)0); do { if (
_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0)
{ if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 843, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
844 Py_CLEAR(self->readnl)do { if (self->readnl) { PyObject *_py_tmp = (PyObject *)(
self->readnl); (self->readnl) = ((void*)0); do { if (_Py_RefTotal
-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0) { if (((PyObject
*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 844, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
845 Py_CLEAR(self->decoded_chars)do { if (self->decoded_chars) { PyObject *_py_tmp = (PyObject
*)(self->decoded_chars); (self->decoded_chars) = ((void
*)0); do { if (_Py_RefTotal-- , --((PyObject*)(_py_tmp))->
ob_refcnt != 0) { if (((PyObject*)_py_tmp)->ob_refcnt <
0) _Py_NegativeRefcount("./Modules/_io/textio.c", 845, (PyObject
*)(_py_tmp)); } else _Py_Dealloc((PyObject *)(_py_tmp)); } while
(0); } } while (0)
;
846 Py_CLEAR(self->pending_bytes)do { if (self->pending_bytes) { PyObject *_py_tmp = (PyObject
*)(self->pending_bytes); (self->pending_bytes) = ((void
*)0); do { if (_Py_RefTotal-- , --((PyObject*)(_py_tmp))->
ob_refcnt != 0) { if (((PyObject*)_py_tmp)->ob_refcnt <
0) _Py_NegativeRefcount("./Modules/_io/textio.c", 846, (PyObject
*)(_py_tmp)); } else _Py_Dealloc((PyObject *)(_py_tmp)); } while
(0); } } while (0)
;
847 Py_CLEAR(self->snapshot)do { if (self->snapshot) { PyObject *_py_tmp = (PyObject *
)(self->snapshot); (self->snapshot) = ((void*)0); do { if
(_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0
) { if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 847, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
848 Py_CLEAR(self->errors)do { if (self->errors) { PyObject *_py_tmp = (PyObject *)(
self->errors); (self->errors) = ((void*)0); do { if (_Py_RefTotal
-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0) { if (((PyObject
*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 848, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
849 Py_CLEAR(self->raw)do { if (self->raw) { PyObject *_py_tmp = (PyObject *)(self
->raw); (self->raw) = ((void*)0); do { if (_Py_RefTotal
-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0) { if (((PyObject
*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 849, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
850 self->decoded_chars_used = 0;
851 self->pending_bytes_count = 0;
852 self->encodefunc = NULL((void*)0);
853
854 if (encoding == NULL((void*)0)) {
855 /* Try os.device_encoding(fileno) */
856 PyObject *fileno;
857 fileno = PyObject_CallMethod_PyObject_CallMethod_SizeT(buffer, "fileno", NULL((void*)0));
858 /* Ignore only AttributeError and UnsupportedOperation */
859 if (fileno == NULL((void*)0)) {
860 if (PyErr_ExceptionMatches(PyExc_AttributeError) ||
861 PyErr_ExceptionMatches(state->unsupported_operation)) {
862 PyErr_Clear();
863 }
864 else {
865 goto error;
866 }
867 }
868 else {
869 self->encoding = PyObject_CallMethod_PyObject_CallMethod_SizeT(state->os_module,
870 "device_encoding",
871 "N", fileno);
872 if (self->encoding == NULL((void*)0))
873 goto error;
874 else if (!PyUnicode_Check(self->encoding)((((((PyObject*)(self->encoding))->ob_type))->tp_flags
& ((1L<<28))) != 0)
)
875 Py_CLEAR(self->encoding)do { if (self->encoding) { PyObject *_py_tmp = (PyObject *
)(self->encoding); (self->encoding) = ((void*)0); do { if
(_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0
) { if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 875, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
876 }
877 }
878 if (encoding == NULL((void*)0) && self->encoding == NULL((void*)0)) {
879 if (state->locale_module == NULL((void*)0)) {
880 state->locale_module = PyImport_ImportModule("locale");
881 if (state->locale_module == NULL((void*)0))
882 goto catch_ImportError;
883 else
884 goto use_locale;
885 }
886 else {
887 use_locale:
888 self->encoding = PyObject_CallMethod_PyObject_CallMethod_SizeT(
889 state->locale_module, "getpreferredencoding", NULL((void*)0));
890 if (self->encoding == NULL((void*)0)) {
891 catch_ImportError:
892 /*
893 Importing locale can raise a ImportError because of
894 _functools, and locale.getpreferredencoding can raise a
895 ImportError if _locale is not available. These will happen
896 during module building.
897 */
898 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
899 PyErr_Clear();
900 self->encoding = PyUnicode_FromStringPyUnicodeUCS2_FromString("ascii");
901 }
902 else
903 goto error;
904 }
905 else if (!PyUnicode_Check(self->encoding)((((((PyObject*)(self->encoding))->ob_type))->tp_flags
& ((1L<<28))) != 0)
)
906 Py_CLEAR(self->encoding)do { if (self->encoding) { PyObject *_py_tmp = (PyObject *
)(self->encoding); (self->encoding) = ((void*)0); do { if
(_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0
) { if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 906, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
907 }
908 }
909 if (self->encoding != NULL((void*)0)) {
910 encoding = _PyUnicode_AsString(self->encoding);
911 if (encoding == NULL((void*)0))
912 goto error;
913 }
914 else if (encoding != NULL((void*)0)) {
915 self->encoding = PyUnicode_FromStringPyUnicodeUCS2_FromString(encoding);
916 if (self->encoding == NULL((void*)0))
917 goto error;
918 }
919 else {
920 PyErr_SetString(PyExc_IOError,
921 "could not determine default encoding");
922 }
923
924 if (errors == NULL((void*)0))
925 errors = "strict";
926 self->errors = PyBytes_FromString(errors);
927 if (self->errors == NULL((void*)0))
928 goto error;
929
930 self->chunk_size = 8192;
931 self->readuniversal = (newline == NULL((void*)0) || newline[0] == '\0');
932 self->line_buffering = line_buffering;
933 self->readtranslate = (newline == NULL((void*)0));
934 if (newline) {
935 self->readnl = PyUnicode_FromStringPyUnicodeUCS2_FromString(newline);
936 if (self->readnl == NULL((void*)0))
937 return -1;
938 }
939 self->writetranslate = (newline == NULL((void*)0) || newline[0] != '\0');
940 if (!self->readuniversal && self->readnl) {
941 self->writenl = _PyUnicode_AsString(self->readnl);
942 if (self->writenl == NULL((void*)0))
943 goto error;
944 if (!strcmp(self->writenl, "\n"))
945 self->writenl = NULL((void*)0);
946 }
947#ifdef MS_WINDOWS
948 else
949 self->writenl = "\r\n";
950#endif
951
952 /* Build the decoder object */
953 res = PyObject_CallMethod_PyObject_CallMethod_SizeT(buffer, "readable", NULL((void*)0));
954 if (res == NULL((void*)0))
955 goto error;
956 r = PyObject_IsTrue(res);
957 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 957, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
958 if (r == -1)
959 goto error;
960 if (r == 1) {
961 self->decoder = PyCodec_IncrementalDecoder(
962 encoding, errors);
963 if (self->decoder == NULL((void*)0))
964 goto error;
965
966 if (self->readuniversal) {
967 PyObject *incrementalDecoder = PyObject_CallFunction_PyObject_CallFunction_SizeT(
968 (PyObject *)&PyIncrementalNewlineDecoder_Type,
969 "Oi", self->decoder, (int)self->readtranslate);
970 if (incrementalDecoder == NULL((void*)0))
971 goto error;
972 Py_CLEAR(self->decoder)do { if (self->decoder) { PyObject *_py_tmp = (PyObject *)
(self->decoder); (self->decoder) = ((void*)0); do { if (
_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0)
{ if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 972, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
973 self->decoder = incrementalDecoder;
974 }
975 }
976
977 /* Build the encoder object */
978 res = PyObject_CallMethod_PyObject_CallMethod_SizeT(buffer, "writable", NULL((void*)0));
979 if (res == NULL((void*)0))
980 goto error;
981 r = PyObject_IsTrue(res);
982 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 982, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
983 if (r == -1)
984 goto error;
985 if (r == 1) {
986 PyObject *ci;
987 self->encoder = PyCodec_IncrementalEncoder(
988 encoding, errors);
989 if (self->encoder == NULL((void*)0))
990 goto error;
991 /* Get the normalized named of the codec */
992 ci = _PyCodec_Lookup(encoding);
993 if (ci == NULL((void*)0))
994 goto error;
995 res = PyObject_GetAttrString(ci, "name");
996 Py_DECREF(ci)do { if (_Py_RefTotal-- , --((PyObject*)(ci))->ob_refcnt !=
0) { if (((PyObject*)ci)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 996, (PyObject *)(ci)); } else _Py_Dealloc
((PyObject *)(ci)); } while (0)
;
997 if (res == NULL((void*)0)) {
998 if (PyErr_ExceptionMatches(PyExc_AttributeError))
999 PyErr_Clear();
1000 else
1001 goto error;
1002 }
1003 else if (PyUnicode_Check(res)((((((PyObject*)(res))->ob_type))->tp_flags & ((1L<<
28))) != 0)
) {
1004 encodefuncentry *e = encodefuncs;
1005 while (e->name != NULL((void*)0)) {
1006 if (!PyUnicode_CompareWithASCIIStringPyUnicodeUCS2_CompareWithASCIIString(res, e->name)) {
1007 self->encodefunc = e->encodefunc;
1008 break;
1009 }
1010 e++;
1011 }
1012 }
1013 Py_XDECREF(res)do { if ((res) == ((void*)0)) ; else do { if (_Py_RefTotal-- ,
--((PyObject*)(res))->ob_refcnt != 0) { if (((PyObject*)res
)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1013, (PyObject *)(res)); } else _Py_Dealloc((PyObject *)(res
)); } while (0); } while (0)
;
1014 }
1015
1016 self->buffer = buffer;
1017 Py_INCREF(buffer)( _Py_RefTotal++ , ((PyObject*)(buffer))->ob_refcnt++);
1018
1019 if (Py_TYPE(buffer)(((PyObject*)(buffer))->ob_type) == &PyBufferedReader_Type ||
1020 Py_TYPE(buffer)(((PyObject*)(buffer))->ob_type) == &PyBufferedWriter_Type ||
1021 Py_TYPE(buffer)(((PyObject*)(buffer))->ob_type) == &PyBufferedRandom_Type) {
1022 raw = PyObject_GetAttrString(buffer, "raw");
1023 /* Cache the raw FileIO object to speed up 'closed' checks */
1024 if (raw == NULL((void*)0)) {
1025 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1026 PyErr_Clear();
1027 else
1028 goto error;
1029 }
1030 else if (Py_TYPE(raw)(((PyObject*)(raw))->ob_type) == &PyFileIO_Type)
1031 self->raw = raw;
1032 else
1033 Py_DECREF(raw)do { if (_Py_RefTotal-- , --((PyObject*)(raw))->ob_refcnt !=
0) { if (((PyObject*)raw)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1033, (PyObject *)(raw)); } else _Py_Dealloc
((PyObject *)(raw)); } while (0)
;
1034 }
1035
1036 res = PyObject_CallMethod_PyObject_CallMethod_SizeT(buffer, "seekable", NULL((void*)0));
1037 if (res == NULL((void*)0))
1038 goto error;
1039 self->seekable = self->telling = PyObject_IsTrue(res);
1040 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1040, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
1041
1042 self->encoding_start_of_stream = 0;
1043 if (self->seekable && self->encoder) {
1044 PyObject *cookieObj;
1045 int cmp;
1046
1047 self->encoding_start_of_stream = 1;
1048
1049 cookieObj = PyObject_CallMethodObjArgs(buffer, _PyIO_str_tell, NULL((void*)0));
1050 if (cookieObj == NULL((void*)0))
1051 goto error;
1052
1053 cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_EQ2);
1054 Py_DECREF(cookieObj)do { if (_Py_RefTotal-- , --((PyObject*)(cookieObj))->ob_refcnt
!= 0) { if (((PyObject*)cookieObj)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1054, (PyObject *)(cookieObj)); } else
_Py_Dealloc((PyObject *)(cookieObj)); } while (0)
;
1055 if (cmp < 0) {
1056 goto error;
1057 }
1058
1059 if (cmp == 0) {
1060 self->encoding_start_of_stream = 0;
1061 res = PyObject_CallMethodObjArgs(self->encoder, _PyIO_str_setstate,
1062 _PyIO_zero, NULL((void*)0));
1063 if (res == NULL((void*)0))
1064 goto error;
1065 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1065, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
1066 }
1067 }
1068
1069 self->ok = 1;
1070 return 0;
1071
1072 error:
1073 return -1;
1074}
1075
1076static int
1077_textiowrapper_clear(textio *self)
1078{
1079 if (self->ok && _PyIOBase_finalize((PyObject *) self) < 0)
1080 return -1;
1081 self->ok = 0;
1082 Py_CLEAR(self->buffer)do { if (self->buffer) { PyObject *_py_tmp = (PyObject *)(
self->buffer); (self->buffer) = ((void*)0); do { if (_Py_RefTotal
-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0) { if (((PyObject
*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1082, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
1083 Py_CLEAR(self->encoding)do { if (self->encoding) { PyObject *_py_tmp = (PyObject *
)(self->encoding); (self->encoding) = ((void*)0); do { if
(_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0
) { if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1083, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
1084 Py_CLEAR(self->encoder)do { if (self->encoder) { PyObject *_py_tmp = (PyObject *)
(self->encoder); (self->encoder) = ((void*)0); do { if (
_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0)
{ if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1084, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
1085 Py_CLEAR(self->decoder)do { if (self->decoder) { PyObject *_py_tmp = (PyObject *)
(self->decoder); (self->decoder) = ((void*)0); do { if (
_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0)
{ if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1085, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
1086 Py_CLEAR(self->readnl)do { if (self->readnl) { PyObject *_py_tmp = (PyObject *)(
self->readnl); (self->readnl) = ((void*)0); do { if (_Py_RefTotal
-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0) { if (((PyObject
*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1086, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
1087 Py_CLEAR(self->decoded_chars)do { if (self->decoded_chars) { PyObject *_py_tmp = (PyObject
*)(self->decoded_chars); (self->decoded_chars) = ((void
*)0); do { if (_Py_RefTotal-- , --((PyObject*)(_py_tmp))->
ob_refcnt != 0) { if (((PyObject*)_py_tmp)->ob_refcnt <
0) _Py_NegativeRefcount("./Modules/_io/textio.c", 1087, (PyObject
*)(_py_tmp)); } else _Py_Dealloc((PyObject *)(_py_tmp)); } while
(0); } } while (0)
;
1088 Py_CLEAR(self->pending_bytes)do { if (self->pending_bytes) { PyObject *_py_tmp = (PyObject
*)(self->pending_bytes); (self->pending_bytes) = ((void
*)0); do { if (_Py_RefTotal-- , --((PyObject*)(_py_tmp))->
ob_refcnt != 0) { if (((PyObject*)_py_tmp)->ob_refcnt <
0) _Py_NegativeRefcount("./Modules/_io/textio.c", 1088, (PyObject
*)(_py_tmp)); } else _Py_Dealloc((PyObject *)(_py_tmp)); } while
(0); } } while (0)
;
1089 Py_CLEAR(self->snapshot)do { if (self->snapshot) { PyObject *_py_tmp = (PyObject *
)(self->snapshot); (self->snapshot) = ((void*)0); do { if
(_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0
) { if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1089, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
1090 Py_CLEAR(self->errors)do { if (self->errors) { PyObject *_py_tmp = (PyObject *)(
self->errors); (self->errors) = ((void*)0); do { if (_Py_RefTotal
-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0) { if (((PyObject
*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1090, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
1091 Py_CLEAR(self->raw)do { if (self->raw) { PyObject *_py_tmp = (PyObject *)(self
->raw); (self->raw) = ((void*)0); do { if (_Py_RefTotal
-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0) { if (((PyObject
*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1091, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
1092 return 0;
1093}
1094
1095static void
1096textiowrapper_dealloc(textio *self)
1097{
1098 self->deallocating = 1;
1099 if (_textiowrapper_clear(self) < 0)
1100 return;
1101 _PyObject_GC_UNTRACK(self)do { PyGC_Head *g = ((PyGC_Head *)(self)-1); (__builtin_expect
(!(g->gc.gc_refs != (-2)), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c"
, 1101, "g->gc.gc_refs != _PyGC_REFS_UNTRACKED") : (void)0
); g->gc.gc_refs = (-2); g->gc.gc_prev->gc.gc_next =
g->gc.gc_next; g->gc.gc_next->gc.gc_prev = g->gc
.gc_prev; g->gc.gc_next = ((void*)0); } while (0);
;
1102 if (self->weakreflist != NULL((void*)0))
1103 PyObject_ClearWeakRefs((PyObject *)self);
1104 Py_CLEAR(self->dict)do { if (self->dict) { PyObject *_py_tmp = (PyObject *)(self
->dict); (self->dict) = ((void*)0); do { if (_Py_RefTotal
-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0) { if (((PyObject
*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1104, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
1105 Py_TYPE(self)(((PyObject*)(self))->ob_type)->tp_free((PyObject *)self);
1106}
1107
1108static int
1109textiowrapper_traverse(textio *self, visitproc visit, void *arg)
1110{
1111 Py_VISIT(self->buffer)do { if (self->buffer) { int vret = visit((PyObject *)(self
->buffer), arg); if (vret) return vret; } } while (0)
;
1112 Py_VISIT(self->encoding)do { if (self->encoding) { int vret = visit((PyObject *)(self
->encoding), arg); if (vret) return vret; } } while (0)
;
1113 Py_VISIT(self->encoder)do { if (self->encoder) { int vret = visit((PyObject *)(self
->encoder), arg); if (vret) return vret; } } while (0)
;
1114 Py_VISIT(self->decoder)do { if (self->decoder) { int vret = visit((PyObject *)(self
->decoder), arg); if (vret) return vret; } } while (0)
;
1115 Py_VISIT(self->readnl)do { if (self->readnl) { int vret = visit((PyObject *)(self
->readnl), arg); if (vret) return vret; } } while (0)
;
1116 Py_VISIT(self->decoded_chars)do { if (self->decoded_chars) { int vret = visit((PyObject
*)(self->decoded_chars), arg); if (vret) return vret; } }
while (0)
;
1117 Py_VISIT(self->pending_bytes)do { if (self->pending_bytes) { int vret = visit((PyObject
*)(self->pending_bytes), arg); if (vret) return vret; } }
while (0)
;
1118 Py_VISIT(self->snapshot)do { if (self->snapshot) { int vret = visit((PyObject *)(self
->snapshot), arg); if (vret) return vret; } } while (0)
;
1119 Py_VISIT(self->errors)do { if (self->errors) { int vret = visit((PyObject *)(self
->errors), arg); if (vret) return vret; } } while (0)
;
1120 Py_VISIT(self->raw)do { if (self->raw) { int vret = visit((PyObject *)(self->
raw), arg); if (vret) return vret; } } while (0)
;
1121
1122 Py_VISIT(self->dict)do { if (self->dict) { int vret = visit((PyObject *)(self->
dict), arg); if (vret) return vret; } } while (0)
;
1123 return 0;
1124}
1125
1126static int
1127textiowrapper_clear(textio *self)
1128{
1129 if (_textiowrapper_clear(self) < 0)
1130 return -1;
1131 Py_CLEAR(self->dict)do { if (self->dict) { PyObject *_py_tmp = (PyObject *)(self
->dict); (self->dict) = ((void*)0); do { if (_Py_RefTotal
-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0) { if (((PyObject
*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1131, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
1132 return 0;
1133}
1134
1135static PyObject *
1136textiowrapper_closed_get(textio *self, void *context);
1137
1138/* This macro takes some shortcuts to make the common case faster. */
1139#define CHECK_CLOSED(self)do { int r; PyObject *_res; if ((((PyObject*)(self))->ob_type
) == &PyTextIOWrapper_Type) { if (self->raw != ((void*
)0)) r = _PyFileIO_closed(self->raw); else { _res = textiowrapper_closed_get
(self, ((void*)0)); if (_res == ((void*)0)) return ((void*)0)
; r = PyObject_IsTrue(_res); do { if (_Py_RefTotal-- , --((PyObject
*)(_res))->ob_refcnt != 0) { if (((PyObject*)_res)->ob_refcnt
< 0) _Py_NegativeRefcount("./Modules/_io/textio.c", 1139,
(PyObject *)(_res)); } else _Py_Dealloc((PyObject *)(_res));
} while (0); if (r < 0) return ((void*)0); } if (r > 0
) { PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."
); return ((void*)0); } } else if (_PyIOBase_check_closed((PyObject
*)self, ((PyObject *) &_Py_TrueStruct)) == ((void*)0)) return
((void*)0); } while (0)
\
1140 do { \
1141 int r; \
1142 PyObject *_res; \
1143 if (Py_TYPE(self)(((PyObject*)(self))->ob_type) == &PyTextIOWrapper_Type) { \
1144 if (self->raw != NULL((void*)0)) \
1145 r = _PyFileIO_closed(self->raw); \
1146 else { \
1147 _res = textiowrapper_closed_get(self, NULL((void*)0)); \
1148 if (_res == NULL((void*)0)) \
1149 return NULL((void*)0); \
1150 r = PyObject_IsTrue(_res); \
1151 Py_DECREF(_res)do { if (_Py_RefTotal-- , --((PyObject*)(_res))->ob_refcnt
!= 0) { if (((PyObject*)_res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1151, (PyObject *)(_res)); } else _Py_Dealloc
((PyObject *)(_res)); } while (0)
; \
1152 if (r < 0) \
1153 return NULL((void*)0); \
1154 } \
1155 if (r > 0) { \
1156 PyErr_SetString(PyExc_ValueError, \
1157 "I/O operation on closed file."); \
1158 return NULL((void*)0); \
1159 } \
1160 } \
1161 else if (_PyIOBase_check_closed((PyObject *)self, Py_True((PyObject *) &_Py_TrueStruct)) == NULL((void*)0)) \
1162 return NULL((void*)0); \
1163 } while (0)
1164
1165#define CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
\
1166 if (self->ok <= 0) { \
1167 if (self->detached) { \
1168 PyErr_SetString(PyExc_ValueError, \
1169 "underlying buffer has been detached"); \
1170 } else { \
1171 PyErr_SetString(PyExc_ValueError, \
1172 "I/O operation on uninitialized object"); \
1173 } \
1174 return NULL((void*)0); \
1175 }
1176
1177#define CHECK_INITIALIZED_INT(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return -1; }
\
1178 if (self->ok <= 0) { \
1179 if (self->detached) { \
1180 PyErr_SetString(PyExc_ValueError, \
1181 "underlying buffer has been detached"); \
1182 } else { \
1183 PyErr_SetString(PyExc_ValueError, \
1184 "I/O operation on uninitialized object"); \
1185 } \
1186 return -1; \
1187 }
1188
1189
1190static PyObject *
1191textiowrapper_detach(textio *self)
1192{
1193 PyObject *buffer, *res;
1194 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
1195 res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL((void*)0));
1196 if (res == NULL((void*)0))
1197 return NULL((void*)0);
1198 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1198, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
1199 buffer = self->buffer;
1200 self->buffer = NULL((void*)0);
1201 self->detached = 1;
1202 self->ok = 0;
1203 return buffer;
1204}
1205
1206Py_LOCAL_INLINE(const Py_UNICODE *)static inline const Py_UNICODE *
1207findchar(const Py_UNICODE *s, Py_ssize_t size, Py_UNICODE ch)
1208{
1209 /* like wcschr, but doesn't stop at NULL characters */
1210 while (size-- > 0) {
1211 if (*s == ch)
1212 return s;
1213 s++;
1214 }
1215 return NULL((void*)0);
1216}
1217
1218/* Flush the internal write buffer. This doesn't explicitly flush the
1219 underlying buffered object, though. */
1220static int
1221_textiowrapper_writeflush(textio *self)
1222{
1223 PyObject *pending, *b, *ret;
1224
1225 if (self->pending_bytes == NULL((void*)0))
1226 return 0;
1227
1228 pending = self->pending_bytes;
1229 Py_INCREF(pending)( _Py_RefTotal++ , ((PyObject*)(pending))->ob_refcnt++);
1230 self->pending_bytes_count = 0;
1231 Py_CLEAR(self->pending_bytes)do { if (self->pending_bytes) { PyObject *_py_tmp = (PyObject
*)(self->pending_bytes); (self->pending_bytes) = ((void
*)0); do { if (_Py_RefTotal-- , --((PyObject*)(_py_tmp))->
ob_refcnt != 0) { if (((PyObject*)_py_tmp)->ob_refcnt <
0) _Py_NegativeRefcount("./Modules/_io/textio.c", 1231, (PyObject
*)(_py_tmp)); } else _Py_Dealloc((PyObject *)(_py_tmp)); } while
(0); } } while (0)
;
1232
1233 b = _PyBytes_Join(_PyIO_empty_bytes, pending);
1234 Py_DECREF(pending)do { if (_Py_RefTotal-- , --((PyObject*)(pending))->ob_refcnt
!= 0) { if (((PyObject*)pending)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1234, (PyObject *)(pending)); } else
_Py_Dealloc((PyObject *)(pending)); } while (0)
;
1235 if (b == NULL((void*)0))
1236 return -1;
1237 ret = PyObject_CallMethodObjArgs(self->buffer,
1238 _PyIO_str_write, b, NULL((void*)0));
1239 Py_DECREF(b)do { if (_Py_RefTotal-- , --((PyObject*)(b))->ob_refcnt !=
0) { if (((PyObject*)b)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1239, (PyObject *)(b)); } else _Py_Dealloc
((PyObject *)(b)); } while (0)
;
1240 if (ret == NULL((void*)0))
1241 return -1;
1242 Py_DECREF(ret)do { if (_Py_RefTotal-- , --((PyObject*)(ret))->ob_refcnt !=
0) { if (((PyObject*)ret)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1242, (PyObject *)(ret)); } else _Py_Dealloc
((PyObject *)(ret)); } while (0)
;
1243 return 0;
1244}
1245
1246static PyObject *
1247textiowrapper_write(textio *self, PyObject *args)
1248{
1249 PyObject *ret;
1250 PyObject *text; /* owned reference */
1251 PyObject *b;
1252 Py_ssize_t textlen;
1253 int haslf = 0;
1254 int needflush = 0;
1255
1256 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
1257
1258 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "U:write", &text)) {
1259 return NULL((void*)0);
1260 }
1261
1262 CHECK_CLOSED(self)do { int r; PyObject *_res; if ((((PyObject*)(self))->ob_type
) == &PyTextIOWrapper_Type) { if (self->raw != ((void*
)0)) r = _PyFileIO_closed(self->raw); else { _res = textiowrapper_closed_get
(self, ((void*)0)); if (_res == ((void*)0)) return ((void*)0)
; r = PyObject_IsTrue(_res); do { if (_Py_RefTotal-- , --((PyObject
*)(_res))->ob_refcnt != 0) { if (((PyObject*)_res)->ob_refcnt
< 0) _Py_NegativeRefcount("./Modules/_io/textio.c", 1262,
(PyObject *)(_res)); } else _Py_Dealloc((PyObject *)(_res));
} while (0); if (r < 0) return ((void*)0); } if (r > 0
) { PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."
); return ((void*)0); } } else if (_PyIOBase_check_closed((PyObject
*)self, ((PyObject *) &_Py_TrueStruct)) == ((void*)0)) return
((void*)0); } while (0)
;
1263
1264 if (self->encoder == NULL((void*)0))
1265 return _unsupported("not writable");
1266
1267 Py_INCREF(text)( _Py_RefTotal++ , ((PyObject*)(text))->ob_refcnt++);
1268
1269 textlen = PyUnicode_GetSizePyUnicodeUCS2_GetSize(text);
1270
1271 if ((self->writetranslate && self->writenl != NULL((void*)0)) || self->line_buffering)
1272 if (findchar(PyUnicode_AS_UNICODE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 1272, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->str))
,
1273 PyUnicode_GET_SIZE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 1273, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->length))
, '\n'))
1274 haslf = 1;
1275
1276 if (haslf && self->writetranslate && self->writenl != NULL((void*)0)) {
1277 PyObject *newtext = PyObject_CallMethod_PyObject_CallMethod_SizeT(
1278 text, "replace", "ss", "\n", self->writenl);
1279 Py_DECREF(text)do { if (_Py_RefTotal-- , --((PyObject*)(text))->ob_refcnt
!= 0) { if (((PyObject*)text)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1279, (PyObject *)(text)); } else _Py_Dealloc
((PyObject *)(text)); } while (0)
;
1280 if (newtext == NULL((void*)0))
1281 return NULL((void*)0);
1282 text = newtext;
1283 }
1284
1285 if (self->line_buffering &&
1286 (haslf ||
1287 findchar(PyUnicode_AS_UNICODE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 1287, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->str))
,
1288 PyUnicode_GET_SIZE(text)((__builtin_expect(!(((((((PyObject*)(text))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 1288, "PyUnicode_Check(text)") : (
void)0),(((PyUnicodeObject *)(text))->length))
, '\r')))
1289 needflush = 1;
1290
1291 /* XXX What if we were just reading? */
1292 if (self->encodefunc != NULL((void*)0)) {
1293 b = (*self->encodefunc)((PyObject *) self, text);
1294 self->encoding_start_of_stream = 0;
1295 }
1296 else
1297 b = PyObject_CallMethodObjArgs(self->encoder,
1298 _PyIO_str_encode, text, NULL((void*)0));
1299 Py_DECREF(text)do { if (_Py_RefTotal-- , --((PyObject*)(text))->ob_refcnt
!= 0) { if (((PyObject*)text)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1299, (PyObject *)(text)); } else _Py_Dealloc
((PyObject *)(text)); } while (0)
;
1300 if (b == NULL((void*)0))
1301 return NULL((void*)0);
1302
1303 if (self->pending_bytes == NULL((void*)0)) {
1304 self->pending_bytes = PyList_New(0);
1305 if (self->pending_bytes == NULL((void*)0)) {
1306 Py_DECREF(b)do { if (_Py_RefTotal-- , --((PyObject*)(b))->ob_refcnt !=
0) { if (((PyObject*)b)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1306, (PyObject *)(b)); } else _Py_Dealloc
((PyObject *)(b)); } while (0)
;
1307 return NULL((void*)0);
1308 }
1309 self->pending_bytes_count = 0;
1310 }
1311 if (PyList_Append(self->pending_bytes, b) < 0) {
1312 Py_DECREF(b)do { if (_Py_RefTotal-- , --((PyObject*)(b))->ob_refcnt !=
0) { if (((PyObject*)b)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1312, (PyObject *)(b)); } else _Py_Dealloc
((PyObject *)(b)); } while (0)
;
1313 return NULL((void*)0);
1314 }
1315 self->pending_bytes_count += PyBytes_GET_SIZE(b)((__builtin_expect(!(((((((PyObject*)(b))->ob_type))->tp_flags
& ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c"
, 1315, "PyBytes_Check(b)") : (void)0),(((PyVarObject*)(b))->
ob_size))
;
1316 Py_DECREF(b)do { if (_Py_RefTotal-- , --((PyObject*)(b))->ob_refcnt !=
0) { if (((PyObject*)b)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1316, (PyObject *)(b)); } else _Py_Dealloc
((PyObject *)(b)); } while (0)
;
1317 if (self->pending_bytes_count > self->chunk_size || needflush) {
1318 if (_textiowrapper_writeflush(self) < 0)
1319 return NULL((void*)0);
1320 }
1321
1322 if (needflush) {
1323 ret = PyObject_CallMethodObjArgs(self->buffer, _PyIO_str_flush, NULL((void*)0));
1324 if (ret == NULL((void*)0))
1325 return NULL((void*)0);
1326 Py_DECREF(ret)do { if (_Py_RefTotal-- , --((PyObject*)(ret))->ob_refcnt !=
0) { if (((PyObject*)ret)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1326, (PyObject *)(ret)); } else _Py_Dealloc
((PyObject *)(ret)); } while (0)
;
1327 }
1328
1329 Py_CLEAR(self->snapshot)do { if (self->snapshot) { PyObject *_py_tmp = (PyObject *
)(self->snapshot); (self->snapshot) = ((void*)0); do { if
(_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0
) { if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1329, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
1330
1331 if (self->decoder) {
1332 ret = PyObject_CallMethod_PyObject_CallMethod_SizeT(self->decoder, "reset", NULL((void*)0));
1333 if (ret == NULL((void*)0))
1334 return NULL((void*)0);
1335 Py_DECREF(ret)do { if (_Py_RefTotal-- , --((PyObject*)(ret))->ob_refcnt !=
0) { if (((PyObject*)ret)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1335, (PyObject *)(ret)); } else _Py_Dealloc
((PyObject *)(ret)); } while (0)
;
1336 }
1337
1338 return PyLong_FromSsize_t(textlen);
1339}
1340
1341/* Steal a reference to chars and store it in the decoded_char buffer;
1342 */
1343static void
1344textiowrapper_set_decoded_chars(textio *self, PyObject *chars)
1345{
1346 Py_CLEAR(self->decoded_chars)do { if (self->decoded_chars) { PyObject *_py_tmp = (PyObject
*)(self->decoded_chars); (self->decoded_chars) = ((void
*)0); do { if (_Py_RefTotal-- , --((PyObject*)(_py_tmp))->
ob_refcnt != 0) { if (((PyObject*)_py_tmp)->ob_refcnt <
0) _Py_NegativeRefcount("./Modules/_io/textio.c", 1346, (PyObject
*)(_py_tmp)); } else _Py_Dealloc((PyObject *)(_py_tmp)); } while
(0); } } while (0)
;
1347 self->decoded_chars = chars;
1348 self->decoded_chars_used = 0;
1349}
1350
1351static PyObject *
1352textiowrapper_get_decoded_chars(textio *self, Py_ssize_t n)
1353{
1354 PyObject *chars;
1355 Py_ssize_t avail;
1356
1357 if (self->decoded_chars == NULL((void*)0))
1358 return PyUnicode_FromStringAndSizePyUnicodeUCS2_FromStringAndSize(NULL((void*)0), 0);
1359
1360 avail = (PyUnicode_GET_SIZE(self->decoded_chars)((__builtin_expect(!(((((((PyObject*)(self->decoded_chars)
)->ob_type))->tp_flags & ((1L<<28))) != 0)), 0
) ? __assert_rtn(__func__, "./Modules/_io/textio.c", 1360, "PyUnicode_Check(self->decoded_chars)"
) : (void)0),(((PyUnicodeObject *)(self->decoded_chars))->
length))
1361 - self->decoded_chars_used);
1362
1363 assert(avail >= 0)(__builtin_expect(!(avail >= 0), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 1363, "avail >= 0") : (void)0)
;
1364
1365 if (n < 0 || n > avail)
1366 n = avail;
1367
1368 if (self->decoded_chars_used > 0 || n < avail) {
1369 chars = PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(
1370 PyUnicode_AS_UNICODE(self->decoded_chars)((__builtin_expect(!(((((((PyObject*)(self->decoded_chars)
)->ob_type))->tp_flags & ((1L<<28))) != 0)), 0
) ? __assert_rtn(__func__, "./Modules/_io/textio.c", 1370, "PyUnicode_Check(self->decoded_chars)"
) : (void)0),(((PyUnicodeObject *)(self->decoded_chars))->
str))
1371 + self->decoded_chars_used, n);
1372 if (chars == NULL((void*)0))
1373 return NULL((void*)0);
1374 }
1375 else {
1376 chars = self->decoded_chars;
1377 Py_INCREF(chars)( _Py_RefTotal++ , ((PyObject*)(chars))->ob_refcnt++);
1378 }
1379
1380 self->decoded_chars_used += n;
1381 return chars;
1382}
1383
1384/* Read and decode the next chunk of data from the BufferedReader.
1385 */
1386static int
1387textiowrapper_read_chunk(textio *self)
1388{
1389 PyObject *dec_buffer = NULL((void*)0);
1
Variable 'dec_buffer' initialized to a null pointer value
1390 PyObject *dec_flags = NULL((void*)0);
1391 PyObject *input_chunk = NULL((void*)0);
1392 PyObject *decoded_chars, *chunk_size;
1393 int eof;
1394
1395 /* The return value is True unless EOF was reached. The decoded string is
1396 * placed in self._decoded_chars (replacing its previous value). The
1397 * entire input chunk is sent to the decoder, though some of it may remain
1398 * buffered in the decoder, yet to be converted.
1399 */
1400
1401 if (self->decoder == NULL((void*)0)) {
2
Taking false branch
1402 _unsupported("not readable");
1403 return -1;
1404 }
1405
1406 if (self->telling) {
3
Taking false branch
1407 /* To prepare for tell(), we need to snapshot a point in the file
1408 * where the decoder's input buffer is empty.
1409 */
1410
1411 PyObject *state = PyObject_CallMethodObjArgs(self->decoder,
1412 _PyIO_str_getstate, NULL((void*)0));
1413 if (state == NULL((void*)0))
1414 return -1;
1415 /* Given this, we know there was a valid snapshot point
1416 * len(dec_buffer) bytes ago with decoder state (b'', dec_flags).
1417 */
1418 if (PyArg_Parse_PyArg_Parse_SizeT(state, "(OO)", &dec_buffer, &dec_flags) < 0) {
1419 Py_DECREF(state)do { if (_Py_RefTotal-- , --((PyObject*)(state))->ob_refcnt
!= 0) { if (((PyObject*)state)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1419, (PyObject *)(state)); } else
_Py_Dealloc((PyObject *)(state)); } while (0)
;
1420 return -1;
1421 }
1422 Py_INCREF(dec_buffer)( _Py_RefTotal++ , ((PyObject*)(dec_buffer))->ob_refcnt++);
1423 Py_INCREF(dec_flags)( _Py_RefTotal++ , ((PyObject*)(dec_flags))->ob_refcnt++);
1424 Py_DECREF(state)do { if (_Py_RefTotal-- , --((PyObject*)(state))->ob_refcnt
!= 0) { if (((PyObject*)state)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1424, (PyObject *)(state)); } else
_Py_Dealloc((PyObject *)(state)); } while (0)
;
1425 }
1426
1427 /* Read a chunk, decode it, and put the result in self._decoded_chars. */
1428 chunk_size = PyLong_FromSsize_t(self->chunk_size);
1429 if (chunk_size == NULL((void*)0))
4
Taking false branch
1430 goto fail;
1431 input_chunk = PyObject_CallMethodObjArgs(self->buffer,
1432 _PyIO_str_read1, chunk_size, NULL((void*)0));
1433 Py_DECREF(chunk_size)do { if (_Py_RefTotal-- , --((PyObject*)(chunk_size))->ob_refcnt
!= 0) { if (((PyObject*)chunk_size)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1433, (PyObject *)(chunk_size)); }
else _Py_Dealloc((PyObject *)(chunk_size)); } while (0)
;
1434 if (input_chunk == NULL((void*)0))
5
Taking false branch
1435 goto fail;
1436 assert(PyBytes_Check(input_chunk))(__builtin_expect(!(((((((PyObject*)(input_chunk))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 1436, "PyBytes_Check(input_chunk)"
) : (void)0)
;
1437
1438 eof = (PyBytes_Size(input_chunk) == 0);
1439
1440 if (Py_TYPE(self->decoder)(((PyObject*)(self->decoder))->ob_type) == &PyIncrementalNewlineDecoder_Type) {
6
Taking true branch
1441 decoded_chars = _PyIncrementalNewlineDecoder_decode(
1442 self->decoder, input_chunk, eof);
1443 }
1444 else {
1445 decoded_chars = PyObject_CallMethodObjArgs(self->decoder,
1446 _PyIO_str_decode, input_chunk, eof ? Py_True((PyObject *) &_Py_TrueStruct) : Py_False((PyObject *) &_Py_FalseStruct), NULL((void*)0));
1447 }
1448
1449 /* TODO sanity check: isinstance(decoded_chars, unicode) */
1450 if (decoded_chars == NULL((void*)0))
7
Taking false branch
1451 goto fail;
1452 textiowrapper_set_decoded_chars(self, decoded_chars);
1453 if (PyUnicode_GET_SIZE(decoded_chars)((__builtin_expect(!(((((((PyObject*)(decoded_chars))->ob_type
))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 1453, "PyUnicode_Check(decoded_chars)"
) : (void)0),(((PyUnicodeObject *)(decoded_chars))->length
))
> 0)
8
Taking false branch
1454 eof = 0;
1455
1456 if (self->telling) {
9
Taking true branch
1457 /* At the snapshot point, len(dec_buffer) bytes before the read, the
1458 * next input to be decoded is dec_buffer + input_chunk.
1459 */
1460 PyObject *next_input = PyNumber_Add(dec_buffer, input_chunk);
1461 if (next_input == NULL((void*)0))
10
Taking false branch
1462 goto fail;
1463 assert (PyBytes_Check(next_input))(__builtin_expect(!(((((((PyObject*)(next_input))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 1463, "PyBytes_Check(next_input)"
) : (void)0)
;
1464 Py_DECREF(dec_buffer)do { if (_Py_RefTotal-- , --((PyObject*)(dec_buffer))->ob_refcnt
!= 0) { if (((PyObject*)dec_buffer)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1464, (PyObject *)(dec_buffer)); }
else _Py_Dealloc((PyObject *)(dec_buffer)); } while (0)
;
11
Within the expansion of the macro 'Py_DECREF':
a
Access to field 'ob_refcnt' results in a dereference of a null pointer (loaded from variable 'dec_buffer')
1465 Py_CLEAR(self->snapshot)do { if (self->snapshot) { PyObject *_py_tmp = (PyObject *
)(self->snapshot); (self->snapshot) = ((void*)0); do { if
(_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0
) { if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1465, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
1466 self->snapshot = Py_BuildValue_Py_BuildValue_SizeT("NN", dec_flags, next_input);
1467 }
1468 Py_DECREF(input_chunk)do { if (_Py_RefTotal-- , --((PyObject*)(input_chunk))->ob_refcnt
!= 0) { if (((PyObject*)input_chunk)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1468, (PyObject *)(input_chunk)); }
else _Py_Dealloc((PyObject *)(input_chunk)); } while (0)
;
1469
1470 return (eof == 0);
1471
1472 fail:
1473 Py_XDECREF(dec_buffer)do { if ((dec_buffer) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(dec_buffer))->ob_refcnt != 0) { if (((
PyObject*)dec_buffer)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1473, (PyObject *)(dec_buffer)); }
else _Py_Dealloc((PyObject *)(dec_buffer)); } while (0); } while
(0)
;
1474 Py_XDECREF(dec_flags)do { if ((dec_flags) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(dec_flags))->ob_refcnt != 0) { if (((PyObject
*)dec_flags)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1474, (PyObject *)(dec_flags)); } else _Py_Dealloc((PyObject
*)(dec_flags)); } while (0); } while (0)
;
1475 Py_XDECREF(input_chunk)do { if ((input_chunk) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(input_chunk))->ob_refcnt != 0) { if ((
(PyObject*)input_chunk)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1475, (PyObject *)(input_chunk)); }
else _Py_Dealloc((PyObject *)(input_chunk)); } while (0); } while
(0)
;
1476 return -1;
1477}
1478
1479static PyObject *
1480textiowrapper_read(textio *self, PyObject *args)
1481{
1482 Py_ssize_t n = -1;
1483 PyObject *result = NULL((void*)0), *chunks = NULL((void*)0);
1484
1485 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
1486
1487 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|O&:read", &_PyIO_ConvertSsize_t, &n))
1488 return NULL((void*)0);
1489
1490 CHECK_CLOSED(self)do { int r; PyObject *_res; if ((((PyObject*)(self))->ob_type
) == &PyTextIOWrapper_Type) { if (self->raw != ((void*
)0)) r = _PyFileIO_closed(self->raw); else { _res = textiowrapper_closed_get
(self, ((void*)0)); if (_res == ((void*)0)) return ((void*)0)
; r = PyObject_IsTrue(_res); do { if (_Py_RefTotal-- , --((PyObject
*)(_res))->ob_refcnt != 0) { if (((PyObject*)_res)->ob_refcnt
< 0) _Py_NegativeRefcount("./Modules/_io/textio.c", 1490,
(PyObject *)(_res)); } else _Py_Dealloc((PyObject *)(_res));
} while (0); if (r < 0) return ((void*)0); } if (r > 0
) { PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."
); return ((void*)0); } } else if (_PyIOBase_check_closed((PyObject
*)self, ((PyObject *) &_Py_TrueStruct)) == ((void*)0)) return
((void*)0); } while (0)
;
1491
1492 if (self->decoder == NULL((void*)0))
1493 return _unsupported("not readable");
1494
1495 if (_textiowrapper_writeflush(self) < 0)
1496 return NULL((void*)0);
1497
1498 if (n < 0) {
1499 /* Read everything */
1500 PyObject *bytes = PyObject_CallMethod_PyObject_CallMethod_SizeT(self->buffer, "read", NULL((void*)0));
1501 PyObject *decoded;
1502 if (bytes == NULL((void*)0))
1503 goto fail;
1504 decoded = PyObject_CallMethodObjArgs(self->decoder, _PyIO_str_decode,
1505 bytes, Py_True((PyObject *) &_Py_TrueStruct), NULL((void*)0));
1506 Py_DECREF(bytes)do { if (_Py_RefTotal-- , --((PyObject*)(bytes))->ob_refcnt
!= 0) { if (((PyObject*)bytes)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1506, (PyObject *)(bytes)); } else
_Py_Dealloc((PyObject *)(bytes)); } while (0)
;
1507 if (decoded == NULL((void*)0))
1508 goto fail;
1509
1510 result = textiowrapper_get_decoded_chars(self, -1);
1511
1512 if (result == NULL((void*)0)) {
1513 Py_DECREF(decoded)do { if (_Py_RefTotal-- , --((PyObject*)(decoded))->ob_refcnt
!= 0) { if (((PyObject*)decoded)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1513, (PyObject *)(decoded)); } else
_Py_Dealloc((PyObject *)(decoded)); } while (0)
;
1514 return NULL((void*)0);
1515 }
1516
1517 PyUnicode_AppendAndDelPyUnicodeUCS2_AppendAndDel(&result, decoded);
1518 if (result == NULL((void*)0))
1519 goto fail;
1520
1521 Py_CLEAR(self->snapshot)do { if (self->snapshot) { PyObject *_py_tmp = (PyObject *
)(self->snapshot); (self->snapshot) = ((void*)0); do { if
(_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0
) { if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1521, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
1522 return result;
1523 }
1524 else {
1525 int res = 1;
1526 Py_ssize_t remaining = n;
1527
1528 result = textiowrapper_get_decoded_chars(self, n);
1529 if (result == NULL((void*)0))
1530 goto fail;
1531 remaining -= PyUnicode_GET_SIZE(result)((__builtin_expect(!(((((((PyObject*)(result))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 1531, "PyUnicode_Check(result)") :
(void)0),(((PyUnicodeObject *)(result))->length))
;
1532
1533 /* Keep reading chunks until we have n characters to return */
1534 while (remaining > 0) {
1535 res = textiowrapper_read_chunk(self);
1536 if (res < 0)
1537 goto fail;
1538 if (res == 0) /* EOF */
1539 break;
1540 if (chunks == NULL((void*)0)) {
1541 chunks = PyList_New(0);
1542 if (chunks == NULL((void*)0))
1543 goto fail;
1544 }
1545 if (PyList_Append(chunks, result) < 0)
1546 goto fail;
1547 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1547, (PyObject *)(result)); } else
_Py_Dealloc((PyObject *)(result)); } while (0)
;
1548 result = textiowrapper_get_decoded_chars(self, remaining);
1549 if (result == NULL((void*)0))
1550 goto fail;
1551 remaining -= PyUnicode_GET_SIZE(result)((__builtin_expect(!(((((((PyObject*)(result))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 1551, "PyUnicode_Check(result)") :
(void)0),(((PyUnicodeObject *)(result))->length))
;
1552 }
1553 if (chunks != NULL((void*)0)) {
1554 if (result != NULL((void*)0) && PyList_Append(chunks, result) < 0)
1555 goto fail;
1556 Py_CLEAR(result)do { if (result) { PyObject *_py_tmp = (PyObject *)(result); (
result) = ((void*)0); do { if (_Py_RefTotal-- , --((PyObject*
)(_py_tmp))->ob_refcnt != 0) { if (((PyObject*)_py_tmp)->
ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1556, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
1557 result = PyUnicode_JoinPyUnicodeUCS2_Join(_PyIO_empty_str, chunks);
1558 if (result == NULL((void*)0))
1559 goto fail;
1560 Py_CLEAR(chunks)do { if (chunks) { PyObject *_py_tmp = (PyObject *)(chunks); (
chunks) = ((void*)0); do { if (_Py_RefTotal-- , --((PyObject*
)(_py_tmp))->ob_refcnt != 0) { if (((PyObject*)_py_tmp)->
ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1560, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
1561 }
1562 return result;
1563 }
1564 fail:
1565 Py_XDECREF(result)do { if ((result) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(result))->ob_refcnt != 0) { if (((PyObject
*)result)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1565, (PyObject *)(result)); } else _Py_Dealloc((PyObject *
)(result)); } while (0); } while (0)
;
1566 Py_XDECREF(chunks)do { if ((chunks) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(chunks))->ob_refcnt != 0) { if (((PyObject
*)chunks)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1566, (PyObject *)(chunks)); } else _Py_Dealloc((PyObject *
)(chunks)); } while (0); } while (0)
;
1567 return NULL((void*)0);
1568}
1569
1570
1571/* NOTE: `end` must point to the real end of the Py_UNICODE storage,
1572 that is to the NUL character. Otherwise the function will produce
1573 incorrect results. */
1574static Py_UNICODE *
1575find_control_char(Py_UNICODE *start, Py_UNICODE *end, Py_UNICODE ch)
1576{
1577 Py_UNICODE *s = start;
1578 for (;;) {
1579 while (*s > ch)
1580 s++;
1581 if (*s == ch)
1582 return s;
1583 if (s == end)
1584 return NULL((void*)0);
1585 s++;
1586 }
1587}
1588
1589Py_ssize_t
1590_PyIO_find_line_ending(
1591 int translated, int universal, PyObject *readnl,
1592 Py_UNICODE *start, Py_UNICODE *end, Py_ssize_t *consumed)
1593{
1594 Py_ssize_t len = end - start;
1595
1596 if (translated) {
1597 /* Newlines are already translated, only search for \n */
1598 Py_UNICODE *pos = find_control_char(start, end, '\n');
1599 if (pos != NULL((void*)0))
1600 return pos - start + 1;
1601 else {
1602 *consumed = len;
1603 return -1;
1604 }
1605 }
1606 else if (universal) {
1607 /* Universal newline search. Find any of \r, \r\n, \n
1608 * The decoder ensures that \r\n are not split in two pieces
1609 */
1610 Py_UNICODE *s = start;
1611 for (;;) {
1612 Py_UNICODE ch;
1613 /* Fast path for non-control chars. The loop always ends
1614 since the Py_UNICODE storage is NUL-terminated. */
1615 while (*s > '\r')
1616 s++;
1617 if (s >= end) {
1618 *consumed = len;
1619 return -1;
1620 }
1621 ch = *s++;
1622 if (ch == '\n')
1623 return s - start;
1624 if (ch == '\r') {
1625 if (*s == '\n')
1626 return s - start + 1;
1627 else
1628 return s - start;
1629 }
1630 }
1631 }
1632 else {
1633 /* Non-universal mode. */
1634 Py_ssize_t readnl_len = PyUnicode_GET_SIZE(readnl)((__builtin_expect(!(((((((PyObject*)(readnl))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 1634, "PyUnicode_Check(readnl)") :
(void)0),(((PyUnicodeObject *)(readnl))->length))
;
1635 Py_UNICODE *nl = PyUnicode_AS_UNICODE(readnl)((__builtin_expect(!(((((((PyObject*)(readnl))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 1635, "PyUnicode_Check(readnl)") :
(void)0),(((PyUnicodeObject *)(readnl))->str))
;
1636 if (readnl_len == 1) {
1637 Py_UNICODE *pos = find_control_char(start, end, nl[0]);
1638 if (pos != NULL((void*)0))
1639 return pos - start + 1;
1640 *consumed = len;
1641 return -1;
1642 }
1643 else {
1644 Py_UNICODE *s = start;
1645 Py_UNICODE *e = end - readnl_len + 1;
1646 Py_UNICODE *pos;
1647 if (e < s)
1648 e = s;
1649 while (s < e) {
1650 Py_ssize_t i;
1651 Py_UNICODE *pos = find_control_char(s, end, nl[0]);
1652 if (pos == NULL((void*)0) || pos >= e)
1653 break;
1654 for (i = 1; i < readnl_len; i++) {
1655 if (pos[i] != nl[i])
1656 break;
1657 }
1658 if (i == readnl_len)
1659 return pos - start + readnl_len;
1660 s = pos + 1;
1661 }
1662 pos = find_control_char(e, end, nl[0]);
1663 if (pos == NULL((void*)0))
1664 *consumed = len;
1665 else
1666 *consumed = pos - start;
1667 return -1;
1668 }
1669 }
1670}
1671
1672static PyObject *
1673_textiowrapper_readline(textio *self, Py_ssize_t limit)
1674{
1675 PyObject *line = NULL((void*)0), *chunks = NULL((void*)0), *remaining = NULL((void*)0);
1676 Py_ssize_t start, endpos, chunked, offset_to_buffer;
1677 int res;
1678
1679 CHECK_CLOSED(self)do { int r; PyObject *_res; if ((((PyObject*)(self))->ob_type
) == &PyTextIOWrapper_Type) { if (self->raw != ((void*
)0)) r = _PyFileIO_closed(self->raw); else { _res = textiowrapper_closed_get
(self, ((void*)0)); if (_res == ((void*)0)) return ((void*)0)
; r = PyObject_IsTrue(_res); do { if (_Py_RefTotal-- , --((PyObject
*)(_res))->ob_refcnt != 0) { if (((PyObject*)_res)->ob_refcnt
< 0) _Py_NegativeRefcount("./Modules/_io/textio.c", 1679,
(PyObject *)(_res)); } else _Py_Dealloc((PyObject *)(_res));
} while (0); if (r < 0) return ((void*)0); } if (r > 0
) { PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."
); return ((void*)0); } } else if (_PyIOBase_check_closed((PyObject
*)self, ((PyObject *) &_Py_TrueStruct)) == ((void*)0)) return
((void*)0); } while (0)
;
1680
1681 if (_textiowrapper_writeflush(self) < 0)
1682 return NULL((void*)0);
1683
1684 chunked = 0;
1685
1686 while (1) {
1687 Py_UNICODE *ptr;
1688 Py_ssize_t line_len;
1689 Py_ssize_t consumed = 0;
1690
1691 /* First, get some data if necessary */
1692 res = 1;
1693 while (!self->decoded_chars ||
1694 !PyUnicode_GET_SIZE(self->decoded_chars)((__builtin_expect(!(((((((PyObject*)(self->decoded_chars)
)->ob_type))->tp_flags & ((1L<<28))) != 0)), 0
) ? __assert_rtn(__func__, "./Modules/_io/textio.c", 1694, "PyUnicode_Check(self->decoded_chars)"
) : (void)0),(((PyUnicodeObject *)(self->decoded_chars))->
length))
) {
1695 res = textiowrapper_read_chunk(self);
1696 if (res < 0)
1697 goto error;
1698 if (res == 0)
1699 break;
1700 }
1701 if (res == 0) {
1702 /* end of file */
1703 textiowrapper_set_decoded_chars(self, NULL((void*)0));
1704 Py_CLEAR(self->snapshot)do { if (self->snapshot) { PyObject *_py_tmp = (PyObject *
)(self->snapshot); (self->snapshot) = ((void*)0); do { if
(_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0
) { if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1704, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
1705 start = endpos = offset_to_buffer = 0;
1706 break;
1707 }
1708
1709 if (remaining == NULL((void*)0)) {
1710 line = self->decoded_chars;
1711 start = self->decoded_chars_used;
1712 offset_to_buffer = 0;
1713 Py_INCREF(line)( _Py_RefTotal++ , ((PyObject*)(line))->ob_refcnt++);
1714 }
1715 else {
1716 assert(self->decoded_chars_used == 0)(__builtin_expect(!(self->decoded_chars_used == 0), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 1716, "self->decoded_chars_used == 0"
) : (void)0)
;
1717 line = PyUnicode_ConcatPyUnicodeUCS2_Concat(remaining, self->decoded_chars);
1718 start = 0;
1719 offset_to_buffer = PyUnicode_GET_SIZE(remaining)((__builtin_expect(!(((((((PyObject*)(remaining))->ob_type
))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 1719, "PyUnicode_Check(remaining)"
) : (void)0),(((PyUnicodeObject *)(remaining))->length))
;
1720 Py_CLEAR(remaining)do { if (remaining) { PyObject *_py_tmp = (PyObject *)(remaining
); (remaining) = ((void*)0); do { if (_Py_RefTotal-- , --((PyObject
*)(_py_tmp))->ob_refcnt != 0) { if (((PyObject*)_py_tmp)->
ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1720, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
1721 if (line == NULL((void*)0))
1722 goto error;
1723 }
1724
1725 ptr = PyUnicode_AS_UNICODE(line)((__builtin_expect(!(((((((PyObject*)(line))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 1725, "PyUnicode_Check(line)") : (
void)0),(((PyUnicodeObject *)(line))->str))
;
1726 line_len = PyUnicode_GET_SIZE(line)((__builtin_expect(!(((((((PyObject*)(line))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 1726, "PyUnicode_Check(line)") : (
void)0),(((PyUnicodeObject *)(line))->length))
;
1727
1728 endpos = _PyIO_find_line_ending(
1729 self->readtranslate, self->readuniversal, self->readnl,
1730 ptr + start, ptr + line_len, &consumed);
1731 if (endpos >= 0) {
1732 endpos += start;
1733 if (limit >= 0 && (endpos - start) + chunked >= limit)
1734 endpos = start + limit - chunked;
1735 break;
1736 }
1737
1738 /* We can put aside up to `endpos` */
1739 endpos = consumed + start;
1740 if (limit >= 0 && (endpos - start) + chunked >= limit) {
1741 /* Didn't find line ending, but reached length limit */
1742 endpos = start + limit - chunked;
1743 break;
1744 }
1745
1746 if (endpos > start) {
1747 /* No line ending seen yet - put aside current data */
1748 PyObject *s;
1749 if (chunks == NULL((void*)0)) {
1750 chunks = PyList_New(0);
1751 if (chunks == NULL((void*)0))
1752 goto error;
1753 }
1754 s = PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(ptr + start, endpos - start);
1755 if (s == NULL((void*)0))
1756 goto error;
1757 if (PyList_Append(chunks, s) < 0) {
1758 Py_DECREF(s)do { if (_Py_RefTotal-- , --((PyObject*)(s))->ob_refcnt !=
0) { if (((PyObject*)s)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1758, (PyObject *)(s)); } else _Py_Dealloc
((PyObject *)(s)); } while (0)
;
1759 goto error;
1760 }
1761 chunked += PyUnicode_GET_SIZE(s)((__builtin_expect(!(((((((PyObject*)(s))->ob_type))->tp_flags
& ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c"
, 1761, "PyUnicode_Check(s)") : (void)0),(((PyUnicodeObject *
)(s))->length))
;
1762 Py_DECREF(s)do { if (_Py_RefTotal-- , --((PyObject*)(s))->ob_refcnt !=
0) { if (((PyObject*)s)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1762, (PyObject *)(s)); } else _Py_Dealloc
((PyObject *)(s)); } while (0)
;
1763 }
1764 /* There may be some remaining bytes we'll have to prepend to the
1765 next chunk of data */
1766 if (endpos < line_len) {
1767 remaining = PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(
1768 ptr + endpos, line_len - endpos);
1769 if (remaining == NULL((void*)0))
1770 goto error;
1771 }
1772 Py_CLEAR(line)do { if (line) { PyObject *_py_tmp = (PyObject *)(line); (line
) = ((void*)0); do { if (_Py_RefTotal-- , --((PyObject*)(_py_tmp
))->ob_refcnt != 0) { if (((PyObject*)_py_tmp)->ob_refcnt
< 0) _Py_NegativeRefcount("./Modules/_io/textio.c", 1772,
(PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *)(_py_tmp
)); } while (0); } } while (0)
;
1773 /* We have consumed the buffer */
1774 textiowrapper_set_decoded_chars(self, NULL((void*)0));
1775 }
1776
1777 if (line != NULL((void*)0)) {
1778 /* Our line ends in the current buffer */
1779 self->decoded_chars_used = endpos - offset_to_buffer;
1780 if (start > 0 || endpos < PyUnicode_GET_SIZE(line)((__builtin_expect(!(((((((PyObject*)(line))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 1780, "PyUnicode_Check(line)") : (
void)0),(((PyUnicodeObject *)(line))->length))
) {
1781 if (start == 0 && Py_REFCNT(line)(((PyObject*)(line))->ob_refcnt) == 1) {
1782 if (PyUnicode_ResizePyUnicodeUCS2_Resize(&line, endpos) < 0)
1783 goto error;
1784 }
1785 else {
1786 PyObject *s = PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(
1787 PyUnicode_AS_UNICODE(line)((__builtin_expect(!(((((((PyObject*)(line))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 1787, "PyUnicode_Check(line)") : (
void)0),(((PyUnicodeObject *)(line))->str))
+ start, endpos - start);
1788 Py_CLEAR(line)do { if (line) { PyObject *_py_tmp = (PyObject *)(line); (line
) = ((void*)0); do { if (_Py_RefTotal-- , --((PyObject*)(_py_tmp
))->ob_refcnt != 0) { if (((PyObject*)_py_tmp)->ob_refcnt
< 0) _Py_NegativeRefcount("./Modules/_io/textio.c", 1788,
(PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *)(_py_tmp
)); } while (0); } } while (0)
;
1789 if (s == NULL((void*)0))
1790 goto error;
1791 line = s;
1792 }
1793 }
1794 }
1795 if (remaining != NULL((void*)0)) {
1796 if (chunks == NULL((void*)0)) {
1797 chunks = PyList_New(0);
1798 if (chunks == NULL((void*)0))
1799 goto error;
1800 }
1801 if (PyList_Append(chunks, remaining) < 0)
1802 goto error;
1803 Py_CLEAR(remaining)do { if (remaining) { PyObject *_py_tmp = (PyObject *)(remaining
); (remaining) = ((void*)0); do { if (_Py_RefTotal-- , --((PyObject
*)(_py_tmp))->ob_refcnt != 0) { if (((PyObject*)_py_tmp)->
ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1803, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
1804 }
1805 if (chunks != NULL((void*)0)) {
1806 if (line != NULL((void*)0) && PyList_Append(chunks, line) < 0)
1807 goto error;
1808 Py_CLEAR(line)do { if (line) { PyObject *_py_tmp = (PyObject *)(line); (line
) = ((void*)0); do { if (_Py_RefTotal-- , --((PyObject*)(_py_tmp
))->ob_refcnt != 0) { if (((PyObject*)_py_tmp)->ob_refcnt
< 0) _Py_NegativeRefcount("./Modules/_io/textio.c", 1808,
(PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *)(_py_tmp
)); } while (0); } } while (0)
;
1809 line = PyUnicode_JoinPyUnicodeUCS2_Join(_PyIO_empty_str, chunks);
1810 if (line == NULL((void*)0))
1811 goto error;
1812 Py_DECREF(chunks)do { if (_Py_RefTotal-- , --((PyObject*)(chunks))->ob_refcnt
!= 0) { if (((PyObject*)chunks)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1812, (PyObject *)(chunks)); } else
_Py_Dealloc((PyObject *)(chunks)); } while (0)
;
1813 }
1814 if (line == NULL((void*)0))
1815 line = PyUnicode_FromStringAndSizePyUnicodeUCS2_FromStringAndSize(NULL((void*)0), 0);
1816
1817 return line;
1818
1819 error:
1820 Py_XDECREF(chunks)do { if ((chunks) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(chunks))->ob_refcnt != 0) { if (((PyObject
*)chunks)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1820, (PyObject *)(chunks)); } else _Py_Dealloc((PyObject *
)(chunks)); } while (0); } while (0)
;
1821 Py_XDECREF(remaining)do { if ((remaining) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(remaining))->ob_refcnt != 0) { if (((PyObject
*)remaining)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1821, (PyObject *)(remaining)); } else _Py_Dealloc((PyObject
*)(remaining)); } while (0); } while (0)
;
1822 Py_XDECREF(line)do { if ((line) == ((void*)0)) ; else do { if (_Py_RefTotal--
, --((PyObject*)(line))->ob_refcnt != 0) { if (((PyObject
*)line)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 1822, (PyObject *)(line)); } else _Py_Dealloc((PyObject *)(
line)); } while (0); } while (0)
;
1823 return NULL((void*)0);
1824}
1825
1826static PyObject *
1827textiowrapper_readline(textio *self, PyObject *args)
1828{
1829 Py_ssize_t limit = -1;
1830
1831 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
1832 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|n:readline", &limit)) {
1833 return NULL((void*)0);
1834 }
1835 return _textiowrapper_readline(self, limit);
1836}
1837
1838/* Seek and Tell */
1839
1840typedef struct {
1841 Py_off_t start_pos;
1842 int dec_flags;
1843 int bytes_to_feed;
1844 int chars_to_skip;
1845 char need_eof;
1846} cookie_type;
1847
1848/*
1849 To speed up cookie packing/unpacking, we store the fields in a temporary
1850 string and call _PyLong_FromByteArray() or _PyLong_AsByteArray (resp.).
1851 The following macros define at which offsets in the intermediary byte
1852 string the various CookieStruct fields will be stored.
1853 */
1854
1855#define COOKIE_BUF_LEN(sizeof(Py_off_t) + 3 * sizeof(int) + sizeof(char)) (sizeof(Py_off_t) + 3 * sizeof(int) + sizeof(char))
1856
1857#if defined(WORDS_BIGENDIAN)
1858
1859# define IS_LITTLE_ENDIAN 0
1860
1861/* We want the least significant byte of start_pos to also be the least
1862 significant byte of the cookie, which means that in big-endian mode we
1863 must copy the fields in reverse order. */
1864
1865# define OFF_START_POS0 (sizeof(char) + 3 * sizeof(int))
1866# define OFF_DEC_FLAGS(sizeof(Py_off_t)) (sizeof(char) + 2 * sizeof(int))
1867# define OFF_BYTES_TO_FEED(sizeof(Py_off_t) + sizeof(int)) (sizeof(char) + sizeof(int))
1868# define OFF_CHARS_TO_SKIP(sizeof(Py_off_t) + 2 * sizeof(int)) (sizeof(char))
1869# define OFF_NEED_EOF(sizeof(Py_off_t) + 3 * sizeof(int)) 0
1870
1871#else
1872
1873# define IS_LITTLE_ENDIAN 1
1874
1875/* Little-endian mode: the least significant byte of start_pos will
1876 naturally end up the least significant byte of the cookie. */
1877
1878# define OFF_START_POS0 0
1879# define OFF_DEC_FLAGS(sizeof(Py_off_t)) (sizeof(Py_off_t))
1880# define OFF_BYTES_TO_FEED(sizeof(Py_off_t) + sizeof(int)) (sizeof(Py_off_t) + sizeof(int))
1881# define OFF_CHARS_TO_SKIP(sizeof(Py_off_t) + 2 * sizeof(int)) (sizeof(Py_off_t) + 2 * sizeof(int))
1882# define OFF_NEED_EOF(sizeof(Py_off_t) + 3 * sizeof(int)) (sizeof(Py_off_t) + 3 * sizeof(int))
1883
1884#endif
1885
1886static int
1887textiowrapper_parse_cookie(cookie_type *cookie, PyObject *cookieObj)
1888{
1889 unsigned char buffer[COOKIE_BUF_LEN(sizeof(Py_off_t) + 3 * sizeof(int) + sizeof(char))];
1890 PyLongObject *cookieLong = (PyLongObject *)PyNumber_Long(cookieObj);
1891 if (cookieLong == NULL((void*)0))
1892 return -1;
1893
1894 if (_PyLong_AsByteArray(cookieLong, buffer, sizeof(buffer),
1895 IS_LITTLE_ENDIAN, 0) < 0) {
1896 Py_DECREF(cookieLong)do { if (_Py_RefTotal-- , --((PyObject*)(cookieLong))->ob_refcnt
!= 0) { if (((PyObject*)cookieLong)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1896, (PyObject *)(cookieLong)); }
else _Py_Dealloc((PyObject *)(cookieLong)); } while (0)
;
1897 return -1;
1898 }
1899 Py_DECREF(cookieLong)do { if (_Py_RefTotal-- , --((PyObject*)(cookieLong))->ob_refcnt
!= 0) { if (((PyObject*)cookieLong)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1899, (PyObject *)(cookieLong)); }
else _Py_Dealloc((PyObject *)(cookieLong)); } while (0)
;
1900
1901 memcpy(&cookie->start_pos, buffer + OFF_START_POS, sizeof(cookie->start_pos))((__builtin_object_size (&cookie->start_pos, 0) != (size_t
) -1) ? __builtin___memcpy_chk (&cookie->start_pos, buffer
+ 0, sizeof(cookie->start_pos), __builtin_object_size (&
cookie->start_pos, 0)) : __inline_memcpy_chk (&cookie->
start_pos, buffer + 0, sizeof(cookie->start_pos)))
;
1902 memcpy(&cookie->dec_flags, buffer + OFF_DEC_FLAGS, sizeof(cookie->dec_flags))((__builtin_object_size (&cookie->dec_flags, 0) != (size_t
) -1) ? __builtin___memcpy_chk (&cookie->dec_flags, buffer
+ (sizeof(Py_off_t)), sizeof(cookie->dec_flags), __builtin_object_size
(&cookie->dec_flags, 0)) : __inline_memcpy_chk (&
cookie->dec_flags, buffer + (sizeof(Py_off_t)), sizeof(cookie
->dec_flags)))
;
1903 memcpy(&cookie->bytes_to_feed, buffer + OFF_BYTES_TO_FEED, sizeof(cookie->bytes_to_feed))((__builtin_object_size (&cookie->bytes_to_feed, 0) !=
(size_t) -1) ? __builtin___memcpy_chk (&cookie->bytes_to_feed
, buffer + (sizeof(Py_off_t) + sizeof(int)), sizeof(cookie->
bytes_to_feed), __builtin_object_size (&cookie->bytes_to_feed
, 0)) : __inline_memcpy_chk (&cookie->bytes_to_feed, buffer
+ (sizeof(Py_off_t) + sizeof(int)), sizeof(cookie->bytes_to_feed
)))
;
1904 memcpy(&cookie->chars_to_skip, buffer + OFF_CHARS_TO_SKIP, sizeof(cookie->chars_to_skip))((__builtin_object_size (&cookie->chars_to_skip, 0) !=
(size_t) -1) ? __builtin___memcpy_chk (&cookie->chars_to_skip
, buffer + (sizeof(Py_off_t) + 2 * sizeof(int)), sizeof(cookie
->chars_to_skip), __builtin_object_size (&cookie->chars_to_skip
, 0)) : __inline_memcpy_chk (&cookie->chars_to_skip, buffer
+ (sizeof(Py_off_t) + 2 * sizeof(int)), sizeof(cookie->chars_to_skip
)))
;
1905 memcpy(&cookie->need_eof, buffer + OFF_NEED_EOF, sizeof(cookie->need_eof))((__builtin_object_size (&cookie->need_eof, 0) != (size_t
) -1) ? __builtin___memcpy_chk (&cookie->need_eof, buffer
+ (sizeof(Py_off_t) + 3 * sizeof(int)), sizeof(cookie->need_eof
), __builtin_object_size (&cookie->need_eof, 0)) : __inline_memcpy_chk
(&cookie->need_eof, buffer + (sizeof(Py_off_t) + 3 * sizeof
(int)), sizeof(cookie->need_eof)))
;
1906
1907 return 0;
1908}
1909
1910static PyObject *
1911textiowrapper_build_cookie(cookie_type *cookie)
1912{
1913 unsigned char buffer[COOKIE_BUF_LEN(sizeof(Py_off_t) + 3 * sizeof(int) + sizeof(char))];
1914
1915 memcpy(buffer + OFF_START_POS, &cookie->start_pos, sizeof(cookie->start_pos))((__builtin_object_size (buffer + 0, 0) != (size_t) -1) ? __builtin___memcpy_chk
(buffer + 0, &cookie->start_pos, sizeof(cookie->start_pos
), __builtin_object_size (buffer + 0, 0)) : __inline_memcpy_chk
(buffer + 0, &cookie->start_pos, sizeof(cookie->start_pos
)))
;
1916 memcpy(buffer + OFF_DEC_FLAGS, &cookie->dec_flags, sizeof(cookie->dec_flags))((__builtin_object_size (buffer + (sizeof(Py_off_t)), 0) != (
size_t) -1) ? __builtin___memcpy_chk (buffer + (sizeof(Py_off_t
)), &cookie->dec_flags, sizeof(cookie->dec_flags), __builtin_object_size
(buffer + (sizeof(Py_off_t)), 0)) : __inline_memcpy_chk (buffer
+ (sizeof(Py_off_t)), &cookie->dec_flags, sizeof(cookie
->dec_flags)))
;
1917 memcpy(buffer + OFF_BYTES_TO_FEED, &cookie->bytes_to_feed, sizeof(cookie->bytes_to_feed))((__builtin_object_size (buffer + (sizeof(Py_off_t) + sizeof(
int)), 0) != (size_t) -1) ? __builtin___memcpy_chk (buffer + (
sizeof(Py_off_t) + sizeof(int)), &cookie->bytes_to_feed
, sizeof(cookie->bytes_to_feed), __builtin_object_size (buffer
+ (sizeof(Py_off_t) + sizeof(int)), 0)) : __inline_memcpy_chk
(buffer + (sizeof(Py_off_t) + sizeof(int)), &cookie->
bytes_to_feed, sizeof(cookie->bytes_to_feed)))
;
1918 memcpy(buffer + OFF_CHARS_TO_SKIP, &cookie->chars_to_skip, sizeof(cookie->chars_to_skip))((__builtin_object_size (buffer + (sizeof(Py_off_t) + 2 * sizeof
(int)), 0) != (size_t) -1) ? __builtin___memcpy_chk (buffer +
(sizeof(Py_off_t) + 2 * sizeof(int)), &cookie->chars_to_skip
, sizeof(cookie->chars_to_skip), __builtin_object_size (buffer
+ (sizeof(Py_off_t) + 2 * sizeof(int)), 0)) : __inline_memcpy_chk
(buffer + (sizeof(Py_off_t) + 2 * sizeof(int)), &cookie->
chars_to_skip, sizeof(cookie->chars_to_skip)))
;
1919 memcpy(buffer + OFF_NEED_EOF, &cookie->need_eof, sizeof(cookie->need_eof))((__builtin_object_size (buffer + (sizeof(Py_off_t) + 3 * sizeof
(int)), 0) != (size_t) -1) ? __builtin___memcpy_chk (buffer +
(sizeof(Py_off_t) + 3 * sizeof(int)), &cookie->need_eof
, sizeof(cookie->need_eof), __builtin_object_size (buffer +
(sizeof(Py_off_t) + 3 * sizeof(int)), 0)) : __inline_memcpy_chk
(buffer + (sizeof(Py_off_t) + 3 * sizeof(int)), &cookie->
need_eof, sizeof(cookie->need_eof)))
;
1920
1921 return _PyLong_FromByteArray(buffer, sizeof(buffer), IS_LITTLE_ENDIAN, 0);
1922}
1923#undef IS_LITTLE_ENDIAN
1924
1925static int
1926_textiowrapper_decoder_setstate(textio *self, cookie_type *cookie)
1927{
1928 PyObject *res;
1929 /* When seeking to the start of the stream, we call decoder.reset()
1930 rather than decoder.getstate().
1931 This is for a few decoders such as utf-16 for which the state value
1932 at start is not (b"", 0) but e.g. (b"", 2) (meaning, in the case of
1933 utf-16, that we are expecting a BOM).
1934 */
1935 if (cookie->start_pos == 0 && cookie->dec_flags == 0)
1936 res = PyObject_CallMethodObjArgs(self->decoder, _PyIO_str_reset, NULL((void*)0));
1937 else
1938 res = PyObject_CallMethod_PyObject_CallMethod_SizeT(self->decoder, "setstate",
1939 "((yi))", "", cookie->dec_flags);
1940 if (res == NULL((void*)0))
1941 return -1;
1942 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1942, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
1943 return 0;
1944}
1945
1946static int
1947_textiowrapper_encoder_setstate(textio *self, cookie_type *cookie)
1948{
1949 PyObject *res;
1950 /* Same as _textiowrapper_decoder_setstate() above. */
1951 if (cookie->start_pos == 0 && cookie->dec_flags == 0) {
1952 res = PyObject_CallMethodObjArgs(self->encoder, _PyIO_str_reset, NULL((void*)0));
1953 self->encoding_start_of_stream = 1;
1954 }
1955 else {
1956 res = PyObject_CallMethodObjArgs(self->encoder, _PyIO_str_setstate,
1957 _PyIO_zero, NULL((void*)0));
1958 self->encoding_start_of_stream = 0;
1959 }
1960 if (res == NULL((void*)0))
1961 return -1;
1962 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 1962, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
1963 return 0;
1964}
1965
1966static PyObject *
1967textiowrapper_seek(textio *self, PyObject *args)
1968{
1969 PyObject *cookieObj, *posobj;
1970 cookie_type cookie;
1971 int whence = 0;
1972 PyObject *res;
1973 int cmp;
1974
1975 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
1976
1977 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O|i:seek", &cookieObj, &whence))
1978 return NULL((void*)0);
1979 CHECK_CLOSED(self)do { int r; PyObject *_res; if ((((PyObject*)(self))->ob_type
) == &PyTextIOWrapper_Type) { if (self->raw != ((void*
)0)) r = _PyFileIO_closed(self->raw); else { _res = textiowrapper_closed_get
(self, ((void*)0)); if (_res == ((void*)0)) return ((void*)0)
; r = PyObject_IsTrue(_res); do { if (_Py_RefTotal-- , --((PyObject
*)(_res))->ob_refcnt != 0) { if (((PyObject*)_res)->ob_refcnt
< 0) _Py_NegativeRefcount("./Modules/_io/textio.c", 1979,
(PyObject *)(_res)); } else _Py_Dealloc((PyObject *)(_res));
} while (0); if (r < 0) return ((void*)0); } if (r > 0
) { PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."
); return ((void*)0); } } else if (_PyIOBase_check_closed((PyObject
*)self, ((PyObject *) &_Py_TrueStruct)) == ((void*)0)) return
((void*)0); } while (0)
;
1980
1981 Py_INCREF(cookieObj)( _Py_RefTotal++ , ((PyObject*)(cookieObj))->ob_refcnt++);
1982
1983 if (!self->seekable) {
1984 _unsupported("underlying stream is not seekable");
1985 goto fail;
1986 }
1987
1988 if (whence == 1) {
1989 /* seek relative to current position */
1990 cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_EQ2);
1991 if (cmp < 0)
1992 goto fail;
1993
1994 if (cmp == 0) {
1995 _unsupported("can't do nonzero cur-relative seeks");
1996 goto fail;
1997 }
1998
1999 /* Seeking to the current position should attempt to
2000 * sync the underlying buffer with the current position.
2001 */
2002 Py_DECREF(cookieObj)do { if (_Py_RefTotal-- , --((PyObject*)(cookieObj))->ob_refcnt
!= 0) { if (((PyObject*)cookieObj)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2002, (PyObject *)(cookieObj)); } else
_Py_Dealloc((PyObject *)(cookieObj)); } while (0)
;
2003 cookieObj = PyObject_CallMethod_PyObject_CallMethod_SizeT((PyObject *)self, "tell", NULL((void*)0));
2004 if (cookieObj == NULL((void*)0))
2005 goto fail;
2006 }
2007 else if (whence == 2) {
2008 /* seek relative to end of file */
2009
2010 cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_EQ2);
2011 if (cmp < 0)
2012 goto fail;
2013
2014 if (cmp == 0) {
2015 _unsupported("can't do nonzero end-relative seeks");
2016 goto fail;
2017 }
2018
2019 res = PyObject_CallMethod_PyObject_CallMethod_SizeT((PyObject *)self, "flush", NULL((void*)0));
2020 if (res == NULL((void*)0))
2021 goto fail;
2022 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2022, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
2023
2024 textiowrapper_set_decoded_chars(self, NULL((void*)0));
2025 Py_CLEAR(self->snapshot)do { if (self->snapshot) { PyObject *_py_tmp = (PyObject *
)(self->snapshot); (self->snapshot) = ((void*)0); do { if
(_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0
) { if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2025, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
2026 if (self->decoder) {
2027 res = PyObject_CallMethod_PyObject_CallMethod_SizeT(self->decoder, "reset", NULL((void*)0));
2028 if (res == NULL((void*)0))
2029 goto fail;
2030 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2030, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
2031 }
2032
2033 res = PyObject_CallMethod_PyObject_CallMethod_SizeT(self->buffer, "seek", "ii", 0, 2);
2034 Py_XDECREF(cookieObj)do { if ((cookieObj) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(cookieObj))->ob_refcnt != 0) { if (((PyObject
*)cookieObj)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 2034, (PyObject *)(cookieObj)); } else _Py_Dealloc((PyObject
*)(cookieObj)); } while (0); } while (0)
;
2035 return res;
2036 }
2037 else if (whence != 0) {
2038 PyErr_Format(PyExc_ValueError,
2039 "invalid whence (%d, should be 0, 1 or 2)", whence);
2040 goto fail;
2041 }
2042
2043 cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_LT0);
2044 if (cmp < 0)
2045 goto fail;
2046
2047 if (cmp == 1) {
2048 PyErr_Format(PyExc_ValueError,
2049 "negative seek position %R", cookieObj);
2050 goto fail;
2051 }
2052
2053 res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL((void*)0));
2054 if (res == NULL((void*)0))
2055 goto fail;
2056 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2056, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
2057
2058 /* The strategy of seek() is to go back to the safe start point
2059 * and replay the effect of read(chars_to_skip) from there.
2060 */
2061 if (textiowrapper_parse_cookie(&cookie, cookieObj) < 0)
2062 goto fail;
2063
2064 /* Seek back to the safe start point. */
2065 posobj = PyLong_FromOff_tPyLong_FromSsize_t(cookie.start_pos);
2066 if (posobj == NULL((void*)0))
2067 goto fail;
2068 res = PyObject_CallMethodObjArgs(self->buffer,
2069 _PyIO_str_seek, posobj, NULL((void*)0));
2070 Py_DECREF(posobj)do { if (_Py_RefTotal-- , --((PyObject*)(posobj))->ob_refcnt
!= 0) { if (((PyObject*)posobj)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2070, (PyObject *)(posobj)); } else
_Py_Dealloc((PyObject *)(posobj)); } while (0)
;
2071 if (res == NULL((void*)0))
2072 goto fail;
2073 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2073, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
2074
2075 textiowrapper_set_decoded_chars(self, NULL((void*)0));
2076 Py_CLEAR(self->snapshot)do { if (self->snapshot) { PyObject *_py_tmp = (PyObject *
)(self->snapshot); (self->snapshot) = ((void*)0); do { if
(_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0
) { if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2076, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
2077
2078 /* Restore the decoder to its state from the safe start point. */
2079 if (self->decoder) {
2080 if (_textiowrapper_decoder_setstate(self, &cookie) < 0)
2081 goto fail;
2082 }
2083
2084 if (cookie.chars_to_skip) {
2085 /* Just like _read_chunk, feed the decoder and save a snapshot. */
2086 PyObject *input_chunk = PyObject_CallMethod_PyObject_CallMethod_SizeT(
2087 self->buffer, "read", "i", cookie.bytes_to_feed);
2088 PyObject *decoded;
2089
2090 if (input_chunk == NULL((void*)0))
2091 goto fail;
2092
2093 assert (PyBytes_Check(input_chunk))(__builtin_expect(!(((((((PyObject*)(input_chunk))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 2093, "PyBytes_Check(input_chunk)"
) : (void)0)
;
2094
2095 self->snapshot = Py_BuildValue_Py_BuildValue_SizeT("iN", cookie.dec_flags, input_chunk);
2096 if (self->snapshot == NULL((void*)0)) {
2097 Py_DECREF(input_chunk)do { if (_Py_RefTotal-- , --((PyObject*)(input_chunk))->ob_refcnt
!= 0) { if (((PyObject*)input_chunk)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2097, (PyObject *)(input_chunk)); }
else _Py_Dealloc((PyObject *)(input_chunk)); } while (0)
;
2098 goto fail;
2099 }
2100
2101 decoded = PyObject_CallMethod_PyObject_CallMethod_SizeT(self->decoder, "decode",
2102 "Oi", input_chunk, (int)cookie.need_eof);
2103
2104 if (decoded == NULL((void*)0))
2105 goto fail;
2106
2107 textiowrapper_set_decoded_chars(self, decoded);
2108
2109 /* Skip chars_to_skip of the decoded characters. */
2110 if (PyUnicode_GetSizePyUnicodeUCS2_GetSize(self->decoded_chars) < cookie.chars_to_skip) {
2111 PyErr_SetString(PyExc_IOError, "can't restore logical file position");
2112 goto fail;
2113 }
2114 self->decoded_chars_used = cookie.chars_to_skip;
2115 }
2116 else {
2117 self->snapshot = Py_BuildValue_Py_BuildValue_SizeT("iy", cookie.dec_flags, "");
2118 if (self->snapshot == NULL((void*)0))
2119 goto fail;
2120 }
2121
2122 /* Finally, reset the encoder (merely useful for proper BOM handling) */
2123 if (self->encoder) {
2124 if (_textiowrapper_encoder_setstate(self, &cookie) < 0)
2125 goto fail;
2126 }
2127 return cookieObj;
2128 fail:
2129 Py_XDECREF(cookieObj)do { if ((cookieObj) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(cookieObj))->ob_refcnt != 0) { if (((PyObject
*)cookieObj)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 2129, (PyObject *)(cookieObj)); } else _Py_Dealloc((PyObject
*)(cookieObj)); } while (0); } while (0)
;
2130 return NULL((void*)0);
2131
2132}
2133
2134static PyObject *
2135textiowrapper_tell(textio *self, PyObject *args)
2136{
2137 PyObject *res;
2138 PyObject *posobj = NULL((void*)0);
2139 cookie_type cookie = {0,0,0,0,0};
2140 PyObject *next_input;
2141 Py_ssize_t chars_to_skip, chars_decoded;
2142 PyObject *saved_state = NULL((void*)0);
2143 char *input, *input_end;
2144
2145 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
2146 CHECK_CLOSED(self)do { int r; PyObject *_res; if ((((PyObject*)(self))->ob_type
) == &PyTextIOWrapper_Type) { if (self->raw != ((void*
)0)) r = _PyFileIO_closed(self->raw); else { _res = textiowrapper_closed_get
(self, ((void*)0)); if (_res == ((void*)0)) return ((void*)0)
; r = PyObject_IsTrue(_res); do { if (_Py_RefTotal-- , --((PyObject
*)(_res))->ob_refcnt != 0) { if (((PyObject*)_res)->ob_refcnt
< 0) _Py_NegativeRefcount("./Modules/_io/textio.c", 2146,
(PyObject *)(_res)); } else _Py_Dealloc((PyObject *)(_res));
} while (0); if (r < 0) return ((void*)0); } if (r > 0
) { PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."
); return ((void*)0); } } else if (_PyIOBase_check_closed((PyObject
*)self, ((PyObject *) &_Py_TrueStruct)) == ((void*)0)) return
((void*)0); } while (0)
;
2147
2148 if (!self->seekable) {
2149 _unsupported("underlying stream is not seekable");
2150 goto fail;
2151 }
2152 if (!self->telling) {
2153 PyErr_SetString(PyExc_IOError,
2154 "telling position disabled by next() call");
2155 goto fail;
2156 }
2157
2158 if (_textiowrapper_writeflush(self) < 0)
2159 return NULL((void*)0);
2160 res = PyObject_CallMethod_PyObject_CallMethod_SizeT((PyObject *)self, "flush", NULL((void*)0));
2161 if (res == NULL((void*)0))
2162 goto fail;
2163 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2163, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
2164
2165 posobj = PyObject_CallMethod_PyObject_CallMethod_SizeT(self->buffer, "tell", NULL((void*)0));
2166 if (posobj == NULL((void*)0))
2167 goto fail;
2168
2169 if (self->decoder == NULL((void*)0) || self->snapshot == NULL((void*)0)) {
2170 assert (self->decoded_chars == NULL || PyUnicode_GetSize(self->decoded_chars) == 0)(__builtin_expect(!(self->decoded_chars == ((void*)0) || PyUnicodeUCS2_GetSize
(self->decoded_chars) == 0), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c"
, 2170, "self->decoded_chars == NULL || PyUnicode_GetSize(self->decoded_chars) == 0"
) : (void)0)
;
2171 return posobj;
2172 }
2173
2174#if defined(HAVE_LARGEFILE_SUPPORT)
2175 cookie.start_pos = PyLong_AsLongLong(posobj);
2176#else
2177 cookie.start_pos = PyLong_AsLong(posobj);
2178#endif
2179 if (PyErr_Occurred())
2180 goto fail;
2181
2182 /* Skip backward to the snapshot point (see _read_chunk). */
2183 if (!PyArg_Parse_PyArg_Parse_SizeT(self->snapshot, "(iO)", &cookie.dec_flags, &next_input))
2184 goto fail;
2185
2186 assert (PyBytes_Check(next_input))(__builtin_expect(!(((((((PyObject*)(next_input))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 2186, "PyBytes_Check(next_input)"
) : (void)0)
;
2187
2188 cookie.start_pos -= PyBytes_GET_SIZE(next_input)((__builtin_expect(!(((((((PyObject*)(next_input))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 2188, "PyBytes_Check(next_input)"
) : (void)0),(((PyVarObject*)(next_input))->ob_size))
;
2189
2190 /* How many decoded characters have been used up since the snapshot? */
2191 if (self->decoded_chars_used == 0) {
2192 /* We haven't moved from the snapshot point. */
2193 Py_DECREF(posobj)do { if (_Py_RefTotal-- , --((PyObject*)(posobj))->ob_refcnt
!= 0) { if (((PyObject*)posobj)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2193, (PyObject *)(posobj)); } else
_Py_Dealloc((PyObject *)(posobj)); } while (0)
;
2194 return textiowrapper_build_cookie(&cookie);
2195 }
2196
2197 chars_to_skip = self->decoded_chars_used;
2198
2199 /* Starting from the snapshot position, we will walk the decoder
2200 * forward until it gives us enough decoded characters.
2201 */
2202 saved_state = PyObject_CallMethodObjArgs(self->decoder,
2203 _PyIO_str_getstate, NULL((void*)0));
2204 if (saved_state == NULL((void*)0))
2205 goto fail;
2206
2207 /* Note our initial start point. */
2208 if (_textiowrapper_decoder_setstate(self, &cookie) < 0)
2209 goto fail;
2210
2211 /* Feed the decoder one byte at a time. As we go, note the
2212 * nearest "safe start point" before the current location
2213 * (a point where the decoder has nothing buffered, so seek()
2214 * can safely start from there and advance to this location).
2215 */
2216 chars_decoded = 0;
2217 input = PyBytes_AS_STRING(next_input)((__builtin_expect(!(((((((PyObject*)(next_input))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 2217, "PyBytes_Check(next_input)"
) : (void)0), (((PyBytesObject *)(next_input))->ob_sval))
;
2218 input_end = input + PyBytes_GET_SIZE(next_input)((__builtin_expect(!(((((((PyObject*)(next_input))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 2218, "PyBytes_Check(next_input)"
) : (void)0),(((PyVarObject*)(next_input))->ob_size))
;
2219 while (input < input_end) {
2220 PyObject *state;
2221 char *dec_buffer;
2222 Py_ssize_t dec_buffer_len;
2223 int dec_flags;
2224
2225 PyObject *decoded = PyObject_CallMethod_PyObject_CallMethod_SizeT(
2226 self->decoder, "decode", "y#", input, 1);
2227 if (decoded == NULL((void*)0))
2228 goto fail;
2229 assert (PyUnicode_Check(decoded))(__builtin_expect(!(((((((PyObject*)(decoded))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 2229, "PyUnicode_Check(decoded)")
: (void)0)
;
2230 chars_decoded += PyUnicode_GET_SIZE(decoded)((__builtin_expect(!(((((((PyObject*)(decoded))->ob_type))
->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 2230, "PyUnicode_Check(decoded)"
) : (void)0),(((PyUnicodeObject *)(decoded))->length))
;
2231 Py_DECREF(decoded)do { if (_Py_RefTotal-- , --((PyObject*)(decoded))->ob_refcnt
!= 0) { if (((PyObject*)decoded)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2231, (PyObject *)(decoded)); } else
_Py_Dealloc((PyObject *)(decoded)); } while (0)
;
2232
2233 cookie.bytes_to_feed += 1;
2234
2235 state = PyObject_CallMethodObjArgs(self->decoder,
2236 _PyIO_str_getstate, NULL((void*)0));
2237 if (state == NULL((void*)0))
2238 goto fail;
2239 if (!PyArg_Parse_PyArg_Parse_SizeT(state, "(y#i)", &dec_buffer, &dec_buffer_len, &dec_flags)) {
2240 Py_DECREF(state)do { if (_Py_RefTotal-- , --((PyObject*)(state))->ob_refcnt
!= 0) { if (((PyObject*)state)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2240, (PyObject *)(state)); } else
_Py_Dealloc((PyObject *)(state)); } while (0)
;
2241 goto fail;
2242 }
2243 Py_DECREF(state)do { if (_Py_RefTotal-- , --((PyObject*)(state))->ob_refcnt
!= 0) { if (((PyObject*)state)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2243, (PyObject *)(state)); } else
_Py_Dealloc((PyObject *)(state)); } while (0)
;
2244
2245 if (dec_buffer_len == 0 && chars_decoded <= chars_to_skip) {
2246 /* Decoder buffer is empty, so this is a safe start point. */
2247 cookie.start_pos += cookie.bytes_to_feed;
2248 chars_to_skip -= chars_decoded;
2249 cookie.dec_flags = dec_flags;
2250 cookie.bytes_to_feed = 0;
2251 chars_decoded = 0;
2252 }
2253 if (chars_decoded >= chars_to_skip)
2254 break;
2255 input++;
2256 }
2257 if (input == input_end) {
2258 /* We didn't get enough decoded data; signal EOF to get more. */
2259 PyObject *decoded = PyObject_CallMethod_PyObject_CallMethod_SizeT(
2260 self->decoder, "decode", "yi", "", /* final = */ 1);
2261 if (decoded == NULL((void*)0))
2262 goto fail;
2263 assert (PyUnicode_Check(decoded))(__builtin_expect(!(((((((PyObject*)(decoded))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 2263, "PyUnicode_Check(decoded)")
: (void)0)
;
2264 chars_decoded += PyUnicode_GET_SIZE(decoded)((__builtin_expect(!(((((((PyObject*)(decoded))->ob_type))
->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 2264, "PyUnicode_Check(decoded)"
) : (void)0),(((PyUnicodeObject *)(decoded))->length))
;
2265 Py_DECREF(decoded)do { if (_Py_RefTotal-- , --((PyObject*)(decoded))->ob_refcnt
!= 0) { if (((PyObject*)decoded)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2265, (PyObject *)(decoded)); } else
_Py_Dealloc((PyObject *)(decoded)); } while (0)
;
2266 cookie.need_eof = 1;
2267
2268 if (chars_decoded < chars_to_skip) {
2269 PyErr_SetString(PyExc_IOError,
2270 "can't reconstruct logical file position");
2271 goto fail;
2272 }
2273 }
2274
2275 /* finally */
2276 Py_XDECREF(posobj)do { if ((posobj) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(posobj))->ob_refcnt != 0) { if (((PyObject
*)posobj)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 2276, (PyObject *)(posobj)); } else _Py_Dealloc((PyObject *
)(posobj)); } while (0); } while (0)
;
2277 res = PyObject_CallMethod_PyObject_CallMethod_SizeT(self->decoder, "setstate", "(O)", saved_state);
2278 Py_DECREF(saved_state)do { if (_Py_RefTotal-- , --((PyObject*)(saved_state))->ob_refcnt
!= 0) { if (((PyObject*)saved_state)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2278, (PyObject *)(saved_state)); }
else _Py_Dealloc((PyObject *)(saved_state)); } while (0)
;
2279 if (res == NULL((void*)0))
2280 return NULL((void*)0);
2281 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2281, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
2282
2283 /* The returned cookie corresponds to the last safe start point. */
2284 cookie.chars_to_skip = Py_SAFE_DOWNCAST(chars_to_skip, Py_ssize_t, int)((__builtin_expect(!((Py_ssize_t)(int)(chars_to_skip) == (chars_to_skip
)), 0) ? __assert_rtn(__func__, "./Modules/_io/textio.c", 2284
, "(Py_ssize_t)(int)(chars_to_skip) == (chars_to_skip)") : (void
)0), (int)(chars_to_skip))
;
2285 return textiowrapper_build_cookie(&cookie);
2286
2287 fail:
2288 Py_XDECREF(posobj)do { if ((posobj) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(posobj))->ob_refcnt != 0) { if (((PyObject
*)posobj)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 2288, (PyObject *)(posobj)); } else _Py_Dealloc((PyObject *
)(posobj)); } while (0); } while (0)
;
2289 if (saved_state) {
2290 PyObject *type, *value, *traceback;
2291 PyErr_Fetch(&type, &value, &traceback);
2292
2293 res = PyObject_CallMethod_PyObject_CallMethod_SizeT(self->decoder, "setstate", "(O)", saved_state);
2294 Py_DECREF(saved_state)do { if (_Py_RefTotal-- , --((PyObject*)(saved_state))->ob_refcnt
!= 0) { if (((PyObject*)saved_state)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2294, (PyObject *)(saved_state)); }
else _Py_Dealloc((PyObject *)(saved_state)); } while (0)
;
2295 if (res == NULL((void*)0))
2296 return NULL((void*)0);
2297 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2297, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
2298
2299 PyErr_Restore(type, value, traceback);
2300 }
2301 return NULL((void*)0);
2302}
2303
2304static PyObject *
2305textiowrapper_truncate(textio *self, PyObject *args)
2306{
2307 PyObject *pos = Py_None(&_Py_NoneStruct);
2308 PyObject *res;
2309
2310 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
2311 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|O:truncate", &pos)) {
2312 return NULL((void*)0);
2313 }
2314
2315 res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_flush, NULL((void*)0));
2316 if (res == NULL((void*)0))
2317 return NULL((void*)0);
2318 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2318, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
2319
2320 return PyObject_CallMethodObjArgs(self->buffer, _PyIO_str_truncate, pos, NULL((void*)0));
2321}
2322
2323static PyObject *
2324textiowrapper_repr(textio *self)
2325{
2326 PyObject *nameobj, *modeobj, *res, *s;
2327
2328 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
2329
2330 res = PyUnicode_FromStringPyUnicodeUCS2_FromString("<_io.TextIOWrapper");
2331 if (res == NULL((void*)0))
2332 return NULL((void*)0);
2333 nameobj = PyObject_GetAttrString((PyObject *) self, "name");
2334 if (nameobj == NULL((void*)0)) {
2335 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2336 PyErr_Clear();
2337 else
2338 goto error;
2339 }
2340 else {
2341 s = PyUnicode_FromFormatPyUnicodeUCS2_FromFormat(" name=%R", nameobj);
2342 Py_DECREF(nameobj)do { if (_Py_RefTotal-- , --((PyObject*)(nameobj))->ob_refcnt
!= 0) { if (((PyObject*)nameobj)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2342, (PyObject *)(nameobj)); } else
_Py_Dealloc((PyObject *)(nameobj)); } while (0)
;
2343 if (s == NULL((void*)0))
2344 goto error;
2345 PyUnicode_AppendAndDelPyUnicodeUCS2_AppendAndDel(&res, s);
2346 if (res == NULL((void*)0))
2347 return NULL((void*)0);
2348 }
2349 modeobj = PyObject_GetAttrString((PyObject *) self, "mode");
2350 if (modeobj == NULL((void*)0)) {
2351 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2352 PyErr_Clear();
2353 else
2354 goto error;
2355 }
2356 else {
2357 s = PyUnicode_FromFormatPyUnicodeUCS2_FromFormat(" mode=%R", modeobj);
2358 Py_DECREF(modeobj)do { if (_Py_RefTotal-- , --((PyObject*)(modeobj))->ob_refcnt
!= 0) { if (((PyObject*)modeobj)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2358, (PyObject *)(modeobj)); } else
_Py_Dealloc((PyObject *)(modeobj)); } while (0)
;
2359 if (s == NULL((void*)0))
2360 goto error;
2361 PyUnicode_AppendAndDelPyUnicodeUCS2_AppendAndDel(&res, s);
2362 if (res == NULL((void*)0))
2363 return NULL((void*)0);
2364 }
2365 s = PyUnicode_FromFormatPyUnicodeUCS2_FromFormat("%U encoding=%R>",
2366 res, self->encoding);
2367 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2367, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
2368 return s;
2369error:
2370 Py_XDECREF(res)do { if ((res) == ((void*)0)) ; else do { if (_Py_RefTotal-- ,
--((PyObject*)(res))->ob_refcnt != 0) { if (((PyObject*)res
)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_io/textio.c"
, 2370, (PyObject *)(res)); } else _Py_Dealloc((PyObject *)(res
)); } while (0); } while (0)
;
2371 return NULL((void*)0);
2372}
2373
2374
2375/* Inquiries */
2376
2377static PyObject *
2378textiowrapper_fileno(textio *self, PyObject *args)
2379{
2380 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
2381 return PyObject_CallMethod_PyObject_CallMethod_SizeT(self->buffer, "fileno", NULL((void*)0));
2382}
2383
2384static PyObject *
2385textiowrapper_seekable(textio *self, PyObject *args)
2386{
2387 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
2388 return PyObject_CallMethod_PyObject_CallMethod_SizeT(self->buffer, "seekable", NULL((void*)0));
2389}
2390
2391static PyObject *
2392textiowrapper_readable(textio *self, PyObject *args)
2393{
2394 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
2395 return PyObject_CallMethod_PyObject_CallMethod_SizeT(self->buffer, "readable", NULL((void*)0));
2396}
2397
2398static PyObject *
2399textiowrapper_writable(textio *self, PyObject *args)
2400{
2401 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
2402 return PyObject_CallMethod_PyObject_CallMethod_SizeT(self->buffer, "writable", NULL((void*)0));
2403}
2404
2405static PyObject *
2406textiowrapper_isatty(textio *self, PyObject *args)
2407{
2408 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
2409 return PyObject_CallMethod_PyObject_CallMethod_SizeT(self->buffer, "isatty", NULL((void*)0));
2410}
2411
2412static PyObject *
2413textiowrapper_getstate(textio *self, PyObject *args)
2414{
2415 PyErr_Format(PyExc_TypeError,
2416 "cannot serialize '%s' object", Py_TYPE(self)(((PyObject*)(self))->ob_type)->tp_name);
2417 return NULL((void*)0);
2418}
2419
2420static PyObject *
2421textiowrapper_flush(textio *self, PyObject *args)
2422{
2423 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
2424 CHECK_CLOSED(self)do { int r; PyObject *_res; if ((((PyObject*)(self))->ob_type
) == &PyTextIOWrapper_Type) { if (self->raw != ((void*
)0)) r = _PyFileIO_closed(self->raw); else { _res = textiowrapper_closed_get
(self, ((void*)0)); if (_res == ((void*)0)) return ((void*)0)
; r = PyObject_IsTrue(_res); do { if (_Py_RefTotal-- , --((PyObject
*)(_res))->ob_refcnt != 0) { if (((PyObject*)_res)->ob_refcnt
< 0) _Py_NegativeRefcount("./Modules/_io/textio.c", 2424,
(PyObject *)(_res)); } else _Py_Dealloc((PyObject *)(_res));
} while (0); if (r < 0) return ((void*)0); } if (r > 0
) { PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."
); return ((void*)0); } } else if (_PyIOBase_check_closed((PyObject
*)self, ((PyObject *) &_Py_TrueStruct)) == ((void*)0)) return
((void*)0); } while (0)
;
2425 self->telling = self->seekable;
2426 if (_textiowrapper_writeflush(self) < 0)
2427 return NULL((void*)0);
2428 return PyObject_CallMethod_PyObject_CallMethod_SizeT(self->buffer, "flush", NULL((void*)0));
2429}
2430
2431static PyObject *
2432textiowrapper_close(textio *self, PyObject *args)
2433{
2434 PyObject *res;
2435 int r;
2436 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
2437
2438 res = textiowrapper_closed_get(self, NULL((void*)0));
2439 if (res == NULL((void*)0))
2440 return NULL((void*)0);
2441 r = PyObject_IsTrue(res);
2442 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2442, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
2443 if (r < 0)
2444 return NULL((void*)0);
2445
2446 if (r > 0) {
2447 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
; /* stream already closed */
2448 }
2449 else {
2450 if (self->deallocating) {
2451 res = PyObject_CallMethod_PyObject_CallMethod_SizeT(self->buffer, "_dealloc_warn", "O", self);
2452 if (res)
2453 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2453, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
2454 else
2455 PyErr_Clear();
2456 }
2457 res = PyObject_CallMethod_PyObject_CallMethod_SizeT((PyObject *)self, "flush", NULL((void*)0));
2458 if (res == NULL((void*)0)) {
2459 return NULL((void*)0);
2460 }
2461 else
2462 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2462, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
2463
2464 return PyObject_CallMethod_PyObject_CallMethod_SizeT(self->buffer, "close", NULL((void*)0));
2465 }
2466}
2467
2468static PyObject *
2469textiowrapper_iternext(textio *self)
2470{
2471 PyObject *line;
2472
2473 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
2474
2475 self->telling = 0;
2476 if (Py_TYPE(self)(((PyObject*)(self))->ob_type) == &PyTextIOWrapper_Type) {
2477 /* Skip method call overhead for speed */
2478 line = _textiowrapper_readline(self, -1);
2479 }
2480 else {
2481 line = PyObject_CallMethodObjArgs((PyObject *)self,
2482 _PyIO_str_readline, NULL((void*)0));
2483 if (line && !PyUnicode_Check(line)((((((PyObject*)(line))->ob_type))->tp_flags & ((1L
<<28))) != 0)
) {
2484 PyErr_Format(PyExc_IOError,
2485 "readline() should have returned an str object, "
2486 "not '%.200s'", Py_TYPE(line)(((PyObject*)(line))->ob_type)->tp_name);
2487 Py_DECREF(line)do { if (_Py_RefTotal-- , --((PyObject*)(line))->ob_refcnt
!= 0) { if (((PyObject*)line)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2487, (PyObject *)(line)); } else _Py_Dealloc
((PyObject *)(line)); } while (0)
;
2488 return NULL((void*)0);
2489 }
2490 }
2491
2492 if (line == NULL((void*)0))
2493 return NULL((void*)0);
2494
2495 if (PyUnicode_GET_SIZE(line)((__builtin_expect(!(((((((PyObject*)(line))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "./Modules/_io/textio.c", 2495, "PyUnicode_Check(line)") : (
void)0),(((PyUnicodeObject *)(line))->length))
== 0) {
2496 /* Reached EOF or would have blocked */
2497 Py_DECREF(line)do { if (_Py_RefTotal-- , --((PyObject*)(line))->ob_refcnt
!= 0) { if (((PyObject*)line)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2497, (PyObject *)(line)); } else _Py_Dealloc
((PyObject *)(line)); } while (0)
;
2498 Py_CLEAR(self->snapshot)do { if (self->snapshot) { PyObject *_py_tmp = (PyObject *
)(self->snapshot); (self->snapshot) = ((void*)0); do { if
(_Py_RefTotal-- , --((PyObject*)(_py_tmp))->ob_refcnt != 0
) { if (((PyObject*)_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_io/textio.c", 2498, (PyObject *)(_py_tmp)); } else
_Py_Dealloc((PyObject *)(_py_tmp)); } while (0); } } while (
0)
;
2499 self->telling = self->seekable;
2500 return NULL((void*)0);
2501 }
2502
2503 return line;
2504}
2505
2506static PyObject *
2507textiowrapper_name_get(textio *self, void *context)
2508{
2509 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
2510 return PyObject_GetAttrString(self->buffer, "name");
2511}
2512
2513static PyObject *
2514textiowrapper_closed_get(textio *self, void *context)
2515{
2516 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
2517 return PyObject_GetAttr(self->buffer, _PyIO_str_closed);
2518}
2519
2520static PyObject *
2521textiowrapper_newlines_get(textio *self, void *context)
2522{
2523 PyObject *res;
2524 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
2525 if (self->decoder == NULL((void*)0))
2526 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2527 res = PyObject_GetAttr(self->decoder, _PyIO_str_newlines);
2528 if (res == NULL((void*)0)) {
2529 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2530 PyErr_Clear();
2531 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2532 }
2533 else {
2534 return NULL((void*)0);
2535 }
2536 }
2537 return res;
2538}
2539
2540static PyObject *
2541textiowrapper_errors_get(textio *self, void *context)
2542{
2543 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
2544 return PyUnicode_FromStringPyUnicodeUCS2_FromString(PyBytes_AS_STRING(self->errors)((__builtin_expect(!(((((((PyObject*)(self->errors))->ob_type
))->tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn
(__func__, "./Modules/_io/textio.c", 2544, "PyBytes_Check(self->errors)"
) : (void)0), (((PyBytesObject *)(self->errors))->ob_sval
))
);
2545}
2546
2547static PyObject *
2548textiowrapper_chunk_size_get(textio *self, void *context)
2549{
2550 CHECK_INITIALIZED(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return ((void*)0); }
;
2551 return PyLong_FromSsize_t(self->chunk_size);
2552}
2553
2554static int
2555textiowrapper_chunk_size_set(textio *self, PyObject *arg, void *context)
2556{
2557 Py_ssize_t n;
2558 CHECK_INITIALIZED_INT(self)if (self->ok <= 0) { if (self->detached) { PyErr_SetString
(PyExc_ValueError, "underlying buffer has been detached"); } else
{ PyErr_SetString(PyExc_ValueError, "I/O operation on uninitialized object"
); } return -1; }
;
2559 n = PyNumber_AsSsize_t(arg, PyExc_TypeError);
2560 if (n == -1 && PyErr_Occurred())
2561 return -1;
2562 if (n <= 0) {
2563 PyErr_SetString(PyExc_ValueError,
2564 "a strictly positive integer is required");
2565 return -1;
2566 }
2567 self->chunk_size = n;
2568 return 0;
2569}
2570
2571static PyMethodDef textiowrapper_methods[] = {
2572 {"detach", (PyCFunction)textiowrapper_detach, METH_NOARGS0x0004},
2573 {"write", (PyCFunction)textiowrapper_write, METH_VARARGS0x0001},
2574 {"read", (PyCFunction)textiowrapper_read, METH_VARARGS0x0001},
2575 {"readline", (PyCFunction)textiowrapper_readline, METH_VARARGS0x0001},
2576 {"flush", (PyCFunction)textiowrapper_flush, METH_NOARGS0x0004},
2577 {"close", (PyCFunction)textiowrapper_close, METH_NOARGS0x0004},
2578
2579 {"fileno", (PyCFunction)textiowrapper_fileno, METH_NOARGS0x0004},
2580 {"seekable", (PyCFunction)textiowrapper_seekable, METH_NOARGS0x0004},
2581 {"readable", (PyCFunction)textiowrapper_readable, METH_NOARGS0x0004},
2582 {"writable", (PyCFunction)textiowrapper_writable, METH_NOARGS0x0004},
2583 {"isatty", (PyCFunction)textiowrapper_isatty, METH_NOARGS0x0004},
2584 {"__getstate__", (PyCFunction)textiowrapper_getstate, METH_NOARGS0x0004},
2585
2586 {"seek", (PyCFunction)textiowrapper_seek, METH_VARARGS0x0001},
2587 {"tell", (PyCFunction)textiowrapper_tell, METH_NOARGS0x0004},
2588 {"truncate", (PyCFunction)textiowrapper_truncate, METH_VARARGS0x0001},
2589 {NULL((void*)0), NULL((void*)0)}
2590};
2591
2592static PyMemberDef textiowrapper_members[] = {
2593 {"encoding", T_OBJECT6, offsetof(textio, encoding)__builtin_offsetof(textio, encoding), READONLY1},
2594 {"buffer", T_OBJECT6, offsetof(textio, buffer)__builtin_offsetof(textio, buffer), READONLY1},
2595 {"line_buffering", T_BOOL14, offsetof(textio, line_buffering)__builtin_offsetof(textio, line_buffering), READONLY1},
2596 {NULL((void*)0)}
2597};
2598
2599static PyGetSetDef textiowrapper_getset[] = {
2600 {"name", (getter)textiowrapper_name_get, NULL((void*)0), NULL((void*)0)},
2601 {"closed", (getter)textiowrapper_closed_get, NULL((void*)0), NULL((void*)0)},
2602/* {"mode", (getter)TextIOWrapper_mode_get, NULL, NULL},
2603*/
2604 {"newlines", (getter)textiowrapper_newlines_get, NULL((void*)0), NULL((void*)0)},
2605 {"errors", (getter)textiowrapper_errors_get, NULL((void*)0), NULL((void*)0)},
2606 {"_CHUNK_SIZE", (getter)textiowrapper_chunk_size_get,
2607 (setter)textiowrapper_chunk_size_set, NULL((void*)0)},
2608 {NULL((void*)0)}
2609};
2610
2611PyTypeObject PyTextIOWrapper_Type = {
2612 PyVarObject_HEAD_INIT(NULL, 0){ { 0, 0, 1, ((void*)0) }, 0 },
2613 "_io.TextIOWrapper", /*tp_name*/
2614 sizeof(textio), /*tp_basicsize*/
2615 0, /*tp_itemsize*/
2616 (destructor)textiowrapper_dealloc, /*tp_dealloc*/
2617 0, /*tp_print*/
2618 0, /*tp_getattr*/
2619 0, /*tps_etattr*/
2620 0, /*tp_compare */
2621 (reprfunc)textiowrapper_repr,/*tp_repr*/
2622 0, /*tp_as_number*/
2623 0, /*tp_as_sequence*/
2624 0, /*tp_as_mapping*/
2625 0, /*tp_hash */
2626 0, /*tp_call*/
2627 0, /*tp_str*/
2628 0, /*tp_getattro*/
2629 0, /*tp_setattro*/
2630 0, /*tp_as_buffer*/
2631 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_BASETYPE(1L<<10)
2632 | Py_TPFLAGS_HAVE_GC(1L<<14), /*tp_flags*/
2633 textiowrapper_doc, /* tp_doc */
2634 (traverseproc)textiowrapper_traverse, /* tp_traverse */
2635 (inquiry)textiowrapper_clear, /* tp_clear */
2636 0, /* tp_richcompare */
2637 offsetof(textio, weakreflist)__builtin_offsetof(textio, weakreflist), /*tp_weaklistoffset*/
2638 0, /* tp_iter */
2639 (iternextfunc)textiowrapper_iternext, /* tp_iternext */
2640 textiowrapper_methods, /* tp_methods */
2641 textiowrapper_members, /* tp_members */
2642 textiowrapper_getset, /* tp_getset */
2643 0, /* tp_base */
2644 0, /* tp_dict */
2645 0, /* tp_descr_get */
2646 0, /* tp_descr_set */
2647 offsetof(textio, dict)__builtin_offsetof(textio, dict), /*tp_dictoffset*/
2648 (initproc)textiowrapper_init, /* tp_init */
2649 0, /* tp_alloc */
2650 PyType_GenericNew, /* tp_new */
2651};