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