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