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