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