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