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