blob: 1482d824f12504ad6d0ad367231c1364f2e32a18 [file] [log] [blame]
Alexandre Vassalotti77250f42008-05-06 19:48:38 +00001#include "Python.h"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002#include "structmember.h" /* for offsetof() */
3#include "_iomodule.h"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +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;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000011 PyObject *dict;
12 PyObject *weakreflist;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000013} bytesio;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +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
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000026get_line(bytesio *self, char **output)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +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
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000059resize_buffer(bytesio *self, size_t size)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +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
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000111write_bytes(bytesio *self, const char *bytes, Py_ssize_t len)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000112{
113 assert(self->buf != NULL);
114 assert(self->pos >= 0);
115 assert(len >= 0);
116
Alexandre Vassalotti1bfe9dc82008-05-07 01:44:31 +0000117 if ((size_t)self->pos + len > self->buf_size) {
118 if (resize_buffer(self, (size_t)self->pos + len) < 0)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000119 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 *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000149bytesio_get_closed(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000150{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000151 if (self->buf == NULL) {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000152 Py_RETURN_TRUE;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000153 }
154 else {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000155 Py_RETURN_FALSE;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000156 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000157}
158
159/* Generic getter for the writable, readable and seekable properties */
160static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000161return_true(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000162{
163 Py_RETURN_TRUE;
164}
165
166PyDoc_STRVAR(flush_doc,
167"flush() -> None. Does nothing.");
168
169static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000170bytesio_flush(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000171{
172 Py_RETURN_NONE;
173}
174
175PyDoc_STRVAR(getval_doc,
Alexandre Vassalotti10dfc1e2008-05-08 01:34:41 +0000176"getvalue() -> bytes.\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000177"\n"
178"Retrieve the entire contents of the BytesIO object.");
179
180static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000181bytesio_getvalue(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000182{
183 CHECK_CLOSED(self);
Christian Heimes72b710a2008-05-26 13:28:38 +0000184 return PyBytes_FromStringAndSize(self->buf, self->string_size);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000185}
186
187PyDoc_STRVAR(isatty_doc,
188"isatty() -> False.\n"
189"\n"
190"Always returns False since BytesIO objects are not connected\n"
191"to a tty-like device.");
192
193static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000194bytesio_isatty(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000195{
196 CHECK_CLOSED(self);
197 Py_RETURN_FALSE;
198}
199
200PyDoc_STRVAR(tell_doc,
201"tell() -> current file position, an integer\n");
202
203static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000204bytesio_tell(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000205{
206 CHECK_CLOSED(self);
207 return PyLong_FromSsize_t(self->pos);
208}
209
210PyDoc_STRVAR(read_doc,
211"read([size]) -> read at most size bytes, returned as a string.\n"
212"\n"
213"If the size argument is negative, read until EOF is reached.\n"
214"Return an empty string at EOF.");
215
216static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000217bytesio_read(bytesio *self, PyObject *args)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000218{
219 Py_ssize_t size, n;
220 char *output;
221 PyObject *arg = Py_None;
222
223 CHECK_CLOSED(self);
224
225 if (!PyArg_ParseTuple(args, "|O:read", &arg))
226 return NULL;
227
228 if (PyLong_Check(arg)) {
229 size = PyLong_AsSsize_t(arg);
Benjamin Petersona8a93042008-09-30 02:18:09 +0000230 if (size == -1 && PyErr_Occurred())
231 return NULL;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000232 }
233 else if (arg == Py_None) {
234 /* Read until EOF is reached, by default. */
235 size = -1;
236 }
237 else {
238 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
239 Py_TYPE(arg)->tp_name);
240 return NULL;
241 }
242
243 /* adjust invalid sizes */
244 n = self->string_size - self->pos;
245 if (size < 0 || size > n) {
246 size = n;
247 if (size < 0)
248 size = 0;
249 }
250
251 assert(self->buf != NULL);
252 output = self->buf + self->pos;
253 self->pos += size;
254
Christian Heimes72b710a2008-05-26 13:28:38 +0000255 return PyBytes_FromStringAndSize(output, size);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000256}
257
258
259PyDoc_STRVAR(read1_doc,
260"read1(size) -> read at most size bytes, returned as a string.\n"
261"\n"
262"If the size argument is negative or omitted, read until EOF is reached.\n"
263"Return an empty string at EOF.");
264
265static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000266bytesio_read1(bytesio *self, PyObject *n)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000267{
268 PyObject *arg, *res;
269
270 arg = PyTuple_Pack(1, n);
271 if (arg == NULL)
272 return NULL;
273 res = bytesio_read(self, arg);
274 Py_DECREF(arg);
275 return res;
276}
277
278PyDoc_STRVAR(readline_doc,
279"readline([size]) -> next line from the file, as a string.\n"
280"\n"
281"Retain newline. A non-negative size argument limits the maximum\n"
282"number of bytes to return (an incomplete line may be returned then).\n"
283"Return an empty string at EOF.\n");
284
285static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000286bytesio_readline(bytesio *self, PyObject *args)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000287{
288 Py_ssize_t size, n;
289 char *output;
290 PyObject *arg = Py_None;
291
292 CHECK_CLOSED(self);
293
294 if (!PyArg_ParseTuple(args, "|O:readline", &arg))
295 return NULL;
296
297 if (PyLong_Check(arg)) {
298 size = PyLong_AsSsize_t(arg);
Benjamin Petersona8a93042008-09-30 02:18:09 +0000299 if (size == -1 && PyErr_Occurred())
300 return NULL;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000301 }
302 else if (arg == Py_None) {
303 /* No size limit, by default. */
304 size = -1;
305 }
306 else {
307 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
308 Py_TYPE(arg)->tp_name);
309 return NULL;
310 }
311
312 n = get_line(self, &output);
313
314 if (size >= 0 && size < n) {
315 size = n - size;
316 n -= size;
317 self->pos -= size;
318 }
319
Christian Heimes72b710a2008-05-26 13:28:38 +0000320 return PyBytes_FromStringAndSize(output, n);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000321}
322
323PyDoc_STRVAR(readlines_doc,
324"readlines([size]) -> list of strings, each a line from the file.\n"
325"\n"
326"Call readline() repeatedly and return a list of the lines so read.\n"
327"The optional size argument, if given, is an approximate bound on the\n"
328"total number of bytes in the lines returned.\n");
329
330static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000331bytesio_readlines(bytesio *self, PyObject *args)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000332{
333 Py_ssize_t maxsize, size, n;
334 PyObject *result, *line;
335 char *output;
336 PyObject *arg = Py_None;
337
338 CHECK_CLOSED(self);
339
340 if (!PyArg_ParseTuple(args, "|O:readlines", &arg))
341 return NULL;
342
343 if (PyLong_Check(arg)) {
344 maxsize = PyLong_AsSsize_t(arg);
Benjamin Petersona8a93042008-09-30 02:18:09 +0000345 if (maxsize == -1 && PyErr_Occurred())
346 return NULL;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000347 }
348 else if (arg == Py_None) {
349 /* No size limit, by default. */
350 maxsize = -1;
351 }
352 else {
353 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
354 Py_TYPE(arg)->tp_name);
355 return NULL;
356 }
357
358 size = 0;
359 result = PyList_New(0);
360 if (!result)
361 return NULL;
362
363 while ((n = get_line(self, &output)) != 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +0000364 line = PyBytes_FromStringAndSize(output, n);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000365 if (!line)
366 goto on_error;
367 if (PyList_Append(result, line) == -1) {
368 Py_DECREF(line);
369 goto on_error;
370 }
371 Py_DECREF(line);
372 size += n;
373 if (maxsize > 0 && size >= maxsize)
374 break;
375 }
376 return result;
377
378 on_error:
379 Py_DECREF(result);
380 return NULL;
381}
382
383PyDoc_STRVAR(readinto_doc,
Alexandre Vassalotti10dfc1e2008-05-08 01:34:41 +0000384"readinto(bytearray) -> int. Read up to len(b) bytes into b.\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000385"\n"
386"Returns number of bytes read (0 for EOF), or None if the object\n"
387"is set not to block as has no data to read.");
388
389static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000390bytesio_readinto(bytesio *self, PyObject *buffer)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000391{
392 void *raw_buffer;
393 Py_ssize_t len;
394
395 CHECK_CLOSED(self);
396
397 if (PyObject_AsWriteBuffer(buffer, &raw_buffer, &len) == -1)
398 return NULL;
399
400 if (self->pos + len > self->string_size)
401 len = self->string_size - self->pos;
402
403 memcpy(raw_buffer, self->buf + self->pos, len);
404 assert(self->pos + len < PY_SSIZE_T_MAX);
405 assert(len >= 0);
406 self->pos += len;
407
408 return PyLong_FromSsize_t(len);
409}
410
411PyDoc_STRVAR(truncate_doc,
412"truncate([size]) -> int. Truncate the file to at most size bytes.\n"
413"\n"
414"Size defaults to the current file position, as returned by tell().\n"
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000415"The current file position is unchanged. Returns the new size.\n");
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000416
417static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000418bytesio_truncate(bytesio *self, PyObject *args)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000419{
420 Py_ssize_t size;
421 PyObject *arg = Py_None;
422
423 CHECK_CLOSED(self);
424
425 if (!PyArg_ParseTuple(args, "|O:truncate", &arg))
426 return NULL;
427
428 if (PyLong_Check(arg)) {
429 size = PyLong_AsSsize_t(arg);
Benjamin Petersona8a93042008-09-30 02:18:09 +0000430 if (size == -1 && PyErr_Occurred())
431 return NULL;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000432 }
433 else if (arg == Py_None) {
434 /* Truncate to current position if no argument is passed. */
435 size = self->pos;
436 }
437 else {
438 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
439 Py_TYPE(arg)->tp_name);
440 return NULL;
441 }
442
443 if (size < 0) {
444 PyErr_Format(PyExc_ValueError,
445 "negative size value %zd", size);
446 return NULL;
447 }
448
449 if (size < self->string_size) {
450 self->string_size = size;
451 if (resize_buffer(self, size) < 0)
452 return NULL;
453 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000454
455 return PyLong_FromSsize_t(size);
456}
457
458static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000459bytesio_iternext(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000460{
461 char *next;
462 Py_ssize_t n;
463
464 CHECK_CLOSED(self);
465
466 n = get_line(self, &next);
467
468 if (!next || n == 0)
469 return NULL;
470
Christian Heimes72b710a2008-05-26 13:28:38 +0000471 return PyBytes_FromStringAndSize(next, n);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000472}
473
474PyDoc_STRVAR(seek_doc,
475"seek(pos, whence=0) -> int. Change stream position.\n"
476"\n"
477"Seek to byte offset pos relative to position indicated by whence:\n"
478" 0 Start of stream (the default). pos should be >= 0;\n"
479" 1 Current position - pos may be negative;\n"
480" 2 End of stream - pos usually negative.\n"
481"Returns the new absolute position.");
482
483static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000484bytesio_seek(bytesio *self, PyObject *args)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000485{
486 Py_ssize_t pos;
487 int mode = 0;
488
489 CHECK_CLOSED(self);
490
491 if (!PyArg_ParseTuple(args, "n|i:seek", &pos, &mode))
492 return NULL;
493
494 if (pos < 0 && mode == 0) {
495 PyErr_Format(PyExc_ValueError,
496 "negative seek value %zd", pos);
497 return NULL;
498 }
499
500 /* mode 0: offset relative to beginning of the string.
501 mode 1: offset relative to current position.
502 mode 2: offset relative the end of the string. */
503 if (mode == 1) {
504 if (pos > PY_SSIZE_T_MAX - self->pos) {
505 PyErr_SetString(PyExc_OverflowError,
506 "new position too large");
507 return NULL;
508 }
509 pos += self->pos;
510 }
511 else if (mode == 2) {
512 if (pos > PY_SSIZE_T_MAX - self->string_size) {
513 PyErr_SetString(PyExc_OverflowError,
514 "new position too large");
515 return NULL;
516 }
517 pos += self->string_size;
518 }
519 else if (mode != 0) {
520 PyErr_Format(PyExc_ValueError,
521 "invalid whence (%i, should be 0, 1 or 2)", mode);
522 return NULL;
523 }
524
525 if (pos < 0)
526 pos = 0;
527 self->pos = pos;
528
529 return PyLong_FromSsize_t(self->pos);
530}
531
532PyDoc_STRVAR(write_doc,
Alexandre Vassalotti10dfc1e2008-05-08 01:34:41 +0000533"write(bytes) -> int. Write bytes to file.\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000534"\n"
535"Return the number of bytes written.");
536
537static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000538bytesio_write(bytesio *self, PyObject *obj)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000539{
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000540 Py_ssize_t n = 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000541 Py_buffer buf;
542 PyObject *result = NULL;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000543
544 CHECK_CLOSED(self);
545
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000546 if (PyObject_GetBuffer(obj, &buf, PyBUF_CONTIG_RO) < 0)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000547 return NULL;
548
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000549 if (buf.len != 0)
550 n = write_bytes(self, buf.buf, buf.len);
551 if (n >= 0)
552 result = PyLong_FromSsize_t(n);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000553
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000554 PyBuffer_Release(&buf);
555 return result;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000556}
557
558PyDoc_STRVAR(writelines_doc,
Alexandre Vassalotti7d060892008-05-07 01:47:37 +0000559"writelines(sequence_of_strings) -> None. Write strings to the file.\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000560"\n"
Alexandre Vassalotti7d060892008-05-07 01:47:37 +0000561"Note that newlines are not added. The sequence can be any iterable\n"
562"object producing strings. This is equivalent to calling write() for\n"
563"each string.");
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000564
565static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000566bytesio_writelines(bytesio *self, PyObject *v)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000567{
568 PyObject *it, *item;
569 PyObject *ret;
570
571 CHECK_CLOSED(self);
572
573 it = PyObject_GetIter(v);
574 if (it == NULL)
575 return NULL;
576
577 while ((item = PyIter_Next(it)) != NULL) {
578 ret = bytesio_write(self, item);
579 Py_DECREF(item);
580 if (ret == NULL) {
581 Py_DECREF(it);
582 return NULL;
583 }
584 Py_DECREF(ret);
585 }
586 Py_DECREF(it);
587
588 /* See if PyIter_Next failed */
589 if (PyErr_Occurred())
590 return NULL;
591
592 Py_RETURN_NONE;
593}
594
595PyDoc_STRVAR(close_doc,
596"close() -> None. Disable all I/O operations.");
597
598static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000599bytesio_close(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000600{
601 if (self->buf != NULL) {
602 PyMem_Free(self->buf);
603 self->buf = NULL;
604 }
605 Py_RETURN_NONE;
606}
607
Alexandre Vassalotticf76e1a2009-07-22 03:24:36 +0000608/* Pickling support.
609
610 Note that only pickle protocol 2 and onward are supported since we use
611 extended __reduce__ API of PEP 307 to make BytesIO instances picklable.
612
613 Providing support for protocol < 2 would require the __reduce_ex__ method
614 which is notably long-winded when defined properly.
615
616 For BytesIO, the implementation would similar to one coded for
617 object.__reduce_ex__, but slightly less general. To be more specific, we
618 could call bytesio_getstate directly and avoid checking for the presence of
619 a fallback __reduce__ method. However, we would still need a __newobj__
620 function to use the efficient instance representation of PEP 307.
621 */
622
623static PyObject *
624bytesio_getstate(bytesio *self)
625{
626 PyObject *initvalue = bytesio_getvalue(self);
627 PyObject *dict;
628 PyObject *state;
629
630 if (initvalue == NULL)
631 return NULL;
632 if (self->dict == NULL) {
633 Py_INCREF(Py_None);
634 dict = Py_None;
635 }
636 else {
637 dict = PyDict_Copy(self->dict);
638 if (dict == NULL)
639 return NULL;
640 }
641
642 state = Py_BuildValue("(OnN)", initvalue, self->pos, dict);
643 Py_DECREF(initvalue);
644 return state;
645}
646
647static PyObject *
648bytesio_setstate(bytesio *self, PyObject *state)
649{
650 PyObject *result;
651 PyObject *position_obj;
652 PyObject *dict;
653 Py_ssize_t pos;
654
655 assert(state != NULL);
656
657 /* We allow the state tuple to be longer than 3, because we may need
658 someday to extend the object's state without breaking
659 backward-compatibility. */
660 if (!PyTuple_Check(state) || Py_SIZE(state) < 3) {
661 PyErr_Format(PyExc_TypeError,
662 "%.200s.__setstate__ argument should be 3-tuple, got %.200s",
663 Py_TYPE(self)->tp_name, Py_TYPE(state)->tp_name);
664 return NULL;
665 }
666 /* Reset the object to its default state. This is only needed to handle
667 the case of repeated calls to __setstate__. */
668 self->string_size = 0;
669 self->pos = 0;
670
671 /* Set the value of the internal buffer. If state[0] does not support the
672 buffer protocol, bytesio_write will raise the appropriate TypeError. */
673 result = bytesio_write(self, PyTuple_GET_ITEM(state, 0));
674 if (result == NULL)
675 return NULL;
676 Py_DECREF(result);
677
678 /* Set carefully the position value. Alternatively, we could use the seek
679 method instead of modifying self->pos directly to better protect the
680 object internal state against errneous (or malicious) inputs. */
681 position_obj = PyTuple_GET_ITEM(state, 1);
682 if (!PyLong_Check(position_obj)) {
683 PyErr_Format(PyExc_TypeError,
684 "second item of state must be an integer, not %.200s",
685 Py_TYPE(position_obj)->tp_name);
686 return NULL;
687 }
688 pos = PyLong_AsSsize_t(position_obj);
689 if (pos == -1 && PyErr_Occurred())
690 return NULL;
691 if (pos < 0) {
692 PyErr_SetString(PyExc_ValueError,
693 "position value cannot be negative");
694 return NULL;
695 }
696 self->pos = pos;
697
698 /* Set the dictionary of the instance variables. */
699 dict = PyTuple_GET_ITEM(state, 2);
700 if (dict != Py_None) {
701 if (!PyDict_Check(dict)) {
702 PyErr_Format(PyExc_TypeError,
703 "third item of state should be a dict, got a %.200s",
704 Py_TYPE(dict)->tp_name);
705 return NULL;
706 }
707 if (self->dict) {
708 /* Alternatively, we could replace the internal dictionary
709 completely. However, it seems more practical to just update it. */
710 if (PyDict_Update(self->dict, dict) < 0)
711 return NULL;
712 }
713 else {
714 Py_INCREF(dict);
715 self->dict = dict;
716 }
717 }
718
719 Py_RETURN_NONE;
720}
721
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000722static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000723bytesio_dealloc(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000724{
Alexandre Vassalottifc477042009-07-22 02:24:49 +0000725 _PyObject_GC_UNTRACK(self);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000726 if (self->buf != NULL) {
727 PyMem_Free(self->buf);
728 self->buf = NULL;
729 }
Alexandre Vassalottifc477042009-07-22 02:24:49 +0000730 Py_CLEAR(self->dict);
731 if (self->weakreflist != NULL)
732 PyObject_ClearWeakRefs((PyObject *) self);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000733 Py_TYPE(self)->tp_free(self);
734}
735
736static PyObject *
737bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
738{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000739 bytesio *self;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000740
741 assert(type != NULL && type->tp_alloc != NULL);
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000742 self = (bytesio *)type->tp_alloc(type, 0);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000743 if (self == NULL)
744 return NULL;
745
Alexandre Vassalotticf76e1a2009-07-22 03:24:36 +0000746 /* tp_alloc initializes all the fields to zero. So we don't have to
747 initialize them here. */
748
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000749 self->buf = (char *)PyMem_Malloc(0);
750 if (self->buf == NULL) {
751 Py_DECREF(self);
752 return PyErr_NoMemory();
753 }
754
755 return (PyObject *)self;
756}
757
758static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000759bytesio_init(bytesio *self, PyObject *args, PyObject *kwds)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000760{
Alexandre Vassalottiba5c7432009-08-04 23:19:13 +0000761 char *kwlist[] = {"initial_bytes", NULL};
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000762 PyObject *initvalue = NULL;
763
Alexandre Vassalottiba5c7432009-08-04 23:19:13 +0000764 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:BytesIO", kwlist,
765 &initvalue))
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000766 return -1;
767
768 /* In case, __init__ is called multiple times. */
769 self->string_size = 0;
770 self->pos = 0;
771
772 if (initvalue && initvalue != Py_None) {
773 PyObject *res;
774 res = bytesio_write(self, initvalue);
775 if (res == NULL)
776 return -1;
777 Py_DECREF(res);
778 self->pos = 0;
779 }
780
781 return 0;
782}
783
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000784static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000785bytesio_traverse(bytesio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000786{
787 Py_VISIT(self->dict);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000788 return 0;
789}
790
791static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000792bytesio_clear(bytesio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000793{
794 Py_CLEAR(self->dict);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000795 return 0;
796}
797
798
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000799static PyGetSetDef bytesio_getsetlist[] = {
800 {"closed", (getter)bytesio_get_closed, NULL,
801 "True if the file is closed."},
Benjamin Peterson1fea3212009-04-19 03:15:20 +0000802 {NULL}, /* sentinel */
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000803};
804
805static struct PyMethodDef bytesio_methods[] = {
806 {"readable", (PyCFunction)return_true, METH_NOARGS, NULL},
807 {"seekable", (PyCFunction)return_true, METH_NOARGS, NULL},
808 {"writable", (PyCFunction)return_true, METH_NOARGS, NULL},
809 {"close", (PyCFunction)bytesio_close, METH_NOARGS, close_doc},
810 {"flush", (PyCFunction)bytesio_flush, METH_NOARGS, flush_doc},
811 {"isatty", (PyCFunction)bytesio_isatty, METH_NOARGS, isatty_doc},
812 {"tell", (PyCFunction)bytesio_tell, METH_NOARGS, tell_doc},
813 {"write", (PyCFunction)bytesio_write, METH_O, write_doc},
814 {"writelines", (PyCFunction)bytesio_writelines, METH_O, writelines_doc},
815 {"read1", (PyCFunction)bytesio_read1, METH_O, read1_doc},
816 {"readinto", (PyCFunction)bytesio_readinto, METH_O, readinto_doc},
817 {"readline", (PyCFunction)bytesio_readline, METH_VARARGS, readline_doc},
818 {"readlines", (PyCFunction)bytesio_readlines, METH_VARARGS, readlines_doc},
819 {"read", (PyCFunction)bytesio_read, METH_VARARGS, read_doc},
820 {"getvalue", (PyCFunction)bytesio_getvalue, METH_VARARGS, getval_doc},
821 {"seek", (PyCFunction)bytesio_seek, METH_VARARGS, seek_doc},
822 {"truncate", (PyCFunction)bytesio_truncate, METH_VARARGS, truncate_doc},
Alexandre Vassalotticf76e1a2009-07-22 03:24:36 +0000823 {"__getstate__", (PyCFunction)bytesio_getstate, METH_NOARGS, NULL},
824 {"__setstate__", (PyCFunction)bytesio_setstate, METH_O, NULL},
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000825 {NULL, NULL} /* sentinel */
826};
827
828PyDoc_STRVAR(bytesio_doc,
829"BytesIO([buffer]) -> object\n"
830"\n"
831"Create a buffered I/O implementation using an in-memory bytes\n"
832"buffer, ready for reading and writing.");
833
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000834PyTypeObject PyBytesIO_Type = {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000835 PyVarObject_HEAD_INIT(NULL, 0)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000836 "_io.BytesIO", /*tp_name*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000837 sizeof(bytesio), /*tp_basicsize*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000838 0, /*tp_itemsize*/
839 (destructor)bytesio_dealloc, /*tp_dealloc*/
840 0, /*tp_print*/
841 0, /*tp_getattr*/
842 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000843 0, /*tp_reserved*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000844 0, /*tp_repr*/
845 0, /*tp_as_number*/
846 0, /*tp_as_sequence*/
847 0, /*tp_as_mapping*/
848 0, /*tp_hash*/
849 0, /*tp_call*/
850 0, /*tp_str*/
851 0, /*tp_getattro*/
852 0, /*tp_setattro*/
853 0, /*tp_as_buffer*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000854 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
855 Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000856 bytesio_doc, /*tp_doc*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000857 (traverseproc)bytesio_traverse, /*tp_traverse*/
858 (inquiry)bytesio_clear, /*tp_clear*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000859 0, /*tp_richcompare*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000860 offsetof(bytesio, weakreflist), /*tp_weaklistoffset*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000861 PyObject_SelfIter, /*tp_iter*/
862 (iternextfunc)bytesio_iternext, /*tp_iternext*/
863 bytesio_methods, /*tp_methods*/
864 0, /*tp_members*/
865 bytesio_getsetlist, /*tp_getset*/
866 0, /*tp_base*/
867 0, /*tp_dict*/
868 0, /*tp_descr_get*/
869 0, /*tp_descr_set*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000870 offsetof(bytesio, dict), /*tp_dictoffset*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000871 (initproc)bytesio_init, /*tp_init*/
872 0, /*tp_alloc*/
873 bytesio_new, /*tp_new*/
874};