blob: 51fad21383bff313da0aeebaa7ff08ab2e8f7815 [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;
Benjamin Peterson4fec9ce2010-11-20 17:31:08 +0000394 Py_ssize_t len, n;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000395
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;
Benjamin Peterson4fec9ce2010-11-20 17:31:08 +0000402 /* adjust invalid sizes */
403 n = self->string_size - self->pos;
404 if (len > n) {
405 len = n;
406 if (len < 0)
407 len = 0;
408 }
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000409
Antoine Pitrou19690592009-06-12 20:14:08 +0000410 memcpy(buf.buf, self->buf + self->pos, len);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000411 assert(self->pos + len < PY_SSIZE_T_MAX);
412 assert(len >= 0);
413 self->pos += len;
414
Antoine Pitrou19690592009-06-12 20:14:08 +0000415 PyBuffer_Release(&buf);
416 return PyLong_FromSsize_t(len);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000417}
418
419PyDoc_STRVAR(truncate_doc,
420"truncate([size]) -> int. Truncate the file to at most size bytes.\n"
421"\n"
422"Size defaults to the current file position, as returned by tell().\n"
Antoine Pitrouf3fa0742010-01-31 22:26:04 +0000423"The current file position is unchanged. Returns the new size.\n");
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000424
425static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000426bytesio_truncate(bytesio *self, PyObject *args)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000427{
428 Py_ssize_t size;
429 PyObject *arg = Py_None;
430
431 CHECK_CLOSED(self);
432
433 if (!PyArg_ParseTuple(args, "|O:truncate", &arg))
434 return NULL;
435
Antoine Pitrou19690592009-06-12 20:14:08 +0000436 if (PyNumber_Check(arg)) {
437 size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
Benjamin Peterson02252482008-09-30 02:11:07 +0000438 if (size == -1 && PyErr_Occurred())
439 return NULL;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000440 }
441 else if (arg == Py_None) {
442 /* Truncate to current position if no argument is passed. */
443 size = self->pos;
444 }
445 else {
446 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
447 Py_TYPE(arg)->tp_name);
448 return NULL;
449 }
450
451 if (size < 0) {
452 PyErr_Format(PyExc_ValueError,
453 "negative size value %zd", size);
454 return NULL;
455 }
456
457 if (size < self->string_size) {
458 self->string_size = size;
459 if (resize_buffer(self, size) < 0)
460 return NULL;
461 }
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000462
Antoine Pitrou19690592009-06-12 20:14:08 +0000463 return PyLong_FromSsize_t(size);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000464}
465
466static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000467bytesio_iternext(bytesio *self)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000468{
469 char *next;
470 Py_ssize_t n;
471
472 CHECK_CLOSED(self);
473
474 n = get_line(self, &next);
475
476 if (!next || n == 0)
477 return NULL;
478
Antoine Pitrou19690592009-06-12 20:14:08 +0000479 return PyBytes_FromStringAndSize(next, n);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000480}
481
482PyDoc_STRVAR(seek_doc,
483"seek(pos, whence=0) -> int. Change stream position.\n"
484"\n"
485"Seek to byte offset pos relative to position indicated by whence:\n"
486" 0 Start of stream (the default). pos should be >= 0;\n"
487" 1 Current position - pos may be negative;\n"
488" 2 End of stream - pos usually negative.\n"
489"Returns the new absolute position.");
490
491static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000492bytesio_seek(bytesio *self, PyObject *args)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000493{
Antoine Pitrou19690592009-06-12 20:14:08 +0000494 PyObject *posobj;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000495 Py_ssize_t pos;
496 int mode = 0;
497
498 CHECK_CLOSED(self);
499
Antoine Pitrou19690592009-06-12 20:14:08 +0000500 if (!PyArg_ParseTuple(args, "O|i:seek", &posobj, &mode))
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000501 return NULL;
502
Antoine Pitrou19690592009-06-12 20:14:08 +0000503 pos = PyNumber_AsSsize_t(posobj, PyExc_OverflowError);
504 if (pos == -1 && PyErr_Occurred())
505 return NULL;
506
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000507 if (pos < 0 && mode == 0) {
508 PyErr_Format(PyExc_ValueError,
509 "negative seek value %zd", pos);
510 return NULL;
511 }
512
513 /* mode 0: offset relative to beginning of the string.
514 mode 1: offset relative to current position.
515 mode 2: offset relative the end of the string. */
516 if (mode == 1) {
517 if (pos > PY_SSIZE_T_MAX - self->pos) {
518 PyErr_SetString(PyExc_OverflowError,
519 "new position too large");
520 return NULL;
521 }
522 pos += self->pos;
523 }
524 else if (mode == 2) {
525 if (pos > PY_SSIZE_T_MAX - self->string_size) {
526 PyErr_SetString(PyExc_OverflowError,
527 "new position too large");
528 return NULL;
529 }
530 pos += self->string_size;
531 }
532 else if (mode != 0) {
533 PyErr_Format(PyExc_ValueError,
534 "invalid whence (%i, should be 0, 1 or 2)", mode);
535 return NULL;
536 }
537
538 if (pos < 0)
539 pos = 0;
540 self->pos = pos;
541
Antoine Pitrou19690592009-06-12 20:14:08 +0000542 return PyLong_FromSsize_t(self->pos);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000543}
544
545PyDoc_STRVAR(write_doc,
546"write(bytes) -> int. Write bytes to file.\n"
547"\n"
548"Return the number of bytes written.");
549
550static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000551bytesio_write(bytesio *self, PyObject *obj)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000552{
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000553 Py_ssize_t n = 0;
Antoine Pitrou19690592009-06-12 20:14:08 +0000554 Py_buffer buf;
555 PyObject *result = NULL;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000556
557 CHECK_CLOSED(self);
558
Antoine Pitrou19690592009-06-12 20:14:08 +0000559 if (PyObject_GetBuffer(obj, &buf, PyBUF_CONTIG_RO) < 0)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000560 return NULL;
561
Antoine Pitrou19690592009-06-12 20:14:08 +0000562 if (buf.len != 0)
563 n = write_bytes(self, buf.buf, buf.len);
564 if (n >= 0)
565 result = PyLong_FromSsize_t(n);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000566
Antoine Pitrou19690592009-06-12 20:14:08 +0000567 PyBuffer_Release(&buf);
568 return result;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000569}
570
571PyDoc_STRVAR(writelines_doc,
572"writelines(sequence_of_strings) -> None. Write strings to the file.\n"
573"\n"
574"Note that newlines are not added. The sequence can be any iterable\n"
575"object producing strings. This is equivalent to calling write() for\n"
576"each string.");
577
578static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000579bytesio_writelines(bytesio *self, PyObject *v)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000580{
581 PyObject *it, *item;
582 PyObject *ret;
583
584 CHECK_CLOSED(self);
585
586 it = PyObject_GetIter(v);
587 if (it == NULL)
588 return NULL;
589
590 while ((item = PyIter_Next(it)) != NULL) {
591 ret = bytesio_write(self, item);
592 Py_DECREF(item);
593 if (ret == NULL) {
594 Py_DECREF(it);
595 return NULL;
596 }
597 Py_DECREF(ret);
598 }
599 Py_DECREF(it);
600
601 /* See if PyIter_Next failed */
602 if (PyErr_Occurred())
603 return NULL;
604
605 Py_RETURN_NONE;
606}
607
608PyDoc_STRVAR(close_doc,
609"close() -> None. Disable all I/O operations.");
610
611static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000612bytesio_close(bytesio *self)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000613{
614 if (self->buf != NULL) {
615 PyMem_Free(self->buf);
616 self->buf = NULL;
617 }
618 Py_RETURN_NONE;
619}
620
Antoine Pitroufa94e802009-10-24 12:23:18 +0000621/* Pickling support.
622
623 Note that only pickle protocol 2 and onward are supported since we use
624 extended __reduce__ API of PEP 307 to make BytesIO instances picklable.
625
626 Providing support for protocol < 2 would require the __reduce_ex__ method
627 which is notably long-winded when defined properly.
628
629 For BytesIO, the implementation would similar to one coded for
630 object.__reduce_ex__, but slightly less general. To be more specific, we
631 could call bytesio_getstate directly and avoid checking for the presence of
632 a fallback __reduce__ method. However, we would still need a __newobj__
633 function to use the efficient instance representation of PEP 307.
634 */
635
636static PyObject *
637bytesio_getstate(bytesio *self)
638{
639 PyObject *initvalue = bytesio_getvalue(self);
640 PyObject *dict;
641 PyObject *state;
642
643 if (initvalue == NULL)
644 return NULL;
645 if (self->dict == NULL) {
646 Py_INCREF(Py_None);
647 dict = Py_None;
648 }
649 else {
650 dict = PyDict_Copy(self->dict);
651 if (dict == NULL)
652 return NULL;
653 }
654
655 state = Py_BuildValue("(OnN)", initvalue, self->pos, dict);
656 Py_DECREF(initvalue);
657 return state;
658}
659
660static PyObject *
661bytesio_setstate(bytesio *self, PyObject *state)
662{
663 PyObject *result;
664 PyObject *position_obj;
665 PyObject *dict;
666 Py_ssize_t pos;
667
668 assert(state != NULL);
669
670 /* We allow the state tuple to be longer than 3, because we may need
671 someday to extend the object's state without breaking
672 backward-compatibility. */
673 if (!PyTuple_Check(state) || Py_SIZE(state) < 3) {
674 PyErr_Format(PyExc_TypeError,
675 "%.200s.__setstate__ argument should be 3-tuple, got %.200s",
676 Py_TYPE(self)->tp_name, Py_TYPE(state)->tp_name);
677 return NULL;
678 }
679 /* Reset the object to its default state. This is only needed to handle
680 the case of repeated calls to __setstate__. */
681 self->string_size = 0;
682 self->pos = 0;
683
684 /* Set the value of the internal buffer. If state[0] does not support the
685 buffer protocol, bytesio_write will raise the appropriate TypeError. */
686 result = bytesio_write(self, PyTuple_GET_ITEM(state, 0));
687 if (result == NULL)
688 return NULL;
689 Py_DECREF(result);
690
691 /* Set carefully the position value. Alternatively, we could use the seek
692 method instead of modifying self->pos directly to better protect the
693 object internal state against errneous (or malicious) inputs. */
694 position_obj = PyTuple_GET_ITEM(state, 1);
695 if (!PyIndex_Check(position_obj)) {
696 PyErr_Format(PyExc_TypeError,
697 "second item of state must be an integer, not %.200s",
698 Py_TYPE(position_obj)->tp_name);
699 return NULL;
700 }
701 pos = PyNumber_AsSsize_t(position_obj, PyExc_OverflowError);
702 if (pos == -1 && PyErr_Occurred())
703 return NULL;
704 if (pos < 0) {
705 PyErr_SetString(PyExc_ValueError,
706 "position value cannot be negative");
707 return NULL;
708 }
709 self->pos = pos;
710
711 /* Set the dictionary of the instance variables. */
712 dict = PyTuple_GET_ITEM(state, 2);
713 if (dict != Py_None) {
714 if (!PyDict_Check(dict)) {
715 PyErr_Format(PyExc_TypeError,
716 "third item of state should be a dict, got a %.200s",
717 Py_TYPE(dict)->tp_name);
718 return NULL;
719 }
720 if (self->dict) {
721 /* Alternatively, we could replace the internal dictionary
722 completely. However, it seems more practical to just update it. */
723 if (PyDict_Update(self->dict, dict) < 0)
724 return NULL;
725 }
726 else {
727 Py_INCREF(dict);
728 self->dict = dict;
729 }
730 }
731
732 Py_RETURN_NONE;
733}
734
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000735static void
Antoine Pitrou19690592009-06-12 20:14:08 +0000736bytesio_dealloc(bytesio *self)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000737{
Antoine Pitrouf98a2672009-10-24 11:59:41 +0000738 _PyObject_GC_UNTRACK(self);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000739 if (self->buf != NULL) {
740 PyMem_Free(self->buf);
741 self->buf = NULL;
742 }
Antoine Pitrouf98a2672009-10-24 11:59:41 +0000743 Py_CLEAR(self->dict);
744 if (self->weakreflist != NULL)
745 PyObject_ClearWeakRefs((PyObject *) self);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000746 Py_TYPE(self)->tp_free(self);
747}
748
749static PyObject *
750bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
751{
Antoine Pitrou19690592009-06-12 20:14:08 +0000752 bytesio *self;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000753
754 assert(type != NULL && type->tp_alloc != NULL);
Antoine Pitrou19690592009-06-12 20:14:08 +0000755 self = (bytesio *)type->tp_alloc(type, 0);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000756 if (self == NULL)
757 return NULL;
758
Antoine Pitroufa94e802009-10-24 12:23:18 +0000759 /* tp_alloc initializes all the fields to zero. So we don't have to
760 initialize them here. */
761
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000762 self->buf = (char *)PyMem_Malloc(0);
763 if (self->buf == NULL) {
764 Py_DECREF(self);
765 return PyErr_NoMemory();
766 }
767
768 return (PyObject *)self;
769}
770
771static int
Antoine Pitrou19690592009-06-12 20:14:08 +0000772bytesio_init(bytesio *self, PyObject *args, PyObject *kwds)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000773{
Antoine Pitrouf7820c12009-10-24 12:28:22 +0000774 char *kwlist[] = {"initial_bytes", NULL};
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000775 PyObject *initvalue = NULL;
776
Antoine Pitrouf7820c12009-10-24 12:28:22 +0000777 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:BytesIO", kwlist,
778 &initvalue))
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000779 return -1;
780
781 /* In case, __init__ is called multiple times. */
782 self->string_size = 0;
783 self->pos = 0;
784
785 if (initvalue && initvalue != Py_None) {
786 PyObject *res;
787 res = bytesio_write(self, initvalue);
788 if (res == NULL)
789 return -1;
790 Py_DECREF(res);
791 self->pos = 0;
792 }
793
794 return 0;
795}
796
Antoine Pitrou19690592009-06-12 20:14:08 +0000797static int
798bytesio_traverse(bytesio *self, visitproc visit, void *arg)
799{
800 Py_VISIT(self->dict);
Antoine Pitrou19690592009-06-12 20:14:08 +0000801 return 0;
802}
803
804static int
805bytesio_clear(bytesio *self)
806{
807 Py_CLEAR(self->dict);
Antoine Pitrou19690592009-06-12 20:14:08 +0000808 return 0;
809}
810
811
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000812static PyGetSetDef bytesio_getsetlist[] = {
813 {"closed", (getter)bytesio_get_closed, NULL,
814 "True if the file is closed."},
Antoine Pitrou19690592009-06-12 20:14:08 +0000815 {NULL}, /* sentinel */
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000816};
817
818static struct PyMethodDef bytesio_methods[] = {
819 {"readable", (PyCFunction)return_true, METH_NOARGS, NULL},
820 {"seekable", (PyCFunction)return_true, METH_NOARGS, NULL},
821 {"writable", (PyCFunction)return_true, METH_NOARGS, NULL},
822 {"close", (PyCFunction)bytesio_close, METH_NOARGS, close_doc},
823 {"flush", (PyCFunction)bytesio_flush, METH_NOARGS, flush_doc},
824 {"isatty", (PyCFunction)bytesio_isatty, METH_NOARGS, isatty_doc},
825 {"tell", (PyCFunction)bytesio_tell, METH_NOARGS, tell_doc},
826 {"write", (PyCFunction)bytesio_write, METH_O, write_doc},
827 {"writelines", (PyCFunction)bytesio_writelines, METH_O, writelines_doc},
828 {"read1", (PyCFunction)bytesio_read1, METH_O, read1_doc},
Antoine Pitrou19690592009-06-12 20:14:08 +0000829 {"readinto", (PyCFunction)bytesio_readinto, METH_VARARGS, readinto_doc},
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000830 {"readline", (PyCFunction)bytesio_readline, METH_VARARGS, readline_doc},
831 {"readlines", (PyCFunction)bytesio_readlines, METH_VARARGS, readlines_doc},
832 {"read", (PyCFunction)bytesio_read, METH_VARARGS, read_doc},
Antoine Pitrou5cd2d8c2010-09-02 22:23:19 +0000833 {"getvalue", (PyCFunction)bytesio_getvalue, METH_NOARGS, getval_doc},
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000834 {"seek", (PyCFunction)bytesio_seek, METH_VARARGS, seek_doc},
835 {"truncate", (PyCFunction)bytesio_truncate, METH_VARARGS, truncate_doc},
Antoine Pitroufa94e802009-10-24 12:23:18 +0000836 {"__getstate__", (PyCFunction)bytesio_getstate, METH_NOARGS, NULL},
837 {"__setstate__", (PyCFunction)bytesio_setstate, METH_O, NULL},
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000838 {NULL, NULL} /* sentinel */
839};
840
841PyDoc_STRVAR(bytesio_doc,
842"BytesIO([buffer]) -> object\n"
843"\n"
844"Create a buffered I/O implementation using an in-memory bytes\n"
845"buffer, ready for reading and writing.");
846
Antoine Pitrou19690592009-06-12 20:14:08 +0000847PyTypeObject PyBytesIO_Type = {
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000848 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrou19690592009-06-12 20:14:08 +0000849 "_io.BytesIO", /*tp_name*/
850 sizeof(bytesio), /*tp_basicsize*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000851 0, /*tp_itemsize*/
852 (destructor)bytesio_dealloc, /*tp_dealloc*/
853 0, /*tp_print*/
854 0, /*tp_getattr*/
855 0, /*tp_setattr*/
Antoine Pitrou19690592009-06-12 20:14:08 +0000856 0, /*tp_reserved*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000857 0, /*tp_repr*/
858 0, /*tp_as_number*/
859 0, /*tp_as_sequence*/
860 0, /*tp_as_mapping*/
861 0, /*tp_hash*/
862 0, /*tp_call*/
863 0, /*tp_str*/
864 0, /*tp_getattro*/
865 0, /*tp_setattro*/
866 0, /*tp_as_buffer*/
Antoine Pitrou19690592009-06-12 20:14:08 +0000867 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
868 Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000869 bytesio_doc, /*tp_doc*/
Antoine Pitrou19690592009-06-12 20:14:08 +0000870 (traverseproc)bytesio_traverse, /*tp_traverse*/
871 (inquiry)bytesio_clear, /*tp_clear*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000872 0, /*tp_richcompare*/
Antoine Pitrou19690592009-06-12 20:14:08 +0000873 offsetof(bytesio, weakreflist), /*tp_weaklistoffset*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000874 PyObject_SelfIter, /*tp_iter*/
875 (iternextfunc)bytesio_iternext, /*tp_iternext*/
876 bytesio_methods, /*tp_methods*/
877 0, /*tp_members*/
878 bytesio_getsetlist, /*tp_getset*/
879 0, /*tp_base*/
880 0, /*tp_dict*/
881 0, /*tp_descr_get*/
882 0, /*tp_descr_set*/
Antoine Pitrou19690592009-06-12 20:14:08 +0000883 offsetof(bytesio, dict), /*tp_dictoffset*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000884 (initproc)bytesio_init, /*tp_init*/
885 0, /*tp_alloc*/
886 bytesio_new, /*tp_new*/
887};