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 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 461 | if (PyNumber_Check(arg)) { |
| 462 | size = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
Benjamin Peterson | c9e435e | 2008-09-30 02:22:04 +0000 | [diff] [blame] | 463 | if (size == -1 && PyErr_Occurred()) |
| 464 | return NULL; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 465 | } |
| 466 | else if (arg == Py_None) { |
| 467 | /* Truncate to current position if no argument is passed. */ |
| 468 | size = self->pos; |
| 469 | } |
| 470 | else { |
| 471 | PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
| 472 | Py_TYPE(arg)->tp_name); |
| 473 | return NULL; |
| 474 | } |
| 475 | |
| 476 | if (size < 0) { |
| 477 | PyErr_Format(PyExc_ValueError, |
| 478 | "Negative size value %zd", size); |
| 479 | return NULL; |
| 480 | } |
| 481 | |
| 482 | if (size < self->string_size) { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 483 | ENSURE_REALIZED(self); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 484 | if (resize_buffer(self, size) < 0) |
| 485 | return NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 486 | self->string_size = size; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 487 | } |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 488 | |
| 489 | return PyLong_FromSsize_t(size); |
| 490 | } |
| 491 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 492 | /*[clinic input] |
| 493 | _io.StringIO.seek |
| 494 | pos: Py_ssize_t |
| 495 | whence: int = 0 |
| 496 | / |
| 497 | |
| 498 | Change stream position. |
| 499 | |
| 500 | Seek to character offset pos relative to position indicated by whence: |
| 501 | 0 Start of stream (the default). pos should be >= 0; |
| 502 | 1 Current position - pos must be 0; |
| 503 | 2 End of stream - pos must be 0. |
| 504 | Returns the new absolute position. |
| 505 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 506 | |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 507 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 508 | _io_StringIO_seek_impl(stringio *self, Py_ssize_t pos, int whence) |
| 509 | /*[clinic end generated code: output=e9e0ac9a8ae71c25 input=e3855b24e7cae06a]*/ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 510 | { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 511 | CHECK_INITIALIZED(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 512 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 513 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 514 | if (whence != 0 && whence != 1 && whence != 2) { |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 515 | PyErr_Format(PyExc_ValueError, |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 516 | "Invalid whence (%i, should be 0, 1 or 2)", whence); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 517 | return NULL; |
| 518 | } |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 519 | else if (pos < 0 && whence == 0) { |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 520 | PyErr_Format(PyExc_ValueError, |
| 521 | "Negative seek position %zd", pos); |
| 522 | return NULL; |
| 523 | } |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 524 | else if (whence != 0 && pos != 0) { |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 525 | PyErr_SetString(PyExc_IOError, |
| 526 | "Can't do nonzero cur-relative seeks"); |
| 527 | return NULL; |
| 528 | } |
| 529 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 530 | /* whence = 0: offset relative to beginning of the string. |
| 531 | whence = 1: no change to current position. |
| 532 | whence = 2: change position to end of file. */ |
| 533 | if (whence == 1) { |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 534 | pos = self->pos; |
| 535 | } |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 536 | else if (whence == 2) { |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 537 | pos = self->string_size; |
| 538 | } |
| 539 | |
| 540 | self->pos = pos; |
| 541 | |
| 542 | return PyLong_FromSsize_t(self->pos); |
| 543 | } |
| 544 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 545 | /*[clinic input] |
| 546 | _io.StringIO.write |
| 547 | s as obj: object |
| 548 | / |
| 549 | |
| 550 | Write string to file. |
| 551 | |
| 552 | Returns the number of characters written, which is always equal to |
| 553 | the length of the string. |
| 554 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 555 | |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 556 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 557 | _io_StringIO_write(stringio *self, PyObject *obj) |
| 558 | /*[clinic end generated code: output=0deaba91a15b94da input=cf96f3b16586e669]*/ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 559 | { |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 560 | Py_ssize_t size; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 561 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 562 | CHECK_INITIALIZED(self); |
| 563 | if (!PyUnicode_Check(obj)) { |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 564 | PyErr_Format(PyExc_TypeError, "string argument expected, got '%s'", |
| 565 | Py_TYPE(obj)->tp_name); |
| 566 | return NULL; |
| 567 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 568 | if (PyUnicode_READY(obj)) |
| 569 | return NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 570 | CHECK_CLOSED(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 571 | size = PyUnicode_GET_LENGTH(obj); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 572 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 573 | if (size > 0 && write_str(self, obj) < 0) |
| 574 | return NULL; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 575 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 576 | return PyLong_FromSsize_t(size); |
| 577 | } |
| 578 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 579 | /*[clinic input] |
| 580 | _io.StringIO.close |
| 581 | |
| 582 | Close the IO object. |
| 583 | |
| 584 | Attempting any further operation after the object is closed |
| 585 | will raise a ValueError. |
| 586 | |
| 587 | This method has no effect if the file is already closed. |
| 588 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 589 | |
| 590 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 591 | _io_StringIO_close_impl(stringio *self) |
| 592 | /*[clinic end generated code: output=04399355cbe518f1 input=cbc10b45f35d6d46]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 593 | { |
| 594 | self->closed = 1; |
| 595 | /* Free up some memory */ |
| 596 | if (resize_buffer(self, 0) < 0) |
| 597 | return NULL; |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 598 | _PyAccu_Destroy(&self->accu); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 599 | Py_CLEAR(self->readnl); |
| 600 | Py_CLEAR(self->writenl); |
| 601 | Py_CLEAR(self->decoder); |
| 602 | Py_RETURN_NONE; |
| 603 | } |
| 604 | |
| 605 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 606 | stringio_traverse(stringio *self, visitproc visit, void *arg) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 607 | { |
| 608 | Py_VISIT(self->dict); |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 613 | stringio_clear(stringio *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 614 | { |
| 615 | Py_CLEAR(self->dict); |
| 616 | return 0; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | static void |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 620 | stringio_dealloc(stringio *self) |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 621 | { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 622 | _PyObject_GC_UNTRACK(self); |
Alexandre Vassalotti | fc47704 | 2009-07-22 02:24:49 +0000 | [diff] [blame] | 623 | self->ok = 0; |
| 624 | if (self->buf) { |
| 625 | PyMem_Free(self->buf); |
| 626 | self->buf = NULL; |
| 627 | } |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 628 | _PyAccu_Destroy(&self->accu); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 629 | Py_CLEAR(self->readnl); |
| 630 | Py_CLEAR(self->writenl); |
| 631 | Py_CLEAR(self->decoder); |
Alexandre Vassalotti | fc47704 | 2009-07-22 02:24:49 +0000 | [diff] [blame] | 632 | Py_CLEAR(self->dict); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 633 | if (self->weakreflist != NULL) |
| 634 | PyObject_ClearWeakRefs((PyObject *) self); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 635 | Py_TYPE(self)->tp_free(self); |
| 636 | } |
| 637 | |
| 638 | static PyObject * |
| 639 | stringio_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 640 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 641 | stringio *self; |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 642 | |
| 643 | assert(type != NULL && type->tp_alloc != NULL); |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 644 | self = (stringio *)type->tp_alloc(type, 0); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 645 | if (self == NULL) |
| 646 | return NULL; |
| 647 | |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 648 | /* tp_alloc initializes all the fields to zero. So we don't have to |
| 649 | initialize them here. */ |
| 650 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 651 | self->buf = (Py_UCS4 *)PyMem_Malloc(0); |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 652 | if (self->buf == NULL) { |
| 653 | Py_DECREF(self); |
| 654 | return PyErr_NoMemory(); |
| 655 | } |
| 656 | |
| 657 | return (PyObject *)self; |
| 658 | } |
| 659 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 660 | /*[clinic input] |
| 661 | _io.StringIO.__init__ |
| 662 | initial_value as value: object(c_default="NULL") = '' |
| 663 | newline as newline_obj: object(c_default="NULL") = '\n' |
| 664 | |
| 665 | Text I/O implementation using an in-memory buffer. |
| 666 | |
| 667 | The initial_value argument sets the value of object. The newline |
| 668 | argument is like the one of TextIOWrapper's constructor. |
| 669 | [clinic start generated code]*/ |
| 670 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 671 | static int |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 672 | _io_StringIO___init___impl(stringio *self, PyObject *value, |
| 673 | PyObject *newline_obj) |
| 674 | /*[clinic end generated code: output=a421ea023b22ef4e input=cee2d9181b2577a3]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 675 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 676 | const char *newline = "\n"; |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 677 | Py_ssize_t value_len; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 678 | |
Martin Panter | f264416 | 2015-10-10 10:17:57 +0000 | [diff] [blame] | 679 | /* Parse the newline argument. We only want to allow unicode objects or |
| 680 | None. */ |
Alexandre Vassalotti | d2bb18b | 2009-07-22 03:07:33 +0000 | [diff] [blame] | 681 | if (newline_obj == Py_None) { |
| 682 | newline = NULL; |
| 683 | } |
| 684 | else if (newline_obj) { |
| 685 | if (!PyUnicode_Check(newline_obj)) { |
| 686 | PyErr_Format(PyExc_TypeError, |
| 687 | "newline must be str or None, not %.200s", |
| 688 | Py_TYPE(newline_obj)->tp_name); |
| 689 | return -1; |
| 690 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 691 | newline = PyUnicode_AsUTF8(newline_obj); |
Alexandre Vassalotti | d2bb18b | 2009-07-22 03:07:33 +0000 | [diff] [blame] | 692 | if (newline == NULL) |
| 693 | return -1; |
| 694 | } |
| 695 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 696 | if (newline && newline[0] != '\0' |
| 697 | && !(newline[0] == '\n' && newline[1] == '\0') |
| 698 | && !(newline[0] == '\r' && newline[1] == '\0') |
| 699 | && !(newline[0] == '\r' && newline[1] == '\n' && newline[2] == '\0')) { |
| 700 | PyErr_Format(PyExc_ValueError, |
Alexandre Vassalotti | d2bb18b | 2009-07-22 03:07:33 +0000 | [diff] [blame] | 701 | "illegal newline value: %R", newline_obj); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 702 | return -1; |
| 703 | } |
| 704 | if (value && value != Py_None && !PyUnicode_Check(value)) { |
Alexandre Vassalotti | d2bb18b | 2009-07-22 03:07:33 +0000 | [diff] [blame] | 705 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 706 | "initial_value must be str or None, not %.200s", |
| 707 | Py_TYPE(value)->tp_name); |
| 708 | return -1; |
| 709 | } |
| 710 | |
| 711 | self->ok = 0; |
| 712 | |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 713 | _PyAccu_Destroy(&self->accu); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 714 | Py_CLEAR(self->readnl); |
| 715 | Py_CLEAR(self->writenl); |
| 716 | Py_CLEAR(self->decoder); |
| 717 | |
Alexandre Vassalotti | d2bb18b | 2009-07-22 03:07:33 +0000 | [diff] [blame] | 718 | assert((newline != NULL && newline_obj != Py_None) || |
| 719 | (newline == NULL && newline_obj == Py_None)); |
| 720 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 721 | if (newline) { |
| 722 | self->readnl = PyUnicode_FromString(newline); |
| 723 | if (self->readnl == NULL) |
| 724 | return -1; |
| 725 | } |
| 726 | self->readuniversal = (newline == NULL || newline[0] == '\0'); |
| 727 | self->readtranslate = (newline == NULL); |
| 728 | /* If newline == "", we don't translate anything. |
| 729 | If newline == "\n" or newline == None, we translate to "\n", which is |
| 730 | a no-op. |
Martin Panter | 9955a37 | 2015-10-07 10:26:23 +0000 | [diff] [blame] | 731 | (for newline == None, TextIOWrapper translates to os.linesep, but it |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 732 | is pointless for StringIO) |
| 733 | */ |
| 734 | if (newline != NULL && newline[0] == '\r') { |
| 735 | self->writenl = self->readnl; |
| 736 | Py_INCREF(self->writenl); |
| 737 | } |
| 738 | |
| 739 | if (self->readuniversal) { |
| 740 | self->decoder = PyObject_CallFunction( |
| 741 | (PyObject *)&PyIncrementalNewlineDecoder_Type, |
| 742 | "Oi", Py_None, (int) self->readtranslate); |
| 743 | if (self->decoder == NULL) |
| 744 | return -1; |
| 745 | } |
| 746 | |
| 747 | /* Now everything is set up, resize buffer to size of initial value, |
| 748 | and copy it */ |
| 749 | self->string_size = 0; |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 750 | if (value && value != Py_None) |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 751 | value_len = PyUnicode_GetLength(value); |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 752 | else |
| 753 | value_len = 0; |
| 754 | if (value_len > 0) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 755 | /* This is a heuristic, for newline translation might change |
| 756 | the string length. */ |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 757 | if (resize_buffer(self, 0) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 758 | return -1; |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 759 | self->state = STATE_REALIZED; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 760 | self->pos = 0; |
| 761 | if (write_str(self, value) < 0) |
| 762 | return -1; |
| 763 | } |
| 764 | else { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 765 | /* Empty stringio object, we can start by accumulating */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 766 | if (resize_buffer(self, 0) < 0) |
| 767 | return -1; |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 768 | if (_PyAccu_Init(&self->accu)) |
| 769 | return -1; |
| 770 | self->state = STATE_ACCUMULATING; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 771 | } |
| 772 | self->pos = 0; |
| 773 | |
| 774 | self->closed = 0; |
| 775 | self->ok = 1; |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | /* Properties and pseudo-properties */ |
Antoine Pitrou | 1d85745 | 2012-09-05 20:11:49 +0200 | [diff] [blame] | 780 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 781 | /*[clinic input] |
| 782 | _io.StringIO.readable |
Antoine Pitrou | 1d85745 | 2012-09-05 20:11:49 +0200 | [diff] [blame] | 783 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 784 | Returns True if the IO object can be read. |
| 785 | [clinic start generated code]*/ |
Antoine Pitrou | 1d85745 | 2012-09-05 20:11:49 +0200 | [diff] [blame] | 786 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 787 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 788 | _io_StringIO_readable_impl(stringio *self) |
| 789 | /*[clinic end generated code: output=b19d44dd8b1ceb99 input=39ce068b224c21ad]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 790 | { |
| 791 | CHECK_INITIALIZED(self); |
Antoine Pitrou | 1d85745 | 2012-09-05 20:11:49 +0200 | [diff] [blame] | 792 | CHECK_CLOSED(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 793 | Py_RETURN_TRUE; |
| 794 | } |
| 795 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 796 | /*[clinic input] |
| 797 | _io.StringIO.writable |
| 798 | |
| 799 | Returns True if the IO object can be written. |
| 800 | [clinic start generated code]*/ |
| 801 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 802 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 803 | _io_StringIO_writable_impl(stringio *self) |
| 804 | /*[clinic end generated code: output=13e4dd77187074ca input=7a691353aac38835]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 805 | { |
| 806 | CHECK_INITIALIZED(self); |
Antoine Pitrou | 1d85745 | 2012-09-05 20:11:49 +0200 | [diff] [blame] | 807 | CHECK_CLOSED(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 808 | Py_RETURN_TRUE; |
| 809 | } |
| 810 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 811 | /*[clinic input] |
| 812 | _io.StringIO.seekable |
| 813 | |
| 814 | Returns True if the IO object can be seeked. |
| 815 | [clinic start generated code]*/ |
| 816 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 817 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 818 | _io_StringIO_seekable_impl(stringio *self) |
| 819 | /*[clinic end generated code: output=4d20b4641c756879 input=4c606d05b32952e6]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 820 | { |
| 821 | CHECK_INITIALIZED(self); |
Antoine Pitrou | 1d85745 | 2012-09-05 20:11:49 +0200 | [diff] [blame] | 822 | CHECK_CLOSED(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 823 | Py_RETURN_TRUE; |
| 824 | } |
| 825 | |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 826 | /* Pickling support. |
| 827 | |
| 828 | The implementation of __getstate__ is similar to the one for BytesIO, |
| 829 | except that we also save the newline parameter. For __setstate__ and unlike |
| 830 | BytesIO, we call __init__ to restore the object's state. Doing so allows us |
| 831 | to avoid decoding the complex newline state while keeping the object |
| 832 | representation compact. |
| 833 | |
| 834 | See comment in bytesio.c regarding why only pickle protocols and onward are |
| 835 | supported. |
| 836 | */ |
| 837 | |
| 838 | static PyObject * |
| 839 | stringio_getstate(stringio *self) |
| 840 | { |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 841 | PyObject *initvalue = _io_StringIO_getvalue_impl(self); |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 842 | PyObject *dict; |
| 843 | PyObject *state; |
| 844 | |
| 845 | if (initvalue == NULL) |
| 846 | return NULL; |
| 847 | if (self->dict == NULL) { |
| 848 | Py_INCREF(Py_None); |
| 849 | dict = Py_None; |
| 850 | } |
| 851 | else { |
| 852 | dict = PyDict_Copy(self->dict); |
| 853 | if (dict == NULL) |
| 854 | return NULL; |
| 855 | } |
| 856 | |
| 857 | state = Py_BuildValue("(OOnN)", initvalue, |
| 858 | self->readnl ? self->readnl : Py_None, |
| 859 | self->pos, dict); |
| 860 | Py_DECREF(initvalue); |
| 861 | return state; |
| 862 | } |
| 863 | |
| 864 | static PyObject * |
| 865 | stringio_setstate(stringio *self, PyObject *state) |
| 866 | { |
| 867 | PyObject *initarg; |
| 868 | PyObject *position_obj; |
| 869 | PyObject *dict; |
| 870 | Py_ssize_t pos; |
| 871 | |
| 872 | assert(state != NULL); |
| 873 | CHECK_CLOSED(self); |
| 874 | |
| 875 | /* We allow the state tuple to be longer than 4, because we may need |
| 876 | someday to extend the object's state without breaking |
| 877 | backward-compatibility. */ |
| 878 | if (!PyTuple_Check(state) || Py_SIZE(state) < 4) { |
| 879 | PyErr_Format(PyExc_TypeError, |
| 880 | "%.200s.__setstate__ argument should be 4-tuple, got %.200s", |
| 881 | Py_TYPE(self)->tp_name, Py_TYPE(state)->tp_name); |
| 882 | return NULL; |
| 883 | } |
| 884 | |
| 885 | /* Initialize the object's state. */ |
| 886 | initarg = PyTuple_GetSlice(state, 0, 2); |
| 887 | if (initarg == NULL) |
| 888 | return NULL; |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 889 | if (_io_StringIO___init__((PyObject *)self, initarg, NULL) < 0) { |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 890 | Py_DECREF(initarg); |
| 891 | return NULL; |
| 892 | } |
| 893 | Py_DECREF(initarg); |
| 894 | |
| 895 | /* Restore the buffer state. Even if __init__ did initialize the buffer, |
Martin Panter | 8f26565 | 2016-04-19 04:03:41 +0000 | [diff] [blame] | 896 | we have to initialize it again since __init__ may translate the |
| 897 | newlines in the initial_value string. We clearly do not want that |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 898 | because the string value in the state tuple has already been translated |
| 899 | once by __init__. So we do not take any chance and replace object's |
| 900 | buffer completely. */ |
| 901 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 902 | PyObject *item; |
| 903 | Py_UCS4 *buf; |
| 904 | Py_ssize_t bufsize; |
| 905 | |
| 906 | item = PyTuple_GET_ITEM(state, 0); |
| 907 | buf = PyUnicode_AsUCS4Copy(item); |
| 908 | if (buf == NULL) |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 909 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 910 | bufsize = PyUnicode_GET_LENGTH(item); |
| 911 | |
| 912 | if (resize_buffer(self, bufsize) < 0) { |
| 913 | PyMem_Free(buf); |
| 914 | return NULL; |
| 915 | } |
| 916 | memcpy(self->buf, buf, bufsize * sizeof(Py_UCS4)); |
| 917 | PyMem_Free(buf); |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 918 | self->string_size = bufsize; |
| 919 | } |
| 920 | |
| 921 | /* Set carefully the position value. Alternatively, we could use the seek |
| 922 | method instead of modifying self->pos directly to better protect the |
| 923 | object internal state against errneous (or malicious) inputs. */ |
| 924 | position_obj = PyTuple_GET_ITEM(state, 2); |
| 925 | if (!PyLong_Check(position_obj)) { |
| 926 | PyErr_Format(PyExc_TypeError, |
| 927 | "third item of state must be an integer, got %.200s", |
| 928 | Py_TYPE(position_obj)->tp_name); |
| 929 | return NULL; |
| 930 | } |
| 931 | pos = PyLong_AsSsize_t(position_obj); |
| 932 | if (pos == -1 && PyErr_Occurred()) |
| 933 | return NULL; |
| 934 | if (pos < 0) { |
| 935 | PyErr_SetString(PyExc_ValueError, |
| 936 | "position value cannot be negative"); |
| 937 | return NULL; |
| 938 | } |
| 939 | self->pos = pos; |
| 940 | |
| 941 | /* Set the dictionary of the instance variables. */ |
| 942 | dict = PyTuple_GET_ITEM(state, 3); |
| 943 | if (dict != Py_None) { |
| 944 | if (!PyDict_Check(dict)) { |
| 945 | PyErr_Format(PyExc_TypeError, |
| 946 | "fourth item of state should be a dict, got a %.200s", |
| 947 | Py_TYPE(dict)->tp_name); |
| 948 | return NULL; |
| 949 | } |
| 950 | if (self->dict) { |
| 951 | /* Alternatively, we could replace the internal dictionary |
| 952 | completely. However, it seems more practical to just update it. */ |
| 953 | if (PyDict_Update(self->dict, dict) < 0) |
| 954 | return NULL; |
| 955 | } |
| 956 | else { |
| 957 | Py_INCREF(dict); |
| 958 | self->dict = dict; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | Py_RETURN_NONE; |
| 963 | } |
| 964 | |
| 965 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 966 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 967 | stringio_closed(stringio *self, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 968 | { |
| 969 | CHECK_INITIALIZED(self); |
| 970 | return PyBool_FromLong(self->closed); |
| 971 | } |
| 972 | |
| 973 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 974 | stringio_line_buffering(stringio *self, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 975 | { |
| 976 | CHECK_INITIALIZED(self); |
| 977 | CHECK_CLOSED(self); |
| 978 | Py_RETURN_FALSE; |
| 979 | } |
| 980 | |
| 981 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 982 | stringio_newlines(stringio *self, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 983 | { |
| 984 | CHECK_INITIALIZED(self); |
| 985 | CHECK_CLOSED(self); |
| 986 | if (self->decoder == NULL) |
| 987 | Py_RETURN_NONE; |
| 988 | return PyObject_GetAttr(self->decoder, _PyIO_str_newlines); |
| 989 | } |
| 990 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 991 | #include "clinic/stringio.c.h" |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 992 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 993 | static struct PyMethodDef stringio_methods[] = { |
| 994 | _IO_STRINGIO_CLOSE_METHODDEF |
| 995 | _IO_STRINGIO_GETVALUE_METHODDEF |
| 996 | _IO_STRINGIO_READ_METHODDEF |
| 997 | _IO_STRINGIO_READLINE_METHODDEF |
| 998 | _IO_STRINGIO_TELL_METHODDEF |
| 999 | _IO_STRINGIO_TRUNCATE_METHODDEF |
| 1000 | _IO_STRINGIO_SEEK_METHODDEF |
| 1001 | _IO_STRINGIO_WRITE_METHODDEF |
| 1002 | |
| 1003 | _IO_STRINGIO_SEEKABLE_METHODDEF |
| 1004 | _IO_STRINGIO_READABLE_METHODDEF |
| 1005 | _IO_STRINGIO_WRITABLE_METHODDEF |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 1006 | |
| 1007 | {"__getstate__", (PyCFunction)stringio_getstate, METH_NOARGS}, |
| 1008 | {"__setstate__", (PyCFunction)stringio_setstate, METH_O}, |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 1009 | {NULL, NULL} /* sentinel */ |
| 1010 | }; |
| 1011 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1012 | static PyGetSetDef stringio_getset[] = { |
| 1013 | {"closed", (getter)stringio_closed, NULL, NULL}, |
| 1014 | {"newlines", (getter)stringio_newlines, NULL, NULL}, |
| 1015 | /* (following comments straight off of the original Python wrapper:) |
| 1016 | XXX Cruft to support the TextIOWrapper API. This would only |
| 1017 | be meaningful if StringIO supported the buffer attribute. |
| 1018 | Hopefully, a better solution, than adding these pseudo-attributes, |
| 1019 | will be found. |
| 1020 | */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1021 | {"line_buffering", (getter)stringio_line_buffering, NULL, NULL}, |
Benjamin Peterson | 1fea321 | 2009-04-19 03:15:20 +0000 | [diff] [blame] | 1022 | {NULL} |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1023 | }; |
| 1024 | |
| 1025 | PyTypeObject PyStringIO_Type = { |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 1026 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1027 | "_io.StringIO", /*tp_name*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1028 | sizeof(stringio), /*tp_basicsize*/ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 1029 | 0, /*tp_itemsize*/ |
| 1030 | (destructor)stringio_dealloc, /*tp_dealloc*/ |
| 1031 | 0, /*tp_print*/ |
| 1032 | 0, /*tp_getattr*/ |
| 1033 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 1034 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 1035 | 0, /*tp_repr*/ |
| 1036 | 0, /*tp_as_number*/ |
| 1037 | 0, /*tp_as_sequence*/ |
| 1038 | 0, /*tp_as_mapping*/ |
| 1039 | 0, /*tp_hash*/ |
| 1040 | 0, /*tp_call*/ |
| 1041 | 0, /*tp_str*/ |
| 1042 | 0, /*tp_getattro*/ |
| 1043 | 0, /*tp_setattro*/ |
| 1044 | 0, /*tp_as_buffer*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1045 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 1046 | | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 1047 | _io_StringIO___init____doc__, /*tp_doc*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1048 | (traverseproc)stringio_traverse, /*tp_traverse*/ |
| 1049 | (inquiry)stringio_clear, /*tp_clear*/ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 1050 | 0, /*tp_richcompare*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1051 | offsetof(stringio, weakreflist), /*tp_weaklistoffset*/ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 1052 | 0, /*tp_iter*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1053 | (iternextfunc)stringio_iternext, /*tp_iternext*/ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 1054 | stringio_methods, /*tp_methods*/ |
| 1055 | 0, /*tp_members*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1056 | stringio_getset, /*tp_getset*/ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 1057 | 0, /*tp_base*/ |
| 1058 | 0, /*tp_dict*/ |
| 1059 | 0, /*tp_descr_get*/ |
| 1060 | 0, /*tp_descr_set*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1061 | offsetof(stringio, dict), /*tp_dictoffset*/ |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 1062 | _io_StringIO___init__, /*tp_init*/ |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 1063 | 0, /*tp_alloc*/ |
| 1064 | stringio_new, /*tp_new*/ |
| 1065 | }; |