Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2 | #include "structmember.h" /* for offsetof() */ |
| 3 | #include "_iomodule.h" |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 4 | |
| 5 | typedef struct { |
| 6 | PyObject_HEAD |
| 7 | char *buf; |
| 8 | Py_ssize_t pos; |
| 9 | Py_ssize_t string_size; |
| 10 | size_t buf_size; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 11 | PyObject *dict; |
| 12 | PyObject *weakreflist; |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 13 | Py_ssize_t exports; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 14 | } bytesio; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 15 | |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 16 | typedef struct { |
| 17 | PyObject_HEAD |
| 18 | bytesio *source; |
| 19 | } bytesiobuf; |
| 20 | |
| 21 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 22 | #define CHECK_CLOSED(self) \ |
| 23 | if ((self)->buf == NULL) { \ |
| 24 | PyErr_SetString(PyExc_ValueError, \ |
| 25 | "I/O operation on closed file."); \ |
| 26 | return NULL; \ |
| 27 | } |
| 28 | |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 29 | #define CHECK_EXPORTS(self) \ |
| 30 | if ((self)->exports > 0) { \ |
| 31 | PyErr_SetString(PyExc_BufferError, \ |
| 32 | "Existing exports of data: object cannot be re-sized"); \ |
| 33 | return NULL; \ |
| 34 | } |
| 35 | |
| 36 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 37 | /* Internal routine to get a line from the buffer of a BytesIO |
| 38 | object. Returns the length between the current position to the |
| 39 | next newline character. */ |
| 40 | static Py_ssize_t |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 41 | get_line(bytesio *self, char **output) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 42 | { |
| 43 | char *n; |
| 44 | const char *str_end; |
| 45 | Py_ssize_t len; |
| 46 | |
| 47 | assert(self->buf != NULL); |
| 48 | |
| 49 | /* Move to the end of the line, up to the end of the string, s. */ |
| 50 | str_end = self->buf + self->string_size; |
| 51 | for (n = self->buf + self->pos; |
| 52 | n < str_end && *n != '\n'; |
| 53 | n++); |
| 54 | |
| 55 | /* Skip the newline character */ |
| 56 | if (n < str_end) |
| 57 | n++; |
| 58 | |
| 59 | /* Get the length from the current position to the end of the line. */ |
| 60 | len = n - (self->buf + self->pos); |
| 61 | *output = self->buf + self->pos; |
| 62 | |
| 63 | assert(len >= 0); |
| 64 | assert(self->pos < PY_SSIZE_T_MAX - len); |
| 65 | self->pos += len; |
| 66 | |
| 67 | return len; |
| 68 | } |
| 69 | |
| 70 | /* Internal routine for changing the size of the buffer of BytesIO objects. |
| 71 | The caller should ensure that the 'size' argument is non-negative. Returns |
| 72 | 0 on success, -1 otherwise. */ |
| 73 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 74 | resize_buffer(bytesio *self, size_t size) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 75 | { |
| 76 | /* Here, unsigned types are used to avoid dealing with signed integer |
| 77 | overflow, which is undefined in C. */ |
| 78 | size_t alloc = self->buf_size; |
| 79 | char *new_buf = NULL; |
| 80 | |
| 81 | assert(self->buf != NULL); |
| 82 | |
| 83 | /* For simplicity, stay in the range of the signed type. Anyway, Python |
| 84 | doesn't allow strings to be longer than this. */ |
| 85 | if (size > PY_SSIZE_T_MAX) |
| 86 | goto overflow; |
| 87 | |
| 88 | if (size < alloc / 2) { |
| 89 | /* Major downsize; resize down to exact size. */ |
| 90 | alloc = size + 1; |
| 91 | } |
| 92 | else if (size < alloc) { |
| 93 | /* Within allocated size; quick exit */ |
| 94 | return 0; |
| 95 | } |
| 96 | else if (size <= alloc * 1.125) { |
| 97 | /* Moderate upsize; overallocate similar to list_resize() */ |
| 98 | alloc = size + (size >> 3) + (size < 9 ? 3 : 6); |
| 99 | } |
| 100 | else { |
| 101 | /* Major upsize; resize up to exact size */ |
| 102 | alloc = size + 1; |
| 103 | } |
| 104 | |
| 105 | if (alloc > ((size_t)-1) / sizeof(char)) |
| 106 | goto overflow; |
| 107 | new_buf = (char *)PyMem_Realloc(self->buf, alloc * sizeof(char)); |
| 108 | if (new_buf == NULL) { |
| 109 | PyErr_NoMemory(); |
| 110 | return -1; |
| 111 | } |
| 112 | self->buf_size = alloc; |
| 113 | self->buf = new_buf; |
| 114 | |
| 115 | return 0; |
| 116 | |
| 117 | overflow: |
| 118 | PyErr_SetString(PyExc_OverflowError, |
| 119 | "new buffer size too large"); |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | /* Internal routine for writing a string of bytes to the buffer of a BytesIO |
| 124 | object. Returns the number of bytes wrote, or -1 on error. */ |
| 125 | static Py_ssize_t |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 126 | write_bytes(bytesio *self, const char *bytes, Py_ssize_t len) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 127 | { |
| 128 | assert(self->buf != NULL); |
| 129 | assert(self->pos >= 0); |
| 130 | assert(len >= 0); |
| 131 | |
Alexandre Vassalotti | 1bfe9dc8 | 2008-05-07 01:44:31 +0000 | [diff] [blame] | 132 | if ((size_t)self->pos + len > self->buf_size) { |
| 133 | if (resize_buffer(self, (size_t)self->pos + len) < 0) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | if (self->pos > self->string_size) { |
| 138 | /* In case of overseek, pad with null bytes the buffer region between |
| 139 | the end of stream and the current position. |
| 140 | |
| 141 | 0 lo string_size hi |
| 142 | | |<---used--->|<----------available----------->| |
| 143 | | | <--to pad-->|<---to write---> | |
| 144 | 0 buf position |
| 145 | */ |
| 146 | memset(self->buf + self->string_size, '\0', |
| 147 | (self->pos - self->string_size) * sizeof(char)); |
| 148 | } |
| 149 | |
| 150 | /* Copy the data to the internal buffer, overwriting some of the existing |
| 151 | data if self->pos < self->string_size. */ |
| 152 | memcpy(self->buf + self->pos, bytes, len); |
| 153 | self->pos += len; |
| 154 | |
| 155 | /* Set the new length of the internal string if it has changed. */ |
| 156 | if (self->string_size < self->pos) { |
| 157 | self->string_size = self->pos; |
| 158 | } |
| 159 | |
| 160 | return len; |
| 161 | } |
| 162 | |
| 163 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 164 | bytesio_get_closed(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 165 | { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 166 | if (self->buf == NULL) { |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 167 | Py_RETURN_TRUE; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 168 | } |
| 169 | else { |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 170 | Py_RETURN_FALSE; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 171 | } |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* Generic getter for the writable, readable and seekable properties */ |
| 175 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 176 | return_true(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 177 | { |
| 178 | Py_RETURN_TRUE; |
| 179 | } |
| 180 | |
| 181 | PyDoc_STRVAR(flush_doc, |
| 182 | "flush() -> None. Does nothing."); |
| 183 | |
| 184 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 185 | bytesio_flush(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 186 | { |
Antoine Pitrou | 6be8876 | 2010-05-03 16:48:20 +0000 | [diff] [blame] | 187 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 188 | Py_RETURN_NONE; |
| 189 | } |
| 190 | |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 191 | PyDoc_STRVAR(getbuffer_doc, |
| 192 | "getbuffer() -> bytes.\n" |
| 193 | "\n" |
| 194 | "Get a read-write view over the contents of the BytesIO object."); |
| 195 | |
| 196 | static PyObject * |
| 197 | bytesio_getbuffer(bytesio *self) |
| 198 | { |
| 199 | PyTypeObject *type = &_PyBytesIOBuffer_Type; |
| 200 | bytesiobuf *buf; |
| 201 | PyObject *view; |
| 202 | |
| 203 | CHECK_CLOSED(self); |
| 204 | |
| 205 | buf = (bytesiobuf *) type->tp_alloc(type, 0); |
| 206 | if (buf == NULL) |
| 207 | return NULL; |
| 208 | Py_INCREF(self); |
| 209 | buf->source = self; |
| 210 | view = PyMemoryView_FromObject((PyObject *) buf); |
| 211 | Py_DECREF(buf); |
| 212 | return view; |
| 213 | } |
| 214 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 215 | PyDoc_STRVAR(getval_doc, |
Alexandre Vassalotti | 10dfc1e | 2008-05-08 01:34:41 +0000 | [diff] [blame] | 216 | "getvalue() -> bytes.\n" |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 217 | "\n" |
| 218 | "Retrieve the entire contents of the BytesIO object."); |
| 219 | |
| 220 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 221 | bytesio_getvalue(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 222 | { |
| 223 | CHECK_CLOSED(self); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 224 | return PyBytes_FromStringAndSize(self->buf, self->string_size); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | PyDoc_STRVAR(isatty_doc, |
| 228 | "isatty() -> False.\n" |
| 229 | "\n" |
| 230 | "Always returns False since BytesIO objects are not connected\n" |
| 231 | "to a tty-like device."); |
| 232 | |
| 233 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 234 | bytesio_isatty(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 235 | { |
| 236 | CHECK_CLOSED(self); |
| 237 | Py_RETURN_FALSE; |
| 238 | } |
| 239 | |
| 240 | PyDoc_STRVAR(tell_doc, |
| 241 | "tell() -> current file position, an integer\n"); |
| 242 | |
| 243 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 244 | bytesio_tell(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 245 | { |
| 246 | CHECK_CLOSED(self); |
| 247 | return PyLong_FromSsize_t(self->pos); |
| 248 | } |
| 249 | |
| 250 | PyDoc_STRVAR(read_doc, |
| 251 | "read([size]) -> read at most size bytes, returned as a string.\n" |
| 252 | "\n" |
| 253 | "If the size argument is negative, read until EOF is reached.\n" |
| 254 | "Return an empty string at EOF."); |
| 255 | |
| 256 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 257 | bytesio_read(bytesio *self, PyObject *args) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 258 | { |
| 259 | Py_ssize_t size, n; |
| 260 | char *output; |
| 261 | PyObject *arg = Py_None; |
| 262 | |
| 263 | CHECK_CLOSED(self); |
| 264 | |
| 265 | if (!PyArg_ParseTuple(args, "|O:read", &arg)) |
| 266 | return NULL; |
| 267 | |
| 268 | if (PyLong_Check(arg)) { |
| 269 | size = PyLong_AsSsize_t(arg); |
Benjamin Peterson | a8a9304 | 2008-09-30 02:18:09 +0000 | [diff] [blame] | 270 | if (size == -1 && PyErr_Occurred()) |
| 271 | return NULL; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 272 | } |
| 273 | else if (arg == Py_None) { |
| 274 | /* Read until EOF is reached, by default. */ |
| 275 | size = -1; |
| 276 | } |
| 277 | else { |
| 278 | PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
| 279 | Py_TYPE(arg)->tp_name); |
| 280 | return NULL; |
| 281 | } |
| 282 | |
| 283 | /* adjust invalid sizes */ |
| 284 | n = self->string_size - self->pos; |
| 285 | if (size < 0 || size > n) { |
| 286 | size = n; |
| 287 | if (size < 0) |
| 288 | size = 0; |
| 289 | } |
| 290 | |
| 291 | assert(self->buf != NULL); |
| 292 | output = self->buf + self->pos; |
| 293 | self->pos += size; |
| 294 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 295 | return PyBytes_FromStringAndSize(output, size); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | |
| 299 | PyDoc_STRVAR(read1_doc, |
| 300 | "read1(size) -> read at most size bytes, returned as a string.\n" |
| 301 | "\n" |
| 302 | "If the size argument is negative or omitted, read until EOF is reached.\n" |
| 303 | "Return an empty string at EOF."); |
| 304 | |
| 305 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 306 | bytesio_read1(bytesio *self, PyObject *n) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 307 | { |
| 308 | PyObject *arg, *res; |
| 309 | |
| 310 | arg = PyTuple_Pack(1, n); |
| 311 | if (arg == NULL) |
| 312 | return NULL; |
| 313 | res = bytesio_read(self, arg); |
| 314 | Py_DECREF(arg); |
| 315 | return res; |
| 316 | } |
| 317 | |
| 318 | PyDoc_STRVAR(readline_doc, |
| 319 | "readline([size]) -> next line from the file, as a string.\n" |
| 320 | "\n" |
| 321 | "Retain newline. A non-negative size argument limits the maximum\n" |
| 322 | "number of bytes to return (an incomplete line may be returned then).\n" |
| 323 | "Return an empty string at EOF.\n"); |
| 324 | |
| 325 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 326 | bytesio_readline(bytesio *self, PyObject *args) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 327 | { |
| 328 | Py_ssize_t size, n; |
| 329 | char *output; |
| 330 | PyObject *arg = Py_None; |
| 331 | |
| 332 | CHECK_CLOSED(self); |
| 333 | |
| 334 | if (!PyArg_ParseTuple(args, "|O:readline", &arg)) |
| 335 | return NULL; |
| 336 | |
| 337 | if (PyLong_Check(arg)) { |
| 338 | size = PyLong_AsSsize_t(arg); |
Benjamin Peterson | a8a9304 | 2008-09-30 02:18:09 +0000 | [diff] [blame] | 339 | if (size == -1 && PyErr_Occurred()) |
| 340 | return NULL; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 341 | } |
| 342 | else if (arg == Py_None) { |
| 343 | /* No size limit, by default. */ |
| 344 | size = -1; |
| 345 | } |
| 346 | else { |
| 347 | PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
| 348 | Py_TYPE(arg)->tp_name); |
| 349 | return NULL; |
| 350 | } |
| 351 | |
| 352 | n = get_line(self, &output); |
| 353 | |
| 354 | if (size >= 0 && size < n) { |
| 355 | size = n - size; |
| 356 | n -= size; |
| 357 | self->pos -= size; |
| 358 | } |
| 359 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 360 | return PyBytes_FromStringAndSize(output, n); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | PyDoc_STRVAR(readlines_doc, |
| 364 | "readlines([size]) -> list of strings, each a line from the file.\n" |
| 365 | "\n" |
| 366 | "Call readline() repeatedly and return a list of the lines so read.\n" |
| 367 | "The optional size argument, if given, is an approximate bound on the\n" |
| 368 | "total number of bytes in the lines returned.\n"); |
| 369 | |
| 370 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 371 | bytesio_readlines(bytesio *self, PyObject *args) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 372 | { |
| 373 | Py_ssize_t maxsize, size, n; |
| 374 | PyObject *result, *line; |
| 375 | char *output; |
| 376 | PyObject *arg = Py_None; |
| 377 | |
| 378 | CHECK_CLOSED(self); |
| 379 | |
| 380 | if (!PyArg_ParseTuple(args, "|O:readlines", &arg)) |
| 381 | return NULL; |
| 382 | |
| 383 | if (PyLong_Check(arg)) { |
| 384 | maxsize = PyLong_AsSsize_t(arg); |
Benjamin Peterson | a8a9304 | 2008-09-30 02:18:09 +0000 | [diff] [blame] | 385 | if (maxsize == -1 && PyErr_Occurred()) |
| 386 | return NULL; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 387 | } |
| 388 | else if (arg == Py_None) { |
| 389 | /* No size limit, by default. */ |
| 390 | maxsize = -1; |
| 391 | } |
| 392 | else { |
| 393 | PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
| 394 | Py_TYPE(arg)->tp_name); |
| 395 | return NULL; |
| 396 | } |
| 397 | |
| 398 | size = 0; |
| 399 | result = PyList_New(0); |
| 400 | if (!result) |
| 401 | return NULL; |
| 402 | |
| 403 | while ((n = get_line(self, &output)) != 0) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 404 | line = PyBytes_FromStringAndSize(output, n); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 405 | if (!line) |
| 406 | goto on_error; |
| 407 | if (PyList_Append(result, line) == -1) { |
| 408 | Py_DECREF(line); |
| 409 | goto on_error; |
| 410 | } |
| 411 | Py_DECREF(line); |
| 412 | size += n; |
| 413 | if (maxsize > 0 && size >= maxsize) |
| 414 | break; |
| 415 | } |
| 416 | return result; |
| 417 | |
| 418 | on_error: |
| 419 | Py_DECREF(result); |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | PyDoc_STRVAR(readinto_doc, |
Alexandre Vassalotti | 10dfc1e | 2008-05-08 01:34:41 +0000 | [diff] [blame] | 424 | "readinto(bytearray) -> int. Read up to len(b) bytes into b.\n" |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 425 | "\n" |
| 426 | "Returns number of bytes read (0 for EOF), or None if the object\n" |
| 427 | "is set not to block as has no data to read."); |
| 428 | |
| 429 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 430 | bytesio_readinto(bytesio *self, PyObject *buffer) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 431 | { |
| 432 | void *raw_buffer; |
| 433 | Py_ssize_t len; |
| 434 | |
| 435 | CHECK_CLOSED(self); |
| 436 | |
| 437 | if (PyObject_AsWriteBuffer(buffer, &raw_buffer, &len) == -1) |
| 438 | return NULL; |
| 439 | |
| 440 | if (self->pos + len > self->string_size) |
| 441 | len = self->string_size - self->pos; |
| 442 | |
| 443 | memcpy(raw_buffer, self->buf + self->pos, len); |
| 444 | assert(self->pos + len < PY_SSIZE_T_MAX); |
| 445 | assert(len >= 0); |
| 446 | self->pos += len; |
| 447 | |
| 448 | return PyLong_FromSsize_t(len); |
| 449 | } |
| 450 | |
| 451 | PyDoc_STRVAR(truncate_doc, |
| 452 | "truncate([size]) -> int. Truncate the file to at most size bytes.\n" |
| 453 | "\n" |
| 454 | "Size defaults to the current file position, as returned by tell().\n" |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 455 | "The current file position is unchanged. Returns the new size.\n"); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 456 | |
| 457 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 458 | bytesio_truncate(bytesio *self, PyObject *args) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 459 | { |
| 460 | Py_ssize_t size; |
| 461 | PyObject *arg = Py_None; |
| 462 | |
| 463 | CHECK_CLOSED(self); |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 464 | CHECK_EXPORTS(self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 465 | |
| 466 | if (!PyArg_ParseTuple(args, "|O:truncate", &arg)) |
| 467 | return NULL; |
| 468 | |
| 469 | if (PyLong_Check(arg)) { |
| 470 | size = PyLong_AsSsize_t(arg); |
Benjamin Peterson | a8a9304 | 2008-09-30 02:18:09 +0000 | [diff] [blame] | 471 | if (size == -1 && PyErr_Occurred()) |
| 472 | return NULL; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 473 | } |
| 474 | else if (arg == Py_None) { |
| 475 | /* Truncate to current position if no argument is passed. */ |
| 476 | size = self->pos; |
| 477 | } |
| 478 | else { |
| 479 | PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
| 480 | Py_TYPE(arg)->tp_name); |
| 481 | return NULL; |
| 482 | } |
| 483 | |
| 484 | if (size < 0) { |
| 485 | PyErr_Format(PyExc_ValueError, |
| 486 | "negative size value %zd", size); |
| 487 | return NULL; |
| 488 | } |
| 489 | |
| 490 | if (size < self->string_size) { |
| 491 | self->string_size = size; |
| 492 | if (resize_buffer(self, size) < 0) |
| 493 | return NULL; |
| 494 | } |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 495 | |
| 496 | return PyLong_FromSsize_t(size); |
| 497 | } |
| 498 | |
| 499 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 500 | bytesio_iternext(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 501 | { |
| 502 | char *next; |
| 503 | Py_ssize_t n; |
| 504 | |
| 505 | CHECK_CLOSED(self); |
| 506 | |
| 507 | n = get_line(self, &next); |
| 508 | |
| 509 | if (!next || n == 0) |
| 510 | return NULL; |
| 511 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 512 | return PyBytes_FromStringAndSize(next, n); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | PyDoc_STRVAR(seek_doc, |
| 516 | "seek(pos, whence=0) -> int. Change stream position.\n" |
| 517 | "\n" |
| 518 | "Seek to byte offset pos relative to position indicated by whence:\n" |
| 519 | " 0 Start of stream (the default). pos should be >= 0;\n" |
| 520 | " 1 Current position - pos may be negative;\n" |
| 521 | " 2 End of stream - pos usually negative.\n" |
| 522 | "Returns the new absolute position."); |
| 523 | |
| 524 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 525 | bytesio_seek(bytesio *self, PyObject *args) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 526 | { |
| 527 | Py_ssize_t pos; |
| 528 | int mode = 0; |
| 529 | |
| 530 | CHECK_CLOSED(self); |
| 531 | |
| 532 | if (!PyArg_ParseTuple(args, "n|i:seek", &pos, &mode)) |
| 533 | return NULL; |
| 534 | |
| 535 | if (pos < 0 && mode == 0) { |
| 536 | PyErr_Format(PyExc_ValueError, |
| 537 | "negative seek value %zd", pos); |
| 538 | return NULL; |
| 539 | } |
| 540 | |
| 541 | /* mode 0: offset relative to beginning of the string. |
| 542 | mode 1: offset relative to current position. |
| 543 | mode 2: offset relative the end of the string. */ |
| 544 | if (mode == 1) { |
| 545 | if (pos > PY_SSIZE_T_MAX - self->pos) { |
| 546 | PyErr_SetString(PyExc_OverflowError, |
| 547 | "new position too large"); |
| 548 | return NULL; |
| 549 | } |
| 550 | pos += self->pos; |
| 551 | } |
| 552 | else if (mode == 2) { |
| 553 | if (pos > PY_SSIZE_T_MAX - self->string_size) { |
| 554 | PyErr_SetString(PyExc_OverflowError, |
| 555 | "new position too large"); |
| 556 | return NULL; |
| 557 | } |
| 558 | pos += self->string_size; |
| 559 | } |
| 560 | else if (mode != 0) { |
| 561 | PyErr_Format(PyExc_ValueError, |
| 562 | "invalid whence (%i, should be 0, 1 or 2)", mode); |
| 563 | return NULL; |
| 564 | } |
| 565 | |
| 566 | if (pos < 0) |
| 567 | pos = 0; |
| 568 | self->pos = pos; |
| 569 | |
| 570 | return PyLong_FromSsize_t(self->pos); |
| 571 | } |
| 572 | |
| 573 | PyDoc_STRVAR(write_doc, |
Alexandre Vassalotti | 10dfc1e | 2008-05-08 01:34:41 +0000 | [diff] [blame] | 574 | "write(bytes) -> int. Write bytes to file.\n" |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 575 | "\n" |
| 576 | "Return the number of bytes written."); |
| 577 | |
| 578 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 579 | bytesio_write(bytesio *self, PyObject *obj) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 580 | { |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 581 | Py_ssize_t n = 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 582 | Py_buffer buf; |
| 583 | PyObject *result = NULL; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 584 | |
| 585 | CHECK_CLOSED(self); |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 586 | CHECK_EXPORTS(self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 587 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 588 | if (PyObject_GetBuffer(obj, &buf, PyBUF_CONTIG_RO) < 0) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 589 | return NULL; |
| 590 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 591 | if (buf.len != 0) |
| 592 | n = write_bytes(self, buf.buf, buf.len); |
| 593 | if (n >= 0) |
| 594 | result = PyLong_FromSsize_t(n); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 595 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 596 | PyBuffer_Release(&buf); |
| 597 | return result; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | PyDoc_STRVAR(writelines_doc, |
Alexandre Vassalotti | 7d06089 | 2008-05-07 01:47:37 +0000 | [diff] [blame] | 601 | "writelines(sequence_of_strings) -> None. Write strings to the file.\n" |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 602 | "\n" |
Alexandre Vassalotti | 7d06089 | 2008-05-07 01:47:37 +0000 | [diff] [blame] | 603 | "Note that newlines are not added. The sequence can be any iterable\n" |
| 604 | "object producing strings. This is equivalent to calling write() for\n" |
| 605 | "each string."); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 606 | |
| 607 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 608 | bytesio_writelines(bytesio *self, PyObject *v) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 609 | { |
| 610 | PyObject *it, *item; |
| 611 | PyObject *ret; |
| 612 | |
| 613 | CHECK_CLOSED(self); |
| 614 | |
| 615 | it = PyObject_GetIter(v); |
| 616 | if (it == NULL) |
| 617 | return NULL; |
| 618 | |
| 619 | while ((item = PyIter_Next(it)) != NULL) { |
| 620 | ret = bytesio_write(self, item); |
| 621 | Py_DECREF(item); |
| 622 | if (ret == NULL) { |
| 623 | Py_DECREF(it); |
| 624 | return NULL; |
| 625 | } |
| 626 | Py_DECREF(ret); |
| 627 | } |
| 628 | Py_DECREF(it); |
| 629 | |
| 630 | /* See if PyIter_Next failed */ |
| 631 | if (PyErr_Occurred()) |
| 632 | return NULL; |
| 633 | |
| 634 | Py_RETURN_NONE; |
| 635 | } |
| 636 | |
| 637 | PyDoc_STRVAR(close_doc, |
| 638 | "close() -> None. Disable all I/O operations."); |
| 639 | |
| 640 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 641 | bytesio_close(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 642 | { |
| 643 | if (self->buf != NULL) { |
| 644 | PyMem_Free(self->buf); |
| 645 | self->buf = NULL; |
| 646 | } |
| 647 | Py_RETURN_NONE; |
| 648 | } |
| 649 | |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 650 | /* Pickling support. |
| 651 | |
| 652 | Note that only pickle protocol 2 and onward are supported since we use |
| 653 | extended __reduce__ API of PEP 307 to make BytesIO instances picklable. |
| 654 | |
| 655 | Providing support for protocol < 2 would require the __reduce_ex__ method |
| 656 | which is notably long-winded when defined properly. |
| 657 | |
| 658 | For BytesIO, the implementation would similar to one coded for |
| 659 | object.__reduce_ex__, but slightly less general. To be more specific, we |
| 660 | could call bytesio_getstate directly and avoid checking for the presence of |
| 661 | a fallback __reduce__ method. However, we would still need a __newobj__ |
| 662 | function to use the efficient instance representation of PEP 307. |
| 663 | */ |
| 664 | |
| 665 | static PyObject * |
| 666 | bytesio_getstate(bytesio *self) |
| 667 | { |
| 668 | PyObject *initvalue = bytesio_getvalue(self); |
| 669 | PyObject *dict; |
| 670 | PyObject *state; |
| 671 | |
| 672 | if (initvalue == NULL) |
| 673 | return NULL; |
| 674 | if (self->dict == NULL) { |
| 675 | Py_INCREF(Py_None); |
| 676 | dict = Py_None; |
| 677 | } |
| 678 | else { |
| 679 | dict = PyDict_Copy(self->dict); |
| 680 | if (dict == NULL) |
| 681 | return NULL; |
| 682 | } |
| 683 | |
| 684 | state = Py_BuildValue("(OnN)", initvalue, self->pos, dict); |
| 685 | Py_DECREF(initvalue); |
| 686 | return state; |
| 687 | } |
| 688 | |
| 689 | static PyObject * |
| 690 | bytesio_setstate(bytesio *self, PyObject *state) |
| 691 | { |
| 692 | PyObject *result; |
| 693 | PyObject *position_obj; |
| 694 | PyObject *dict; |
| 695 | Py_ssize_t pos; |
| 696 | |
| 697 | assert(state != NULL); |
| 698 | |
| 699 | /* We allow the state tuple to be longer than 3, because we may need |
| 700 | someday to extend the object's state without breaking |
| 701 | backward-compatibility. */ |
| 702 | if (!PyTuple_Check(state) || Py_SIZE(state) < 3) { |
| 703 | PyErr_Format(PyExc_TypeError, |
| 704 | "%.200s.__setstate__ argument should be 3-tuple, got %.200s", |
| 705 | Py_TYPE(self)->tp_name, Py_TYPE(state)->tp_name); |
| 706 | return NULL; |
| 707 | } |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 708 | CHECK_EXPORTS(self); |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 709 | /* Reset the object to its default state. This is only needed to handle |
| 710 | the case of repeated calls to __setstate__. */ |
| 711 | self->string_size = 0; |
| 712 | self->pos = 0; |
| 713 | |
| 714 | /* Set the value of the internal buffer. If state[0] does not support the |
| 715 | buffer protocol, bytesio_write will raise the appropriate TypeError. */ |
| 716 | result = bytesio_write(self, PyTuple_GET_ITEM(state, 0)); |
| 717 | if (result == NULL) |
| 718 | return NULL; |
| 719 | Py_DECREF(result); |
| 720 | |
| 721 | /* Set carefully the position value. Alternatively, we could use the seek |
| 722 | method instead of modifying self->pos directly to better protect the |
| 723 | object internal state against errneous (or malicious) inputs. */ |
| 724 | position_obj = PyTuple_GET_ITEM(state, 1); |
| 725 | if (!PyLong_Check(position_obj)) { |
| 726 | PyErr_Format(PyExc_TypeError, |
| 727 | "second item of state must be an integer, not %.200s", |
| 728 | Py_TYPE(position_obj)->tp_name); |
| 729 | return NULL; |
| 730 | } |
| 731 | pos = PyLong_AsSsize_t(position_obj); |
| 732 | if (pos == -1 && PyErr_Occurred()) |
| 733 | return NULL; |
| 734 | if (pos < 0) { |
| 735 | PyErr_SetString(PyExc_ValueError, |
| 736 | "position value cannot be negative"); |
| 737 | return NULL; |
| 738 | } |
| 739 | self->pos = pos; |
| 740 | |
| 741 | /* Set the dictionary of the instance variables. */ |
| 742 | dict = PyTuple_GET_ITEM(state, 2); |
| 743 | if (dict != Py_None) { |
| 744 | if (!PyDict_Check(dict)) { |
| 745 | PyErr_Format(PyExc_TypeError, |
| 746 | "third item of state should be a dict, got a %.200s", |
| 747 | Py_TYPE(dict)->tp_name); |
| 748 | return NULL; |
| 749 | } |
| 750 | if (self->dict) { |
| 751 | /* Alternatively, we could replace the internal dictionary |
| 752 | completely. However, it seems more practical to just update it. */ |
| 753 | if (PyDict_Update(self->dict, dict) < 0) |
| 754 | return NULL; |
| 755 | } |
| 756 | else { |
| 757 | Py_INCREF(dict); |
| 758 | self->dict = dict; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | Py_RETURN_NONE; |
| 763 | } |
| 764 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 765 | static void |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 766 | bytesio_dealloc(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 767 | { |
Alexandre Vassalotti | fc47704 | 2009-07-22 02:24:49 +0000 | [diff] [blame] | 768 | _PyObject_GC_UNTRACK(self); |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 769 | if (self->exports > 0) { |
| 770 | PyErr_SetString(PyExc_SystemError, |
| 771 | "deallocated BytesIO object has exported buffers"); |
| 772 | PyErr_Print(); |
| 773 | } |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 774 | if (self->buf != NULL) { |
| 775 | PyMem_Free(self->buf); |
| 776 | self->buf = NULL; |
| 777 | } |
Alexandre Vassalotti | fc47704 | 2009-07-22 02:24:49 +0000 | [diff] [blame] | 778 | Py_CLEAR(self->dict); |
| 779 | if (self->weakreflist != NULL) |
| 780 | PyObject_ClearWeakRefs((PyObject *) self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 781 | Py_TYPE(self)->tp_free(self); |
| 782 | } |
| 783 | |
| 784 | static PyObject * |
| 785 | bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 786 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 787 | bytesio *self; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 788 | |
| 789 | assert(type != NULL && type->tp_alloc != NULL); |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 790 | self = (bytesio *)type->tp_alloc(type, 0); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 791 | if (self == NULL) |
| 792 | return NULL; |
| 793 | |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 794 | /* tp_alloc initializes all the fields to zero. So we don't have to |
| 795 | initialize them here. */ |
| 796 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 797 | self->buf = (char *)PyMem_Malloc(0); |
| 798 | if (self->buf == NULL) { |
| 799 | Py_DECREF(self); |
| 800 | return PyErr_NoMemory(); |
| 801 | } |
| 802 | |
| 803 | return (PyObject *)self; |
| 804 | } |
| 805 | |
| 806 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 807 | bytesio_init(bytesio *self, PyObject *args, PyObject *kwds) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 808 | { |
Alexandre Vassalotti | ba5c743 | 2009-08-04 23:19:13 +0000 | [diff] [blame] | 809 | char *kwlist[] = {"initial_bytes", NULL}; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 810 | PyObject *initvalue = NULL; |
| 811 | |
Alexandre Vassalotti | ba5c743 | 2009-08-04 23:19:13 +0000 | [diff] [blame] | 812 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:BytesIO", kwlist, |
| 813 | &initvalue)) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 814 | return -1; |
| 815 | |
| 816 | /* In case, __init__ is called multiple times. */ |
| 817 | self->string_size = 0; |
| 818 | self->pos = 0; |
| 819 | |
| 820 | if (initvalue && initvalue != Py_None) { |
| 821 | PyObject *res; |
| 822 | res = bytesio_write(self, initvalue); |
| 823 | if (res == NULL) |
| 824 | return -1; |
| 825 | Py_DECREF(res); |
| 826 | self->pos = 0; |
| 827 | } |
| 828 | |
| 829 | return 0; |
| 830 | } |
| 831 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 832 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 833 | bytesio_traverse(bytesio *self, visitproc visit, void *arg) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 834 | { |
| 835 | Py_VISIT(self->dict); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 840 | bytesio_clear(bytesio *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 841 | { |
| 842 | Py_CLEAR(self->dict); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 847 | static PyGetSetDef bytesio_getsetlist[] = { |
| 848 | {"closed", (getter)bytesio_get_closed, NULL, |
| 849 | "True if the file is closed."}, |
Benjamin Peterson | 1fea321 | 2009-04-19 03:15:20 +0000 | [diff] [blame] | 850 | {NULL}, /* sentinel */ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 851 | }; |
| 852 | |
| 853 | static struct PyMethodDef bytesio_methods[] = { |
| 854 | {"readable", (PyCFunction)return_true, METH_NOARGS, NULL}, |
| 855 | {"seekable", (PyCFunction)return_true, METH_NOARGS, NULL}, |
| 856 | {"writable", (PyCFunction)return_true, METH_NOARGS, NULL}, |
| 857 | {"close", (PyCFunction)bytesio_close, METH_NOARGS, close_doc}, |
| 858 | {"flush", (PyCFunction)bytesio_flush, METH_NOARGS, flush_doc}, |
| 859 | {"isatty", (PyCFunction)bytesio_isatty, METH_NOARGS, isatty_doc}, |
| 860 | {"tell", (PyCFunction)bytesio_tell, METH_NOARGS, tell_doc}, |
| 861 | {"write", (PyCFunction)bytesio_write, METH_O, write_doc}, |
| 862 | {"writelines", (PyCFunction)bytesio_writelines, METH_O, writelines_doc}, |
| 863 | {"read1", (PyCFunction)bytesio_read1, METH_O, read1_doc}, |
| 864 | {"readinto", (PyCFunction)bytesio_readinto, METH_O, readinto_doc}, |
| 865 | {"readline", (PyCFunction)bytesio_readline, METH_VARARGS, readline_doc}, |
| 866 | {"readlines", (PyCFunction)bytesio_readlines, METH_VARARGS, readlines_doc}, |
| 867 | {"read", (PyCFunction)bytesio_read, METH_VARARGS, read_doc}, |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 868 | {"getbuffer", (PyCFunction)bytesio_getbuffer, METH_NOARGS, getbuffer_doc}, |
Antoine Pitrou | d5c3f6c | 2010-09-02 19:48:07 +0000 | [diff] [blame] | 869 | {"getvalue", (PyCFunction)bytesio_getvalue, METH_NOARGS, getval_doc}, |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 870 | {"seek", (PyCFunction)bytesio_seek, METH_VARARGS, seek_doc}, |
| 871 | {"truncate", (PyCFunction)bytesio_truncate, METH_VARARGS, truncate_doc}, |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 872 | {"__getstate__", (PyCFunction)bytesio_getstate, METH_NOARGS, NULL}, |
| 873 | {"__setstate__", (PyCFunction)bytesio_setstate, METH_O, NULL}, |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 874 | {NULL, NULL} /* sentinel */ |
| 875 | }; |
| 876 | |
| 877 | PyDoc_STRVAR(bytesio_doc, |
| 878 | "BytesIO([buffer]) -> object\n" |
| 879 | "\n" |
| 880 | "Create a buffered I/O implementation using an in-memory bytes\n" |
| 881 | "buffer, ready for reading and writing."); |
| 882 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 883 | PyTypeObject PyBytesIO_Type = { |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 884 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 885 | "_io.BytesIO", /*tp_name*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 886 | sizeof(bytesio), /*tp_basicsize*/ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 887 | 0, /*tp_itemsize*/ |
| 888 | (destructor)bytesio_dealloc, /*tp_dealloc*/ |
| 889 | 0, /*tp_print*/ |
| 890 | 0, /*tp_getattr*/ |
| 891 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 892 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 893 | 0, /*tp_repr*/ |
| 894 | 0, /*tp_as_number*/ |
| 895 | 0, /*tp_as_sequence*/ |
| 896 | 0, /*tp_as_mapping*/ |
| 897 | 0, /*tp_hash*/ |
| 898 | 0, /*tp_call*/ |
| 899 | 0, /*tp_str*/ |
| 900 | 0, /*tp_getattro*/ |
| 901 | 0, /*tp_setattro*/ |
| 902 | 0, /*tp_as_buffer*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 903 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 904 | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 905 | bytesio_doc, /*tp_doc*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 906 | (traverseproc)bytesio_traverse, /*tp_traverse*/ |
| 907 | (inquiry)bytesio_clear, /*tp_clear*/ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 908 | 0, /*tp_richcompare*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 909 | offsetof(bytesio, weakreflist), /*tp_weaklistoffset*/ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 910 | PyObject_SelfIter, /*tp_iter*/ |
| 911 | (iternextfunc)bytesio_iternext, /*tp_iternext*/ |
| 912 | bytesio_methods, /*tp_methods*/ |
| 913 | 0, /*tp_members*/ |
| 914 | bytesio_getsetlist, /*tp_getset*/ |
| 915 | 0, /*tp_base*/ |
| 916 | 0, /*tp_dict*/ |
| 917 | 0, /*tp_descr_get*/ |
| 918 | 0, /*tp_descr_set*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 919 | offsetof(bytesio, dict), /*tp_dictoffset*/ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 920 | (initproc)bytesio_init, /*tp_init*/ |
| 921 | 0, /*tp_alloc*/ |
| 922 | bytesio_new, /*tp_new*/ |
| 923 | }; |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 924 | |
| 925 | |
| 926 | /* |
| 927 | * Implementation of the small intermediate object used by getbuffer(). |
| 928 | * getbuffer() returns a memoryview over this object, which should make it |
| 929 | * invisible from Python code. |
| 930 | */ |
| 931 | |
| 932 | static int |
| 933 | bytesiobuf_getbuffer(bytesiobuf *obj, Py_buffer *view, int flags) |
| 934 | { |
| 935 | int ret; |
| 936 | void *ptr; |
| 937 | bytesio *b = (bytesio *) obj->source; |
| 938 | if (view == NULL) { |
| 939 | b->exports++; |
| 940 | return 0; |
| 941 | } |
| 942 | ptr = (void *) obj; |
| 943 | ret = PyBuffer_FillInfo(view, (PyObject*)obj, b->buf, b->string_size, |
| 944 | 0, flags); |
| 945 | if (ret >= 0) { |
| 946 | b->exports++; |
| 947 | } |
| 948 | return ret; |
| 949 | } |
| 950 | |
| 951 | static void |
| 952 | bytesiobuf_releasebuffer(bytesiobuf *obj, Py_buffer *view) |
| 953 | { |
| 954 | bytesio *b = (bytesio *) obj->source; |
| 955 | b->exports--; |
| 956 | } |
| 957 | |
| 958 | static int |
| 959 | bytesiobuf_traverse(bytesiobuf *self, visitproc visit, void *arg) |
| 960 | { |
| 961 | Py_VISIT(self->source); |
| 962 | return 0; |
| 963 | } |
| 964 | |
| 965 | static void |
| 966 | bytesiobuf_dealloc(bytesiobuf *self) |
| 967 | { |
| 968 | Py_CLEAR(self->source); |
| 969 | Py_TYPE(self)->tp_free(self); |
| 970 | } |
| 971 | |
| 972 | static PyBufferProcs bytesiobuf_as_buffer = { |
| 973 | (getbufferproc) bytesiobuf_getbuffer, |
| 974 | (releasebufferproc) bytesiobuf_releasebuffer, |
| 975 | }; |
| 976 | |
| 977 | PyTypeObject _PyBytesIOBuffer_Type = { |
| 978 | PyVarObject_HEAD_INIT(NULL, 0) |
| 979 | "_io._BytesIOBuffer", /*tp_name*/ |
| 980 | sizeof(bytesiobuf), /*tp_basicsize*/ |
| 981 | 0, /*tp_itemsize*/ |
| 982 | (destructor)bytesiobuf_dealloc, /*tp_dealloc*/ |
| 983 | 0, /*tp_print*/ |
| 984 | 0, /*tp_getattr*/ |
| 985 | 0, /*tp_setattr*/ |
| 986 | 0, /*tp_reserved*/ |
| 987 | 0, /*tp_repr*/ |
| 988 | 0, /*tp_as_number*/ |
| 989 | 0, /*tp_as_sequence*/ |
| 990 | 0, /*tp_as_mapping*/ |
| 991 | 0, /*tp_hash*/ |
| 992 | 0, /*tp_call*/ |
| 993 | 0, /*tp_str*/ |
| 994 | 0, /*tp_getattro*/ |
| 995 | 0, /*tp_setattro*/ |
| 996 | &bytesiobuf_as_buffer, /*tp_as_buffer*/ |
| 997 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
| 998 | 0, /*tp_doc*/ |
| 999 | (traverseproc)bytesiobuf_traverse, /*tp_traverse*/ |
| 1000 | 0, /*tp_clear*/ |
| 1001 | 0, /*tp_richcompare*/ |
| 1002 | 0, /*tp_weaklistoffset*/ |
| 1003 | 0, /*tp_iter*/ |
| 1004 | 0, /*tp_iternext*/ |
| 1005 | 0, /*tp_methods*/ |
| 1006 | 0, /*tp_members*/ |
| 1007 | 0, /*tp_getset*/ |
| 1008 | 0, /*tp_base*/ |
| 1009 | 0, /*tp_dict*/ |
| 1010 | 0, /*tp_descr_get*/ |
| 1011 | 0, /*tp_descr_set*/ |
| 1012 | 0, /*tp_dictoffset*/ |
| 1013 | 0, /*tp_init*/ |
| 1014 | 0, /*tp_alloc*/ |
| 1015 | 0, /*tp_new*/ |
| 1016 | }; |