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