Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1 | #define PY_SSIZE_T_CLEAN |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 2 | #include "Python.h" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 3 | #include "structmember.h" |
Antoine Pitrou | d0acb41 | 2012-03-22 14:42:18 +0100 | [diff] [blame] | 4 | #include "accu.h" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 5 | #include "_iomodule.h" |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 6 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 7 | /* Implementation note: the buffer is always at least one character longer |
| 8 | than the enclosed string, for proper functioning of _PyIO_find_line_ending. |
| 9 | */ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 10 | |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 11 | #define STATE_REALIZED 1 |
| 12 | #define STATE_ACCUMULATING 2 |
| 13 | |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 14 | typedef struct { |
| 15 | PyObject_HEAD |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 16 | Py_UCS4 *buf; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 17 | Py_ssize_t pos; |
| 18 | Py_ssize_t string_size; |
| 19 | size_t buf_size; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 20 | |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 21 | /* The stringio object can be in two states: accumulating or realized. |
| 22 | In accumulating state, the internal buffer contains nothing and |
| 23 | the contents are given by the embedded _PyAccu structure. |
| 24 | In realized state, the internal buffer is meaningful and the |
| 25 | _PyAccu is destroyed. |
| 26 | */ |
| 27 | int state; |
| 28 | _PyAccu accu; |
| 29 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 30 | char ok; /* initialized? */ |
| 31 | char closed; |
| 32 | char readuniversal; |
| 33 | char readtranslate; |
| 34 | PyObject *decoder; |
| 35 | PyObject *readnl; |
| 36 | PyObject *writenl; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 37 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 38 | PyObject *dict; |
| 39 | PyObject *weakreflist; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 40 | } stringio; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 41 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 42 | #define CHECK_INITIALIZED(self) \ |
| 43 | if (self->ok <= 0) { \ |
| 44 | PyErr_SetString(PyExc_ValueError, \ |
| 45 | "I/O operation on uninitialized object"); \ |
| 46 | return NULL; \ |
| 47 | } |
| 48 | |
| 49 | #define CHECK_CLOSED(self) \ |
| 50 | if (self->closed) { \ |
| 51 | PyErr_SetString(PyExc_ValueError, \ |
| 52 | "I/O operation on closed file"); \ |
| 53 | return NULL; \ |
| 54 | } |
| 55 | |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 56 | #define ENSURE_REALIZED(self) \ |
| 57 | if (realize(self) < 0) { \ |
| 58 | return NULL; \ |
| 59 | } |
| 60 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 61 | PyDoc_STRVAR(stringio_doc, |
| 62 | "Text I/O implementation using an in-memory buffer.\n" |
| 63 | "\n" |
| 64 | "The initial_value argument sets the value of object. The newline\n" |
| 65 | "argument is like the one of TextIOWrapper's constructor."); |
| 66 | |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 67 | |
| 68 | /* Internal routine for changing the size, in terms of characters, of the |
| 69 | buffer of StringIO objects. The caller should ensure that the 'size' |
| 70 | argument is non-negative. Returns 0 on success, -1 otherwise. */ |
| 71 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 72 | resize_buffer(stringio *self, size_t size) |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 73 | { |
| 74 | /* Here, unsigned types are used to avoid dealing with signed integer |
| 75 | overflow, which is undefined in C. */ |
| 76 | size_t alloc = self->buf_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 77 | Py_UCS4 *new_buf = NULL; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 78 | |
| 79 | assert(self->buf != NULL); |
| 80 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 81 | /* Reserve one more char for line ending detection. */ |
| 82 | size = size + 1; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 83 | /* For simplicity, stay in the range of the signed type. Anyway, Python |
| 84 | doesn't allow strings to be longer than this. */ |
| 85 | if (size > PY_SSIZE_T_MAX) |
| 86 | goto overflow; |
| 87 | |
| 88 | if (size < alloc / 2) { |
| 89 | /* Major downsize; resize down to exact size. */ |
| 90 | alloc = size + 1; |
| 91 | } |
| 92 | else if (size < alloc) { |
| 93 | /* Within allocated size; quick exit */ |
| 94 | return 0; |
| 95 | } |
| 96 | else if (size <= alloc * 1.125) { |
| 97 | /* Moderate upsize; overallocate similar to list_resize() */ |
| 98 | alloc = size + (size >> 3) + (size < 9 ? 3 : 6); |
| 99 | } |
| 100 | else { |
| 101 | /* Major upsize; resize up to exact size */ |
| 102 | alloc = size + 1; |
| 103 | } |
| 104 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 105 | if (alloc > PY_SIZE_MAX / sizeof(Py_UCS4)) |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 106 | goto overflow; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 107 | new_buf = (Py_UCS4 *)PyMem_Realloc(self->buf, alloc * sizeof(Py_UCS4)); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 108 | if (new_buf == NULL) { |
| 109 | PyErr_NoMemory(); |
| 110 | return -1; |
| 111 | } |
| 112 | self->buf_size = alloc; |
| 113 | self->buf = new_buf; |
| 114 | |
| 115 | return 0; |
| 116 | |
| 117 | overflow: |
| 118 | PyErr_SetString(PyExc_OverflowError, |
| 119 | "new buffer size too large"); |
| 120 | return -1; |
| 121 | } |
| 122 | |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 123 | static PyObject * |
| 124 | make_intermediate(stringio *self) |
| 125 | { |
| 126 | PyObject *intermediate = _PyAccu_Finish(&self->accu); |
| 127 | self->state = STATE_REALIZED; |
| 128 | if (intermediate == NULL) |
| 129 | return NULL; |
| 130 | if (_PyAccu_Init(&self->accu) || |
| 131 | _PyAccu_Accumulate(&self->accu, intermediate)) { |
| 132 | Py_DECREF(intermediate); |
| 133 | return NULL; |
| 134 | } |
| 135 | self->state = STATE_ACCUMULATING; |
| 136 | return intermediate; |
| 137 | } |
| 138 | |
| 139 | static int |
| 140 | realize(stringio *self) |
| 141 | { |
| 142 | Py_ssize_t len; |
| 143 | PyObject *intermediate; |
| 144 | |
| 145 | if (self->state == STATE_REALIZED) |
| 146 | return 0; |
| 147 | assert(self->state == STATE_ACCUMULATING); |
| 148 | self->state = STATE_REALIZED; |
| 149 | |
| 150 | intermediate = _PyAccu_Finish(&self->accu); |
| 151 | if (intermediate == NULL) |
| 152 | return -1; |
| 153 | |
| 154 | /* Append the intermediate string to the internal buffer. |
| 155 | The length should be equal to the current cursor position. |
| 156 | */ |
| 157 | len = PyUnicode_GET_LENGTH(intermediate); |
| 158 | if (resize_buffer(self, len) < 0) { |
| 159 | Py_DECREF(intermediate); |
| 160 | return -1; |
| 161 | } |
| 162 | if (!PyUnicode_AsUCS4(intermediate, self->buf, len, 0)) { |
| 163 | Py_DECREF(intermediate); |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | Py_DECREF(intermediate); |
| 168 | return 0; |
| 169 | } |
| 170 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 171 | /* Internal routine for writing a whole PyUnicode object to the buffer of a |
| 172 | StringIO object. Returns 0 on success, or -1 on error. */ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 173 | static Py_ssize_t |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 174 | write_str(stringio *self, PyObject *obj) |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 175 | { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 176 | Py_ssize_t len; |
| 177 | PyObject *decoded = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 178 | |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 179 | assert(self->buf != NULL); |
| 180 | assert(self->pos >= 0); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 181 | |
| 182 | if (self->decoder != NULL) { |
| 183 | decoded = _PyIncrementalNewlineDecoder_decode( |
| 184 | self->decoder, obj, 1 /* always final */); |
| 185 | } |
| 186 | else { |
| 187 | decoded = obj; |
| 188 | Py_INCREF(decoded); |
| 189 | } |
| 190 | if (self->writenl) { |
| 191 | PyObject *translated = PyUnicode_Replace( |
| 192 | decoded, _PyIO_str_nl, self->writenl, -1); |
| 193 | Py_DECREF(decoded); |
| 194 | decoded = translated; |
| 195 | } |
| 196 | if (decoded == NULL) |
| 197 | return -1; |
| 198 | |
| 199 | assert(PyUnicode_Check(decoded)); |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 200 | if (PyUnicode_READY(decoded)) { |
| 201 | Py_DECREF(decoded); |
| 202 | return -1; |
| 203 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 204 | len = PyUnicode_GET_LENGTH(decoded); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 205 | assert(len >= 0); |
| 206 | |
| 207 | /* This overflow check is not strictly necessary. However, it avoids us to |
| 208 | deal with funky things like comparing an unsigned and a signed |
| 209 | integer. */ |
| 210 | if (self->pos > PY_SSIZE_T_MAX - len) { |
| 211 | PyErr_SetString(PyExc_OverflowError, |
| 212 | "new position too large"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 213 | goto fail; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 214 | } |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 215 | |
| 216 | if (self->state == STATE_ACCUMULATING) { |
| 217 | if (self->string_size == self->pos) { |
| 218 | if (_PyAccu_Accumulate(&self->accu, decoded)) |
| 219 | goto fail; |
| 220 | goto success; |
| 221 | } |
| 222 | if (realize(self)) |
| 223 | goto fail; |
| 224 | } |
| 225 | |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 226 | if (self->pos + len > self->string_size) { |
| 227 | if (resize_buffer(self, self->pos + len) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 228 | goto fail; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | if (self->pos > self->string_size) { |
| 232 | /* In case of overseek, pad with null bytes the buffer region between |
| 233 | the end of stream and the current position. |
| 234 | |
| 235 | 0 lo string_size hi |
| 236 | | |<---used--->|<----------available----------->| |
| 237 | | | <--to pad-->|<---to write---> | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 238 | 0 buf position |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 239 | |
| 240 | */ |
| 241 | memset(self->buf + self->string_size, '\0', |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 242 | (self->pos - self->string_size) * sizeof(Py_UCS4)); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* Copy the data to the internal buffer, overwriting some of the |
| 246 | existing data if self->pos < self->string_size. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 247 | if (!PyUnicode_AsUCS4(decoded, |
| 248 | self->buf + self->pos, |
| 249 | self->buf_size - self->pos, |
| 250 | 0)) |
| 251 | goto fail; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 252 | |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 253 | success: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 254 | /* Set the new length of the internal string if it has changed. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 255 | self->pos += len; |
| 256 | if (self->string_size < self->pos) |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 257 | self->string_size = self->pos; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 258 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 259 | Py_DECREF(decoded); |
| 260 | return 0; |
| 261 | |
| 262 | fail: |
| 263 | Py_XDECREF(decoded); |
| 264 | return -1; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 267 | PyDoc_STRVAR(stringio_getvalue_doc, |
| 268 | "Retrieve the entire contents of the object."); |
| 269 | |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 270 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 271 | stringio_getvalue(stringio *self) |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 272 | { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 273 | CHECK_INITIALIZED(self); |
| 274 | CHECK_CLOSED(self); |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 275 | if (self->state == STATE_ACCUMULATING) |
| 276 | return make_intermediate(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 277 | return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, self->buf, |
| 278 | self->string_size); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 281 | PyDoc_STRVAR(stringio_tell_doc, |
| 282 | "Tell the current file position."); |
| 283 | |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 284 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 285 | stringio_tell(stringio *self) |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 286 | { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 287 | CHECK_INITIALIZED(self); |
| 288 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 289 | return PyLong_FromSsize_t(self->pos); |
| 290 | } |
| 291 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 292 | PyDoc_STRVAR(stringio_read_doc, |
| 293 | "Read at most n characters, returned as a string.\n" |
| 294 | "\n" |
| 295 | "If the argument is negative or omitted, read until EOF\n" |
| 296 | "is reached. Return an empty string at EOF.\n"); |
| 297 | |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 298 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 299 | stringio_read(stringio *self, PyObject *args) |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 300 | { |
| 301 | Py_ssize_t size, n; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 302 | Py_UCS4 *output; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 303 | PyObject *arg = Py_None; |
| 304 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 305 | CHECK_INITIALIZED(self); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 306 | if (!PyArg_ParseTuple(args, "|O:read", &arg)) |
| 307 | return NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 308 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 309 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 310 | if (PyNumber_Check(arg)) { |
| 311 | size = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
Amaury Forgeot d'Arc | 58fb905 | 2008-09-30 20:22:44 +0000 | [diff] [blame] | 312 | if (size == -1 && PyErr_Occurred()) |
| 313 | return NULL; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 314 | } |
| 315 | else if (arg == Py_None) { |
| 316 | /* Read until EOF is reached, by default. */ |
| 317 | size = -1; |
| 318 | } |
| 319 | else { |
| 320 | PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
| 321 | Py_TYPE(arg)->tp_name); |
| 322 | return NULL; |
| 323 | } |
| 324 | |
| 325 | /* adjust invalid sizes */ |
| 326 | n = self->string_size - self->pos; |
| 327 | if (size < 0 || size > n) { |
| 328 | size = n; |
| 329 | if (size < 0) |
| 330 | size = 0; |
| 331 | } |
| 332 | |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 333 | /* Optimization for seek(0); read() */ |
| 334 | if (self->state == STATE_ACCUMULATING && self->pos == 0 && size == n) { |
| 335 | PyObject *result = make_intermediate(self); |
| 336 | self->pos = self->string_size; |
| 337 | return result; |
| 338 | } |
| 339 | |
| 340 | ENSURE_REALIZED(self); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 341 | output = self->buf + self->pos; |
| 342 | self->pos += size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 343 | return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, size); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 346 | /* Internal helper, used by stringio_readline and stringio_iternext */ |
| 347 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 348 | _stringio_readline(stringio *self, Py_ssize_t limit) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 349 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 350 | Py_UCS4 *start, *end, old_char; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 351 | Py_ssize_t len, consumed; |
| 352 | |
| 353 | /* In case of overseek, return the empty string */ |
| 354 | if (self->pos >= self->string_size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 355 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 356 | |
| 357 | start = self->buf + self->pos; |
| 358 | if (limit < 0 || limit > self->string_size - self->pos) |
| 359 | limit = self->string_size - self->pos; |
| 360 | |
| 361 | end = start + limit; |
| 362 | old_char = *end; |
| 363 | *end = '\0'; |
| 364 | len = _PyIO_find_line_ending( |
| 365 | self->readtranslate, self->readuniversal, self->readnl, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 366 | PyUnicode_4BYTE_KIND, (char*)start, (char*)end, &consumed); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 367 | *end = old_char; |
| 368 | /* If we haven't found any line ending, we just return everything |
| 369 | (`consumed` is ignored). */ |
| 370 | if (len < 0) |
| 371 | len = limit; |
| 372 | self->pos += len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 373 | return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, start, len); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | PyDoc_STRVAR(stringio_readline_doc, |
| 377 | "Read until newline or EOF.\n" |
| 378 | "\n" |
| 379 | "Returns an empty string if EOF is hit immediately.\n"); |
| 380 | |
| 381 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 382 | stringio_readline(stringio *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 383 | { |
| 384 | PyObject *arg = Py_None; |
| 385 | Py_ssize_t limit = -1; |
| 386 | |
| 387 | CHECK_INITIALIZED(self); |
| 388 | if (!PyArg_ParseTuple(args, "|O:readline", &arg)) |
| 389 | return NULL; |
| 390 | CHECK_CLOSED(self); |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 391 | ENSURE_REALIZED(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 392 | |
| 393 | if (PyNumber_Check(arg)) { |
| 394 | limit = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 395 | if (limit == -1 && PyErr_Occurred()) |
| 396 | return NULL; |
| 397 | } |
| 398 | else if (arg != Py_None) { |
| 399 | PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
| 400 | Py_TYPE(arg)->tp_name); |
| 401 | return NULL; |
| 402 | } |
| 403 | return _stringio_readline(self, limit); |
| 404 | } |
| 405 | |
| 406 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 407 | stringio_iternext(stringio *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 408 | { |
| 409 | PyObject *line; |
| 410 | |
| 411 | CHECK_INITIALIZED(self); |
| 412 | CHECK_CLOSED(self); |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 413 | ENSURE_REALIZED(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 414 | |
| 415 | if (Py_TYPE(self) == &PyStringIO_Type) { |
| 416 | /* Skip method call overhead for speed */ |
| 417 | line = _stringio_readline(self, -1); |
| 418 | } |
| 419 | else { |
| 420 | /* XXX is subclassing StringIO really supported? */ |
| 421 | line = PyObject_CallMethodObjArgs((PyObject *)self, |
| 422 | _PyIO_str_readline, NULL); |
| 423 | if (line && !PyUnicode_Check(line)) { |
| 424 | PyErr_Format(PyExc_IOError, |
| 425 | "readline() should have returned an str object, " |
| 426 | "not '%.200s'", Py_TYPE(line)->tp_name); |
| 427 | Py_DECREF(line); |
| 428 | return NULL; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (line == NULL) |
| 433 | return NULL; |
| 434 | |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 435 | if (PyUnicode_GET_LENGTH(line) == 0) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 436 | /* Reached EOF */ |
| 437 | Py_DECREF(line); |
| 438 | return NULL; |
| 439 | } |
| 440 | |
| 441 | return line; |
| 442 | } |
| 443 | |
| 444 | PyDoc_STRVAR(stringio_truncate_doc, |
| 445 | "Truncate size to pos.\n" |
| 446 | "\n" |
| 447 | "The pos argument defaults to the current file position, as\n" |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 448 | "returned by tell(). The current file position is unchanged.\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 449 | "Returns the new absolute position.\n"); |
| 450 | |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 451 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 452 | stringio_truncate(stringio *self, PyObject *args) |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 453 | { |
| 454 | Py_ssize_t size; |
| 455 | PyObject *arg = Py_None; |
| 456 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 457 | CHECK_INITIALIZED(self); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 458 | if (!PyArg_ParseTuple(args, "|O:truncate", &arg)) |
| 459 | return NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 460 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 461 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 462 | if (PyNumber_Check(arg)) { |
| 463 | size = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
Benjamin Peterson | c9e435e | 2008-09-30 02:22:04 +0000 | [diff] [blame] | 464 | if (size == -1 && PyErr_Occurred()) |
| 465 | return NULL; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 466 | } |
| 467 | else if (arg == Py_None) { |
| 468 | /* Truncate to current position if no argument is passed. */ |
| 469 | size = self->pos; |
| 470 | } |
| 471 | else { |
| 472 | PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
| 473 | Py_TYPE(arg)->tp_name); |
| 474 | return NULL; |
| 475 | } |
| 476 | |
| 477 | if (size < 0) { |
| 478 | PyErr_Format(PyExc_ValueError, |
| 479 | "Negative size value %zd", size); |
| 480 | return NULL; |
| 481 | } |
| 482 | |
| 483 | if (size < self->string_size) { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 484 | ENSURE_REALIZED(self); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 485 | if (resize_buffer(self, size) < 0) |
| 486 | return NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 487 | self->string_size = size; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 488 | } |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 489 | |
| 490 | return PyLong_FromSsize_t(size); |
| 491 | } |
| 492 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 493 | PyDoc_STRVAR(stringio_seek_doc, |
| 494 | "Change stream position.\n" |
| 495 | "\n" |
| 496 | "Seek to character offset pos relative to position indicated by whence:\n" |
| 497 | " 0 Start of stream (the default). pos should be >= 0;\n" |
| 498 | " 1 Current position - pos must be 0;\n" |
| 499 | " 2 End of stream - pos must be 0.\n" |
| 500 | "Returns the new absolute position.\n"); |
| 501 | |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 502 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 503 | stringio_seek(stringio *self, PyObject *args) |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 504 | { |
| 505 | Py_ssize_t pos; |
| 506 | int mode = 0; |
| 507 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 508 | CHECK_INITIALIZED(self); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 509 | if (!PyArg_ParseTuple(args, "n|i:seek", &pos, &mode)) |
| 510 | return NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 511 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 512 | |
| 513 | if (mode != 0 && mode != 1 && mode != 2) { |
| 514 | PyErr_Format(PyExc_ValueError, |
| 515 | "Invalid whence (%i, should be 0, 1 or 2)", mode); |
| 516 | return NULL; |
| 517 | } |
| 518 | else if (pos < 0 && mode == 0) { |
| 519 | PyErr_Format(PyExc_ValueError, |
| 520 | "Negative seek position %zd", pos); |
| 521 | return NULL; |
| 522 | } |
| 523 | else if (mode != 0 && pos != 0) { |
| 524 | PyErr_SetString(PyExc_IOError, |
| 525 | "Can't do nonzero cur-relative seeks"); |
| 526 | return NULL; |
| 527 | } |
| 528 | |
| 529 | /* mode 0: offset relative to beginning of the string. |
| 530 | mode 1: no change to current position. |
| 531 | mode 2: change position to end of file. */ |
| 532 | if (mode == 1) { |
| 533 | pos = self->pos; |
| 534 | } |
| 535 | else if (mode == 2) { |
| 536 | pos = self->string_size; |
| 537 | } |
| 538 | |
| 539 | self->pos = pos; |
| 540 | |
| 541 | return PyLong_FromSsize_t(self->pos); |
| 542 | } |
| 543 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 544 | PyDoc_STRVAR(stringio_write_doc, |
| 545 | "Write string to file.\n" |
| 546 | "\n" |
| 547 | "Returns the number of characters written, which is always equal to\n" |
| 548 | "the length of the string.\n"); |
| 549 | |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 550 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 551 | stringio_write(stringio *self, PyObject *obj) |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 552 | { |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 553 | Py_ssize_t size; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 554 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 555 | CHECK_INITIALIZED(self); |
| 556 | if (!PyUnicode_Check(obj)) { |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 557 | PyErr_Format(PyExc_TypeError, "string argument expected, got '%s'", |
| 558 | Py_TYPE(obj)->tp_name); |
| 559 | return NULL; |
| 560 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 561 | if (PyUnicode_READY(obj)) |
| 562 | return NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 563 | CHECK_CLOSED(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 564 | size = PyUnicode_GET_LENGTH(obj); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 565 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 566 | if (size > 0 && write_str(self, obj) < 0) |
| 567 | return NULL; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 568 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 569 | return PyLong_FromSsize_t(size); |
| 570 | } |
| 571 | |
| 572 | PyDoc_STRVAR(stringio_close_doc, |
| 573 | "Close the IO object. Attempting any further operation after the\n" |
| 574 | "object is closed will raise a ValueError.\n" |
| 575 | "\n" |
| 576 | "This method has no effect if the file is already closed.\n"); |
| 577 | |
| 578 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 579 | stringio_close(stringio *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 580 | { |
| 581 | self->closed = 1; |
| 582 | /* Free up some memory */ |
| 583 | if (resize_buffer(self, 0) < 0) |
| 584 | return NULL; |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 585 | _PyAccu_Destroy(&self->accu); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 586 | Py_CLEAR(self->readnl); |
| 587 | Py_CLEAR(self->writenl); |
| 588 | Py_CLEAR(self->decoder); |
| 589 | Py_RETURN_NONE; |
| 590 | } |
| 591 | |
| 592 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 593 | stringio_traverse(stringio *self, visitproc visit, void *arg) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 594 | { |
| 595 | Py_VISIT(self->dict); |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 600 | stringio_clear(stringio *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 601 | { |
| 602 | Py_CLEAR(self->dict); |
| 603 | return 0; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | static void |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 607 | stringio_dealloc(stringio *self) |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 608 | { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 609 | _PyObject_GC_UNTRACK(self); |
Alexandre Vassalotti | fc47704 | 2009-07-22 02:24:49 +0000 | [diff] [blame] | 610 | self->ok = 0; |
| 611 | if (self->buf) { |
| 612 | PyMem_Free(self->buf); |
| 613 | self->buf = NULL; |
| 614 | } |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 615 | _PyAccu_Destroy(&self->accu); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 616 | Py_CLEAR(self->readnl); |
| 617 | Py_CLEAR(self->writenl); |
| 618 | Py_CLEAR(self->decoder); |
Alexandre Vassalotti | fc47704 | 2009-07-22 02:24:49 +0000 | [diff] [blame] | 619 | Py_CLEAR(self->dict); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 620 | if (self->weakreflist != NULL) |
| 621 | PyObject_ClearWeakRefs((PyObject *) self); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 622 | Py_TYPE(self)->tp_free(self); |
| 623 | } |
| 624 | |
| 625 | static PyObject * |
| 626 | stringio_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 627 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 628 | stringio *self; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 629 | |
| 630 | assert(type != NULL && type->tp_alloc != NULL); |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 631 | self = (stringio *)type->tp_alloc(type, 0); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 632 | if (self == NULL) |
| 633 | return NULL; |
| 634 | |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 635 | /* tp_alloc initializes all the fields to zero. So we don't have to |
| 636 | initialize them here. */ |
| 637 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 638 | self->buf = (Py_UCS4 *)PyMem_Malloc(0); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 639 | if (self->buf == NULL) { |
| 640 | Py_DECREF(self); |
| 641 | return PyErr_NoMemory(); |
| 642 | } |
| 643 | |
| 644 | return (PyObject *)self; |
| 645 | } |
| 646 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 647 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 648 | stringio_init(stringio *self, PyObject *args, PyObject *kwds) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 649 | { |
| 650 | char *kwlist[] = {"initial_value", "newline", NULL}; |
| 651 | PyObject *value = NULL; |
Alexandre Vassalotti | d2bb18b | 2009-07-22 03:07:33 +0000 | [diff] [blame] | 652 | PyObject *newline_obj = NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 653 | char *newline = "\n"; |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 654 | Py_ssize_t value_len; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 655 | |
Alexandre Vassalotti | d2bb18b | 2009-07-22 03:07:33 +0000 | [diff] [blame] | 656 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:__init__", kwlist, |
| 657 | &value, &newline_obj)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 658 | return -1; |
| 659 | |
Alexandre Vassalotti | d2bb18b | 2009-07-22 03:07:33 +0000 | [diff] [blame] | 660 | /* Parse the newline argument. This used to be done with the 'z' |
| 661 | specifier, however this allowed any object with the buffer interface to |
| 662 | be converted. Thus we have to parse it manually since we only want to |
| 663 | allow unicode objects or None. */ |
| 664 | if (newline_obj == Py_None) { |
| 665 | newline = NULL; |
| 666 | } |
| 667 | else if (newline_obj) { |
| 668 | if (!PyUnicode_Check(newline_obj)) { |
| 669 | PyErr_Format(PyExc_TypeError, |
| 670 | "newline must be str or None, not %.200s", |
| 671 | Py_TYPE(newline_obj)->tp_name); |
| 672 | return -1; |
| 673 | } |
| 674 | newline = _PyUnicode_AsString(newline_obj); |
| 675 | if (newline == NULL) |
| 676 | return -1; |
| 677 | } |
| 678 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 679 | if (newline && newline[0] != '\0' |
| 680 | && !(newline[0] == '\n' && newline[1] == '\0') |
| 681 | && !(newline[0] == '\r' && newline[1] == '\0') |
| 682 | && !(newline[0] == '\r' && newline[1] == '\n' && newline[2] == '\0')) { |
| 683 | PyErr_Format(PyExc_ValueError, |
Alexandre Vassalotti | d2bb18b | 2009-07-22 03:07:33 +0000 | [diff] [blame] | 684 | "illegal newline value: %R", newline_obj); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 685 | return -1; |
| 686 | } |
| 687 | if (value && value != Py_None && !PyUnicode_Check(value)) { |
Alexandre Vassalotti | d2bb18b | 2009-07-22 03:07:33 +0000 | [diff] [blame] | 688 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 689 | "initial_value must be str or None, not %.200s", |
| 690 | Py_TYPE(value)->tp_name); |
| 691 | return -1; |
| 692 | } |
| 693 | |
| 694 | self->ok = 0; |
| 695 | |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 696 | _PyAccu_Destroy(&self->accu); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 697 | Py_CLEAR(self->readnl); |
| 698 | Py_CLEAR(self->writenl); |
| 699 | Py_CLEAR(self->decoder); |
| 700 | |
Alexandre Vassalotti | d2bb18b | 2009-07-22 03:07:33 +0000 | [diff] [blame] | 701 | assert((newline != NULL && newline_obj != Py_None) || |
| 702 | (newline == NULL && newline_obj == Py_None)); |
| 703 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 704 | if (newline) { |
| 705 | self->readnl = PyUnicode_FromString(newline); |
| 706 | if (self->readnl == NULL) |
| 707 | return -1; |
| 708 | } |
| 709 | self->readuniversal = (newline == NULL || newline[0] == '\0'); |
| 710 | self->readtranslate = (newline == NULL); |
| 711 | /* If newline == "", we don't translate anything. |
| 712 | If newline == "\n" or newline == None, we translate to "\n", which is |
| 713 | a no-op. |
| 714 | (for newline == None, TextIOWrapper translates to os.sepline, but it |
| 715 | is pointless for StringIO) |
| 716 | */ |
| 717 | if (newline != NULL && newline[0] == '\r') { |
| 718 | self->writenl = self->readnl; |
| 719 | Py_INCREF(self->writenl); |
| 720 | } |
| 721 | |
| 722 | if (self->readuniversal) { |
| 723 | self->decoder = PyObject_CallFunction( |
| 724 | (PyObject *)&PyIncrementalNewlineDecoder_Type, |
| 725 | "Oi", Py_None, (int) self->readtranslate); |
| 726 | if (self->decoder == NULL) |
| 727 | return -1; |
| 728 | } |
| 729 | |
| 730 | /* Now everything is set up, resize buffer to size of initial value, |
| 731 | and copy it */ |
| 732 | self->string_size = 0; |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 733 | if (value && value != Py_None) |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 734 | value_len = PyUnicode_GetLength(value); |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 735 | else |
| 736 | value_len = 0; |
| 737 | if (value_len > 0) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 738 | /* This is a heuristic, for newline translation might change |
| 739 | the string length. */ |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 740 | if (resize_buffer(self, 0) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 741 | return -1; |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 742 | self->state = STATE_REALIZED; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 743 | self->pos = 0; |
| 744 | if (write_str(self, value) < 0) |
| 745 | return -1; |
| 746 | } |
| 747 | else { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 748 | /* Empty stringio object, we can start by accumulating */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 749 | if (resize_buffer(self, 0) < 0) |
| 750 | return -1; |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 751 | if (_PyAccu_Init(&self->accu)) |
| 752 | return -1; |
| 753 | self->state = STATE_ACCUMULATING; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 754 | } |
| 755 | self->pos = 0; |
| 756 | |
| 757 | self->closed = 0; |
| 758 | self->ok = 1; |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | /* Properties and pseudo-properties */ |
| 763 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 764 | stringio_seekable(stringio *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 765 | { |
| 766 | CHECK_INITIALIZED(self); |
| 767 | Py_RETURN_TRUE; |
| 768 | } |
| 769 | |
| 770 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 771 | stringio_readable(stringio *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 772 | { |
| 773 | CHECK_INITIALIZED(self); |
| 774 | Py_RETURN_TRUE; |
| 775 | } |
| 776 | |
| 777 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 778 | stringio_writable(stringio *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 779 | { |
| 780 | CHECK_INITIALIZED(self); |
| 781 | Py_RETURN_TRUE; |
| 782 | } |
| 783 | |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 784 | /* Pickling support. |
| 785 | |
| 786 | The implementation of __getstate__ is similar to the one for BytesIO, |
| 787 | except that we also save the newline parameter. For __setstate__ and unlike |
| 788 | BytesIO, we call __init__ to restore the object's state. Doing so allows us |
| 789 | to avoid decoding the complex newline state while keeping the object |
| 790 | representation compact. |
| 791 | |
| 792 | See comment in bytesio.c regarding why only pickle protocols and onward are |
| 793 | supported. |
| 794 | */ |
| 795 | |
| 796 | static PyObject * |
| 797 | stringio_getstate(stringio *self) |
| 798 | { |
| 799 | PyObject *initvalue = stringio_getvalue(self); |
| 800 | PyObject *dict; |
| 801 | PyObject *state; |
| 802 | |
| 803 | if (initvalue == NULL) |
| 804 | return NULL; |
| 805 | if (self->dict == NULL) { |
| 806 | Py_INCREF(Py_None); |
| 807 | dict = Py_None; |
| 808 | } |
| 809 | else { |
| 810 | dict = PyDict_Copy(self->dict); |
| 811 | if (dict == NULL) |
| 812 | return NULL; |
| 813 | } |
| 814 | |
| 815 | state = Py_BuildValue("(OOnN)", initvalue, |
| 816 | self->readnl ? self->readnl : Py_None, |
| 817 | self->pos, dict); |
| 818 | Py_DECREF(initvalue); |
| 819 | return state; |
| 820 | } |
| 821 | |
| 822 | static PyObject * |
| 823 | stringio_setstate(stringio *self, PyObject *state) |
| 824 | { |
| 825 | PyObject *initarg; |
| 826 | PyObject *position_obj; |
| 827 | PyObject *dict; |
| 828 | Py_ssize_t pos; |
| 829 | |
| 830 | assert(state != NULL); |
| 831 | CHECK_CLOSED(self); |
| 832 | |
| 833 | /* We allow the state tuple to be longer than 4, because we may need |
| 834 | someday to extend the object's state without breaking |
| 835 | backward-compatibility. */ |
| 836 | if (!PyTuple_Check(state) || Py_SIZE(state) < 4) { |
| 837 | PyErr_Format(PyExc_TypeError, |
| 838 | "%.200s.__setstate__ argument should be 4-tuple, got %.200s", |
| 839 | Py_TYPE(self)->tp_name, Py_TYPE(state)->tp_name); |
| 840 | return NULL; |
| 841 | } |
| 842 | |
| 843 | /* Initialize the object's state. */ |
| 844 | initarg = PyTuple_GetSlice(state, 0, 2); |
| 845 | if (initarg == NULL) |
| 846 | return NULL; |
| 847 | if (stringio_init(self, initarg, NULL) < 0) { |
| 848 | Py_DECREF(initarg); |
| 849 | return NULL; |
| 850 | } |
| 851 | Py_DECREF(initarg); |
| 852 | |
| 853 | /* Restore the buffer state. Even if __init__ did initialize the buffer, |
| 854 | we have to initialize it again since __init__ may translates the |
| 855 | newlines in the inital_value string. We clearly do not want that |
| 856 | because the string value in the state tuple has already been translated |
| 857 | once by __init__. So we do not take any chance and replace object's |
| 858 | buffer completely. */ |
| 859 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 860 | PyObject *item; |
| 861 | Py_UCS4 *buf; |
| 862 | Py_ssize_t bufsize; |
| 863 | |
| 864 | item = PyTuple_GET_ITEM(state, 0); |
| 865 | buf = PyUnicode_AsUCS4Copy(item); |
| 866 | if (buf == NULL) |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 867 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 868 | bufsize = PyUnicode_GET_LENGTH(item); |
| 869 | |
| 870 | if (resize_buffer(self, bufsize) < 0) { |
| 871 | PyMem_Free(buf); |
| 872 | return NULL; |
| 873 | } |
| 874 | memcpy(self->buf, buf, bufsize * sizeof(Py_UCS4)); |
| 875 | PyMem_Free(buf); |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 876 | self->string_size = bufsize; |
| 877 | } |
| 878 | |
| 879 | /* Set carefully the position value. Alternatively, we could use the seek |
| 880 | method instead of modifying self->pos directly to better protect the |
| 881 | object internal state against errneous (or malicious) inputs. */ |
| 882 | position_obj = PyTuple_GET_ITEM(state, 2); |
| 883 | if (!PyLong_Check(position_obj)) { |
| 884 | PyErr_Format(PyExc_TypeError, |
| 885 | "third item of state must be an integer, got %.200s", |
| 886 | Py_TYPE(position_obj)->tp_name); |
| 887 | return NULL; |
| 888 | } |
| 889 | pos = PyLong_AsSsize_t(position_obj); |
| 890 | if (pos == -1 && PyErr_Occurred()) |
| 891 | return NULL; |
| 892 | if (pos < 0) { |
| 893 | PyErr_SetString(PyExc_ValueError, |
| 894 | "position value cannot be negative"); |
| 895 | return NULL; |
| 896 | } |
| 897 | self->pos = pos; |
| 898 | |
| 899 | /* Set the dictionary of the instance variables. */ |
| 900 | dict = PyTuple_GET_ITEM(state, 3); |
| 901 | if (dict != Py_None) { |
| 902 | if (!PyDict_Check(dict)) { |
| 903 | PyErr_Format(PyExc_TypeError, |
| 904 | "fourth item of state should be a dict, got a %.200s", |
| 905 | Py_TYPE(dict)->tp_name); |
| 906 | return NULL; |
| 907 | } |
| 908 | if (self->dict) { |
| 909 | /* Alternatively, we could replace the internal dictionary |
| 910 | completely. However, it seems more practical to just update it. */ |
| 911 | if (PyDict_Update(self->dict, dict) < 0) |
| 912 | return NULL; |
| 913 | } |
| 914 | else { |
| 915 | Py_INCREF(dict); |
| 916 | self->dict = dict; |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | Py_RETURN_NONE; |
| 921 | } |
| 922 | |
| 923 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 924 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 925 | stringio_closed(stringio *self, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 926 | { |
| 927 | CHECK_INITIALIZED(self); |
| 928 | return PyBool_FromLong(self->closed); |
| 929 | } |
| 930 | |
| 931 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 932 | stringio_line_buffering(stringio *self, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 933 | { |
| 934 | CHECK_INITIALIZED(self); |
| 935 | CHECK_CLOSED(self); |
| 936 | Py_RETURN_FALSE; |
| 937 | } |
| 938 | |
| 939 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 940 | stringio_newlines(stringio *self, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 941 | { |
| 942 | CHECK_INITIALIZED(self); |
| 943 | CHECK_CLOSED(self); |
| 944 | if (self->decoder == NULL) |
| 945 | Py_RETURN_NONE; |
| 946 | return PyObject_GetAttr(self->decoder, _PyIO_str_newlines); |
| 947 | } |
| 948 | |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 949 | static struct PyMethodDef stringio_methods[] = { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 950 | {"close", (PyCFunction)stringio_close, METH_NOARGS, stringio_close_doc}, |
Antoine Pitrou | d5c3f6c | 2010-09-02 19:48:07 +0000 | [diff] [blame] | 951 | {"getvalue", (PyCFunction)stringio_getvalue, METH_NOARGS, stringio_getvalue_doc}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 952 | {"read", (PyCFunction)stringio_read, METH_VARARGS, stringio_read_doc}, |
| 953 | {"readline", (PyCFunction)stringio_readline, METH_VARARGS, stringio_readline_doc}, |
| 954 | {"tell", (PyCFunction)stringio_tell, METH_NOARGS, stringio_tell_doc}, |
| 955 | {"truncate", (PyCFunction)stringio_truncate, METH_VARARGS, stringio_truncate_doc}, |
| 956 | {"seek", (PyCFunction)stringio_seek, METH_VARARGS, stringio_seek_doc}, |
| 957 | {"write", (PyCFunction)stringio_write, METH_O, stringio_write_doc}, |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 958 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 959 | {"seekable", (PyCFunction)stringio_seekable, METH_NOARGS}, |
| 960 | {"readable", (PyCFunction)stringio_readable, METH_NOARGS}, |
| 961 | {"writable", (PyCFunction)stringio_writable, METH_NOARGS}, |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 962 | |
| 963 | {"__getstate__", (PyCFunction)stringio_getstate, METH_NOARGS}, |
| 964 | {"__setstate__", (PyCFunction)stringio_setstate, METH_O}, |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 965 | {NULL, NULL} /* sentinel */ |
| 966 | }; |
| 967 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 968 | static PyGetSetDef stringio_getset[] = { |
| 969 | {"closed", (getter)stringio_closed, NULL, NULL}, |
| 970 | {"newlines", (getter)stringio_newlines, NULL, NULL}, |
| 971 | /* (following comments straight off of the original Python wrapper:) |
| 972 | XXX Cruft to support the TextIOWrapper API. This would only |
| 973 | be meaningful if StringIO supported the buffer attribute. |
| 974 | Hopefully, a better solution, than adding these pseudo-attributes, |
| 975 | will be found. |
| 976 | */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 977 | {"line_buffering", (getter)stringio_line_buffering, NULL, NULL}, |
Benjamin Peterson | 1fea321 | 2009-04-19 03:15:20 +0000 | [diff] [blame] | 978 | {NULL} |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 979 | }; |
| 980 | |
| 981 | PyTypeObject PyStringIO_Type = { |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 982 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 983 | "_io.StringIO", /*tp_name*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 984 | sizeof(stringio), /*tp_basicsize*/ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 985 | 0, /*tp_itemsize*/ |
| 986 | (destructor)stringio_dealloc, /*tp_dealloc*/ |
| 987 | 0, /*tp_print*/ |
| 988 | 0, /*tp_getattr*/ |
| 989 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 990 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 991 | 0, /*tp_repr*/ |
| 992 | 0, /*tp_as_number*/ |
| 993 | 0, /*tp_as_sequence*/ |
| 994 | 0, /*tp_as_mapping*/ |
| 995 | 0, /*tp_hash*/ |
| 996 | 0, /*tp_call*/ |
| 997 | 0, /*tp_str*/ |
| 998 | 0, /*tp_getattro*/ |
| 999 | 0, /*tp_setattro*/ |
| 1000 | 0, /*tp_as_buffer*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1001 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 1002 | | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
| 1003 | stringio_doc, /*tp_doc*/ |
| 1004 | (traverseproc)stringio_traverse, /*tp_traverse*/ |
| 1005 | (inquiry)stringio_clear, /*tp_clear*/ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 1006 | 0, /*tp_richcompare*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1007 | offsetof(stringio, weakreflist), /*tp_weaklistoffset*/ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 1008 | 0, /*tp_iter*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1009 | (iternextfunc)stringio_iternext, /*tp_iternext*/ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 1010 | stringio_methods, /*tp_methods*/ |
| 1011 | 0, /*tp_members*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1012 | stringio_getset, /*tp_getset*/ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 1013 | 0, /*tp_base*/ |
| 1014 | 0, /*tp_dict*/ |
| 1015 | 0, /*tp_descr_get*/ |
| 1016 | 0, /*tp_descr_set*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1017 | offsetof(stringio, dict), /*tp_dictoffset*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1018 | (initproc)stringio_init, /*tp_init*/ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 1019 | 0, /*tp_alloc*/ |
| 1020 | stringio_new, /*tp_new*/ |
| 1021 | }; |