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