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