blob: c8fb3eb95f4bd160b1a057fbba1fba0a92e8507e [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{
Antoine Pitroufaf90072010-05-03 16:58:19 +0000172 CHECK_CLOSED(self);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000173 Py_RETURN_NONE;
174}
175
176PyDoc_STRVAR(getval_doc,
Alexandre Vassalotti10dfc1e2008-05-08 01:34:41 +0000177"getvalue() -> bytes.\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000178"\n"
179"Retrieve the entire contents of the BytesIO object.");
180
181static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000182bytesio_getvalue(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000183{
184 CHECK_CLOSED(self);
Christian Heimes72b710a2008-05-26 13:28:38 +0000185 return PyBytes_FromStringAndSize(self->buf, self->string_size);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +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 *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000195bytesio_isatty(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +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 *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000205bytesio_tell(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000206{
207 CHECK_CLOSED(self);
208 return PyLong_FromSsize_t(self->pos);
209}
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 *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000218bytesio_read(bytesio *self, PyObject *args)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +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
229 if (PyLong_Check(arg)) {
230 size = PyLong_AsSsize_t(arg);
Benjamin Petersona8a93042008-09-30 02:18:09 +0000231 if (size == -1 && PyErr_Occurred())
232 return NULL;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +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
Christian Heimes72b710a2008-05-26 13:28:38 +0000256 return PyBytes_FromStringAndSize(output, size);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +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 *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000267bytesio_read1(bytesio *self, PyObject *n)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +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 *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000287bytesio_readline(bytesio *self, PyObject *args)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +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
298 if (PyLong_Check(arg)) {
299 size = PyLong_AsSsize_t(arg);
Benjamin Petersona8a93042008-09-30 02:18:09 +0000300 if (size == -1 && PyErr_Occurred())
301 return NULL;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +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
Christian Heimes72b710a2008-05-26 13:28:38 +0000321 return PyBytes_FromStringAndSize(output, n);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +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 *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000332bytesio_readlines(bytesio *self, PyObject *args)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +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
344 if (PyLong_Check(arg)) {
345 maxsize = PyLong_AsSsize_t(arg);
Benjamin Petersona8a93042008-09-30 02:18:09 +0000346 if (maxsize == -1 && PyErr_Occurred())
347 return NULL;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +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) {
Christian Heimes72b710a2008-05-26 13:28:38 +0000365 line = PyBytes_FromStringAndSize(output, n);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +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,
Alexandre Vassalotti10dfc1e2008-05-08 01:34:41 +0000385"readinto(bytearray) -> int. Read up to len(b) bytes into b.\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000386"\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 *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000391bytesio_readinto(bytesio *self, PyObject *buffer)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000392{
393 void *raw_buffer;
Benjamin Peterson5a1ca6e2010-11-20 17:26:25 +0000394 Py_ssize_t len, n;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000395
396 CHECK_CLOSED(self);
397
398 if (PyObject_AsWriteBuffer(buffer, &raw_buffer, &len) == -1)
399 return NULL;
400
Benjamin Peterson5a1ca6e2010-11-20 17:26:25 +0000401 /* adjust invalid sizes */
402 n = self->string_size - self->pos;
403 if (len > n) {
404 len = n;
405 if (len < 0)
406 len = 0;
407 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000408
409 memcpy(raw_buffer, self->buf + self->pos, len);
410 assert(self->pos + len < PY_SSIZE_T_MAX);
411 assert(len >= 0);
412 self->pos += len;
413
414 return PyLong_FromSsize_t(len);
415}
416
417PyDoc_STRVAR(truncate_doc,
418"truncate([size]) -> int. Truncate the file to at most size bytes.\n"
419"\n"
420"Size defaults to the current file position, as returned by tell().\n"
Antoine Pitrou66f9fea2010-01-31 23:20:26 +0000421"The current file position is unchanged. Returns the new size.\n");
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000422
423static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000424bytesio_truncate(bytesio *self, PyObject *args)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000425{
426 Py_ssize_t size;
427 PyObject *arg = Py_None;
428
429 CHECK_CLOSED(self);
430
431 if (!PyArg_ParseTuple(args, "|O:truncate", &arg))
432 return NULL;
433
434 if (PyLong_Check(arg)) {
435 size = PyLong_AsSsize_t(arg);
Benjamin Petersona8a93042008-09-30 02:18:09 +0000436 if (size == -1 && PyErr_Occurred())
437 return NULL;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000438 }
439 else if (arg == Py_None) {
440 /* Truncate to current position if no argument is passed. */
441 size = self->pos;
442 }
443 else {
444 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
445 Py_TYPE(arg)->tp_name);
446 return NULL;
447 }
448
449 if (size < 0) {
450 PyErr_Format(PyExc_ValueError,
451 "negative size value %zd", size);
452 return NULL;
453 }
454
455 if (size < self->string_size) {
456 self->string_size = size;
457 if (resize_buffer(self, size) < 0)
458 return NULL;
459 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000460
461 return PyLong_FromSsize_t(size);
462}
463
464static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000465bytesio_iternext(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000466{
467 char *next;
468 Py_ssize_t n;
469
470 CHECK_CLOSED(self);
471
472 n = get_line(self, &next);
473
474 if (!next || n == 0)
475 return NULL;
476
Christian Heimes72b710a2008-05-26 13:28:38 +0000477 return PyBytes_FromStringAndSize(next, n);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000478}
479
480PyDoc_STRVAR(seek_doc,
481"seek(pos, whence=0) -> int. Change stream position.\n"
482"\n"
483"Seek to byte offset pos relative to position indicated by whence:\n"
484" 0 Start of stream (the default). pos should be >= 0;\n"
485" 1 Current position - pos may be negative;\n"
486" 2 End of stream - pos usually negative.\n"
487"Returns the new absolute position.");
488
489static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000490bytesio_seek(bytesio *self, PyObject *args)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000491{
492 Py_ssize_t pos;
493 int mode = 0;
494
495 CHECK_CLOSED(self);
496
497 if (!PyArg_ParseTuple(args, "n|i:seek", &pos, &mode))
498 return NULL;
499
500 if (pos < 0 && mode == 0) {
501 PyErr_Format(PyExc_ValueError,
502 "negative seek value %zd", pos);
503 return NULL;
504 }
505
506 /* mode 0: offset relative to beginning of the string.
507 mode 1: offset relative to current position.
508 mode 2: offset relative the end of the string. */
509 if (mode == 1) {
510 if (pos > PY_SSIZE_T_MAX - self->pos) {
511 PyErr_SetString(PyExc_OverflowError,
512 "new position too large");
513 return NULL;
514 }
515 pos += self->pos;
516 }
517 else if (mode == 2) {
518 if (pos > PY_SSIZE_T_MAX - self->string_size) {
519 PyErr_SetString(PyExc_OverflowError,
520 "new position too large");
521 return NULL;
522 }
523 pos += self->string_size;
524 }
525 else if (mode != 0) {
526 PyErr_Format(PyExc_ValueError,
527 "invalid whence (%i, should be 0, 1 or 2)", mode);
528 return NULL;
529 }
530
531 if (pos < 0)
532 pos = 0;
533 self->pos = pos;
534
535 return PyLong_FromSsize_t(self->pos);
536}
537
538PyDoc_STRVAR(write_doc,
Alexandre Vassalotti10dfc1e2008-05-08 01:34:41 +0000539"write(bytes) -> int. Write bytes to file.\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000540"\n"
541"Return the number of bytes written.");
542
543static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000544bytesio_write(bytesio *self, PyObject *obj)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000545{
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000546 Py_ssize_t n = 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000547 Py_buffer buf;
548 PyObject *result = NULL;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000549
550 CHECK_CLOSED(self);
551
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000552 if (PyObject_GetBuffer(obj, &buf, PyBUF_CONTIG_RO) < 0)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000553 return NULL;
554
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000555 if (buf.len != 0)
556 n = write_bytes(self, buf.buf, buf.len);
557 if (n >= 0)
558 result = PyLong_FromSsize_t(n);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000559
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000560 PyBuffer_Release(&buf);
561 return result;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000562}
563
564PyDoc_STRVAR(writelines_doc,
Alexandre Vassalotti7d060892008-05-07 01:47:37 +0000565"writelines(sequence_of_strings) -> None. Write strings to the file.\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000566"\n"
Alexandre Vassalotti7d060892008-05-07 01:47:37 +0000567"Note that newlines are not added. The sequence can be any iterable\n"
568"object producing strings. This is equivalent to calling write() for\n"
569"each string.");
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000570
571static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000572bytesio_writelines(bytesio *self, PyObject *v)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000573{
574 PyObject *it, *item;
575 PyObject *ret;
576
577 CHECK_CLOSED(self);
578
579 it = PyObject_GetIter(v);
580 if (it == NULL)
581 return NULL;
582
583 while ((item = PyIter_Next(it)) != NULL) {
584 ret = bytesio_write(self, item);
585 Py_DECREF(item);
586 if (ret == NULL) {
587 Py_DECREF(it);
588 return NULL;
589 }
590 Py_DECREF(ret);
591 }
592 Py_DECREF(it);
593
594 /* See if PyIter_Next failed */
595 if (PyErr_Occurred())
596 return NULL;
597
598 Py_RETURN_NONE;
599}
600
601PyDoc_STRVAR(close_doc,
602"close() -> None. Disable all I/O operations.");
603
604static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000605bytesio_close(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000606{
607 if (self->buf != NULL) {
608 PyMem_Free(self->buf);
609 self->buf = NULL;
610 }
611 Py_RETURN_NONE;
612}
613
614static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000615bytesio_dealloc(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000616{
Georg Brandl194da4a2009-08-13 09:34:05 +0000617 _PyObject_GC_UNTRACK(self);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000618 if (self->buf != NULL) {
619 PyMem_Free(self->buf);
620 self->buf = NULL;
621 }
Georg Brandl194da4a2009-08-13 09:34:05 +0000622 Py_CLEAR(self->dict);
623 if (self->weakreflist != NULL)
624 PyObject_ClearWeakRefs((PyObject *) self);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000625 Py_TYPE(self)->tp_free(self);
626}
627
628static PyObject *
629bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
630{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000631 bytesio *self;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000632
633 assert(type != NULL && type->tp_alloc != NULL);
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000634 self = (bytesio *)type->tp_alloc(type, 0);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000635 if (self == NULL)
636 return NULL;
637
638 self->string_size = 0;
639 self->pos = 0;
640 self->buf_size = 0;
641 self->buf = (char *)PyMem_Malloc(0);
642 if (self->buf == NULL) {
643 Py_DECREF(self);
644 return PyErr_NoMemory();
645 }
646
647 return (PyObject *)self;
648}
649
650static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000651bytesio_init(bytesio *self, PyObject *args, PyObject *kwds)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000652{
Georg Brandl97b28f82009-08-13 08:35:19 +0000653 char *kwlist[] = {"initial_bytes", NULL};
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000654 PyObject *initvalue = NULL;
655
Georg Brandl97b28f82009-08-13 08:35:19 +0000656 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:BytesIO", kwlist,
657 &initvalue))
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000658 return -1;
659
660 /* In case, __init__ is called multiple times. */
661 self->string_size = 0;
662 self->pos = 0;
663
664 if (initvalue && initvalue != Py_None) {
665 PyObject *res;
666 res = bytesio_write(self, initvalue);
667 if (res == NULL)
668 return -1;
669 Py_DECREF(res);
670 self->pos = 0;
671 }
672
673 return 0;
674}
675
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000676static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000677bytesio_traverse(bytesio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000678{
679 Py_VISIT(self->dict);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000680 return 0;
681}
682
683static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000684bytesio_clear(bytesio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000685{
686 Py_CLEAR(self->dict);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000687 return 0;
688}
689
690
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000691static PyGetSetDef bytesio_getsetlist[] = {
692 {"closed", (getter)bytesio_get_closed, NULL,
693 "True if the file is closed."},
Benjamin Peterson1fea3212009-04-19 03:15:20 +0000694 {NULL}, /* sentinel */
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000695};
696
697static struct PyMethodDef bytesio_methods[] = {
698 {"readable", (PyCFunction)return_true, METH_NOARGS, NULL},
699 {"seekable", (PyCFunction)return_true, METH_NOARGS, NULL},
700 {"writable", (PyCFunction)return_true, METH_NOARGS, NULL},
701 {"close", (PyCFunction)bytesio_close, METH_NOARGS, close_doc},
702 {"flush", (PyCFunction)bytesio_flush, METH_NOARGS, flush_doc},
703 {"isatty", (PyCFunction)bytesio_isatty, METH_NOARGS, isatty_doc},
704 {"tell", (PyCFunction)bytesio_tell, METH_NOARGS, tell_doc},
705 {"write", (PyCFunction)bytesio_write, METH_O, write_doc},
706 {"writelines", (PyCFunction)bytesio_writelines, METH_O, writelines_doc},
707 {"read1", (PyCFunction)bytesio_read1, METH_O, read1_doc},
708 {"readinto", (PyCFunction)bytesio_readinto, METH_O, readinto_doc},
709 {"readline", (PyCFunction)bytesio_readline, METH_VARARGS, readline_doc},
710 {"readlines", (PyCFunction)bytesio_readlines, METH_VARARGS, readlines_doc},
711 {"read", (PyCFunction)bytesio_read, METH_VARARGS, read_doc},
Antoine Pitroub82d5e12010-09-02 22:21:32 +0000712 {"getvalue", (PyCFunction)bytesio_getvalue, METH_NOARGS, getval_doc},
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000713 {"seek", (PyCFunction)bytesio_seek, METH_VARARGS, seek_doc},
714 {"truncate", (PyCFunction)bytesio_truncate, METH_VARARGS, truncate_doc},
715 {NULL, NULL} /* sentinel */
716};
717
718PyDoc_STRVAR(bytesio_doc,
719"BytesIO([buffer]) -> object\n"
720"\n"
721"Create a buffered I/O implementation using an in-memory bytes\n"
722"buffer, ready for reading and writing.");
723
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000724PyTypeObject PyBytesIO_Type = {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000725 PyVarObject_HEAD_INIT(NULL, 0)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000726 "_io.BytesIO", /*tp_name*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000727 sizeof(bytesio), /*tp_basicsize*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000728 0, /*tp_itemsize*/
729 (destructor)bytesio_dealloc, /*tp_dealloc*/
730 0, /*tp_print*/
731 0, /*tp_getattr*/
732 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000733 0, /*tp_reserved*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000734 0, /*tp_repr*/
735 0, /*tp_as_number*/
736 0, /*tp_as_sequence*/
737 0, /*tp_as_mapping*/
738 0, /*tp_hash*/
739 0, /*tp_call*/
740 0, /*tp_str*/
741 0, /*tp_getattro*/
742 0, /*tp_setattro*/
743 0, /*tp_as_buffer*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000744 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
745 Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000746 bytesio_doc, /*tp_doc*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000747 (traverseproc)bytesio_traverse, /*tp_traverse*/
748 (inquiry)bytesio_clear, /*tp_clear*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000749 0, /*tp_richcompare*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000750 offsetof(bytesio, weakreflist), /*tp_weaklistoffset*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000751 PyObject_SelfIter, /*tp_iter*/
752 (iternextfunc)bytesio_iternext, /*tp_iternext*/
753 bytesio_methods, /*tp_methods*/
754 0, /*tp_members*/
755 bytesio_getsetlist, /*tp_getset*/
756 0, /*tp_base*/
757 0, /*tp_dict*/
758 0, /*tp_descr_get*/
759 0, /*tp_descr_set*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000760 offsetof(bytesio, dict), /*tp_dictoffset*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000761 (initproc)bytesio_init, /*tp_init*/
762 0, /*tp_alloc*/
763 bytesio_new, /*tp_new*/
764};