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" |
| 11 | #include "structmember.h" |
| 12 | #include "_iomodule.h" |
| 13 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 14 | /*[clinic input] |
| 15 | module _io |
| 16 | class _io.IncrementalNewlineDecoder "nldecoder_object *" "&PyIncrementalNewlineDecoder_Type" |
| 17 | class _io.TextIOWrapper "textio *" "&TextIOWrapper_TYpe" |
| 18 | [clinic start generated code]*/ |
| 19 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=2097a4fc85670c26]*/ |
| 20 | |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 21 | _Py_IDENTIFIER(close); |
| 22 | _Py_IDENTIFIER(_dealloc_warn); |
| 23 | _Py_IDENTIFIER(decode); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 24 | _Py_IDENTIFIER(fileno); |
| 25 | _Py_IDENTIFIER(flush); |
| 26 | _Py_IDENTIFIER(getpreferredencoding); |
| 27 | _Py_IDENTIFIER(isatty); |
Martin v. Löwis | 767046a | 2011-10-14 15:35:36 +0200 | [diff] [blame] | 28 | _Py_IDENTIFIER(mode); |
| 29 | _Py_IDENTIFIER(name); |
| 30 | _Py_IDENTIFIER(raw); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 31 | _Py_IDENTIFIER(read); |
Martin v. Löwis | 767046a | 2011-10-14 15:35:36 +0200 | [diff] [blame] | 32 | _Py_IDENTIFIER(read1); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 33 | _Py_IDENTIFIER(readable); |
| 34 | _Py_IDENTIFIER(replace); |
| 35 | _Py_IDENTIFIER(reset); |
| 36 | _Py_IDENTIFIER(seek); |
| 37 | _Py_IDENTIFIER(seekable); |
| 38 | _Py_IDENTIFIER(setstate); |
| 39 | _Py_IDENTIFIER(tell); |
| 40 | _Py_IDENTIFIER(writable); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 41 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 42 | /* TextIOBase */ |
| 43 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 44 | PyDoc_STRVAR(textiobase_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 45 | "Base class for text I/O.\n" |
| 46 | "\n" |
| 47 | "This class provides a character and line based interface to stream\n" |
| 48 | "I/O. There is no readinto method because Python's character strings\n" |
| 49 | "are immutable. There is no public constructor.\n" |
| 50 | ); |
| 51 | |
| 52 | static PyObject * |
| 53 | _unsupported(const char *message) |
| 54 | { |
Antoine Pitrou | 712cb73 | 2013-12-21 15:51:54 +0100 | [diff] [blame] | 55 | _PyIO_State *state = IO_STATE(); |
| 56 | if (state != NULL) |
| 57 | PyErr_SetString(state->unsupported_operation, message); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 58 | return NULL; |
| 59 | } |
| 60 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 61 | PyDoc_STRVAR(textiobase_detach_doc, |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 62 | "Separate the underlying buffer from the TextIOBase and return it.\n" |
| 63 | "\n" |
| 64 | "After the underlying buffer has been detached, the TextIO is in an\n" |
| 65 | "unusable state.\n" |
| 66 | ); |
| 67 | |
| 68 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 69 | textiobase_detach(PyObject *self) |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 70 | { |
| 71 | return _unsupported("detach"); |
| 72 | } |
| 73 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 74 | PyDoc_STRVAR(textiobase_read_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 75 | "Read at most n characters from stream.\n" |
| 76 | "\n" |
| 77 | "Read from underlying buffer until we have n characters or we hit EOF.\n" |
| 78 | "If n is negative or omitted, read until EOF.\n" |
| 79 | ); |
| 80 | |
| 81 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 82 | textiobase_read(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 83 | { |
| 84 | return _unsupported("read"); |
| 85 | } |
| 86 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 87 | PyDoc_STRVAR(textiobase_readline_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 88 | "Read until newline or EOF.\n" |
| 89 | "\n" |
| 90 | "Returns an empty string if EOF is hit immediately.\n" |
| 91 | ); |
| 92 | |
| 93 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 94 | textiobase_readline(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 95 | { |
| 96 | return _unsupported("readline"); |
| 97 | } |
| 98 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 99 | PyDoc_STRVAR(textiobase_write_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 100 | "Write string to stream.\n" |
| 101 | "Returns the number of characters written (which is always equal to\n" |
| 102 | "the length of the string).\n" |
| 103 | ); |
| 104 | |
| 105 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 106 | textiobase_write(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 107 | { |
| 108 | return _unsupported("write"); |
| 109 | } |
| 110 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 111 | PyDoc_STRVAR(textiobase_encoding_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 112 | "Encoding of the text stream.\n" |
| 113 | "\n" |
| 114 | "Subclasses should override.\n" |
| 115 | ); |
| 116 | |
| 117 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 118 | textiobase_encoding_get(PyObject *self, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 119 | { |
| 120 | Py_RETURN_NONE; |
| 121 | } |
| 122 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 123 | PyDoc_STRVAR(textiobase_newlines_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 124 | "Line endings translated so far.\n" |
| 125 | "\n" |
| 126 | "Only line endings translated during reading are considered.\n" |
| 127 | "\n" |
| 128 | "Subclasses should override.\n" |
| 129 | ); |
| 130 | |
| 131 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 132 | textiobase_newlines_get(PyObject *self, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 133 | { |
| 134 | Py_RETURN_NONE; |
| 135 | } |
| 136 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 137 | PyDoc_STRVAR(textiobase_errors_doc, |
Benjamin Peterson | 0926ad1 | 2009-06-06 18:02:12 +0000 | [diff] [blame] | 138 | "The error setting of the decoder or encoder.\n" |
| 139 | "\n" |
| 140 | "Subclasses should override.\n" |
| 141 | ); |
| 142 | |
| 143 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 144 | textiobase_errors_get(PyObject *self, void *context) |
Benjamin Peterson | 0926ad1 | 2009-06-06 18:02:12 +0000 | [diff] [blame] | 145 | { |
| 146 | Py_RETURN_NONE; |
| 147 | } |
| 148 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 149 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 150 | static PyMethodDef textiobase_methods[] = { |
| 151 | {"detach", (PyCFunction)textiobase_detach, METH_NOARGS, textiobase_detach_doc}, |
| 152 | {"read", textiobase_read, METH_VARARGS, textiobase_read_doc}, |
| 153 | {"readline", textiobase_readline, METH_VARARGS, textiobase_readline_doc}, |
| 154 | {"write", textiobase_write, METH_VARARGS, textiobase_write_doc}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 155 | {NULL, NULL} |
| 156 | }; |
| 157 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 158 | static PyGetSetDef textiobase_getset[] = { |
| 159 | {"encoding", (getter)textiobase_encoding_get, NULL, textiobase_encoding_doc}, |
| 160 | {"newlines", (getter)textiobase_newlines_get, NULL, textiobase_newlines_doc}, |
| 161 | {"errors", (getter)textiobase_errors_get, NULL, textiobase_errors_doc}, |
Benjamin Peterson | 1fea321 | 2009-04-19 03:15:20 +0000 | [diff] [blame] | 162 | {NULL} |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | PyTypeObject PyTextIOBase_Type = { |
| 166 | PyVarObject_HEAD_INIT(NULL, 0) |
| 167 | "_io._TextIOBase", /*tp_name*/ |
| 168 | 0, /*tp_basicsize*/ |
| 169 | 0, /*tp_itemsize*/ |
| 170 | 0, /*tp_dealloc*/ |
| 171 | 0, /*tp_print*/ |
| 172 | 0, /*tp_getattr*/ |
| 173 | 0, /*tp_setattr*/ |
| 174 | 0, /*tp_compare */ |
| 175 | 0, /*tp_repr*/ |
| 176 | 0, /*tp_as_number*/ |
| 177 | 0, /*tp_as_sequence*/ |
| 178 | 0, /*tp_as_mapping*/ |
| 179 | 0, /*tp_hash */ |
| 180 | 0, /*tp_call*/ |
| 181 | 0, /*tp_str*/ |
| 182 | 0, /*tp_getattro*/ |
| 183 | 0, /*tp_setattro*/ |
| 184 | 0, /*tp_as_buffer*/ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 185 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 186 | | Py_TPFLAGS_HAVE_FINALIZE, /*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) { |
| 255 | self->errors = PyUnicode_FromString("strict"); |
| 256 | if (self->errors == NULL) |
| 257 | return -1; |
| 258 | } |
| 259 | else { |
| 260 | Py_INCREF(errors); |
| 261 | self->errors = errors; |
| 262 | } |
| 263 | |
| 264 | self->translate = translate; |
| 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; |
| 650 | const char *writenl; /* utf-8 encoded, NULL stands for \n */ |
| 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() */ |
| 676 | PyObject *pending_bytes; /* list of bytes objects waiting to be |
| 677 | written, or NULL */ |
| 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 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 697 | /* A couple of specialized cases in order to bypass the slow incremental |
| 698 | encoding methods for the most popular encodings. */ |
| 699 | |
| 700 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 701 | ascii_encode(textio *self, PyObject *text) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 702 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 703 | return _PyUnicode_AsASCIIString(text, PyBytes_AS_STRING(self->errors)); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 707 | utf16be_encode(textio *self, PyObject *text) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 708 | { |
Antoine Pitrou | 5c398e8 | 2011-11-13 04:11:37 +0100 | [diff] [blame] | 709 | return _PyUnicode_EncodeUTF16(text, |
| 710 | PyBytes_AS_STRING(self->errors), 1); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 714 | utf16le_encode(textio *self, PyObject *text) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 715 | { |
Antoine Pitrou | 5c398e8 | 2011-11-13 04:11:37 +0100 | [diff] [blame] | 716 | return _PyUnicode_EncodeUTF16(text, |
| 717 | PyBytes_AS_STRING(self->errors), -1); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 721 | utf16_encode(textio *self, PyObject *text) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 722 | { |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 723 | if (!self->encoding_start_of_stream) { |
| 724 | /* Skip the BOM and use native byte ordering */ |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 725 | #if PY_BIG_ENDIAN |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 726 | return utf16be_encode(self, text); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 727 | #else |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 728 | return utf16le_encode(self, text); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 729 | #endif |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 730 | } |
Antoine Pitrou | 5c398e8 | 2011-11-13 04:11:37 +0100 | [diff] [blame] | 731 | return _PyUnicode_EncodeUTF16(text, |
| 732 | PyBytes_AS_STRING(self->errors), 0); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 735 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 736 | utf32be_encode(textio *self, PyObject *text) |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 737 | { |
Antoine Pitrou | 5c398e8 | 2011-11-13 04:11:37 +0100 | [diff] [blame] | 738 | return _PyUnicode_EncodeUTF32(text, |
| 739 | PyBytes_AS_STRING(self->errors), 1); |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 743 | utf32le_encode(textio *self, PyObject *text) |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 744 | { |
Antoine Pitrou | 5c398e8 | 2011-11-13 04:11:37 +0100 | [diff] [blame] | 745 | return _PyUnicode_EncodeUTF32(text, |
| 746 | PyBytes_AS_STRING(self->errors), -1); |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 750 | utf32_encode(textio *self, PyObject *text) |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 751 | { |
| 752 | if (!self->encoding_start_of_stream) { |
| 753 | /* Skip the BOM and use native byte ordering */ |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 754 | #if PY_BIG_ENDIAN |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 755 | return utf32be_encode(self, text); |
| 756 | #else |
| 757 | return utf32le_encode(self, text); |
| 758 | #endif |
| 759 | } |
Antoine Pitrou | 5c398e8 | 2011-11-13 04:11:37 +0100 | [diff] [blame] | 760 | return _PyUnicode_EncodeUTF32(text, |
| 761 | PyBytes_AS_STRING(self->errors), 0); |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 762 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 763 | |
| 764 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 765 | utf8_encode(textio *self, PyObject *text) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 766 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 767 | return _PyUnicode_AsUTF8String(text, PyBytes_AS_STRING(self->errors)); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 771 | latin1_encode(textio *self, PyObject *text) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 772 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 773 | return _PyUnicode_AsLatin1String(text, PyBytes_AS_STRING(self->errors)); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /* Map normalized encoding names onto the specialized encoding funcs */ |
| 777 | |
| 778 | typedef struct { |
| 779 | const char *name; |
| 780 | encodefunc_t encodefunc; |
| 781 | } encodefuncentry; |
| 782 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 783 | static const encodefuncentry encodefuncs[] = { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 784 | {"ascii", (encodefunc_t) ascii_encode}, |
| 785 | {"iso8859-1", (encodefunc_t) latin1_encode}, |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 786 | {"utf-8", (encodefunc_t) utf8_encode}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 787 | {"utf-16-be", (encodefunc_t) utf16be_encode}, |
| 788 | {"utf-16-le", (encodefunc_t) utf16le_encode}, |
| 789 | {"utf-16", (encodefunc_t) utf16_encode}, |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 790 | {"utf-32-be", (encodefunc_t) utf32be_encode}, |
| 791 | {"utf-32-le", (encodefunc_t) utf32le_encode}, |
| 792 | {"utf-32", (encodefunc_t) utf32_encode}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 793 | {NULL, NULL} |
| 794 | }; |
| 795 | |
| 796 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 797 | /*[clinic input] |
| 798 | _io.TextIOWrapper.__init__ |
| 799 | buffer: object |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 800 | encoding: str(accept={str, NoneType}) = NULL |
| 801 | errors: str(accept={str, NoneType}) = NULL |
| 802 | newline: str(accept={str, NoneType}) = NULL |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 803 | line_buffering: bool(accept={int}) = False |
| 804 | write_through: bool(accept={int}) = False |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 805 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 806 | Character and line based layer over a BufferedIOBase object, buffer. |
| 807 | |
| 808 | encoding gives the name of the encoding that the stream will be |
| 809 | decoded or encoded with. It defaults to locale.getpreferredencoding(False). |
| 810 | |
| 811 | errors determines the strictness of encoding and decoding (see |
| 812 | help(codecs.Codec) or the documentation for codecs.register) and |
| 813 | defaults to "strict". |
| 814 | |
| 815 | newline controls how line endings are handled. It can be None, '', |
| 816 | '\n', '\r', and '\r\n'. It works as follows: |
| 817 | |
| 818 | * On input, if newline is None, universal newlines mode is |
| 819 | enabled. Lines in the input can end in '\n', '\r', or '\r\n', and |
| 820 | these are translated into '\n' before being returned to the |
| 821 | caller. If it is '', universal newline mode is enabled, but line |
| 822 | endings are returned to the caller untranslated. If it has any of |
| 823 | the other legal values, input lines are only terminated by the given |
| 824 | string, and the line ending is returned to the caller untranslated. |
| 825 | |
| 826 | * On output, if newline is None, any '\n' characters written are |
| 827 | translated to the system default line separator, os.linesep. If |
| 828 | newline is '' or '\n', no translation takes place. If newline is any |
| 829 | of the other legal values, any '\n' characters written are translated |
| 830 | to the given string. |
| 831 | |
| 832 | If line_buffering is True, a call to flush is implied when a call to |
| 833 | write contains a newline character. |
| 834 | [clinic start generated code]*/ |
| 835 | |
| 836 | static int |
| 837 | _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer, |
| 838 | const char *encoding, const char *errors, |
| 839 | const char *newline, int line_buffering, |
| 840 | int write_through) |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 841 | /*[clinic end generated code: output=56a83402ce2a8381 input=598d10cc5f2ed7dd]*/ |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 842 | { |
| 843 | PyObject *raw, *codec_info = NULL; |
| 844 | _PyIO_State *state = NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 845 | PyObject *res; |
| 846 | int r; |
| 847 | |
| 848 | self->ok = 0; |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 849 | self->detached = 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 850 | |
| 851 | if (newline && newline[0] != '\0' |
| 852 | && !(newline[0] == '\n' && newline[1] == '\0') |
| 853 | && !(newline[0] == '\r' && newline[1] == '\0') |
| 854 | && !(newline[0] == '\r' && newline[1] == '\n' && newline[2] == '\0')) { |
| 855 | PyErr_Format(PyExc_ValueError, |
| 856 | "illegal newline value: %s", newline); |
| 857 | return -1; |
| 858 | } |
| 859 | |
| 860 | Py_CLEAR(self->buffer); |
| 861 | Py_CLEAR(self->encoding); |
| 862 | Py_CLEAR(self->encoder); |
| 863 | Py_CLEAR(self->decoder); |
| 864 | Py_CLEAR(self->readnl); |
| 865 | Py_CLEAR(self->decoded_chars); |
| 866 | Py_CLEAR(self->pending_bytes); |
| 867 | Py_CLEAR(self->snapshot); |
| 868 | Py_CLEAR(self->errors); |
| 869 | Py_CLEAR(self->raw); |
| 870 | self->decoded_chars_used = 0; |
| 871 | self->pending_bytes_count = 0; |
| 872 | self->encodefunc = NULL; |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 873 | self->b2cratio = 0.0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 874 | |
| 875 | if (encoding == NULL) { |
| 876 | /* Try os.device_encoding(fileno) */ |
| 877 | PyObject *fileno; |
Antoine Pitrou | 712cb73 | 2013-12-21 15:51:54 +0100 | [diff] [blame] | 878 | state = IO_STATE(); |
| 879 | if (state == NULL) |
| 880 | goto error; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 881 | fileno = _PyObject_CallMethodId(buffer, &PyId_fileno, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 882 | /* Ignore only AttributeError and UnsupportedOperation */ |
| 883 | if (fileno == NULL) { |
| 884 | if (PyErr_ExceptionMatches(PyExc_AttributeError) || |
| 885 | PyErr_ExceptionMatches(state->unsupported_operation)) { |
| 886 | PyErr_Clear(); |
| 887 | } |
| 888 | else { |
| 889 | goto error; |
| 890 | } |
| 891 | } |
| 892 | else { |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 893 | int fd = _PyLong_AsInt(fileno); |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 894 | Py_DECREF(fileno); |
| 895 | if (fd == -1 && PyErr_Occurred()) { |
| 896 | goto error; |
| 897 | } |
| 898 | |
| 899 | self->encoding = _Py_device_encoding(fd); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 900 | if (self->encoding == NULL) |
| 901 | goto error; |
| 902 | else if (!PyUnicode_Check(self->encoding)) |
| 903 | Py_CLEAR(self->encoding); |
| 904 | } |
| 905 | } |
| 906 | if (encoding == NULL && self->encoding == NULL) { |
Antoine Pitrou | 932ff83 | 2013-08-01 21:04:50 +0200 | [diff] [blame] | 907 | PyObject *locale_module = _PyIO_get_locale_module(state); |
| 908 | if (locale_module == NULL) |
| 909 | goto catch_ImportError; |
Victor Stinner | 61bdb0d | 2016-12-09 15:39:28 +0100 | [diff] [blame] | 910 | self->encoding = _PyObject_CallMethodIdObjArgs( |
| 911 | locale_module, &PyId_getpreferredencoding, Py_False, NULL); |
Antoine Pitrou | 932ff83 | 2013-08-01 21:04:50 +0200 | [diff] [blame] | 912 | Py_DECREF(locale_module); |
| 913 | if (self->encoding == NULL) { |
| 914 | catch_ImportError: |
| 915 | /* |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 916 | Importing locale can raise an ImportError because of |
| 917 | _functools, and locale.getpreferredencoding can raise an |
Antoine Pitrou | 932ff83 | 2013-08-01 21:04:50 +0200 | [diff] [blame] | 918 | ImportError if _locale is not available. These will happen |
| 919 | during module building. |
| 920 | */ |
| 921 | if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 922 | PyErr_Clear(); |
| 923 | self->encoding = PyUnicode_FromString("ascii"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 924 | } |
Antoine Pitrou | 932ff83 | 2013-08-01 21:04:50 +0200 | [diff] [blame] | 925 | else |
| 926 | goto error; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 927 | } |
Antoine Pitrou | 932ff83 | 2013-08-01 21:04:50 +0200 | [diff] [blame] | 928 | else if (!PyUnicode_Check(self->encoding)) |
| 929 | Py_CLEAR(self->encoding); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 930 | } |
Victor Stinner | f6c5783 | 2010-05-19 01:17:01 +0000 | [diff] [blame] | 931 | if (self->encoding != NULL) { |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 932 | encoding = PyUnicode_AsUTF8(self->encoding); |
Victor Stinner | f6c5783 | 2010-05-19 01:17:01 +0000 | [diff] [blame] | 933 | if (encoding == NULL) |
| 934 | goto error; |
| 935 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 936 | else if (encoding != NULL) { |
| 937 | self->encoding = PyUnicode_FromString(encoding); |
| 938 | if (self->encoding == NULL) |
| 939 | goto error; |
| 940 | } |
| 941 | else { |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 942 | PyErr_SetString(PyExc_OSError, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 943 | "could not determine default encoding"); |
| 944 | } |
| 945 | |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 946 | /* Check we have been asked for a real text encoding */ |
| 947 | codec_info = _PyCodec_LookupTextEncoding(encoding, "codecs.open()"); |
| 948 | if (codec_info == NULL) { |
| 949 | Py_CLEAR(self->encoding); |
| 950 | goto error; |
| 951 | } |
| 952 | |
| 953 | /* XXX: Failures beyond this point have the potential to leak elements |
| 954 | * of the partially constructed object (like self->encoding) |
| 955 | */ |
| 956 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 957 | if (errors == NULL) |
| 958 | errors = "strict"; |
| 959 | self->errors = PyBytes_FromString(errors); |
| 960 | if (self->errors == NULL) |
| 961 | goto error; |
| 962 | |
| 963 | self->chunk_size = 8192; |
| 964 | self->readuniversal = (newline == NULL || newline[0] == '\0'); |
| 965 | self->line_buffering = line_buffering; |
Antoine Pitrou | e96ec68 | 2011-07-23 21:46:35 +0200 | [diff] [blame] | 966 | self->write_through = write_through; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 967 | self->readtranslate = (newline == NULL); |
| 968 | if (newline) { |
| 969 | self->readnl = PyUnicode_FromString(newline); |
| 970 | if (self->readnl == NULL) |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 971 | goto error; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 972 | } |
| 973 | self->writetranslate = (newline == NULL || newline[0] != '\0'); |
| 974 | if (!self->readuniversal && self->readnl) { |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 975 | self->writenl = PyUnicode_AsUTF8(self->readnl); |
Victor Stinner | f6c5783 | 2010-05-19 01:17:01 +0000 | [diff] [blame] | 976 | if (self->writenl == NULL) |
| 977 | goto error; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 978 | if (!strcmp(self->writenl, "\n")) |
| 979 | self->writenl = NULL; |
| 980 | } |
| 981 | #ifdef MS_WINDOWS |
| 982 | else |
| 983 | self->writenl = "\r\n"; |
| 984 | #endif |
| 985 | |
| 986 | /* Build the decoder object */ |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 987 | res = _PyObject_CallMethodId(buffer, &PyId_readable, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 988 | if (res == NULL) |
| 989 | goto error; |
| 990 | r = PyObject_IsTrue(res); |
| 991 | Py_DECREF(res); |
| 992 | if (r == -1) |
| 993 | goto error; |
| 994 | if (r == 1) { |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 995 | self->decoder = _PyCodecInfo_GetIncrementalDecoder(codec_info, |
| 996 | errors); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 997 | if (self->decoder == NULL) |
| 998 | goto error; |
| 999 | |
| 1000 | if (self->readuniversal) { |
| 1001 | PyObject *incrementalDecoder = PyObject_CallFunction( |
| 1002 | (PyObject *)&PyIncrementalNewlineDecoder_Type, |
| 1003 | "Oi", self->decoder, (int)self->readtranslate); |
| 1004 | if (incrementalDecoder == NULL) |
| 1005 | goto error; |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 1006 | Py_XSETREF(self->decoder, incrementalDecoder); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | /* Build the encoder object */ |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1011 | res = _PyObject_CallMethodId(buffer, &PyId_writable, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1012 | if (res == NULL) |
| 1013 | goto error; |
| 1014 | r = PyObject_IsTrue(res); |
| 1015 | Py_DECREF(res); |
| 1016 | if (r == -1) |
| 1017 | goto error; |
| 1018 | if (r == 1) { |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 1019 | self->encoder = _PyCodecInfo_GetIncrementalEncoder(codec_info, |
| 1020 | errors); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1021 | if (self->encoder == NULL) |
| 1022 | goto error; |
Martin Panter | 536d70e | 2017-01-14 08:23:08 +0000 | [diff] [blame] | 1023 | /* Get the normalized name of the codec */ |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 1024 | res = _PyObject_GetAttrId(codec_info, &PyId_name); |
Benjamin Peterson | 2cfca79 | 2009-06-06 20:46:48 +0000 | [diff] [blame] | 1025 | if (res == NULL) { |
| 1026 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1027 | PyErr_Clear(); |
| 1028 | else |
| 1029 | goto error; |
| 1030 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1031 | else if (PyUnicode_Check(res)) { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1032 | const encodefuncentry *e = encodefuncs; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1033 | while (e->name != NULL) { |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1034 | if (_PyUnicode_EqualToASCIIString(res, e->name)) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1035 | self->encodefunc = e->encodefunc; |
| 1036 | break; |
| 1037 | } |
| 1038 | e++; |
| 1039 | } |
| 1040 | } |
| 1041 | Py_XDECREF(res); |
| 1042 | } |
| 1043 | |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 1044 | /* Finished sorting out the codec details */ |
Benjamin Peterson | 6c14f23 | 2014-11-12 10:19:46 -0500 | [diff] [blame] | 1045 | Py_CLEAR(codec_info); |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 1046 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1047 | self->buffer = buffer; |
| 1048 | Py_INCREF(buffer); |
Antoine Pitrou | 24f3629 | 2009-03-28 22:16:42 +0000 | [diff] [blame] | 1049 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1050 | if (Py_TYPE(buffer) == &PyBufferedReader_Type || |
| 1051 | Py_TYPE(buffer) == &PyBufferedWriter_Type || |
| 1052 | Py_TYPE(buffer) == &PyBufferedRandom_Type) { |
Martin v. Löwis | 767046a | 2011-10-14 15:35:36 +0200 | [diff] [blame] | 1053 | raw = _PyObject_GetAttrId(buffer, &PyId_raw); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1054 | /* Cache the raw FileIO object to speed up 'closed' checks */ |
Benjamin Peterson | 2cfca79 | 2009-06-06 20:46:48 +0000 | [diff] [blame] | 1055 | if (raw == NULL) { |
| 1056 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1057 | PyErr_Clear(); |
| 1058 | else |
| 1059 | goto error; |
| 1060 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1061 | else if (Py_TYPE(raw) == &PyFileIO_Type) |
| 1062 | self->raw = raw; |
| 1063 | else |
| 1064 | Py_DECREF(raw); |
| 1065 | } |
| 1066 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1067 | res = _PyObject_CallMethodId(buffer, &PyId_seekable, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1068 | if (res == NULL) |
| 1069 | goto error; |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 1070 | r = PyObject_IsTrue(res); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1071 | Py_DECREF(res); |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 1072 | if (r < 0) |
| 1073 | goto error; |
| 1074 | self->seekable = self->telling = r; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1075 | |
Martin v. Löwis | 767046a | 2011-10-14 15:35:36 +0200 | [diff] [blame] | 1076 | self->has_read1 = _PyObject_HasAttrId(buffer, &PyId_read1); |
Antoine Pitrou | e96ec68 | 2011-07-23 21:46:35 +0200 | [diff] [blame] | 1077 | |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 1078 | self->encoding_start_of_stream = 0; |
| 1079 | if (self->seekable && self->encoder) { |
| 1080 | PyObject *cookieObj; |
| 1081 | int cmp; |
| 1082 | |
| 1083 | self->encoding_start_of_stream = 1; |
| 1084 | |
| 1085 | cookieObj = PyObject_CallMethodObjArgs(buffer, _PyIO_str_tell, NULL); |
| 1086 | if (cookieObj == NULL) |
| 1087 | goto error; |
| 1088 | |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 1089 | cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ); |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 1090 | Py_DECREF(cookieObj); |
| 1091 | if (cmp < 0) { |
| 1092 | goto error; |
| 1093 | } |
| 1094 | |
| 1095 | if (cmp == 0) { |
| 1096 | self->encoding_start_of_stream = 0; |
| 1097 | res = PyObject_CallMethodObjArgs(self->encoder, _PyIO_str_setstate, |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 1098 | _PyLong_Zero, NULL); |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 1099 | if (res == NULL) |
| 1100 | goto error; |
| 1101 | Py_DECREF(res); |
| 1102 | } |
| 1103 | } |
| 1104 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1105 | self->ok = 1; |
| 1106 | return 0; |
| 1107 | |
| 1108 | error: |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 1109 | Py_XDECREF(codec_info); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1110 | return -1; |
| 1111 | } |
| 1112 | |
Antoine Pitrou | 3c2817b | 2017-06-03 12:32:28 +0200 | [diff] [blame] | 1113 | /* Return *default_value* if ob is None, 0 if ob is false, 1 if ob is true, |
| 1114 | * -1 on error. |
| 1115 | */ |
| 1116 | static int |
| 1117 | convert_optional_bool(PyObject *obj, int default_value) |
| 1118 | { |
| 1119 | long v; |
| 1120 | if (obj == Py_None) { |
| 1121 | v = default_value; |
| 1122 | } |
| 1123 | else { |
| 1124 | v = PyLong_AsLong(obj); |
| 1125 | if (v == -1 && PyErr_Occurred()) |
| 1126 | return -1; |
| 1127 | } |
| 1128 | return v != 0; |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | /*[clinic input] |
| 1133 | _io.TextIOWrapper.reconfigure |
| 1134 | * |
| 1135 | line_buffering as line_buffering_obj: object = None |
| 1136 | write_through as write_through_obj: object = None |
| 1137 | |
| 1138 | Reconfigure the text stream with new parameters. |
| 1139 | |
| 1140 | This also does an implicit stream flush. |
| 1141 | |
| 1142 | [clinic start generated code]*/ |
| 1143 | |
| 1144 | static PyObject * |
| 1145 | _io_TextIOWrapper_reconfigure_impl(textio *self, |
| 1146 | PyObject *line_buffering_obj, |
| 1147 | PyObject *write_through_obj) |
| 1148 | /*[clinic end generated code: output=7cdf79e7001e2856 input=baade27ecb9db7bc]*/ |
| 1149 | { |
| 1150 | int line_buffering; |
| 1151 | int write_through; |
| 1152 | PyObject *res; |
| 1153 | |
| 1154 | line_buffering = convert_optional_bool(line_buffering_obj, |
| 1155 | self->line_buffering); |
| 1156 | write_through = convert_optional_bool(write_through_obj, |
| 1157 | self->write_through); |
| 1158 | if (line_buffering < 0 || write_through < 0) { |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_flush, NULL); |
| 1162 | Py_XDECREF(res); |
| 1163 | if (res == NULL) { |
| 1164 | return NULL; |
| 1165 | } |
| 1166 | self->line_buffering = line_buffering; |
| 1167 | self->write_through = write_through; |
| 1168 | Py_RETURN_NONE; |
| 1169 | } |
| 1170 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1171 | static int |
Serhiy Storchaka | a7c972e | 2016-11-03 15:37:01 +0200 | [diff] [blame] | 1172 | textiowrapper_clear(textio *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1173 | { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1174 | self->ok = 0; |
| 1175 | Py_CLEAR(self->buffer); |
| 1176 | Py_CLEAR(self->encoding); |
| 1177 | Py_CLEAR(self->encoder); |
| 1178 | Py_CLEAR(self->decoder); |
| 1179 | Py_CLEAR(self->readnl); |
| 1180 | Py_CLEAR(self->decoded_chars); |
| 1181 | Py_CLEAR(self->pending_bytes); |
| 1182 | Py_CLEAR(self->snapshot); |
| 1183 | Py_CLEAR(self->errors); |
| 1184 | Py_CLEAR(self->raw); |
Serhiy Storchaka | a7c972e | 2016-11-03 15:37:01 +0200 | [diff] [blame] | 1185 | |
| 1186 | Py_CLEAR(self->dict); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1187 | return 0; |
| 1188 | } |
| 1189 | |
| 1190 | static void |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1191 | textiowrapper_dealloc(textio *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1192 | { |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1193 | self->finalizing = 1; |
| 1194 | if (_PyIOBase_finalize((PyObject *) self) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1195 | return; |
Serhiy Storchaka | a7c972e | 2016-11-03 15:37:01 +0200 | [diff] [blame] | 1196 | self->ok = 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1197 | _PyObject_GC_UNTRACK(self); |
| 1198 | if (self->weakreflist != NULL) |
| 1199 | PyObject_ClearWeakRefs((PyObject *)self); |
Serhiy Storchaka | a7c972e | 2016-11-03 15:37:01 +0200 | [diff] [blame] | 1200 | textiowrapper_clear(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1201 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 1202 | } |
| 1203 | |
| 1204 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1205 | textiowrapper_traverse(textio *self, visitproc visit, void *arg) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1206 | { |
| 1207 | Py_VISIT(self->buffer); |
| 1208 | Py_VISIT(self->encoding); |
| 1209 | Py_VISIT(self->encoder); |
| 1210 | Py_VISIT(self->decoder); |
| 1211 | Py_VISIT(self->readnl); |
| 1212 | Py_VISIT(self->decoded_chars); |
| 1213 | Py_VISIT(self->pending_bytes); |
| 1214 | Py_VISIT(self->snapshot); |
| 1215 | Py_VISIT(self->errors); |
| 1216 | Py_VISIT(self->raw); |
| 1217 | |
| 1218 | Py_VISIT(self->dict); |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1222 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1223 | textiowrapper_closed_get(textio *self, void *context); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1224 | |
| 1225 | /* This macro takes some shortcuts to make the common case faster. */ |
| 1226 | #define CHECK_CLOSED(self) \ |
| 1227 | do { \ |
| 1228 | int r; \ |
| 1229 | PyObject *_res; \ |
| 1230 | if (Py_TYPE(self) == &PyTextIOWrapper_Type) { \ |
| 1231 | if (self->raw != NULL) \ |
| 1232 | r = _PyFileIO_closed(self->raw); \ |
| 1233 | else { \ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1234 | _res = textiowrapper_closed_get(self, NULL); \ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1235 | if (_res == NULL) \ |
| 1236 | return NULL; \ |
| 1237 | r = PyObject_IsTrue(_res); \ |
| 1238 | Py_DECREF(_res); \ |
| 1239 | if (r < 0) \ |
| 1240 | return NULL; \ |
| 1241 | } \ |
| 1242 | if (r > 0) { \ |
| 1243 | PyErr_SetString(PyExc_ValueError, \ |
| 1244 | "I/O operation on closed file."); \ |
| 1245 | return NULL; \ |
| 1246 | } \ |
| 1247 | } \ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1248 | else if (_PyIOBase_check_closed((PyObject *)self, Py_True) == NULL) \ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1249 | return NULL; \ |
| 1250 | } while (0) |
| 1251 | |
| 1252 | #define CHECK_INITIALIZED(self) \ |
| 1253 | if (self->ok <= 0) { \ |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 1254 | PyErr_SetString(PyExc_ValueError, \ |
| 1255 | "I/O operation on uninitialized object"); \ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1256 | return NULL; \ |
| 1257 | } |
| 1258 | |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 1259 | #define CHECK_ATTACHED(self) \ |
| 1260 | CHECK_INITIALIZED(self); \ |
| 1261 | if (self->detached) { \ |
| 1262 | PyErr_SetString(PyExc_ValueError, \ |
| 1263 | "underlying buffer has been detached"); \ |
| 1264 | return NULL; \ |
| 1265 | } |
| 1266 | |
| 1267 | #define CHECK_ATTACHED_INT(self) \ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1268 | if (self->ok <= 0) { \ |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 1269 | PyErr_SetString(PyExc_ValueError, \ |
| 1270 | "I/O operation on uninitialized object"); \ |
| 1271 | return -1; \ |
| 1272 | } else if (self->detached) { \ |
| 1273 | PyErr_SetString(PyExc_ValueError, \ |
| 1274 | "underlying buffer has been detached"); \ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1275 | return -1; \ |
| 1276 | } |
| 1277 | |
| 1278 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 1279 | /*[clinic input] |
| 1280 | _io.TextIOWrapper.detach |
| 1281 | [clinic start generated code]*/ |
| 1282 | |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 1283 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 1284 | _io_TextIOWrapper_detach_impl(textio *self) |
| 1285 | /*[clinic end generated code: output=7ba3715cd032d5f2 input=e5a71fbda9e1d9f9]*/ |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 1286 | { |
| 1287 | PyObject *buffer, *res; |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 1288 | CHECK_ATTACHED(self); |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 1289 | res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL); |
| 1290 | if (res == NULL) |
| 1291 | return NULL; |
| 1292 | Py_DECREF(res); |
| 1293 | buffer = self->buffer; |
| 1294 | self->buffer = NULL; |
| 1295 | self->detached = 1; |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 1296 | return buffer; |
| 1297 | } |
| 1298 | |
Antoine Pitrou | 24f3629 | 2009-03-28 22:16:42 +0000 | [diff] [blame] | 1299 | /* Flush the internal write buffer. This doesn't explicitly flush the |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1300 | underlying buffered object, though. */ |
| 1301 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1302 | _textiowrapper_writeflush(textio *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1303 | { |
Amaury Forgeot d'Arc | ccd686a | 2009-08-29 23:00:38 +0000 | [diff] [blame] | 1304 | PyObject *pending, *b, *ret; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1305 | |
| 1306 | if (self->pending_bytes == NULL) |
| 1307 | return 0; |
Amaury Forgeot d'Arc | ccd686a | 2009-08-29 23:00:38 +0000 | [diff] [blame] | 1308 | |
| 1309 | pending = self->pending_bytes; |
| 1310 | Py_INCREF(pending); |
| 1311 | self->pending_bytes_count = 0; |
| 1312 | Py_CLEAR(self->pending_bytes); |
| 1313 | |
| 1314 | b = _PyBytes_Join(_PyIO_empty_bytes, pending); |
| 1315 | Py_DECREF(pending); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1316 | if (b == NULL) |
| 1317 | return -1; |
Gregory P. Smith | b9817b0 | 2013-02-01 13:03:39 -0800 | [diff] [blame] | 1318 | ret = NULL; |
| 1319 | do { |
| 1320 | ret = PyObject_CallMethodObjArgs(self->buffer, |
| 1321 | _PyIO_str_write, b, NULL); |
| 1322 | } while (ret == NULL && _PyIO_trap_eintr()); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1323 | Py_DECREF(b); |
| 1324 | if (ret == NULL) |
| 1325 | return -1; |
| 1326 | Py_DECREF(ret); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1327 | return 0; |
| 1328 | } |
| 1329 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 1330 | /*[clinic input] |
| 1331 | _io.TextIOWrapper.write |
| 1332 | text: unicode |
| 1333 | / |
| 1334 | [clinic start generated code]*/ |
| 1335 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1336 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 1337 | _io_TextIOWrapper_write_impl(textio *self, PyObject *text) |
| 1338 | /*[clinic end generated code: output=d2deb0d50771fcec input=fdf19153584a0e44]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1339 | { |
| 1340 | PyObject *ret; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1341 | PyObject *b; |
| 1342 | Py_ssize_t textlen; |
| 1343 | int haslf = 0; |
Antoine Pitrou | c644e7c | 2014-05-09 00:24:50 +0200 | [diff] [blame] | 1344 | int needflush = 0, text_needflush = 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1345 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1346 | if (PyUnicode_READY(text) == -1) |
| 1347 | return NULL; |
| 1348 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 1349 | CHECK_ATTACHED(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1350 | CHECK_CLOSED(self); |
| 1351 | |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 1352 | if (self->encoder == NULL) |
| 1353 | return _unsupported("not writable"); |
Benjamin Peterson | 81971ea | 2009-05-14 22:01:31 +0000 | [diff] [blame] | 1354 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1355 | Py_INCREF(text); |
| 1356 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1357 | textlen = PyUnicode_GET_LENGTH(text); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1358 | |
| 1359 | if ((self->writetranslate && self->writenl != NULL) || self->line_buffering) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1360 | if (PyUnicode_FindChar(text, '\n', 0, PyUnicode_GET_LENGTH(text), 1) != -1) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1361 | haslf = 1; |
| 1362 | |
| 1363 | if (haslf && self->writetranslate && self->writenl != NULL) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1364 | PyObject *newtext = _PyObject_CallMethodId( |
| 1365 | text, &PyId_replace, "ss", "\n", self->writenl); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1366 | Py_DECREF(text); |
| 1367 | if (newtext == NULL) |
| 1368 | return NULL; |
| 1369 | text = newtext; |
| 1370 | } |
| 1371 | |
Antoine Pitrou | e96ec68 | 2011-07-23 21:46:35 +0200 | [diff] [blame] | 1372 | if (self->write_through) |
Antoine Pitrou | c644e7c | 2014-05-09 00:24:50 +0200 | [diff] [blame] | 1373 | text_needflush = 1; |
| 1374 | if (self->line_buffering && |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1375 | (haslf || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1376 | PyUnicode_FindChar(text, '\r', 0, PyUnicode_GET_LENGTH(text), 1) != -1)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1377 | needflush = 1; |
| 1378 | |
| 1379 | /* XXX What if we were just reading? */ |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 1380 | if (self->encodefunc != NULL) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1381 | b = (*self->encodefunc)((PyObject *) self, text); |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 1382 | self->encoding_start_of_stream = 0; |
| 1383 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1384 | else |
| 1385 | b = PyObject_CallMethodObjArgs(self->encoder, |
| 1386 | _PyIO_str_encode, text, NULL); |
| 1387 | Py_DECREF(text); |
| 1388 | if (b == NULL) |
| 1389 | return NULL; |
Oren Milman | a5b4ea1 | 2017-08-25 21:14:54 +0300 | [diff] [blame] | 1390 | if (!PyBytes_Check(b)) { |
| 1391 | PyErr_Format(PyExc_TypeError, |
| 1392 | "encoder should return a bytes object, not '%.200s'", |
| 1393 | Py_TYPE(b)->tp_name); |
| 1394 | Py_DECREF(b); |
| 1395 | return NULL; |
| 1396 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1397 | |
| 1398 | if (self->pending_bytes == NULL) { |
| 1399 | self->pending_bytes = PyList_New(0); |
| 1400 | if (self->pending_bytes == NULL) { |
| 1401 | Py_DECREF(b); |
| 1402 | return NULL; |
| 1403 | } |
| 1404 | self->pending_bytes_count = 0; |
| 1405 | } |
| 1406 | if (PyList_Append(self->pending_bytes, b) < 0) { |
| 1407 | Py_DECREF(b); |
| 1408 | return NULL; |
| 1409 | } |
| 1410 | self->pending_bytes_count += PyBytes_GET_SIZE(b); |
| 1411 | Py_DECREF(b); |
Antoine Pitrou | c644e7c | 2014-05-09 00:24:50 +0200 | [diff] [blame] | 1412 | if (self->pending_bytes_count > self->chunk_size || needflush || |
| 1413 | text_needflush) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1414 | if (_textiowrapper_writeflush(self) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1415 | return NULL; |
| 1416 | } |
Antoine Pitrou | 24f3629 | 2009-03-28 22:16:42 +0000 | [diff] [blame] | 1417 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1418 | if (needflush) { |
| 1419 | ret = PyObject_CallMethodObjArgs(self->buffer, _PyIO_str_flush, NULL); |
| 1420 | if (ret == NULL) |
| 1421 | return NULL; |
| 1422 | Py_DECREF(ret); |
| 1423 | } |
| 1424 | |
| 1425 | Py_CLEAR(self->snapshot); |
| 1426 | |
| 1427 | if (self->decoder) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1428 | ret = _PyObject_CallMethodId(self->decoder, &PyId_reset, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1429 | if (ret == NULL) |
| 1430 | return NULL; |
| 1431 | Py_DECREF(ret); |
| 1432 | } |
| 1433 | |
| 1434 | return PyLong_FromSsize_t(textlen); |
| 1435 | } |
| 1436 | |
| 1437 | /* Steal a reference to chars and store it in the decoded_char buffer; |
| 1438 | */ |
| 1439 | static void |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1440 | textiowrapper_set_decoded_chars(textio *self, PyObject *chars) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1441 | { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 1442 | Py_XSETREF(self->decoded_chars, chars); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1443 | self->decoded_chars_used = 0; |
| 1444 | } |
| 1445 | |
| 1446 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1447 | textiowrapper_get_decoded_chars(textio *self, Py_ssize_t n) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1448 | { |
| 1449 | PyObject *chars; |
| 1450 | Py_ssize_t avail; |
| 1451 | |
| 1452 | if (self->decoded_chars == NULL) |
| 1453 | return PyUnicode_FromStringAndSize(NULL, 0); |
| 1454 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1455 | /* decoded_chars is guaranteed to be "ready". */ |
| 1456 | avail = (PyUnicode_GET_LENGTH(self->decoded_chars) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1457 | - self->decoded_chars_used); |
| 1458 | |
| 1459 | assert(avail >= 0); |
| 1460 | |
| 1461 | if (n < 0 || n > avail) |
| 1462 | n = avail; |
| 1463 | |
| 1464 | if (self->decoded_chars_used > 0 || n < avail) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1465 | chars = PyUnicode_Substring(self->decoded_chars, |
| 1466 | self->decoded_chars_used, |
| 1467 | self->decoded_chars_used + n); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1468 | if (chars == NULL) |
| 1469 | return NULL; |
| 1470 | } |
| 1471 | else { |
| 1472 | chars = self->decoded_chars; |
| 1473 | Py_INCREF(chars); |
| 1474 | } |
| 1475 | |
| 1476 | self->decoded_chars_used += n; |
| 1477 | return chars; |
| 1478 | } |
| 1479 | |
| 1480 | /* Read and decode the next chunk of data from the BufferedReader. |
| 1481 | */ |
| 1482 | static int |
Antoine Pitrou | e532456 | 2011-11-19 00:39:01 +0100 | [diff] [blame] | 1483 | textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1484 | { |
| 1485 | PyObject *dec_buffer = NULL; |
| 1486 | PyObject *dec_flags = NULL; |
| 1487 | PyObject *input_chunk = NULL; |
Antoine Pitrou | b850389 | 2014-04-29 10:14:02 +0200 | [diff] [blame] | 1488 | Py_buffer input_chunk_buf; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1489 | PyObject *decoded_chars, *chunk_size; |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 1490 | Py_ssize_t nbytes, nchars; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1491 | int eof; |
| 1492 | |
| 1493 | /* The return value is True unless EOF was reached. The decoded string is |
| 1494 | * placed in self._decoded_chars (replacing its previous value). The |
| 1495 | * entire input chunk is sent to the decoder, though some of it may remain |
| 1496 | * buffered in the decoder, yet to be converted. |
| 1497 | */ |
| 1498 | |
| 1499 | if (self->decoder == NULL) { |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 1500 | _unsupported("not readable"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1501 | return -1; |
| 1502 | } |
| 1503 | |
| 1504 | if (self->telling) { |
| 1505 | /* To prepare for tell(), we need to snapshot a point in the file |
| 1506 | * where the decoder's input buffer is empty. |
| 1507 | */ |
| 1508 | |
| 1509 | PyObject *state = PyObject_CallMethodObjArgs(self->decoder, |
| 1510 | _PyIO_str_getstate, NULL); |
| 1511 | if (state == NULL) |
| 1512 | return -1; |
| 1513 | /* Given this, we know there was a valid snapshot point |
| 1514 | * len(dec_buffer) bytes ago with decoder state (b'', dec_flags). |
| 1515 | */ |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1516 | if (!PyTuple_Check(state)) { |
| 1517 | PyErr_SetString(PyExc_TypeError, |
| 1518 | "illegal decoder state"); |
| 1519 | Py_DECREF(state); |
| 1520 | return -1; |
| 1521 | } |
| 1522 | if (!PyArg_ParseTuple(state, |
| 1523 | "OO;illegal decoder state", &dec_buffer, &dec_flags)) |
| 1524 | { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1525 | Py_DECREF(state); |
| 1526 | return -1; |
| 1527 | } |
Antoine Pitrou | b850389 | 2014-04-29 10:14:02 +0200 | [diff] [blame] | 1528 | |
| 1529 | if (!PyBytes_Check(dec_buffer)) { |
| 1530 | PyErr_Format(PyExc_TypeError, |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1531 | "illegal decoder state: the first item should be a " |
| 1532 | "bytes object, not '%.200s'", |
Antoine Pitrou | b850389 | 2014-04-29 10:14:02 +0200 | [diff] [blame] | 1533 | Py_TYPE(dec_buffer)->tp_name); |
| 1534 | Py_DECREF(state); |
| 1535 | return -1; |
| 1536 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1537 | Py_INCREF(dec_buffer); |
| 1538 | Py_INCREF(dec_flags); |
| 1539 | Py_DECREF(state); |
| 1540 | } |
| 1541 | |
| 1542 | /* 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] | 1543 | if (size_hint > 0) { |
Victor Stinner | f8facac | 2011-11-22 02:30:47 +0100 | [diff] [blame] | 1544 | 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] | 1545 | } |
| 1546 | chunk_size = PyLong_FromSsize_t(Py_MAX(self->chunk_size, size_hint)); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1547 | if (chunk_size == NULL) |
| 1548 | goto fail; |
Antoine Pitrou | b850389 | 2014-04-29 10:14:02 +0200 | [diff] [blame] | 1549 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1550 | input_chunk = PyObject_CallMethodObjArgs(self->buffer, |
Antoine Pitrou | e96ec68 | 2011-07-23 21:46:35 +0200 | [diff] [blame] | 1551 | (self->has_read1 ? _PyIO_str_read1: _PyIO_str_read), |
| 1552 | chunk_size, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1553 | Py_DECREF(chunk_size); |
| 1554 | if (input_chunk == NULL) |
| 1555 | goto fail; |
Antoine Pitrou | b850389 | 2014-04-29 10:14:02 +0200 | [diff] [blame] | 1556 | |
| 1557 | if (PyObject_GetBuffer(input_chunk, &input_chunk_buf, 0) != 0) { |
Serhiy Storchaka | 94dc673 | 2013-02-03 17:03:31 +0200 | [diff] [blame] | 1558 | PyErr_Format(PyExc_TypeError, |
Antoine Pitrou | b850389 | 2014-04-29 10:14:02 +0200 | [diff] [blame] | 1559 | "underlying %s() should have returned a bytes-like object, " |
Serhiy Storchaka | 94dc673 | 2013-02-03 17:03:31 +0200 | [diff] [blame] | 1560 | "not '%.200s'", (self->has_read1 ? "read1": "read"), |
| 1561 | Py_TYPE(input_chunk)->tp_name); |
| 1562 | goto fail; |
| 1563 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1564 | |
Antoine Pitrou | b850389 | 2014-04-29 10:14:02 +0200 | [diff] [blame] | 1565 | nbytes = input_chunk_buf.len; |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 1566 | eof = (nbytes == 0); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1567 | if (Py_TYPE(self->decoder) == &PyIncrementalNewlineDecoder_Type) { |
| 1568 | decoded_chars = _PyIncrementalNewlineDecoder_decode( |
| 1569 | self->decoder, input_chunk, eof); |
| 1570 | } |
| 1571 | else { |
| 1572 | decoded_chars = PyObject_CallMethodObjArgs(self->decoder, |
| 1573 | _PyIO_str_decode, input_chunk, eof ? Py_True : Py_False, NULL); |
| 1574 | } |
Antoine Pitrou | b850389 | 2014-04-29 10:14:02 +0200 | [diff] [blame] | 1575 | PyBuffer_Release(&input_chunk_buf); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1576 | |
Serhiy Storchaka | 94dc673 | 2013-02-03 17:03:31 +0200 | [diff] [blame] | 1577 | if (check_decoded(decoded_chars) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1578 | goto fail; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1579 | textiowrapper_set_decoded_chars(self, decoded_chars); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1580 | nchars = PyUnicode_GET_LENGTH(decoded_chars); |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 1581 | if (nchars > 0) |
| 1582 | self->b2cratio = (double) nbytes / nchars; |
| 1583 | else |
| 1584 | self->b2cratio = 0.0; |
| 1585 | if (nchars > 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1586 | eof = 0; |
| 1587 | |
| 1588 | if (self->telling) { |
| 1589 | /* At the snapshot point, len(dec_buffer) bytes before the read, the |
| 1590 | * next input to be decoded is dec_buffer + input_chunk. |
| 1591 | */ |
Antoine Pitrou | b850389 | 2014-04-29 10:14:02 +0200 | [diff] [blame] | 1592 | PyObject *next_input = dec_buffer; |
| 1593 | PyBytes_Concat(&next_input, input_chunk); |
| 1594 | if (next_input == NULL) { |
| 1595 | dec_buffer = NULL; /* Reference lost to PyBytes_Concat */ |
Serhiy Storchaka | 94dc673 | 2013-02-03 17:03:31 +0200 | [diff] [blame] | 1596 | goto fail; |
| 1597 | } |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 1598 | Py_XSETREF(self->snapshot, Py_BuildValue("NN", dec_flags, next_input)); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1599 | } |
| 1600 | Py_DECREF(input_chunk); |
| 1601 | |
| 1602 | return (eof == 0); |
| 1603 | |
| 1604 | fail: |
| 1605 | Py_XDECREF(dec_buffer); |
| 1606 | Py_XDECREF(dec_flags); |
| 1607 | Py_XDECREF(input_chunk); |
| 1608 | return -1; |
| 1609 | } |
| 1610 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 1611 | /*[clinic input] |
| 1612 | _io.TextIOWrapper.read |
Serhiy Storchaka | 762bf40 | 2017-03-30 09:15:31 +0300 | [diff] [blame] | 1613 | size as n: Py_ssize_t(accept={int, NoneType}) = -1 |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 1614 | / |
| 1615 | [clinic start generated code]*/ |
| 1616 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1617 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 1618 | _io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n) |
Serhiy Storchaka | 762bf40 | 2017-03-30 09:15:31 +0300 | [diff] [blame] | 1619 | /*[clinic end generated code: output=7e651ce6cc6a25a6 input=123eecbfe214aeb8]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1620 | { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1621 | PyObject *result = NULL, *chunks = NULL; |
| 1622 | |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 1623 | CHECK_ATTACHED(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1624 | CHECK_CLOSED(self); |
| 1625 | |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 1626 | if (self->decoder == NULL) |
| 1627 | return _unsupported("not readable"); |
Benjamin Peterson | a1b4901 | 2009-03-31 23:11:32 +0000 | [diff] [blame] | 1628 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1629 | if (_textiowrapper_writeflush(self) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1630 | return NULL; |
| 1631 | |
| 1632 | if (n < 0) { |
| 1633 | /* Read everything */ |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1634 | PyObject *bytes = _PyObject_CallMethodId(self->buffer, &PyId_read, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1635 | PyObject *decoded; |
| 1636 | if (bytes == NULL) |
| 1637 | goto fail; |
Victor Stinner | fd82113 | 2011-05-25 22:01:33 +0200 | [diff] [blame] | 1638 | |
| 1639 | if (Py_TYPE(self->decoder) == &PyIncrementalNewlineDecoder_Type) |
| 1640 | decoded = _PyIncrementalNewlineDecoder_decode(self->decoder, |
| 1641 | bytes, 1); |
| 1642 | else |
| 1643 | decoded = PyObject_CallMethodObjArgs( |
| 1644 | self->decoder, _PyIO_str_decode, bytes, Py_True, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1645 | Py_DECREF(bytes); |
Serhiy Storchaka | 94dc673 | 2013-02-03 17:03:31 +0200 | [diff] [blame] | 1646 | if (check_decoded(decoded) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1647 | goto fail; |
| 1648 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1649 | result = textiowrapper_get_decoded_chars(self, -1); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1650 | |
| 1651 | if (result == NULL) { |
| 1652 | Py_DECREF(decoded); |
| 1653 | return NULL; |
| 1654 | } |
| 1655 | |
| 1656 | PyUnicode_AppendAndDel(&result, decoded); |
| 1657 | if (result == NULL) |
| 1658 | goto fail; |
| 1659 | |
| 1660 | Py_CLEAR(self->snapshot); |
| 1661 | return result; |
| 1662 | } |
| 1663 | else { |
| 1664 | int res = 1; |
| 1665 | Py_ssize_t remaining = n; |
| 1666 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1667 | result = textiowrapper_get_decoded_chars(self, n); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1668 | if (result == NULL) |
| 1669 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1670 | if (PyUnicode_READY(result) == -1) |
| 1671 | goto fail; |
| 1672 | remaining -= PyUnicode_GET_LENGTH(result); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1673 | |
| 1674 | /* Keep reading chunks until we have n characters to return */ |
| 1675 | while (remaining > 0) { |
Antoine Pitrou | e532456 | 2011-11-19 00:39:01 +0100 | [diff] [blame] | 1676 | res = textiowrapper_read_chunk(self, remaining); |
Gregory P. Smith | 5135992 | 2012-06-23 23:55:39 -0700 | [diff] [blame] | 1677 | if (res < 0) { |
| 1678 | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() |
| 1679 | when EINTR occurs so we needn't do it ourselves. */ |
| 1680 | if (_PyIO_trap_eintr()) { |
| 1681 | continue; |
| 1682 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1683 | goto fail; |
Gregory P. Smith | 5135992 | 2012-06-23 23:55:39 -0700 | [diff] [blame] | 1684 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1685 | if (res == 0) /* EOF */ |
| 1686 | break; |
| 1687 | if (chunks == NULL) { |
| 1688 | chunks = PyList_New(0); |
| 1689 | if (chunks == NULL) |
| 1690 | goto fail; |
| 1691 | } |
Antoine Pitrou | e532456 | 2011-11-19 00:39:01 +0100 | [diff] [blame] | 1692 | if (PyUnicode_GET_LENGTH(result) > 0 && |
| 1693 | PyList_Append(chunks, result) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1694 | goto fail; |
| 1695 | Py_DECREF(result); |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1696 | result = textiowrapper_get_decoded_chars(self, remaining); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1697 | if (result == NULL) |
| 1698 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1699 | remaining -= PyUnicode_GET_LENGTH(result); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1700 | } |
| 1701 | if (chunks != NULL) { |
| 1702 | if (result != NULL && PyList_Append(chunks, result) < 0) |
| 1703 | goto fail; |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 1704 | Py_XSETREF(result, PyUnicode_Join(_PyIO_empty_str, chunks)); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1705 | if (result == NULL) |
| 1706 | goto fail; |
| 1707 | Py_CLEAR(chunks); |
| 1708 | } |
| 1709 | return result; |
| 1710 | } |
| 1711 | fail: |
| 1712 | Py_XDECREF(result); |
| 1713 | Py_XDECREF(chunks); |
| 1714 | return NULL; |
| 1715 | } |
| 1716 | |
| 1717 | |
Victor Stinner | f7b8cb6 | 2011-09-29 03:28:17 +0200 | [diff] [blame] | 1718 | /* 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] | 1719 | that is to the NUL character. Otherwise the function will produce |
| 1720 | incorrect results. */ |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1721 | static const char * |
| 1722 | 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] | 1723 | { |
Antoine Pitrou | c28e2e5 | 2011-11-13 03:53:42 +0100 | [diff] [blame] | 1724 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1725 | assert(ch < 256); |
| 1726 | return (char *) memchr((void *) s, (char) ch, end - s); |
| 1727 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1728 | for (;;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1729 | while (PyUnicode_READ(kind, s, 0) > ch) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1730 | s += kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1731 | if (PyUnicode_READ(kind, s, 0) == ch) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1732 | return s; |
| 1733 | if (s == end) |
| 1734 | return NULL; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1735 | s += kind; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | Py_ssize_t |
| 1740 | _PyIO_find_line_ending( |
| 1741 | int translated, int universal, PyObject *readnl, |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1742 | int kind, const char *start, const char *end, Py_ssize_t *consumed) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1743 | { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1744 | Py_ssize_t len = ((char*)end - (char*)start)/kind; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1745 | |
| 1746 | if (translated) { |
| 1747 | /* Newlines are already translated, only search for \n */ |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1748 | const char *pos = find_control_char(kind, start, end, '\n'); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1749 | if (pos != NULL) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1750 | return (pos - start)/kind + 1; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1751 | else { |
| 1752 | *consumed = len; |
| 1753 | return -1; |
| 1754 | } |
| 1755 | } |
| 1756 | else if (universal) { |
| 1757 | /* Universal newline search. Find any of \r, \r\n, \n |
| 1758 | * The decoder ensures that \r\n are not split in two pieces |
| 1759 | */ |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1760 | const char *s = start; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1761 | for (;;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1762 | Py_UCS4 ch; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1763 | /* Fast path for non-control chars. The loop always ends |
Victor Stinner | f7b8cb6 | 2011-09-29 03:28:17 +0200 | [diff] [blame] | 1764 | since the Unicode string is NUL-terminated. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1765 | while (PyUnicode_READ(kind, s, 0) > '\r') |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1766 | s += kind; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1767 | if (s >= end) { |
| 1768 | *consumed = len; |
| 1769 | return -1; |
| 1770 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1771 | ch = PyUnicode_READ(kind, s, 0); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1772 | s += kind; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1773 | if (ch == '\n') |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1774 | return (s - start)/kind; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1775 | if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1776 | if (PyUnicode_READ(kind, s, 0) == '\n') |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1777 | return (s - start)/kind + 1; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1778 | else |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1779 | return (s - start)/kind; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | } |
| 1783 | else { |
| 1784 | /* Non-universal mode. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1785 | Py_ssize_t readnl_len = PyUnicode_GET_LENGTH(readnl); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1786 | Py_UCS1 *nl = PyUnicode_1BYTE_DATA(readnl); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1787 | /* Assume that readnl is an ASCII character. */ |
| 1788 | assert(PyUnicode_KIND(readnl) == PyUnicode_1BYTE_KIND); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1789 | if (readnl_len == 1) { |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1790 | const char *pos = find_control_char(kind, start, end, nl[0]); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1791 | if (pos != NULL) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1792 | return (pos - start)/kind + 1; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1793 | *consumed = len; |
| 1794 | return -1; |
| 1795 | } |
| 1796 | else { |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1797 | const char *s = start; |
| 1798 | const char *e = end - (readnl_len - 1)*kind; |
| 1799 | const char *pos; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1800 | if (e < s) |
| 1801 | e = s; |
| 1802 | while (s < e) { |
| 1803 | Py_ssize_t i; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1804 | const char *pos = find_control_char(kind, s, end, nl[0]); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1805 | if (pos == NULL || pos >= e) |
| 1806 | break; |
| 1807 | for (i = 1; i < readnl_len; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1808 | if (PyUnicode_READ(kind, pos, i) != nl[i]) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1809 | break; |
| 1810 | } |
| 1811 | if (i == readnl_len) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1812 | return (pos - start)/kind + readnl_len; |
| 1813 | s = pos + kind; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1814 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1815 | pos = find_control_char(kind, e, end, nl[0]); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1816 | if (pos == NULL) |
| 1817 | *consumed = len; |
| 1818 | else |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1819 | *consumed = (pos - start)/kind; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1820 | return -1; |
| 1821 | } |
| 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1826 | _textiowrapper_readline(textio *self, Py_ssize_t limit) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1827 | { |
| 1828 | PyObject *line = NULL, *chunks = NULL, *remaining = NULL; |
| 1829 | Py_ssize_t start, endpos, chunked, offset_to_buffer; |
| 1830 | int res; |
| 1831 | |
| 1832 | CHECK_CLOSED(self); |
| 1833 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1834 | if (_textiowrapper_writeflush(self) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1835 | return NULL; |
| 1836 | |
| 1837 | chunked = 0; |
| 1838 | |
| 1839 | while (1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1840 | char *ptr; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1841 | Py_ssize_t line_len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1842 | int kind; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1843 | Py_ssize_t consumed = 0; |
| 1844 | |
| 1845 | /* First, get some data if necessary */ |
| 1846 | res = 1; |
| 1847 | while (!self->decoded_chars || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1848 | !PyUnicode_GET_LENGTH(self->decoded_chars)) { |
Antoine Pitrou | e532456 | 2011-11-19 00:39:01 +0100 | [diff] [blame] | 1849 | res = textiowrapper_read_chunk(self, 0); |
Gregory P. Smith | 5135992 | 2012-06-23 23:55:39 -0700 | [diff] [blame] | 1850 | if (res < 0) { |
| 1851 | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() |
| 1852 | when EINTR occurs so we needn't do it ourselves. */ |
| 1853 | if (_PyIO_trap_eintr()) { |
| 1854 | continue; |
| 1855 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1856 | goto error; |
Gregory P. Smith | 5135992 | 2012-06-23 23:55:39 -0700 | [diff] [blame] | 1857 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1858 | if (res == 0) |
| 1859 | break; |
| 1860 | } |
| 1861 | if (res == 0) { |
| 1862 | /* end of file */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1863 | textiowrapper_set_decoded_chars(self, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1864 | Py_CLEAR(self->snapshot); |
| 1865 | start = endpos = offset_to_buffer = 0; |
| 1866 | break; |
| 1867 | } |
| 1868 | |
| 1869 | if (remaining == NULL) { |
| 1870 | line = self->decoded_chars; |
| 1871 | start = self->decoded_chars_used; |
| 1872 | offset_to_buffer = 0; |
| 1873 | Py_INCREF(line); |
| 1874 | } |
| 1875 | else { |
| 1876 | assert(self->decoded_chars_used == 0); |
| 1877 | line = PyUnicode_Concat(remaining, self->decoded_chars); |
| 1878 | start = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1879 | offset_to_buffer = PyUnicode_GET_LENGTH(remaining); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1880 | Py_CLEAR(remaining); |
| 1881 | if (line == NULL) |
| 1882 | goto error; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1883 | if (PyUnicode_READY(line) == -1) |
| 1884 | goto error; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1887 | ptr = PyUnicode_DATA(line); |
| 1888 | line_len = PyUnicode_GET_LENGTH(line); |
| 1889 | kind = PyUnicode_KIND(line); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1890 | |
| 1891 | endpos = _PyIO_find_line_ending( |
| 1892 | self->readtranslate, self->readuniversal, self->readnl, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1893 | kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1894 | ptr + kind * start, |
| 1895 | ptr + kind * line_len, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1896 | &consumed); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1897 | if (endpos >= 0) { |
| 1898 | endpos += start; |
| 1899 | if (limit >= 0 && (endpos - start) + chunked >= limit) |
| 1900 | endpos = start + limit - chunked; |
| 1901 | break; |
| 1902 | } |
| 1903 | |
| 1904 | /* We can put aside up to `endpos` */ |
| 1905 | endpos = consumed + start; |
| 1906 | if (limit >= 0 && (endpos - start) + chunked >= limit) { |
| 1907 | /* Didn't find line ending, but reached length limit */ |
| 1908 | endpos = start + limit - chunked; |
| 1909 | break; |
| 1910 | } |
| 1911 | |
| 1912 | if (endpos > start) { |
| 1913 | /* No line ending seen yet - put aside current data */ |
| 1914 | PyObject *s; |
| 1915 | if (chunks == NULL) { |
| 1916 | chunks = PyList_New(0); |
| 1917 | if (chunks == NULL) |
| 1918 | goto error; |
| 1919 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1920 | s = PyUnicode_Substring(line, start, endpos); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1921 | if (s == NULL) |
| 1922 | goto error; |
| 1923 | if (PyList_Append(chunks, s) < 0) { |
| 1924 | Py_DECREF(s); |
| 1925 | goto error; |
| 1926 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1927 | chunked += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1928 | Py_DECREF(s); |
| 1929 | } |
| 1930 | /* There may be some remaining bytes we'll have to prepend to the |
| 1931 | next chunk of data */ |
| 1932 | if (endpos < line_len) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1933 | remaining = PyUnicode_Substring(line, endpos, line_len); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1934 | if (remaining == NULL) |
| 1935 | goto error; |
| 1936 | } |
| 1937 | Py_CLEAR(line); |
| 1938 | /* We have consumed the buffer */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1939 | textiowrapper_set_decoded_chars(self, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | if (line != NULL) { |
| 1943 | /* Our line ends in the current buffer */ |
| 1944 | self->decoded_chars_used = endpos - offset_to_buffer; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1945 | if (start > 0 || endpos < PyUnicode_GET_LENGTH(line)) { |
| 1946 | PyObject *s = PyUnicode_Substring(line, start, endpos); |
| 1947 | Py_CLEAR(line); |
| 1948 | if (s == NULL) |
| 1949 | goto error; |
| 1950 | line = s; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1951 | } |
| 1952 | } |
| 1953 | if (remaining != NULL) { |
| 1954 | if (chunks == NULL) { |
| 1955 | chunks = PyList_New(0); |
| 1956 | if (chunks == NULL) |
| 1957 | goto error; |
| 1958 | } |
| 1959 | if (PyList_Append(chunks, remaining) < 0) |
| 1960 | goto error; |
| 1961 | Py_CLEAR(remaining); |
| 1962 | } |
| 1963 | if (chunks != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1964 | if (line != NULL) { |
| 1965 | if (PyList_Append(chunks, line) < 0) |
| 1966 | goto error; |
| 1967 | Py_DECREF(line); |
| 1968 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1969 | line = PyUnicode_Join(_PyIO_empty_str, chunks); |
| 1970 | if (line == NULL) |
| 1971 | goto error; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1972 | Py_CLEAR(chunks); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1973 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1974 | if (line == NULL) { |
| 1975 | Py_INCREF(_PyIO_empty_str); |
| 1976 | line = _PyIO_empty_str; |
| 1977 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1978 | |
| 1979 | return line; |
| 1980 | |
| 1981 | error: |
| 1982 | Py_XDECREF(chunks); |
| 1983 | Py_XDECREF(remaining); |
| 1984 | Py_XDECREF(line); |
| 1985 | return NULL; |
| 1986 | } |
| 1987 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 1988 | /*[clinic input] |
| 1989 | _io.TextIOWrapper.readline |
| 1990 | size: Py_ssize_t = -1 |
| 1991 | / |
| 1992 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1993 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 1994 | static PyObject * |
| 1995 | _io_TextIOWrapper_readline_impl(textio *self, Py_ssize_t size) |
| 1996 | /*[clinic end generated code: output=344afa98804e8b25 input=56c7172483b36db6]*/ |
| 1997 | { |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 1998 | CHECK_ATTACHED(self); |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 1999 | return _textiowrapper_readline(self, size); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | /* Seek and Tell */ |
| 2003 | |
| 2004 | typedef struct { |
| 2005 | Py_off_t start_pos; |
| 2006 | int dec_flags; |
| 2007 | int bytes_to_feed; |
| 2008 | int chars_to_skip; |
| 2009 | char need_eof; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2010 | } cookie_type; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2011 | |
| 2012 | /* |
| 2013 | To speed up cookie packing/unpacking, we store the fields in a temporary |
| 2014 | string and call _PyLong_FromByteArray() or _PyLong_AsByteArray (resp.). |
| 2015 | The following macros define at which offsets in the intermediary byte |
| 2016 | string the various CookieStruct fields will be stored. |
| 2017 | */ |
| 2018 | |
| 2019 | #define COOKIE_BUF_LEN (sizeof(Py_off_t) + 3 * sizeof(int) + sizeof(char)) |
| 2020 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 2021 | #if PY_BIG_ENDIAN |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2022 | /* We want the least significant byte of start_pos to also be the least |
| 2023 | significant byte of the cookie, which means that in big-endian mode we |
| 2024 | must copy the fields in reverse order. */ |
| 2025 | |
| 2026 | # define OFF_START_POS (sizeof(char) + 3 * sizeof(int)) |
| 2027 | # define OFF_DEC_FLAGS (sizeof(char) + 2 * sizeof(int)) |
| 2028 | # define OFF_BYTES_TO_FEED (sizeof(char) + sizeof(int)) |
| 2029 | # define OFF_CHARS_TO_SKIP (sizeof(char)) |
| 2030 | # define OFF_NEED_EOF 0 |
| 2031 | |
| 2032 | #else |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2033 | /* Little-endian mode: the least significant byte of start_pos will |
| 2034 | naturally end up the least significant byte of the cookie. */ |
| 2035 | |
| 2036 | # define OFF_START_POS 0 |
| 2037 | # define OFF_DEC_FLAGS (sizeof(Py_off_t)) |
| 2038 | # define OFF_BYTES_TO_FEED (sizeof(Py_off_t) + sizeof(int)) |
| 2039 | # define OFF_CHARS_TO_SKIP (sizeof(Py_off_t) + 2 * sizeof(int)) |
| 2040 | # define OFF_NEED_EOF (sizeof(Py_off_t) + 3 * sizeof(int)) |
| 2041 | |
| 2042 | #endif |
| 2043 | |
| 2044 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2045 | textiowrapper_parse_cookie(cookie_type *cookie, PyObject *cookieObj) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2046 | { |
| 2047 | unsigned char buffer[COOKIE_BUF_LEN]; |
| 2048 | PyLongObject *cookieLong = (PyLongObject *)PyNumber_Long(cookieObj); |
| 2049 | if (cookieLong == NULL) |
| 2050 | return -1; |
| 2051 | |
| 2052 | if (_PyLong_AsByteArray(cookieLong, buffer, sizeof(buffer), |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 2053 | PY_LITTLE_ENDIAN, 0) < 0) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2054 | Py_DECREF(cookieLong); |
| 2055 | return -1; |
| 2056 | } |
| 2057 | Py_DECREF(cookieLong); |
| 2058 | |
Antoine Pitrou | 2db74c2 | 2009-03-06 21:49:02 +0000 | [diff] [blame] | 2059 | memcpy(&cookie->start_pos, buffer + OFF_START_POS, sizeof(cookie->start_pos)); |
| 2060 | memcpy(&cookie->dec_flags, buffer + OFF_DEC_FLAGS, sizeof(cookie->dec_flags)); |
| 2061 | memcpy(&cookie->bytes_to_feed, buffer + OFF_BYTES_TO_FEED, sizeof(cookie->bytes_to_feed)); |
| 2062 | memcpy(&cookie->chars_to_skip, buffer + OFF_CHARS_TO_SKIP, sizeof(cookie->chars_to_skip)); |
| 2063 | memcpy(&cookie->need_eof, buffer + OFF_NEED_EOF, sizeof(cookie->need_eof)); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2064 | |
| 2065 | return 0; |
| 2066 | } |
| 2067 | |
| 2068 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2069 | textiowrapper_build_cookie(cookie_type *cookie) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2070 | { |
| 2071 | unsigned char buffer[COOKIE_BUF_LEN]; |
| 2072 | |
Antoine Pitrou | 2db74c2 | 2009-03-06 21:49:02 +0000 | [diff] [blame] | 2073 | memcpy(buffer + OFF_START_POS, &cookie->start_pos, sizeof(cookie->start_pos)); |
| 2074 | memcpy(buffer + OFF_DEC_FLAGS, &cookie->dec_flags, sizeof(cookie->dec_flags)); |
| 2075 | memcpy(buffer + OFF_BYTES_TO_FEED, &cookie->bytes_to_feed, sizeof(cookie->bytes_to_feed)); |
| 2076 | memcpy(buffer + OFF_CHARS_TO_SKIP, &cookie->chars_to_skip, sizeof(cookie->chars_to_skip)); |
| 2077 | memcpy(buffer + OFF_NEED_EOF, &cookie->need_eof, sizeof(cookie->need_eof)); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2078 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 2079 | return _PyLong_FromByteArray(buffer, sizeof(buffer), |
| 2080 | PY_LITTLE_ENDIAN, 0); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2081 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2082 | |
| 2083 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2084 | _textiowrapper_decoder_setstate(textio *self, cookie_type *cookie) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2085 | { |
| 2086 | PyObject *res; |
| 2087 | /* When seeking to the start of the stream, we call decoder.reset() |
| 2088 | rather than decoder.getstate(). |
| 2089 | This is for a few decoders such as utf-16 for which the state value |
| 2090 | at start is not (b"", 0) but e.g. (b"", 2) (meaning, in the case of |
| 2091 | utf-16, that we are expecting a BOM). |
| 2092 | */ |
| 2093 | if (cookie->start_pos == 0 && cookie->dec_flags == 0) |
| 2094 | res = PyObject_CallMethodObjArgs(self->decoder, _PyIO_str_reset, NULL); |
| 2095 | else |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2096 | res = _PyObject_CallMethodId(self->decoder, &PyId_setstate, |
| 2097 | "((yi))", "", cookie->dec_flags); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2098 | if (res == NULL) |
| 2099 | return -1; |
| 2100 | Py_DECREF(res); |
| 2101 | return 0; |
| 2102 | } |
| 2103 | |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 2104 | static int |
Antoine Pitrou | 85e3ee7 | 2015-04-13 20:01:21 +0200 | [diff] [blame] | 2105 | _textiowrapper_encoder_reset(textio *self, int start_of_stream) |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 2106 | { |
| 2107 | PyObject *res; |
Antoine Pitrou | 85e3ee7 | 2015-04-13 20:01:21 +0200 | [diff] [blame] | 2108 | if (start_of_stream) { |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 2109 | res = PyObject_CallMethodObjArgs(self->encoder, _PyIO_str_reset, NULL); |
| 2110 | self->encoding_start_of_stream = 1; |
| 2111 | } |
| 2112 | else { |
| 2113 | res = PyObject_CallMethodObjArgs(self->encoder, _PyIO_str_setstate, |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2114 | _PyLong_Zero, NULL); |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 2115 | self->encoding_start_of_stream = 0; |
| 2116 | } |
| 2117 | if (res == NULL) |
| 2118 | return -1; |
| 2119 | Py_DECREF(res); |
| 2120 | return 0; |
| 2121 | } |
| 2122 | |
Antoine Pitrou | 85e3ee7 | 2015-04-13 20:01:21 +0200 | [diff] [blame] | 2123 | static int |
| 2124 | _textiowrapper_encoder_setstate(textio *self, cookie_type *cookie) |
| 2125 | { |
| 2126 | /* Same as _textiowrapper_decoder_setstate() above. */ |
| 2127 | return _textiowrapper_encoder_reset( |
| 2128 | self, cookie->start_pos == 0 && cookie->dec_flags == 0); |
| 2129 | } |
| 2130 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2131 | /*[clinic input] |
| 2132 | _io.TextIOWrapper.seek |
| 2133 | cookie as cookieObj: object |
| 2134 | whence: int = 0 |
| 2135 | / |
| 2136 | [clinic start generated code]*/ |
| 2137 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2138 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2139 | _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence) |
| 2140 | /*[clinic end generated code: output=0a15679764e2d04d input=0458abeb3d7842be]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2141 | { |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2142 | PyObject *posobj; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2143 | cookie_type cookie; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2144 | PyObject *res; |
| 2145 | int cmp; |
| 2146 | |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2147 | CHECK_ATTACHED(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2148 | CHECK_CLOSED(self); |
| 2149 | |
| 2150 | Py_INCREF(cookieObj); |
| 2151 | |
| 2152 | if (!self->seekable) { |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 2153 | _unsupported("underlying stream is not seekable"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2154 | goto fail; |
| 2155 | } |
| 2156 | |
| 2157 | if (whence == 1) { |
| 2158 | /* seek relative to current position */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2159 | cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2160 | if (cmp < 0) |
| 2161 | goto fail; |
| 2162 | |
| 2163 | if (cmp == 0) { |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 2164 | _unsupported("can't do nonzero cur-relative seeks"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2165 | goto fail; |
| 2166 | } |
| 2167 | |
| 2168 | /* Seeking to the current position should attempt to |
| 2169 | * sync the underlying buffer with the current position. |
| 2170 | */ |
| 2171 | Py_DECREF(cookieObj); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2172 | cookieObj = _PyObject_CallMethodId((PyObject *)self, &PyId_tell, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2173 | if (cookieObj == NULL) |
| 2174 | goto fail; |
| 2175 | } |
| 2176 | else if (whence == 2) { |
| 2177 | /* seek relative to end of file */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2178 | cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2179 | if (cmp < 0) |
| 2180 | goto fail; |
| 2181 | |
| 2182 | if (cmp == 0) { |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 2183 | _unsupported("can't do nonzero end-relative seeks"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2184 | goto fail; |
| 2185 | } |
| 2186 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2187 | res = _PyObject_CallMethodId((PyObject *)self, &PyId_flush, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2188 | if (res == NULL) |
| 2189 | goto fail; |
| 2190 | Py_DECREF(res); |
| 2191 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2192 | textiowrapper_set_decoded_chars(self, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2193 | Py_CLEAR(self->snapshot); |
| 2194 | if (self->decoder) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2195 | res = _PyObject_CallMethodId(self->decoder, &PyId_reset, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2196 | if (res == NULL) |
| 2197 | goto fail; |
| 2198 | Py_DECREF(res); |
| 2199 | } |
| 2200 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2201 | res = _PyObject_CallMethodId(self->buffer, &PyId_seek, "ii", 0, 2); |
Antoine Pitrou | 85e3ee7 | 2015-04-13 20:01:21 +0200 | [diff] [blame] | 2202 | Py_CLEAR(cookieObj); |
| 2203 | if (res == NULL) |
| 2204 | goto fail; |
| 2205 | if (self->encoder) { |
| 2206 | /* If seek() == 0, we are at the start of stream, otherwise not */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2207 | cmp = PyObject_RichCompareBool(res, _PyLong_Zero, Py_EQ); |
Antoine Pitrou | 85e3ee7 | 2015-04-13 20:01:21 +0200 | [diff] [blame] | 2208 | if (cmp < 0 || _textiowrapper_encoder_reset(self, cmp)) { |
| 2209 | Py_DECREF(res); |
| 2210 | goto fail; |
| 2211 | } |
| 2212 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2213 | return res; |
| 2214 | } |
| 2215 | else if (whence != 0) { |
| 2216 | PyErr_Format(PyExc_ValueError, |
| 2217 | "invalid whence (%d, should be 0, 1 or 2)", whence); |
| 2218 | goto fail; |
| 2219 | } |
| 2220 | |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2221 | cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_LT); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2222 | if (cmp < 0) |
| 2223 | goto fail; |
| 2224 | |
| 2225 | if (cmp == 1) { |
| 2226 | PyErr_Format(PyExc_ValueError, |
| 2227 | "negative seek position %R", cookieObj); |
| 2228 | goto fail; |
| 2229 | } |
| 2230 | |
| 2231 | res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL); |
| 2232 | if (res == NULL) |
| 2233 | goto fail; |
| 2234 | Py_DECREF(res); |
| 2235 | |
| 2236 | /* The strategy of seek() is to go back to the safe start point |
| 2237 | * and replay the effect of read(chars_to_skip) from there. |
| 2238 | */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2239 | if (textiowrapper_parse_cookie(&cookie, cookieObj) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2240 | goto fail; |
| 2241 | |
| 2242 | /* Seek back to the safe start point. */ |
| 2243 | posobj = PyLong_FromOff_t(cookie.start_pos); |
| 2244 | if (posobj == NULL) |
| 2245 | goto fail; |
| 2246 | res = PyObject_CallMethodObjArgs(self->buffer, |
| 2247 | _PyIO_str_seek, posobj, NULL); |
| 2248 | Py_DECREF(posobj); |
| 2249 | if (res == NULL) |
| 2250 | goto fail; |
| 2251 | Py_DECREF(res); |
| 2252 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2253 | textiowrapper_set_decoded_chars(self, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2254 | Py_CLEAR(self->snapshot); |
| 2255 | |
| 2256 | /* Restore the decoder to its state from the safe start point. */ |
| 2257 | if (self->decoder) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2258 | if (_textiowrapper_decoder_setstate(self, &cookie) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2259 | goto fail; |
| 2260 | } |
| 2261 | |
| 2262 | if (cookie.chars_to_skip) { |
| 2263 | /* 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] | 2264 | PyObject *input_chunk = _PyObject_CallMethodId( |
| 2265 | self->buffer, &PyId_read, "i", cookie.bytes_to_feed); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2266 | PyObject *decoded; |
| 2267 | |
| 2268 | if (input_chunk == NULL) |
| 2269 | goto fail; |
| 2270 | |
Serhiy Storchaka | 94dc673 | 2013-02-03 17:03:31 +0200 | [diff] [blame] | 2271 | if (!PyBytes_Check(input_chunk)) { |
| 2272 | PyErr_Format(PyExc_TypeError, |
| 2273 | "underlying read() should have returned a bytes " |
| 2274 | "object, not '%.200s'", |
| 2275 | Py_TYPE(input_chunk)->tp_name); |
| 2276 | Py_DECREF(input_chunk); |
| 2277 | goto fail; |
| 2278 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2279 | |
| 2280 | self->snapshot = Py_BuildValue("iN", cookie.dec_flags, input_chunk); |
| 2281 | if (self->snapshot == NULL) { |
| 2282 | Py_DECREF(input_chunk); |
| 2283 | goto fail; |
| 2284 | } |
| 2285 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2286 | decoded = _PyObject_CallMethodId(self->decoder, &PyId_decode, |
| 2287 | "Oi", input_chunk, (int)cookie.need_eof); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2288 | |
Serhiy Storchaka | 94dc673 | 2013-02-03 17:03:31 +0200 | [diff] [blame] | 2289 | if (check_decoded(decoded) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2290 | goto fail; |
| 2291 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2292 | textiowrapper_set_decoded_chars(self, decoded); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2293 | |
| 2294 | /* Skip chars_to_skip of the decoded characters. */ |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 2295 | if (PyUnicode_GetLength(self->decoded_chars) < cookie.chars_to_skip) { |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 2296 | PyErr_SetString(PyExc_OSError, "can't restore logical file position"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2297 | goto fail; |
| 2298 | } |
| 2299 | self->decoded_chars_used = cookie.chars_to_skip; |
| 2300 | } |
| 2301 | else { |
| 2302 | self->snapshot = Py_BuildValue("iy", cookie.dec_flags, ""); |
| 2303 | if (self->snapshot == NULL) |
| 2304 | goto fail; |
| 2305 | } |
| 2306 | |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 2307 | /* Finally, reset the encoder (merely useful for proper BOM handling) */ |
| 2308 | if (self->encoder) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2309 | if (_textiowrapper_encoder_setstate(self, &cookie) < 0) |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 2310 | goto fail; |
| 2311 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2312 | return cookieObj; |
| 2313 | fail: |
| 2314 | Py_XDECREF(cookieObj); |
| 2315 | return NULL; |
| 2316 | |
| 2317 | } |
| 2318 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2319 | /*[clinic input] |
| 2320 | _io.TextIOWrapper.tell |
| 2321 | [clinic start generated code]*/ |
| 2322 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2323 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2324 | _io_TextIOWrapper_tell_impl(textio *self) |
| 2325 | /*[clinic end generated code: output=4f168c08bf34ad5f input=9a2caf88c24f9ddf]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2326 | { |
| 2327 | PyObject *res; |
| 2328 | PyObject *posobj = NULL; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2329 | cookie_type cookie = {0,0,0,0,0}; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2330 | PyObject *next_input; |
| 2331 | Py_ssize_t chars_to_skip, chars_decoded; |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2332 | Py_ssize_t skip_bytes, skip_back; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2333 | PyObject *saved_state = NULL; |
| 2334 | char *input, *input_end; |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2335 | Py_ssize_t dec_buffer_len; |
| 2336 | int dec_flags; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2337 | |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2338 | CHECK_ATTACHED(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2339 | CHECK_CLOSED(self); |
| 2340 | |
| 2341 | if (!self->seekable) { |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 2342 | _unsupported("underlying stream is not seekable"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2343 | goto fail; |
| 2344 | } |
| 2345 | if (!self->telling) { |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 2346 | PyErr_SetString(PyExc_OSError, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2347 | "telling position disabled by next() call"); |
| 2348 | goto fail; |
| 2349 | } |
| 2350 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2351 | if (_textiowrapper_writeflush(self) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2352 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2353 | res = _PyObject_CallMethodId((PyObject *)self, &PyId_flush, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2354 | if (res == NULL) |
| 2355 | goto fail; |
| 2356 | Py_DECREF(res); |
| 2357 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2358 | posobj = _PyObject_CallMethodId(self->buffer, &PyId_tell, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2359 | if (posobj == NULL) |
| 2360 | goto fail; |
| 2361 | |
| 2362 | if (self->decoder == NULL || self->snapshot == NULL) { |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 2363 | assert (self->decoded_chars == NULL || PyUnicode_GetLength(self->decoded_chars) == 0); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2364 | return posobj; |
| 2365 | } |
| 2366 | |
| 2367 | #if defined(HAVE_LARGEFILE_SUPPORT) |
| 2368 | cookie.start_pos = PyLong_AsLongLong(posobj); |
| 2369 | #else |
| 2370 | cookie.start_pos = PyLong_AsLong(posobj); |
| 2371 | #endif |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2372 | Py_DECREF(posobj); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2373 | if (PyErr_Occurred()) |
| 2374 | goto fail; |
| 2375 | |
| 2376 | /* Skip backward to the snapshot point (see _read_chunk). */ |
Oren Milman | 13614e3 | 2017-08-24 19:51:24 +0300 | [diff] [blame] | 2377 | assert(PyTuple_Check(self->snapshot)); |
Serhiy Storchaka | bb72c47 | 2015-04-19 20:38:19 +0300 | [diff] [blame] | 2378 | if (!PyArg_ParseTuple(self->snapshot, "iO", &cookie.dec_flags, &next_input)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2379 | goto fail; |
| 2380 | |
| 2381 | assert (PyBytes_Check(next_input)); |
| 2382 | |
| 2383 | cookie.start_pos -= PyBytes_GET_SIZE(next_input); |
| 2384 | |
| 2385 | /* How many decoded characters have been used up since the snapshot? */ |
| 2386 | if (self->decoded_chars_used == 0) { |
| 2387 | /* We haven't moved from the snapshot point. */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2388 | return textiowrapper_build_cookie(&cookie); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | chars_to_skip = self->decoded_chars_used; |
| 2392 | |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2393 | /* Decoder state will be restored at the end */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2394 | saved_state = PyObject_CallMethodObjArgs(self->decoder, |
| 2395 | _PyIO_str_getstate, NULL); |
| 2396 | if (saved_state == NULL) |
| 2397 | goto fail; |
| 2398 | |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2399 | #define DECODER_GETSTATE() do { \ |
Serhiy Storchaka | 008d88b | 2015-05-06 09:53:07 +0300 | [diff] [blame] | 2400 | PyObject *dec_buffer; \ |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2401 | PyObject *_state = PyObject_CallMethodObjArgs(self->decoder, \ |
| 2402 | _PyIO_str_getstate, NULL); \ |
| 2403 | if (_state == NULL) \ |
| 2404 | goto fail; \ |
Oren Milman | 13614e3 | 2017-08-24 19:51:24 +0300 | [diff] [blame] | 2405 | if (!PyTuple_Check(_state)) { \ |
| 2406 | PyErr_SetString(PyExc_TypeError, \ |
| 2407 | "illegal decoder state"); \ |
| 2408 | Py_DECREF(_state); \ |
| 2409 | goto fail; \ |
| 2410 | } \ |
| 2411 | if (!PyArg_ParseTuple(_state, "Oi;illegal decoder state", \ |
| 2412 | &dec_buffer, &dec_flags)) \ |
| 2413 | { \ |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2414 | Py_DECREF(_state); \ |
| 2415 | goto fail; \ |
| 2416 | } \ |
Serhiy Storchaka | 008d88b | 2015-05-06 09:53:07 +0300 | [diff] [blame] | 2417 | if (!PyBytes_Check(dec_buffer)) { \ |
| 2418 | PyErr_Format(PyExc_TypeError, \ |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2419 | "illegal decoder state: the first item should be a " \ |
| 2420 | "bytes object, not '%.200s'", \ |
Serhiy Storchaka | 008d88b | 2015-05-06 09:53:07 +0300 | [diff] [blame] | 2421 | Py_TYPE(dec_buffer)->tp_name); \ |
| 2422 | Py_DECREF(_state); \ |
| 2423 | goto fail; \ |
| 2424 | } \ |
| 2425 | dec_buffer_len = PyBytes_GET_SIZE(dec_buffer); \ |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2426 | Py_DECREF(_state); \ |
| 2427 | } while (0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2428 | |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2429 | #define DECODER_DECODE(start, len, res) do { \ |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2430 | PyObject *_decoded = _PyObject_CallMethodId( \ |
| 2431 | self->decoder, &PyId_decode, "y#", start, len); \ |
Serhiy Storchaka | d03ce4a | 2013-02-03 17:07:32 +0200 | [diff] [blame] | 2432 | if (check_decoded(_decoded) < 0) \ |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2433 | goto fail; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2434 | res = PyUnicode_GET_LENGTH(_decoded); \ |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2435 | Py_DECREF(_decoded); \ |
| 2436 | } while (0) |
| 2437 | |
| 2438 | /* Fast search for an acceptable start point, close to our |
| 2439 | current pos */ |
| 2440 | skip_bytes = (Py_ssize_t) (self->b2cratio * chars_to_skip); |
| 2441 | skip_back = 1; |
| 2442 | assert(skip_back <= PyBytes_GET_SIZE(next_input)); |
| 2443 | input = PyBytes_AS_STRING(next_input); |
| 2444 | while (skip_bytes > 0) { |
| 2445 | /* Decode up to temptative start point */ |
| 2446 | if (_textiowrapper_decoder_setstate(self, &cookie) < 0) |
| 2447 | goto fail; |
| 2448 | DECODER_DECODE(input, skip_bytes, chars_decoded); |
| 2449 | if (chars_decoded <= chars_to_skip) { |
| 2450 | DECODER_GETSTATE(); |
| 2451 | if (dec_buffer_len == 0) { |
| 2452 | /* Before pos and no bytes buffered in decoder => OK */ |
| 2453 | cookie.dec_flags = dec_flags; |
| 2454 | chars_to_skip -= chars_decoded; |
| 2455 | break; |
| 2456 | } |
| 2457 | /* Skip back by buffered amount and reset heuristic */ |
| 2458 | skip_bytes -= dec_buffer_len; |
| 2459 | skip_back = 1; |
| 2460 | } |
| 2461 | else { |
| 2462 | /* We're too far ahead, skip back a bit */ |
| 2463 | skip_bytes -= skip_back; |
| 2464 | skip_back *= 2; |
| 2465 | } |
| 2466 | } |
| 2467 | if (skip_bytes <= 0) { |
| 2468 | skip_bytes = 0; |
| 2469 | if (_textiowrapper_decoder_setstate(self, &cookie) < 0) |
| 2470 | goto fail; |
| 2471 | } |
| 2472 | |
| 2473 | /* Note our initial start point. */ |
| 2474 | cookie.start_pos += skip_bytes; |
Victor Stinner | 9a28297 | 2013-06-24 23:01:33 +0200 | [diff] [blame] | 2475 | 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] | 2476 | if (chars_to_skip == 0) |
| 2477 | goto finally; |
| 2478 | |
| 2479 | /* We should be close to the desired position. Now feed the decoder one |
| 2480 | * byte at a time until we reach the `chars_to_skip` target. |
| 2481 | * As we go, note the nearest "safe start point" before the current |
| 2482 | * location (a point where the decoder has nothing buffered, so seek() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2483 | * can safely start from there and advance to this location). |
| 2484 | */ |
| 2485 | chars_decoded = 0; |
| 2486 | input = PyBytes_AS_STRING(next_input); |
| 2487 | input_end = input + PyBytes_GET_SIZE(next_input); |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2488 | input += skip_bytes; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2489 | while (input < input_end) { |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2490 | Py_ssize_t n; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2491 | |
Serhiy Storchaka | ec67d18 | 2013-08-20 20:04:47 +0300 | [diff] [blame] | 2492 | DECODER_DECODE(input, (Py_ssize_t)1, n); |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2493 | /* We got n chars for 1 byte */ |
| 2494 | chars_decoded += n; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2495 | cookie.bytes_to_feed += 1; |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2496 | DECODER_GETSTATE(); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2497 | |
| 2498 | if (dec_buffer_len == 0 && chars_decoded <= chars_to_skip) { |
| 2499 | /* Decoder buffer is empty, so this is a safe start point. */ |
| 2500 | cookie.start_pos += cookie.bytes_to_feed; |
| 2501 | chars_to_skip -= chars_decoded; |
| 2502 | cookie.dec_flags = dec_flags; |
| 2503 | cookie.bytes_to_feed = 0; |
| 2504 | chars_decoded = 0; |
| 2505 | } |
| 2506 | if (chars_decoded >= chars_to_skip) |
| 2507 | break; |
| 2508 | input++; |
| 2509 | } |
| 2510 | if (input == input_end) { |
| 2511 | /* 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] | 2512 | PyObject *decoded = _PyObject_CallMethodId( |
| 2513 | self->decoder, &PyId_decode, "yi", "", /* final = */ 1); |
Serhiy Storchaka | 94dc673 | 2013-02-03 17:03:31 +0200 | [diff] [blame] | 2514 | if (check_decoded(decoded) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2515 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2516 | chars_decoded += PyUnicode_GET_LENGTH(decoded); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2517 | Py_DECREF(decoded); |
| 2518 | cookie.need_eof = 1; |
| 2519 | |
| 2520 | if (chars_decoded < chars_to_skip) { |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 2521 | PyErr_SetString(PyExc_OSError, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2522 | "can't reconstruct logical file position"); |
| 2523 | goto fail; |
| 2524 | } |
| 2525 | } |
| 2526 | |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2527 | finally: |
Victor Stinner | 7e42541 | 2016-12-09 00:36:19 +0100 | [diff] [blame] | 2528 | res = _PyObject_CallMethodIdObjArgs(self->decoder, &PyId_setstate, saved_state, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2529 | Py_DECREF(saved_state); |
| 2530 | if (res == NULL) |
| 2531 | return NULL; |
| 2532 | Py_DECREF(res); |
| 2533 | |
| 2534 | /* The returned cookie corresponds to the last safe start point. */ |
| 2535 | 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] | 2536 | return textiowrapper_build_cookie(&cookie); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2537 | |
Antoine Pitrou | 211b81d | 2011-02-25 20:27:33 +0000 | [diff] [blame] | 2538 | fail: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2539 | if (saved_state) { |
| 2540 | PyObject *type, *value, *traceback; |
| 2541 | PyErr_Fetch(&type, &value, &traceback); |
Victor Stinner | 7e42541 | 2016-12-09 00:36:19 +0100 | [diff] [blame] | 2542 | res = _PyObject_CallMethodIdObjArgs(self->decoder, &PyId_setstate, saved_state, NULL); |
Serhiy Storchaka | 04d09eb | 2015-03-30 09:58:41 +0300 | [diff] [blame] | 2543 | _PyErr_ChainExceptions(type, value, traceback); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2544 | Py_DECREF(saved_state); |
Serhiy Storchaka | 04d09eb | 2015-03-30 09:58:41 +0300 | [diff] [blame] | 2545 | Py_XDECREF(res); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2546 | } |
| 2547 | return NULL; |
| 2548 | } |
| 2549 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2550 | /*[clinic input] |
| 2551 | _io.TextIOWrapper.truncate |
| 2552 | pos: object = None |
| 2553 | / |
| 2554 | [clinic start generated code]*/ |
| 2555 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2556 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2557 | _io_TextIOWrapper_truncate_impl(textio *self, PyObject *pos) |
| 2558 | /*[clinic end generated code: output=90ec2afb9bb7745f input=56ec8baa65aea377]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2559 | { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2560 | PyObject *res; |
| 2561 | |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2562 | CHECK_ATTACHED(self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2563 | |
| 2564 | res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_flush, NULL); |
| 2565 | if (res == NULL) |
| 2566 | return NULL; |
| 2567 | Py_DECREF(res); |
| 2568 | |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 2569 | return PyObject_CallMethodObjArgs(self->buffer, _PyIO_str_truncate, pos, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Benjamin Peterson | c4c0eae | 2009-03-09 00:07:03 +0000 | [diff] [blame] | 2572 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2573 | textiowrapper_repr(textio *self) |
Benjamin Peterson | c4c0eae | 2009-03-09 00:07:03 +0000 | [diff] [blame] | 2574 | { |
Antoine Pitrou | a4815ca | 2011-01-09 20:38:15 +0000 | [diff] [blame] | 2575 | PyObject *nameobj, *modeobj, *res, *s; |
Serhiy Storchaka | a5af6e1 | 2017-03-19 19:25:29 +0200 | [diff] [blame] | 2576 | int status; |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 2577 | |
| 2578 | CHECK_INITIALIZED(self); |
| 2579 | |
Antoine Pitrou | a4815ca | 2011-01-09 20:38:15 +0000 | [diff] [blame] | 2580 | res = PyUnicode_FromString("<_io.TextIOWrapper"); |
| 2581 | if (res == NULL) |
| 2582 | return NULL; |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2583 | |
Serhiy Storchaka | a5af6e1 | 2017-03-19 19:25:29 +0200 | [diff] [blame] | 2584 | status = Py_ReprEnter((PyObject *)self); |
| 2585 | if (status != 0) { |
| 2586 | if (status > 0) { |
| 2587 | PyErr_Format(PyExc_RuntimeError, |
| 2588 | "reentrant call inside %s.__repr__", |
| 2589 | Py_TYPE(self)->tp_name); |
| 2590 | } |
| 2591 | goto error; |
| 2592 | } |
Martin v. Löwis | 767046a | 2011-10-14 15:35:36 +0200 | [diff] [blame] | 2593 | nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name); |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 2594 | if (nameobj == NULL) { |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2595 | if (PyErr_ExceptionMatches(PyExc_Exception)) |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 2596 | PyErr_Clear(); |
| 2597 | else |
Antoine Pitrou | a4815ca | 2011-01-09 20:38:15 +0000 | [diff] [blame] | 2598 | goto error; |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 2599 | } |
| 2600 | else { |
Antoine Pitrou | a4815ca | 2011-01-09 20:38:15 +0000 | [diff] [blame] | 2601 | s = PyUnicode_FromFormat(" name=%R", nameobj); |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 2602 | Py_DECREF(nameobj); |
Antoine Pitrou | a4815ca | 2011-01-09 20:38:15 +0000 | [diff] [blame] | 2603 | if (s == NULL) |
| 2604 | goto error; |
| 2605 | PyUnicode_AppendAndDel(&res, s); |
| 2606 | if (res == NULL) |
Serhiy Storchaka | a5af6e1 | 2017-03-19 19:25:29 +0200 | [diff] [blame] | 2607 | goto error; |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 2608 | } |
Martin v. Löwis | 767046a | 2011-10-14 15:35:36 +0200 | [diff] [blame] | 2609 | modeobj = _PyObject_GetAttrId((PyObject *) self, &PyId_mode); |
Antoine Pitrou | a4815ca | 2011-01-09 20:38:15 +0000 | [diff] [blame] | 2610 | if (modeobj == NULL) { |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2611 | if (PyErr_ExceptionMatches(PyExc_Exception)) |
Antoine Pitrou | a4815ca | 2011-01-09 20:38:15 +0000 | [diff] [blame] | 2612 | PyErr_Clear(); |
| 2613 | else |
| 2614 | goto error; |
| 2615 | } |
| 2616 | else { |
| 2617 | s = PyUnicode_FromFormat(" mode=%R", modeobj); |
| 2618 | Py_DECREF(modeobj); |
| 2619 | if (s == NULL) |
| 2620 | goto error; |
| 2621 | PyUnicode_AppendAndDel(&res, s); |
| 2622 | if (res == NULL) |
Serhiy Storchaka | a5af6e1 | 2017-03-19 19:25:29 +0200 | [diff] [blame] | 2623 | goto error; |
Antoine Pitrou | a4815ca | 2011-01-09 20:38:15 +0000 | [diff] [blame] | 2624 | } |
| 2625 | s = PyUnicode_FromFormat("%U encoding=%R>", |
| 2626 | res, self->encoding); |
| 2627 | Py_DECREF(res); |
Serhiy Storchaka | a5af6e1 | 2017-03-19 19:25:29 +0200 | [diff] [blame] | 2628 | if (status == 0) { |
| 2629 | Py_ReprLeave((PyObject *)self); |
| 2630 | } |
Antoine Pitrou | a4815ca | 2011-01-09 20:38:15 +0000 | [diff] [blame] | 2631 | return s; |
Serhiy Storchaka | a5af6e1 | 2017-03-19 19:25:29 +0200 | [diff] [blame] | 2632 | |
| 2633 | error: |
Antoine Pitrou | a4815ca | 2011-01-09 20:38:15 +0000 | [diff] [blame] | 2634 | Py_XDECREF(res); |
Serhiy Storchaka | a5af6e1 | 2017-03-19 19:25:29 +0200 | [diff] [blame] | 2635 | if (status == 0) { |
| 2636 | Py_ReprLeave((PyObject *)self); |
| 2637 | } |
Antoine Pitrou | a4815ca | 2011-01-09 20:38:15 +0000 | [diff] [blame] | 2638 | return NULL; |
Benjamin Peterson | c4c0eae | 2009-03-09 00:07:03 +0000 | [diff] [blame] | 2639 | } |
| 2640 | |
| 2641 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2642 | /* Inquiries */ |
| 2643 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2644 | /*[clinic input] |
| 2645 | _io.TextIOWrapper.fileno |
| 2646 | [clinic start generated code]*/ |
| 2647 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2648 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2649 | _io_TextIOWrapper_fileno_impl(textio *self) |
| 2650 | /*[clinic end generated code: output=21490a4c3da13e6c input=c488ca83d0069f9b]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2651 | { |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2652 | CHECK_ATTACHED(self); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2653 | return _PyObject_CallMethodId(self->buffer, &PyId_fileno, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2656 | /*[clinic input] |
| 2657 | _io.TextIOWrapper.seekable |
| 2658 | [clinic start generated code]*/ |
| 2659 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2660 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2661 | _io_TextIOWrapper_seekable_impl(textio *self) |
| 2662 | /*[clinic end generated code: output=ab223dbbcffc0f00 input=8b005ca06e1fca13]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2663 | { |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2664 | CHECK_ATTACHED(self); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2665 | return _PyObject_CallMethodId(self->buffer, &PyId_seekable, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2668 | /*[clinic input] |
| 2669 | _io.TextIOWrapper.readable |
| 2670 | [clinic start generated code]*/ |
| 2671 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2672 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2673 | _io_TextIOWrapper_readable_impl(textio *self) |
| 2674 | /*[clinic end generated code: output=72ff7ba289a8a91b input=0704ea7e01b0d3eb]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2675 | { |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2676 | CHECK_ATTACHED(self); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2677 | return _PyObject_CallMethodId(self->buffer, &PyId_readable, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2680 | /*[clinic input] |
| 2681 | _io.TextIOWrapper.writable |
| 2682 | [clinic start generated code]*/ |
| 2683 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2684 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2685 | _io_TextIOWrapper_writable_impl(textio *self) |
| 2686 | /*[clinic end generated code: output=a728c71790d03200 input=c41740bc9d8636e8]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2687 | { |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2688 | CHECK_ATTACHED(self); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2689 | return _PyObject_CallMethodId(self->buffer, &PyId_writable, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2692 | /*[clinic input] |
| 2693 | _io.TextIOWrapper.isatty |
| 2694 | [clinic start generated code]*/ |
| 2695 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2696 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2697 | _io_TextIOWrapper_isatty_impl(textio *self) |
| 2698 | /*[clinic end generated code: output=12be1a35bace882e input=fb68d9f2c99bbfff]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2699 | { |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2700 | CHECK_ATTACHED(self); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2701 | return _PyObject_CallMethodId(self->buffer, &PyId_isatty, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | static PyObject * |
Antoine Pitrou | 243757e | 2010-11-05 21:15:39 +0000 | [diff] [blame] | 2705 | textiowrapper_getstate(textio *self, PyObject *args) |
| 2706 | { |
| 2707 | PyErr_Format(PyExc_TypeError, |
| 2708 | "cannot serialize '%s' object", Py_TYPE(self)->tp_name); |
| 2709 | return NULL; |
| 2710 | } |
| 2711 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2712 | /*[clinic input] |
| 2713 | _io.TextIOWrapper.flush |
| 2714 | [clinic start generated code]*/ |
| 2715 | |
Antoine Pitrou | 243757e | 2010-11-05 21:15:39 +0000 | [diff] [blame] | 2716 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2717 | _io_TextIOWrapper_flush_impl(textio *self) |
| 2718 | /*[clinic end generated code: output=59de9165f9c2e4d2 input=928c60590694ab85]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2719 | { |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2720 | CHECK_ATTACHED(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2721 | CHECK_CLOSED(self); |
| 2722 | self->telling = self->seekable; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2723 | if (_textiowrapper_writeflush(self) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2724 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2725 | return _PyObject_CallMethodId(self->buffer, &PyId_flush, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2728 | /*[clinic input] |
| 2729 | _io.TextIOWrapper.close |
| 2730 | [clinic start generated code]*/ |
| 2731 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2732 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2733 | _io_TextIOWrapper_close_impl(textio *self) |
| 2734 | /*[clinic end generated code: output=056ccf8b4876e4f4 input=9c2114315eae1948]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2735 | { |
| 2736 | PyObject *res; |
Antoine Pitrou | 6be8876 | 2010-05-03 16:48:20 +0000 | [diff] [blame] | 2737 | int r; |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2738 | CHECK_ATTACHED(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2739 | |
Antoine Pitrou | 6be8876 | 2010-05-03 16:48:20 +0000 | [diff] [blame] | 2740 | res = textiowrapper_closed_get(self, NULL); |
| 2741 | if (res == NULL) |
| 2742 | return NULL; |
| 2743 | r = PyObject_IsTrue(res); |
| 2744 | Py_DECREF(res); |
| 2745 | if (r < 0) |
| 2746 | return NULL; |
Victor Stinner | f6c5783 | 2010-05-19 01:17:01 +0000 | [diff] [blame] | 2747 | |
Antoine Pitrou | 6be8876 | 2010-05-03 16:48:20 +0000 | [diff] [blame] | 2748 | if (r > 0) { |
| 2749 | Py_RETURN_NONE; /* stream already closed */ |
| 2750 | } |
| 2751 | else { |
Benjamin Peterson | 6862361 | 2012-12-20 11:53:11 -0600 | [diff] [blame] | 2752 | PyObject *exc = NULL, *val, *tb; |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 2753 | if (self->finalizing) { |
Victor Stinner | 61bdb0d | 2016-12-09 15:39:28 +0100 | [diff] [blame] | 2754 | res = _PyObject_CallMethodIdObjArgs(self->buffer, |
| 2755 | &PyId__dealloc_warn, |
| 2756 | self, NULL); |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 2757 | if (res) |
| 2758 | Py_DECREF(res); |
| 2759 | else |
| 2760 | PyErr_Clear(); |
| 2761 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2762 | res = _PyObject_CallMethodId((PyObject *)self, &PyId_flush, NULL); |
Benjamin Peterson | 6862361 | 2012-12-20 11:53:11 -0600 | [diff] [blame] | 2763 | if (res == NULL) |
| 2764 | PyErr_Fetch(&exc, &val, &tb); |
Antoine Pitrou | 6be8876 | 2010-05-03 16:48:20 +0000 | [diff] [blame] | 2765 | else |
| 2766 | Py_DECREF(res); |
| 2767 | |
Benjamin Peterson | 6862361 | 2012-12-20 11:53:11 -0600 | [diff] [blame] | 2768 | res = _PyObject_CallMethodId(self->buffer, &PyId_close, NULL); |
| 2769 | if (exc != NULL) { |
Serhiy Storchaka | e2bd2a7 | 2014-10-08 22:31:52 +0300 | [diff] [blame] | 2770 | _PyErr_ChainExceptions(exc, val, tb); |
| 2771 | Py_CLEAR(res); |
Benjamin Peterson | 6862361 | 2012-12-20 11:53:11 -0600 | [diff] [blame] | 2772 | } |
| 2773 | return res; |
Antoine Pitrou | 6be8876 | 2010-05-03 16:48:20 +0000 | [diff] [blame] | 2774 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
| 2777 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2778 | textiowrapper_iternext(textio *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2779 | { |
| 2780 | PyObject *line; |
| 2781 | |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2782 | CHECK_ATTACHED(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2783 | |
| 2784 | self->telling = 0; |
| 2785 | if (Py_TYPE(self) == &PyTextIOWrapper_Type) { |
| 2786 | /* Skip method call overhead for speed */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2787 | line = _textiowrapper_readline(self, -1); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2788 | } |
| 2789 | else { |
| 2790 | line = PyObject_CallMethodObjArgs((PyObject *)self, |
| 2791 | _PyIO_str_readline, NULL); |
| 2792 | if (line && !PyUnicode_Check(line)) { |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 2793 | PyErr_Format(PyExc_OSError, |
Serhiy Storchaka | b6a9c97 | 2016-04-17 09:39:28 +0300 | [diff] [blame] | 2794 | "readline() should have returned a str object, " |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2795 | "not '%.200s'", Py_TYPE(line)->tp_name); |
| 2796 | Py_DECREF(line); |
| 2797 | return NULL; |
| 2798 | } |
| 2799 | } |
| 2800 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2801 | if (line == NULL || PyUnicode_READY(line) == -1) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2802 | return NULL; |
| 2803 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2804 | if (PyUnicode_GET_LENGTH(line) == 0) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2805 | /* Reached EOF or would have blocked */ |
| 2806 | Py_DECREF(line); |
| 2807 | Py_CLEAR(self->snapshot); |
| 2808 | self->telling = self->seekable; |
| 2809 | return NULL; |
| 2810 | } |
| 2811 | |
| 2812 | return line; |
| 2813 | } |
| 2814 | |
| 2815 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2816 | textiowrapper_name_get(textio *self, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2817 | { |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2818 | CHECK_ATTACHED(self); |
Martin v. Löwis | 767046a | 2011-10-14 15:35:36 +0200 | [diff] [blame] | 2819 | return _PyObject_GetAttrId(self->buffer, &PyId_name); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2823 | textiowrapper_closed_get(textio *self, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2824 | { |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2825 | CHECK_ATTACHED(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2826 | return PyObject_GetAttr(self->buffer, _PyIO_str_closed); |
| 2827 | } |
| 2828 | |
| 2829 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2830 | textiowrapper_newlines_get(textio *self, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2831 | { |
| 2832 | PyObject *res; |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2833 | CHECK_ATTACHED(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2834 | if (self->decoder == NULL) |
| 2835 | Py_RETURN_NONE; |
| 2836 | res = PyObject_GetAttr(self->decoder, _PyIO_str_newlines); |
| 2837 | if (res == NULL) { |
Benjamin Peterson | 2cfca79 | 2009-06-06 20:46:48 +0000 | [diff] [blame] | 2838 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 2839 | PyErr_Clear(); |
| 2840 | Py_RETURN_NONE; |
| 2841 | } |
| 2842 | else { |
| 2843 | return NULL; |
| 2844 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2845 | } |
| 2846 | return res; |
| 2847 | } |
| 2848 | |
| 2849 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2850 | textiowrapper_errors_get(textio *self, void *context) |
Benjamin Peterson | 0926ad1 | 2009-06-06 18:02:12 +0000 | [diff] [blame] | 2851 | { |
| 2852 | CHECK_INITIALIZED(self); |
| 2853 | return PyUnicode_FromString(PyBytes_AS_STRING(self->errors)); |
| 2854 | } |
| 2855 | |
| 2856 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2857 | textiowrapper_chunk_size_get(textio *self, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2858 | { |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2859 | CHECK_ATTACHED(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2860 | return PyLong_FromSsize_t(self->chunk_size); |
| 2861 | } |
| 2862 | |
| 2863 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2864 | textiowrapper_chunk_size_set(textio *self, PyObject *arg, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2865 | { |
| 2866 | Py_ssize_t n; |
Benjamin Peterson | 10e76b6 | 2014-12-21 20:51:50 -0600 | [diff] [blame] | 2867 | CHECK_ATTACHED_INT(self); |
Antoine Pitrou | cb4ae81 | 2011-07-13 21:07:49 +0200 | [diff] [blame] | 2868 | n = PyNumber_AsSsize_t(arg, PyExc_ValueError); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2869 | if (n == -1 && PyErr_Occurred()) |
| 2870 | return -1; |
| 2871 | if (n <= 0) { |
| 2872 | PyErr_SetString(PyExc_ValueError, |
| 2873 | "a strictly positive integer is required"); |
| 2874 | return -1; |
| 2875 | } |
| 2876 | self->chunk_size = n; |
| 2877 | return 0; |
| 2878 | } |
| 2879 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2880 | #include "clinic/textio.c.h" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2881 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2882 | static PyMethodDef incrementalnewlinedecoder_methods[] = { |
| 2883 | _IO_INCREMENTALNEWLINEDECODER_DECODE_METHODDEF |
| 2884 | _IO_INCREMENTALNEWLINEDECODER_GETSTATE_METHODDEF |
| 2885 | _IO_INCREMENTALNEWLINEDECODER_SETSTATE_METHODDEF |
| 2886 | _IO_INCREMENTALNEWLINEDECODER_RESET_METHODDEF |
| 2887 | {NULL} |
| 2888 | }; |
| 2889 | |
| 2890 | static PyGetSetDef incrementalnewlinedecoder_getset[] = { |
| 2891 | {"newlines", (getter)incrementalnewlinedecoder_newlines_get, NULL, NULL}, |
| 2892 | {NULL} |
| 2893 | }; |
| 2894 | |
| 2895 | PyTypeObject PyIncrementalNewlineDecoder_Type = { |
| 2896 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2897 | "_io.IncrementalNewlineDecoder", /*tp_name*/ |
| 2898 | sizeof(nldecoder_object), /*tp_basicsize*/ |
| 2899 | 0, /*tp_itemsize*/ |
| 2900 | (destructor)incrementalnewlinedecoder_dealloc, /*tp_dealloc*/ |
| 2901 | 0, /*tp_print*/ |
| 2902 | 0, /*tp_getattr*/ |
| 2903 | 0, /*tp_setattr*/ |
| 2904 | 0, /*tp_compare */ |
| 2905 | 0, /*tp_repr*/ |
| 2906 | 0, /*tp_as_number*/ |
| 2907 | 0, /*tp_as_sequence*/ |
| 2908 | 0, /*tp_as_mapping*/ |
| 2909 | 0, /*tp_hash */ |
| 2910 | 0, /*tp_call*/ |
| 2911 | 0, /*tp_str*/ |
| 2912 | 0, /*tp_getattro*/ |
| 2913 | 0, /*tp_setattro*/ |
| 2914 | 0, /*tp_as_buffer*/ |
| 2915 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
| 2916 | _io_IncrementalNewlineDecoder___init____doc__, /* tp_doc */ |
| 2917 | 0, /* tp_traverse */ |
| 2918 | 0, /* tp_clear */ |
| 2919 | 0, /* tp_richcompare */ |
| 2920 | 0, /*tp_weaklistoffset*/ |
| 2921 | 0, /* tp_iter */ |
| 2922 | 0, /* tp_iternext */ |
| 2923 | incrementalnewlinedecoder_methods, /* tp_methods */ |
| 2924 | 0, /* tp_members */ |
| 2925 | incrementalnewlinedecoder_getset, /* tp_getset */ |
| 2926 | 0, /* tp_base */ |
| 2927 | 0, /* tp_dict */ |
| 2928 | 0, /* tp_descr_get */ |
| 2929 | 0, /* tp_descr_set */ |
| 2930 | 0, /* tp_dictoffset */ |
| 2931 | _io_IncrementalNewlineDecoder___init__, /* tp_init */ |
| 2932 | 0, /* tp_alloc */ |
| 2933 | PyType_GenericNew, /* tp_new */ |
| 2934 | }; |
| 2935 | |
| 2936 | |
| 2937 | static PyMethodDef textiowrapper_methods[] = { |
| 2938 | _IO_TEXTIOWRAPPER_DETACH_METHODDEF |
Antoine Pitrou | 3c2817b | 2017-06-03 12:32:28 +0200 | [diff] [blame] | 2939 | _IO_TEXTIOWRAPPER_RECONFIGURE_METHODDEF |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2940 | _IO_TEXTIOWRAPPER_WRITE_METHODDEF |
| 2941 | _IO_TEXTIOWRAPPER_READ_METHODDEF |
| 2942 | _IO_TEXTIOWRAPPER_READLINE_METHODDEF |
| 2943 | _IO_TEXTIOWRAPPER_FLUSH_METHODDEF |
| 2944 | _IO_TEXTIOWRAPPER_CLOSE_METHODDEF |
| 2945 | |
| 2946 | _IO_TEXTIOWRAPPER_FILENO_METHODDEF |
| 2947 | _IO_TEXTIOWRAPPER_SEEKABLE_METHODDEF |
| 2948 | _IO_TEXTIOWRAPPER_READABLE_METHODDEF |
| 2949 | _IO_TEXTIOWRAPPER_WRITABLE_METHODDEF |
| 2950 | _IO_TEXTIOWRAPPER_ISATTY_METHODDEF |
Antoine Pitrou | 243757e | 2010-11-05 21:15:39 +0000 | [diff] [blame] | 2951 | {"__getstate__", (PyCFunction)textiowrapper_getstate, METH_NOARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2952 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 2953 | _IO_TEXTIOWRAPPER_SEEK_METHODDEF |
| 2954 | _IO_TEXTIOWRAPPER_TELL_METHODDEF |
| 2955 | _IO_TEXTIOWRAPPER_TRUNCATE_METHODDEF |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2956 | {NULL, NULL} |
| 2957 | }; |
| 2958 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2959 | static PyMemberDef textiowrapper_members[] = { |
| 2960 | {"encoding", T_OBJECT, offsetof(textio, encoding), READONLY}, |
| 2961 | {"buffer", T_OBJECT, offsetof(textio, buffer), READONLY}, |
| 2962 | {"line_buffering", T_BOOL, offsetof(textio, line_buffering), READONLY}, |
Antoine Pitrou | 3c2817b | 2017-06-03 12:32:28 +0200 | [diff] [blame] | 2963 | {"write_through", T_BOOL, offsetof(textio, write_through), READONLY}, |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 2964 | {"_finalizing", T_BOOL, offsetof(textio, finalizing), 0}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2965 | {NULL} |
| 2966 | }; |
| 2967 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2968 | static PyGetSetDef textiowrapper_getset[] = { |
| 2969 | {"name", (getter)textiowrapper_name_get, NULL, NULL}, |
| 2970 | {"closed", (getter)textiowrapper_closed_get, NULL, NULL}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2971 | /* {"mode", (getter)TextIOWrapper_mode_get, NULL, NULL}, |
| 2972 | */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2973 | {"newlines", (getter)textiowrapper_newlines_get, NULL, NULL}, |
| 2974 | {"errors", (getter)textiowrapper_errors_get, NULL, NULL}, |
| 2975 | {"_CHUNK_SIZE", (getter)textiowrapper_chunk_size_get, |
| 2976 | (setter)textiowrapper_chunk_size_set, NULL}, |
Benjamin Peterson | 1fea321 | 2009-04-19 03:15:20 +0000 | [diff] [blame] | 2977 | {NULL} |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2978 | }; |
| 2979 | |
| 2980 | PyTypeObject PyTextIOWrapper_Type = { |
| 2981 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2982 | "_io.TextIOWrapper", /*tp_name*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2983 | sizeof(textio), /*tp_basicsize*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2984 | 0, /*tp_itemsize*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2985 | (destructor)textiowrapper_dealloc, /*tp_dealloc*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2986 | 0, /*tp_print*/ |
| 2987 | 0, /*tp_getattr*/ |
Benjamin Peterson | c4c0eae | 2009-03-09 00:07:03 +0000 | [diff] [blame] | 2988 | 0, /*tps_etattr*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2989 | 0, /*tp_compare */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2990 | (reprfunc)textiowrapper_repr,/*tp_repr*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2991 | 0, /*tp_as_number*/ |
| 2992 | 0, /*tp_as_sequence*/ |
| 2993 | 0, /*tp_as_mapping*/ |
| 2994 | 0, /*tp_hash */ |
| 2995 | 0, /*tp_call*/ |
| 2996 | 0, /*tp_str*/ |
| 2997 | 0, /*tp_getattro*/ |
| 2998 | 0, /*tp_setattro*/ |
| 2999 | 0, /*tp_as_buffer*/ |
| 3000 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3001 | | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/ |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 3002 | _io_TextIOWrapper___init____doc__, /* tp_doc */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 3003 | (traverseproc)textiowrapper_traverse, /* tp_traverse */ |
| 3004 | (inquiry)textiowrapper_clear, /* tp_clear */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 3005 | 0, /* tp_richcompare */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 3006 | offsetof(textio, weakreflist), /*tp_weaklistoffset*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 3007 | 0, /* tp_iter */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 3008 | (iternextfunc)textiowrapper_iternext, /* tp_iternext */ |
| 3009 | textiowrapper_methods, /* tp_methods */ |
| 3010 | textiowrapper_members, /* tp_members */ |
| 3011 | textiowrapper_getset, /* tp_getset */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 3012 | 0, /* tp_base */ |
| 3013 | 0, /* tp_dict */ |
| 3014 | 0, /* tp_descr_get */ |
| 3015 | 0, /* tp_descr_set */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 3016 | offsetof(textio, dict), /*tp_dictoffset*/ |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 3017 | _io_TextIOWrapper___init__, /* tp_init */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 3018 | 0, /* tp_alloc */ |
| 3019 | PyType_GenericNew, /* tp_new */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3020 | 0, /* tp_free */ |
| 3021 | 0, /* tp_is_gc */ |
| 3022 | 0, /* tp_bases */ |
| 3023 | 0, /* tp_mro */ |
| 3024 | 0, /* tp_cache */ |
| 3025 | 0, /* tp_subclasses */ |
| 3026 | 0, /* tp_weaklist */ |
| 3027 | 0, /* tp_del */ |
| 3028 | 0, /* tp_version_tag */ |
| 3029 | 0, /* tp_finalize */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 3030 | }; |