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