blob: 15dbc338f87953b9ea1f35c71063a4218d235f72 [file] [log] [blame]
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +00001#include "Python.h"
Antoine Pitrou19690592009-06-12 20:14:08 +00002#include "structmember.h" /* for offsetof() */
3#include "_iomodule.h"
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +00004
5typedef struct {
6 PyObject_HEAD
7 char *buf;
8 Py_ssize_t pos;
9 Py_ssize_t string_size;
10 size_t buf_size;
Antoine Pitrou19690592009-06-12 20:14:08 +000011 PyObject *dict;
12 PyObject *weakreflist;
13} bytesio;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +000014
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. */
25static Py_ssize_t
Antoine Pitrou19690592009-06-12 20:14:08 +000026get_line(bytesio *self, char **output)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +000027{
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. */
58static int
Antoine Pitrou19690592009-06-12 20:14:08 +000059resize_buffer(bytesio *self, size_t size)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +000060{
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. */
110static Py_ssize_t
Antoine Pitrou19690592009-06-12 20:14:08 +0000111write_bytes(bytesio *self, const char *bytes, Py_ssize_t len)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000112{
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
148static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000149bytesio_get_closed(bytesio *self)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000150{
Antoine Pitrou19690592009-06-12 20:14:08 +0000151 if (self->buf == NULL) {
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000152 Py_RETURN_TRUE;
Antoine Pitrou19690592009-06-12 20:14:08 +0000153 }
154 else {
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000155 Py_RETURN_FALSE;
Antoine Pitrou19690592009-06-12 20:14:08 +0000156 }
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000157}
158
159/* Generic getter for the writable, readable and seekable properties */
160static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000161return_true(bytesio *self)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000162{
163 Py_RETURN_TRUE;
164}
165
166PyDoc_STRVAR(flush_doc,
167"flush() -> None. Does nothing.");
168
169static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000170bytesio_flush(bytesio *self)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000171{
Antoine Pitrouf7fd8e42010-05-03 16:25:33 +0000172 CHECK_CLOSED(self);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000173 Py_RETURN_NONE;
174}
175
176PyDoc_STRVAR(getval_doc,
177"getvalue() -> bytes.\n"
178"\n"
179"Retrieve the entire contents of the BytesIO object.");
180
181static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000182bytesio_getvalue(bytesio *self)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000183{
184 CHECK_CLOSED(self);
Antoine Pitrou19690592009-06-12 20:14:08 +0000185 return PyBytes_FromStringAndSize(self->buf, self->string_size);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000186}
187
188PyDoc_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
194static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000195bytesio_isatty(bytesio *self)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000196{
197 CHECK_CLOSED(self);
198 Py_RETURN_FALSE;
199}
200
201PyDoc_STRVAR(tell_doc,
202"tell() -> current file position, an integer\n");
203
204static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000205bytesio_tell(bytesio *self)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000206{
207 CHECK_CLOSED(self);
Antoine Pitrou19690592009-06-12 20:14:08 +0000208 return PyLong_FromSsize_t(self->pos);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000209}
210
211PyDoc_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
217static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000218bytesio_read(bytesio *self, PyObject *args)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000219{
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
Antoine Pitrou19690592009-06-12 20:14:08 +0000229 if (PyNumber_Check(arg)) {
230 size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
Benjamin Peterson02252482008-09-30 02:11:07 +0000231 if (size == -1 && PyErr_Occurred())
232 return NULL;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000233 }
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
Antoine Pitrou19690592009-06-12 20:14:08 +0000256 return PyBytes_FromStringAndSize(output, size);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000257}
258
259
260PyDoc_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
266static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000267bytesio_read1(bytesio *self, PyObject *n)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000268{
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
279PyDoc_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
286static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000287bytesio_readline(bytesio *self, PyObject *args)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000288{
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
Antoine Pitrou19690592009-06-12 20:14:08 +0000298 if (PyNumber_Check(arg)) {
299 size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
Benjamin Peterson02252482008-09-30 02:11:07 +0000300 if (size == -1 && PyErr_Occurred())
301 return NULL;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000302 }
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
Antoine Pitrou19690592009-06-12 20:14:08 +0000321 return PyBytes_FromStringAndSize(output, n);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000322}
323
324PyDoc_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
331static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000332bytesio_readlines(bytesio *self, PyObject *args)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000333{
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
Antoine Pitrou19690592009-06-12 20:14:08 +0000344 if (PyNumber_Check(arg)) {
345 maxsize = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
Benjamin Peterson02252482008-09-30 02:11:07 +0000346 if (maxsize == -1 && PyErr_Occurred())
347 return NULL;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000348 }
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) {
Antoine Pitrou19690592009-06-12 20:14:08 +0000365 line = PyBytes_FromStringAndSize(output, n);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000366 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
384PyDoc_STRVAR(readinto_doc,
385"readinto(bytearray) -> int. Read up to len(b) bytes into b.\n"
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
390static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000391bytesio_readinto(bytesio *self, PyObject *args)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000392{
Antoine Pitrou19690592009-06-12 20:14:08 +0000393 Py_buffer buf;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000394 Py_ssize_t len;
395
396 CHECK_CLOSED(self);
397
Antoine Pitrou19690592009-06-12 20:14:08 +0000398 if (!PyArg_ParseTuple(args, "w*", &buf))
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000399 return NULL;
400
Antoine Pitrou19690592009-06-12 20:14:08 +0000401 len = buf.len;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000402 if (self->pos + len > self->string_size)
403 len = self->string_size - self->pos;
404
Antoine Pitrou19690592009-06-12 20:14:08 +0000405 memcpy(buf.buf, self->buf + self->pos, len);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000406 assert(self->pos + len < PY_SSIZE_T_MAX);
407 assert(len >= 0);
408 self->pos += len;
409
Antoine Pitrou19690592009-06-12 20:14:08 +0000410 PyBuffer_Release(&buf);
411 return PyLong_FromSsize_t(len);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000412}
413
414PyDoc_STRVAR(truncate_doc,
415"truncate([size]) -> int. Truncate the file to at most size bytes.\n"
416"\n"
417"Size defaults to the current file position, as returned by tell().\n"
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000418"The current file position is unchanged. Returns the new size.\n");
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000419
420static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000421bytesio_truncate(bytesio *self, PyObject *args)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000422{
423 Py_ssize_t size;
424 PyObject *arg = Py_None;
425
426 CHECK_CLOSED(self);
427
428 if (!PyArg_ParseTuple(args, "|O:truncate", &arg))
429 return NULL;
430
Antoine Pitrou19690592009-06-12 20:14:08 +0000431 if (PyNumber_Check(arg)) {
432 size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
Benjamin Peterson02252482008-09-30 02:11:07 +0000433 if (size == -1 && PyErr_Occurred())
434 return NULL;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000435 }
436 else if (arg == Py_None) {
437 /* Truncate to current position if no argument is passed. */
438 size = self->pos;
439 }
440 else {
441 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
442 Py_TYPE(arg)->tp_name);
443 return NULL;
444 }
445
446 if (size < 0) {
447 PyErr_Format(PyExc_ValueError,
448 "negative size value %zd", size);
449 return NULL;
450 }
451
452 if (size < self->string_size) {
453 self->string_size = size;
454 if (resize_buffer(self, size) < 0)
455 return NULL;
456 }
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000457
Antoine Pitrou19690592009-06-12 20:14:08 +0000458 return PyLong_FromSsize_t(size);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000459}
460
461static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000462bytesio_iternext(bytesio *self)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000463{
464 char *next;
465 Py_ssize_t n;
466
467 CHECK_CLOSED(self);
468
469 n = get_line(self, &next);
470
471 if (!next || n == 0)
472 return NULL;
473
Antoine Pitrou19690592009-06-12 20:14:08 +0000474 return PyBytes_FromStringAndSize(next, n);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000475}
476
477PyDoc_STRVAR(seek_doc,
478"seek(pos, whence=0) -> int. Change stream position.\n"
479"\n"
480"Seek to byte offset pos relative to position indicated by whence:\n"
481" 0 Start of stream (the default). pos should be >= 0;\n"
482" 1 Current position - pos may be negative;\n"
483" 2 End of stream - pos usually negative.\n"
484"Returns the new absolute position.");
485
486static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000487bytesio_seek(bytesio *self, PyObject *args)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000488{
Antoine Pitrou19690592009-06-12 20:14:08 +0000489 PyObject *posobj;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000490 Py_ssize_t pos;
491 int mode = 0;
492
493 CHECK_CLOSED(self);
494
Antoine Pitrou19690592009-06-12 20:14:08 +0000495 if (!PyArg_ParseTuple(args, "O|i:seek", &posobj, &mode))
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000496 return NULL;
497
Antoine Pitrou19690592009-06-12 20:14:08 +0000498 pos = PyNumber_AsSsize_t(posobj, PyExc_OverflowError);
499 if (pos == -1 && PyErr_Occurred())
500 return NULL;
501
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000502 if (pos < 0 && mode == 0) {
503 PyErr_Format(PyExc_ValueError,
504 "negative seek value %zd", pos);
505 return NULL;
506 }
507
508 /* mode 0: offset relative to beginning of the string.
509 mode 1: offset relative to current position.
510 mode 2: offset relative the end of the string. */
511 if (mode == 1) {
512 if (pos > PY_SSIZE_T_MAX - self->pos) {
513 PyErr_SetString(PyExc_OverflowError,
514 "new position too large");
515 return NULL;
516 }
517 pos += self->pos;
518 }
519 else if (mode == 2) {
520 if (pos > PY_SSIZE_T_MAX - self->string_size) {
521 PyErr_SetString(PyExc_OverflowError,
522 "new position too large");
523 return NULL;
524 }
525 pos += self->string_size;
526 }
527 else if (mode != 0) {
528 PyErr_Format(PyExc_ValueError,
529 "invalid whence (%i, should be 0, 1 or 2)", mode);
530 return NULL;
531 }
532
533 if (pos < 0)
534 pos = 0;
535 self->pos = pos;
536
Antoine Pitrou19690592009-06-12 20:14:08 +0000537 return PyLong_FromSsize_t(self->pos);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000538}
539
540PyDoc_STRVAR(write_doc,
541"write(bytes) -> int. Write bytes to file.\n"
542"\n"
543"Return the number of bytes written.");
544
545static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000546bytesio_write(bytesio *self, PyObject *obj)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000547{
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000548 Py_ssize_t n = 0;
Antoine Pitrou19690592009-06-12 20:14:08 +0000549 Py_buffer buf;
550 PyObject *result = NULL;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000551
552 CHECK_CLOSED(self);
553
Antoine Pitrou19690592009-06-12 20:14:08 +0000554 if (PyObject_GetBuffer(obj, &buf, PyBUF_CONTIG_RO) < 0)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000555 return NULL;
556
Antoine Pitrou19690592009-06-12 20:14:08 +0000557 if (buf.len != 0)
558 n = write_bytes(self, buf.buf, buf.len);
559 if (n >= 0)
560 result = PyLong_FromSsize_t(n);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000561
Antoine Pitrou19690592009-06-12 20:14:08 +0000562 PyBuffer_Release(&buf);
563 return result;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000564}
565
566PyDoc_STRVAR(writelines_doc,
567"writelines(sequence_of_strings) -> None. Write strings to the file.\n"
568"\n"
569"Note that newlines are not added. The sequence can be any iterable\n"
570"object producing strings. This is equivalent to calling write() for\n"
571"each string.");
572
573static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000574bytesio_writelines(bytesio *self, PyObject *v)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000575{
576 PyObject *it, *item;
577 PyObject *ret;
578
579 CHECK_CLOSED(self);
580
581 it = PyObject_GetIter(v);
582 if (it == NULL)
583 return NULL;
584
585 while ((item = PyIter_Next(it)) != NULL) {
586 ret = bytesio_write(self, item);
587 Py_DECREF(item);
588 if (ret == NULL) {
589 Py_DECREF(it);
590 return NULL;
591 }
592 Py_DECREF(ret);
593 }
594 Py_DECREF(it);
595
596 /* See if PyIter_Next failed */
597 if (PyErr_Occurred())
598 return NULL;
599
600 Py_RETURN_NONE;
601}
602
603PyDoc_STRVAR(close_doc,
604"close() -> None. Disable all I/O operations.");
605
606static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000607bytesio_close(bytesio *self)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000608{
609 if (self->buf != NULL) {
610 PyMem_Free(self->buf);
611 self->buf = NULL;
612 }
613 Py_RETURN_NONE;
614}
615
Antoine Pitroufa94e802009-10-24 12:23:18 +0000616/* Pickling support.
617
618 Note that only pickle protocol 2 and onward are supported since we use
619 extended __reduce__ API of PEP 307 to make BytesIO instances picklable.
620
621 Providing support for protocol < 2 would require the __reduce_ex__ method
622 which is notably long-winded when defined properly.
623
624 For BytesIO, the implementation would similar to one coded for
625 object.__reduce_ex__, but slightly less general. To be more specific, we
626 could call bytesio_getstate directly and avoid checking for the presence of
627 a fallback __reduce__ method. However, we would still need a __newobj__
628 function to use the efficient instance representation of PEP 307.
629 */
630
631static PyObject *
632bytesio_getstate(bytesio *self)
633{
634 PyObject *initvalue = bytesio_getvalue(self);
635 PyObject *dict;
636 PyObject *state;
637
638 if (initvalue == NULL)
639 return NULL;
640 if (self->dict == NULL) {
641 Py_INCREF(Py_None);
642 dict = Py_None;
643 }
644 else {
645 dict = PyDict_Copy(self->dict);
646 if (dict == NULL)
647 return NULL;
648 }
649
650 state = Py_BuildValue("(OnN)", initvalue, self->pos, dict);
651 Py_DECREF(initvalue);
652 return state;
653}
654
655static PyObject *
656bytesio_setstate(bytesio *self, PyObject *state)
657{
658 PyObject *result;
659 PyObject *position_obj;
660 PyObject *dict;
661 Py_ssize_t pos;
662
663 assert(state != NULL);
664
665 /* We allow the state tuple to be longer than 3, because we may need
666 someday to extend the object's state without breaking
667 backward-compatibility. */
668 if (!PyTuple_Check(state) || Py_SIZE(state) < 3) {
669 PyErr_Format(PyExc_TypeError,
670 "%.200s.__setstate__ argument should be 3-tuple, got %.200s",
671 Py_TYPE(self)->tp_name, Py_TYPE(state)->tp_name);
672 return NULL;
673 }
674 /* Reset the object to its default state. This is only needed to handle
675 the case of repeated calls to __setstate__. */
676 self->string_size = 0;
677 self->pos = 0;
678
679 /* Set the value of the internal buffer. If state[0] does not support the
680 buffer protocol, bytesio_write will raise the appropriate TypeError. */
681 result = bytesio_write(self, PyTuple_GET_ITEM(state, 0));
682 if (result == NULL)
683 return NULL;
684 Py_DECREF(result);
685
686 /* Set carefully the position value. Alternatively, we could use the seek
687 method instead of modifying self->pos directly to better protect the
688 object internal state against errneous (or malicious) inputs. */
689 position_obj = PyTuple_GET_ITEM(state, 1);
690 if (!PyIndex_Check(position_obj)) {
691 PyErr_Format(PyExc_TypeError,
692 "second item of state must be an integer, not %.200s",
693 Py_TYPE(position_obj)->tp_name);
694 return NULL;
695 }
696 pos = PyNumber_AsSsize_t(position_obj, PyExc_OverflowError);
697 if (pos == -1 && PyErr_Occurred())
698 return NULL;
699 if (pos < 0) {
700 PyErr_SetString(PyExc_ValueError,
701 "position value cannot be negative");
702 return NULL;
703 }
704 self->pos = pos;
705
706 /* Set the dictionary of the instance variables. */
707 dict = PyTuple_GET_ITEM(state, 2);
708 if (dict != Py_None) {
709 if (!PyDict_Check(dict)) {
710 PyErr_Format(PyExc_TypeError,
711 "third item of state should be a dict, got a %.200s",
712 Py_TYPE(dict)->tp_name);
713 return NULL;
714 }
715 if (self->dict) {
716 /* Alternatively, we could replace the internal dictionary
717 completely. However, it seems more practical to just update it. */
718 if (PyDict_Update(self->dict, dict) < 0)
719 return NULL;
720 }
721 else {
722 Py_INCREF(dict);
723 self->dict = dict;
724 }
725 }
726
727 Py_RETURN_NONE;
728}
729
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000730static void
Antoine Pitrou19690592009-06-12 20:14:08 +0000731bytesio_dealloc(bytesio *self)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000732{
Antoine Pitrouf98a2672009-10-24 11:59:41 +0000733 _PyObject_GC_UNTRACK(self);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000734 if (self->buf != NULL) {
735 PyMem_Free(self->buf);
736 self->buf = NULL;
737 }
Antoine Pitrouf98a2672009-10-24 11:59:41 +0000738 Py_CLEAR(self->dict);
739 if (self->weakreflist != NULL)
740 PyObject_ClearWeakRefs((PyObject *) self);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000741 Py_TYPE(self)->tp_free(self);
742}
743
744static PyObject *
745bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
746{
Antoine Pitrou19690592009-06-12 20:14:08 +0000747 bytesio *self;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000748
749 assert(type != NULL && type->tp_alloc != NULL);
Antoine Pitrou19690592009-06-12 20:14:08 +0000750 self = (bytesio *)type->tp_alloc(type, 0);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000751 if (self == NULL)
752 return NULL;
753
Antoine Pitroufa94e802009-10-24 12:23:18 +0000754 /* tp_alloc initializes all the fields to zero. So we don't have to
755 initialize them here. */
756
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000757 self->buf = (char *)PyMem_Malloc(0);
758 if (self->buf == NULL) {
759 Py_DECREF(self);
760 return PyErr_NoMemory();
761 }
762
763 return (PyObject *)self;
764}
765
766static int
Antoine Pitrou19690592009-06-12 20:14:08 +0000767bytesio_init(bytesio *self, PyObject *args, PyObject *kwds)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000768{
Antoine Pitrouf7820c12009-10-24 12:28:22 +0000769 char *kwlist[] = {"initial_bytes", NULL};
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000770 PyObject *initvalue = NULL;
771
Antoine Pitrouf7820c12009-10-24 12:28:22 +0000772 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:BytesIO", kwlist,
773 &initvalue))
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000774 return -1;
775
776 /* In case, __init__ is called multiple times. */
777 self->string_size = 0;
778 self->pos = 0;
779
780 if (initvalue && initvalue != Py_None) {
781 PyObject *res;
782 res = bytesio_write(self, initvalue);
783 if (res == NULL)
784 return -1;
785 Py_DECREF(res);
786 self->pos = 0;
787 }
788
789 return 0;
790}
791
Antoine Pitrou19690592009-06-12 20:14:08 +0000792static int
793bytesio_traverse(bytesio *self, visitproc visit, void *arg)
794{
795 Py_VISIT(self->dict);
Antoine Pitrou19690592009-06-12 20:14:08 +0000796 return 0;
797}
798
799static int
800bytesio_clear(bytesio *self)
801{
802 Py_CLEAR(self->dict);
Antoine Pitrou19690592009-06-12 20:14:08 +0000803 return 0;
804}
805
806
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000807static PyGetSetDef bytesio_getsetlist[] = {
808 {"closed", (getter)bytesio_get_closed, NULL,
809 "True if the file is closed."},
Antoine Pitrou19690592009-06-12 20:14:08 +0000810 {NULL}, /* sentinel */
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000811};
812
813static struct PyMethodDef bytesio_methods[] = {
814 {"readable", (PyCFunction)return_true, METH_NOARGS, NULL},
815 {"seekable", (PyCFunction)return_true, METH_NOARGS, NULL},
816 {"writable", (PyCFunction)return_true, METH_NOARGS, NULL},
817 {"close", (PyCFunction)bytesio_close, METH_NOARGS, close_doc},
818 {"flush", (PyCFunction)bytesio_flush, METH_NOARGS, flush_doc},
819 {"isatty", (PyCFunction)bytesio_isatty, METH_NOARGS, isatty_doc},
820 {"tell", (PyCFunction)bytesio_tell, METH_NOARGS, tell_doc},
821 {"write", (PyCFunction)bytesio_write, METH_O, write_doc},
822 {"writelines", (PyCFunction)bytesio_writelines, METH_O, writelines_doc},
823 {"read1", (PyCFunction)bytesio_read1, METH_O, read1_doc},
Antoine Pitrou19690592009-06-12 20:14:08 +0000824 {"readinto", (PyCFunction)bytesio_readinto, METH_VARARGS, readinto_doc},
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000825 {"readline", (PyCFunction)bytesio_readline, METH_VARARGS, readline_doc},
826 {"readlines", (PyCFunction)bytesio_readlines, METH_VARARGS, readlines_doc},
827 {"read", (PyCFunction)bytesio_read, METH_VARARGS, read_doc},
Antoine Pitrou5cd2d8c2010-09-02 22:23:19 +0000828 {"getvalue", (PyCFunction)bytesio_getvalue, METH_NOARGS, getval_doc},
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000829 {"seek", (PyCFunction)bytesio_seek, METH_VARARGS, seek_doc},
830 {"truncate", (PyCFunction)bytesio_truncate, METH_VARARGS, truncate_doc},
Antoine Pitroufa94e802009-10-24 12:23:18 +0000831 {"__getstate__", (PyCFunction)bytesio_getstate, METH_NOARGS, NULL},
832 {"__setstate__", (PyCFunction)bytesio_setstate, METH_O, NULL},
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000833 {NULL, NULL} /* sentinel */
834};
835
836PyDoc_STRVAR(bytesio_doc,
837"BytesIO([buffer]) -> object\n"
838"\n"
839"Create a buffered I/O implementation using an in-memory bytes\n"
840"buffer, ready for reading and writing.");
841
Antoine Pitrou19690592009-06-12 20:14:08 +0000842PyTypeObject PyBytesIO_Type = {
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000843 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrou19690592009-06-12 20:14:08 +0000844 "_io.BytesIO", /*tp_name*/
845 sizeof(bytesio), /*tp_basicsize*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000846 0, /*tp_itemsize*/
847 (destructor)bytesio_dealloc, /*tp_dealloc*/
848 0, /*tp_print*/
849 0, /*tp_getattr*/
850 0, /*tp_setattr*/
Antoine Pitrou19690592009-06-12 20:14:08 +0000851 0, /*tp_reserved*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000852 0, /*tp_repr*/
853 0, /*tp_as_number*/
854 0, /*tp_as_sequence*/
855 0, /*tp_as_mapping*/
856 0, /*tp_hash*/
857 0, /*tp_call*/
858 0, /*tp_str*/
859 0, /*tp_getattro*/
860 0, /*tp_setattro*/
861 0, /*tp_as_buffer*/
Antoine Pitrou19690592009-06-12 20:14:08 +0000862 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
863 Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000864 bytesio_doc, /*tp_doc*/
Antoine Pitrou19690592009-06-12 20:14:08 +0000865 (traverseproc)bytesio_traverse, /*tp_traverse*/
866 (inquiry)bytesio_clear, /*tp_clear*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000867 0, /*tp_richcompare*/
Antoine Pitrou19690592009-06-12 20:14:08 +0000868 offsetof(bytesio, weakreflist), /*tp_weaklistoffset*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000869 PyObject_SelfIter, /*tp_iter*/
870 (iternextfunc)bytesio_iternext, /*tp_iternext*/
871 bytesio_methods, /*tp_methods*/
872 0, /*tp_members*/
873 bytesio_getsetlist, /*tp_getset*/
874 0, /*tp_base*/
875 0, /*tp_dict*/
876 0, /*tp_descr_get*/
877 0, /*tp_descr_set*/
Antoine Pitrou19690592009-06-12 20:14:08 +0000878 offsetof(bytesio, dict), /*tp_dictoffset*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000879 (initproc)bytesio_init, /*tp_init*/
880 0, /*tp_alloc*/
881 bytesio_new, /*tp_new*/
882};