blob: f477550b868b2f09c57b21f8034cd081bd7deb9c [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{
172 Py_RETURN_NONE;
173}
174
175PyDoc_STRVAR(getval_doc,
176"getvalue() -> bytes.\n"
177"\n"
178"Retrieve the entire contents of the BytesIO object.");
179
180static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000181bytesio_getvalue(bytesio *self)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000182{
183 CHECK_CLOSED(self);
Antoine Pitrou19690592009-06-12 20:14:08 +0000184 return PyBytes_FromStringAndSize(self->buf, self->string_size);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +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 *
Antoine Pitrou19690592009-06-12 20:14:08 +0000194bytesio_isatty(bytesio *self)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +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 *
Antoine Pitrou19690592009-06-12 20:14:08 +0000204bytesio_tell(bytesio *self)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000205{
206 CHECK_CLOSED(self);
Antoine Pitrou19690592009-06-12 20:14:08 +0000207 return PyLong_FromSsize_t(self->pos);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000208}
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 *
Antoine Pitrou19690592009-06-12 20:14:08 +0000217bytesio_read(bytesio *self, PyObject *args)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +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
Antoine Pitrou19690592009-06-12 20:14:08 +0000228 if (PyNumber_Check(arg)) {
229 size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
Benjamin Peterson02252482008-09-30 02:11:07 +0000230 if (size == -1 && PyErr_Occurred())
231 return NULL;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +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
Antoine Pitrou19690592009-06-12 20:14:08 +0000255 return PyBytes_FromStringAndSize(output, size);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +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 *
Antoine Pitrou19690592009-06-12 20:14:08 +0000266bytesio_read1(bytesio *self, PyObject *n)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +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 *
Antoine Pitrou19690592009-06-12 20:14:08 +0000286bytesio_readline(bytesio *self, PyObject *args)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +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
Antoine Pitrou19690592009-06-12 20:14:08 +0000297 if (PyNumber_Check(arg)) {
298 size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
Benjamin Peterson02252482008-09-30 02:11:07 +0000299 if (size == -1 && PyErr_Occurred())
300 return NULL;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +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
Antoine Pitrou19690592009-06-12 20:14:08 +0000320 return PyBytes_FromStringAndSize(output, n);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +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 *
Antoine Pitrou19690592009-06-12 20:14:08 +0000331bytesio_readlines(bytesio *self, PyObject *args)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +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
Antoine Pitrou19690592009-06-12 20:14:08 +0000343 if (PyNumber_Check(arg)) {
344 maxsize = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
Benjamin Peterson02252482008-09-30 02:11:07 +0000345 if (maxsize == -1 && PyErr_Occurred())
346 return NULL;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +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) {
Antoine Pitrou19690592009-06-12 20:14:08 +0000364 line = PyBytes_FromStringAndSize(output, n);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +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,
384"readinto(bytearray) -> int. Read up to len(b) bytes into b.\n"
385"\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 *
Antoine Pitrou19690592009-06-12 20:14:08 +0000390bytesio_readinto(bytesio *self, PyObject *args)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000391{
Antoine Pitrou19690592009-06-12 20:14:08 +0000392 Py_buffer buf;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000393 Py_ssize_t len;
394
395 CHECK_CLOSED(self);
396
Antoine Pitrou19690592009-06-12 20:14:08 +0000397 if (!PyArg_ParseTuple(args, "w*", &buf))
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000398 return NULL;
399
Antoine Pitrou19690592009-06-12 20:14:08 +0000400 len = buf.len;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000401 if (self->pos + len > self->string_size)
402 len = self->string_size - self->pos;
403
Antoine Pitrou19690592009-06-12 20:14:08 +0000404 memcpy(buf.buf, self->buf + self->pos, len);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000405 assert(self->pos + len < PY_SSIZE_T_MAX);
406 assert(len >= 0);
407 self->pos += len;
408
Antoine Pitrou19690592009-06-12 20:14:08 +0000409 PyBuffer_Release(&buf);
410 return PyLong_FromSsize_t(len);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000411}
412
413PyDoc_STRVAR(truncate_doc,
414"truncate([size]) -> int. Truncate the file to at most size bytes.\n"
415"\n"
416"Size defaults to the current file position, as returned by tell().\n"
417"Returns the new size. Imply an absolute seek to the position size.");
418
419static PyObject *
Antoine Pitrou19690592009-06-12 20:14:08 +0000420bytesio_truncate(bytesio *self, PyObject *args)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000421{
422 Py_ssize_t size;
423 PyObject *arg = Py_None;
424
425 CHECK_CLOSED(self);
426
427 if (!PyArg_ParseTuple(args, "|O:truncate", &arg))
428 return NULL;
429
Antoine Pitrou19690592009-06-12 20:14:08 +0000430 if (PyNumber_Check(arg)) {
431 size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
Benjamin Peterson02252482008-09-30 02:11:07 +0000432 if (size == -1 && PyErr_Occurred())
433 return NULL;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000434 }
435 else if (arg == Py_None) {
436 /* Truncate to current position if no argument is passed. */
437 size = self->pos;
438 }
439 else {
440 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
441 Py_TYPE(arg)->tp_name);
442 return NULL;
443 }
444
445 if (size < 0) {
446 PyErr_Format(PyExc_ValueError,
447 "negative size value %zd", size);
448 return NULL;
449 }
450
451 if (size < self->string_size) {
452 self->string_size = size;
453 if (resize_buffer(self, size) < 0)
454 return NULL;
455 }
456 self->pos = size;
457
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
616static void
Antoine Pitrou19690592009-06-12 20:14:08 +0000617bytesio_dealloc(bytesio *self)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000618{
Antoine Pitrouf98a2672009-10-24 11:59:41 +0000619 _PyObject_GC_UNTRACK(self);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000620 if (self->buf != NULL) {
621 PyMem_Free(self->buf);
622 self->buf = NULL;
623 }
Antoine Pitrouf98a2672009-10-24 11:59:41 +0000624 Py_CLEAR(self->dict);
625 if (self->weakreflist != NULL)
626 PyObject_ClearWeakRefs((PyObject *) self);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000627 Py_TYPE(self)->tp_free(self);
628}
629
630static PyObject *
631bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
632{
Antoine Pitrou19690592009-06-12 20:14:08 +0000633 bytesio *self;
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000634
635 assert(type != NULL && type->tp_alloc != NULL);
Antoine Pitrou19690592009-06-12 20:14:08 +0000636 self = (bytesio *)type->tp_alloc(type, 0);
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000637 if (self == NULL)
638 return NULL;
639
640 self->string_size = 0;
641 self->pos = 0;
642 self->buf_size = 0;
643 self->buf = (char *)PyMem_Malloc(0);
644 if (self->buf == NULL) {
645 Py_DECREF(self);
646 return PyErr_NoMemory();
647 }
648
649 return (PyObject *)self;
650}
651
652static int
Antoine Pitrou19690592009-06-12 20:14:08 +0000653bytesio_init(bytesio *self, PyObject *args, PyObject *kwds)
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000654{
655 PyObject *initvalue = NULL;
656
657 if (!PyArg_ParseTuple(args, "|O:BytesIO", &initvalue))
658 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
Antoine Pitrou19690592009-06-12 20:14:08 +0000676static int
677bytesio_traverse(bytesio *self, visitproc visit, void *arg)
678{
679 Py_VISIT(self->dict);
Antoine Pitrou19690592009-06-12 20:14:08 +0000680 return 0;
681}
682
683static int
684bytesio_clear(bytesio *self)
685{
686 Py_CLEAR(self->dict);
Antoine Pitrou19690592009-06-12 20:14:08 +0000687 return 0;
688}
689
690
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000691static PyGetSetDef bytesio_getsetlist[] = {
692 {"closed", (getter)bytesio_get_closed, NULL,
693 "True if the file is closed."},
Antoine Pitrou19690592009-06-12 20:14:08 +0000694 {NULL}, /* sentinel */
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +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},
Antoine Pitrou19690592009-06-12 20:14:08 +0000708 {"readinto", (PyCFunction)bytesio_readinto, METH_VARARGS, readinto_doc},
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000709 {"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},
712 {"getvalue", (PyCFunction)bytesio_getvalue, METH_VARARGS, getval_doc},
713 {"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
Antoine Pitrou19690592009-06-12 20:14:08 +0000724PyTypeObject PyBytesIO_Type = {
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000725 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrou19690592009-06-12 20:14:08 +0000726 "_io.BytesIO", /*tp_name*/
727 sizeof(bytesio), /*tp_basicsize*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000728 0, /*tp_itemsize*/
729 (destructor)bytesio_dealloc, /*tp_dealloc*/
730 0, /*tp_print*/
731 0, /*tp_getattr*/
732 0, /*tp_setattr*/
Antoine Pitrou19690592009-06-12 20:14:08 +0000733 0, /*tp_reserved*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +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*/
Antoine Pitrou19690592009-06-12 20:14:08 +0000744 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
745 Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000746 bytesio_doc, /*tp_doc*/
Antoine Pitrou19690592009-06-12 20:14:08 +0000747 (traverseproc)bytesio_traverse, /*tp_traverse*/
748 (inquiry)bytesio_clear, /*tp_clear*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000749 0, /*tp_richcompare*/
Antoine Pitrou19690592009-06-12 20:14:08 +0000750 offsetof(bytesio, weakreflist), /*tp_weaklistoffset*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +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*/
Antoine Pitrou19690592009-06-12 20:14:08 +0000760 offsetof(bytesio, dict), /*tp_dictoffset*/
Alexandre Vassalotti1aed6242008-05-09 21:49:43 +0000761 (initproc)bytesio_init, /*tp_init*/
762 0, /*tp_alloc*/
763 bytesio_new, /*tp_new*/
764};