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