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