blob: b55ab72e0052cdd11a665e4667ca3cf027206ce0 [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;
Antoine Pitrou972ee132010-09-06 18:48:21 +000013 Py_ssize_t exports;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000014} bytesio;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +000015
Antoine Pitrou972ee132010-09-06 18:48:21 +000016typedef struct {
17 PyObject_HEAD
18 bytesio *source;
19} bytesiobuf;
20
21
Alexandre Vassalotti77250f42008-05-06 19:48:38 +000022#define CHECK_CLOSED(self) \
23 if ((self)->buf == NULL) { \
24 PyErr_SetString(PyExc_ValueError, \
25 "I/O operation on closed file."); \
26 return NULL; \
27 }
28
Antoine Pitrou972ee132010-09-06 18:48:21 +000029#define CHECK_EXPORTS(self) \
30 if ((self)->exports > 0) { \
31 PyErr_SetString(PyExc_BufferError, \
32 "Existing exports of data: object cannot be re-sized"); \
33 return NULL; \
34 }
35
36
Alexandre Vassalotti77250f42008-05-06 19:48:38 +000037/* Internal routine to get a line from the buffer of a BytesIO
38 object. Returns the length between the current position to the
39 next newline character. */
40static Py_ssize_t
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000041get_line(bytesio *self, char **output)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +000042{
43 char *n;
44 const char *str_end;
45 Py_ssize_t len;
46
47 assert(self->buf != NULL);
48
49 /* Move to the end of the line, up to the end of the string, s. */
50 str_end = self->buf + self->string_size;
51 for (n = self->buf + self->pos;
52 n < str_end && *n != '\n';
53 n++);
54
55 /* Skip the newline character */
56 if (n < str_end)
57 n++;
58
59 /* Get the length from the current position to the end of the line. */
60 len = n - (self->buf + self->pos);
61 *output = self->buf + self->pos;
62
63 assert(len >= 0);
64 assert(self->pos < PY_SSIZE_T_MAX - len);
65 self->pos += len;
66
67 return len;
68}
69
70/* Internal routine for changing the size of the buffer of BytesIO objects.
71 The caller should ensure that the 'size' argument is non-negative. Returns
72 0 on success, -1 otherwise. */
73static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000074resize_buffer(bytesio *self, size_t size)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +000075{
76 /* Here, unsigned types are used to avoid dealing with signed integer
77 overflow, which is undefined in C. */
78 size_t alloc = self->buf_size;
79 char *new_buf = NULL;
80
81 assert(self->buf != NULL);
82
83 /* For simplicity, stay in the range of the signed type. Anyway, Python
84 doesn't allow strings to be longer than this. */
85 if (size > PY_SSIZE_T_MAX)
86 goto overflow;
87
88 if (size < alloc / 2) {
89 /* Major downsize; resize down to exact size. */
90 alloc = size + 1;
91 }
92 else if (size < alloc) {
93 /* Within allocated size; quick exit */
94 return 0;
95 }
96 else if (size <= alloc * 1.125) {
97 /* Moderate upsize; overallocate similar to list_resize() */
98 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
99 }
100 else {
101 /* Major upsize; resize up to exact size */
102 alloc = size + 1;
103 }
104
105 if (alloc > ((size_t)-1) / sizeof(char))
106 goto overflow;
107 new_buf = (char *)PyMem_Realloc(self->buf, alloc * sizeof(char));
108 if (new_buf == NULL) {
109 PyErr_NoMemory();
110 return -1;
111 }
112 self->buf_size = alloc;
113 self->buf = new_buf;
114
115 return 0;
116
117 overflow:
118 PyErr_SetString(PyExc_OverflowError,
119 "new buffer size too large");
120 return -1;
121}
122
123/* Internal routine for writing a string of bytes to the buffer of a BytesIO
Antoine Pitrou1d857452012-09-05 20:11:49 +0200124 object. Returns the number of bytes written, or -1 on error. */
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000125static Py_ssize_t
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000126write_bytes(bytesio *self, const char *bytes, Py_ssize_t len)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000127{
128 assert(self->buf != NULL);
129 assert(self->pos >= 0);
130 assert(len >= 0);
131
Alexandre Vassalotti1bfe9dc82008-05-07 01:44:31 +0000132 if ((size_t)self->pos + len > self->buf_size) {
133 if (resize_buffer(self, (size_t)self->pos + len) < 0)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000134 return -1;
135 }
136
137 if (self->pos > self->string_size) {
138 /* In case of overseek, pad with null bytes the buffer region between
139 the end of stream and the current position.
140
141 0 lo string_size hi
142 | |<---used--->|<----------available----------->|
143 | | <--to pad-->|<---to write---> |
144 0 buf position
145 */
146 memset(self->buf + self->string_size, '\0',
147 (self->pos - self->string_size) * sizeof(char));
148 }
149
150 /* Copy the data to the internal buffer, overwriting some of the existing
151 data if self->pos < self->string_size. */
152 memcpy(self->buf + self->pos, bytes, len);
153 self->pos += len;
154
155 /* Set the new length of the internal string if it has changed. */
156 if (self->string_size < self->pos) {
157 self->string_size = self->pos;
158 }
159
160 return len;
161}
162
163static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000164bytesio_get_closed(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000165{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000166 if (self->buf == NULL) {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000167 Py_RETURN_TRUE;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000168 }
169 else {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000170 Py_RETURN_FALSE;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000171 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000172}
173
Antoine Pitrou1d857452012-09-05 20:11:49 +0200174PyDoc_STRVAR(readable_doc,
175"readable() -> bool. Returns True if the IO object can be read.");
176
177PyDoc_STRVAR(writable_doc,
178"writable() -> bool. Returns True if the IO object can be written.");
179
180PyDoc_STRVAR(seekable_doc,
181"seekable() -> bool. Returns True if the IO object can be seeked.");
182
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000183/* Generic getter for the writable, readable and seekable properties */
184static PyObject *
Antoine Pitrou1d857452012-09-05 20:11:49 +0200185return_not_closed(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000186{
Antoine Pitrou1d857452012-09-05 20:11:49 +0200187 CHECK_CLOSED(self);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000188 Py_RETURN_TRUE;
189}
190
191PyDoc_STRVAR(flush_doc,
192"flush() -> None. Does nothing.");
193
194static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000195bytesio_flush(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000196{
Antoine Pitrou6be88762010-05-03 16:48:20 +0000197 CHECK_CLOSED(self);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000198 Py_RETURN_NONE;
199}
200
Antoine Pitrou972ee132010-09-06 18:48:21 +0000201PyDoc_STRVAR(getbuffer_doc,
202"getbuffer() -> bytes.\n"
203"\n"
204"Get a read-write view over the contents of the BytesIO object.");
205
206static PyObject *
207bytesio_getbuffer(bytesio *self)
208{
209 PyTypeObject *type = &_PyBytesIOBuffer_Type;
210 bytesiobuf *buf;
211 PyObject *view;
212
213 CHECK_CLOSED(self);
214
215 buf = (bytesiobuf *) type->tp_alloc(type, 0);
216 if (buf == NULL)
217 return NULL;
218 Py_INCREF(self);
219 buf->source = self;
220 view = PyMemoryView_FromObject((PyObject *) buf);
221 Py_DECREF(buf);
222 return view;
223}
224
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000225PyDoc_STRVAR(getval_doc,
Alexandre Vassalotti10dfc1e2008-05-08 01:34:41 +0000226"getvalue() -> bytes.\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000227"\n"
228"Retrieve the entire contents of the BytesIO object.");
229
230static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000231bytesio_getvalue(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000232{
233 CHECK_CLOSED(self);
Christian Heimes72b710a2008-05-26 13:28:38 +0000234 return PyBytes_FromStringAndSize(self->buf, self->string_size);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000235}
236
237PyDoc_STRVAR(isatty_doc,
238"isatty() -> False.\n"
239"\n"
240"Always returns False since BytesIO objects are not connected\n"
241"to a tty-like device.");
242
243static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000244bytesio_isatty(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000245{
246 CHECK_CLOSED(self);
247 Py_RETURN_FALSE;
248}
249
250PyDoc_STRVAR(tell_doc,
251"tell() -> current file position, an integer\n");
252
253static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000254bytesio_tell(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000255{
256 CHECK_CLOSED(self);
257 return PyLong_FromSsize_t(self->pos);
258}
259
260PyDoc_STRVAR(read_doc,
Serhiy Storchakab817b772015-04-10 02:18:44 +0300261"read([size]) -> read at most size bytes, returned as a bytes object.\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000262"\n"
263"If the size argument is negative, read until EOF is reached.\n"
Serhiy Storchakab817b772015-04-10 02:18:44 +0300264"Return an empty bytes object at EOF.");
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000265
266static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000267bytesio_read(bytesio *self, PyObject *args)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000268{
269 Py_ssize_t size, n;
270 char *output;
271 PyObject *arg = Py_None;
272
273 CHECK_CLOSED(self);
274
275 if (!PyArg_ParseTuple(args, "|O:read", &arg))
276 return NULL;
277
278 if (PyLong_Check(arg)) {
279 size = PyLong_AsSsize_t(arg);
Benjamin Petersona8a93042008-09-30 02:18:09 +0000280 if (size == -1 && PyErr_Occurred())
281 return NULL;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000282 }
283 else if (arg == Py_None) {
284 /* Read until EOF is reached, by default. */
285 size = -1;
286 }
287 else {
288 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
289 Py_TYPE(arg)->tp_name);
290 return NULL;
291 }
292
293 /* adjust invalid sizes */
294 n = self->string_size - self->pos;
295 if (size < 0 || size > n) {
296 size = n;
297 if (size < 0)
298 size = 0;
299 }
300
301 assert(self->buf != NULL);
302 output = self->buf + self->pos;
303 self->pos += size;
304
Christian Heimes72b710a2008-05-26 13:28:38 +0000305 return PyBytes_FromStringAndSize(output, size);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000306}
307
308
309PyDoc_STRVAR(read1_doc,
Serhiy Storchakab817b772015-04-10 02:18:44 +0300310"read1(size) -> read at most size bytes, returned as a bytes object.\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000311"\n"
312"If the size argument is negative or omitted, read until EOF is reached.\n"
Serhiy Storchakab817b772015-04-10 02:18:44 +0300313"Return an empty bytes object at EOF.");
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000314
315static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000316bytesio_read1(bytesio *self, PyObject *n)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000317{
318 PyObject *arg, *res;
319
320 arg = PyTuple_Pack(1, n);
321 if (arg == NULL)
322 return NULL;
323 res = bytesio_read(self, arg);
324 Py_DECREF(arg);
325 return res;
326}
327
328PyDoc_STRVAR(readline_doc,
Serhiy Storchakab817b772015-04-10 02:18:44 +0300329"readline([size]) -> next line from the file, as a bytes object.\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000330"\n"
331"Retain newline. A non-negative size argument limits the maximum\n"
332"number of bytes to return (an incomplete line may be returned then).\n"
Serhiy Storchakab817b772015-04-10 02:18:44 +0300333"Return an empty bytes object at EOF.\n");
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000334
335static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000336bytesio_readline(bytesio *self, PyObject *args)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000337{
338 Py_ssize_t size, n;
339 char *output;
340 PyObject *arg = Py_None;
341
342 CHECK_CLOSED(self);
343
344 if (!PyArg_ParseTuple(args, "|O:readline", &arg))
345 return NULL;
346
347 if (PyLong_Check(arg)) {
348 size = PyLong_AsSsize_t(arg);
Benjamin Petersona8a93042008-09-30 02:18:09 +0000349 if (size == -1 && PyErr_Occurred())
350 return NULL;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000351 }
352 else if (arg == Py_None) {
353 /* No size limit, by default. */
354 size = -1;
355 }
356 else {
357 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
358 Py_TYPE(arg)->tp_name);
359 return NULL;
360 }
361
362 n = get_line(self, &output);
363
364 if (size >= 0 && size < n) {
365 size = n - size;
366 n -= size;
367 self->pos -= size;
368 }
369
Christian Heimes72b710a2008-05-26 13:28:38 +0000370 return PyBytes_FromStringAndSize(output, n);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000371}
372
373PyDoc_STRVAR(readlines_doc,
374"readlines([size]) -> list of strings, each a line from the file.\n"
375"\n"
376"Call readline() repeatedly and return a list of the lines so read.\n"
377"The optional size argument, if given, is an approximate bound on the\n"
378"total number of bytes in the lines returned.\n");
379
380static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000381bytesio_readlines(bytesio *self, PyObject *args)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000382{
383 Py_ssize_t maxsize, size, n;
384 PyObject *result, *line;
385 char *output;
386 PyObject *arg = Py_None;
387
388 CHECK_CLOSED(self);
389
390 if (!PyArg_ParseTuple(args, "|O:readlines", &arg))
391 return NULL;
392
393 if (PyLong_Check(arg)) {
394 maxsize = PyLong_AsSsize_t(arg);
Benjamin Petersona8a93042008-09-30 02:18:09 +0000395 if (maxsize == -1 && PyErr_Occurred())
396 return NULL;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000397 }
398 else if (arg == Py_None) {
399 /* No size limit, by default. */
400 maxsize = -1;
401 }
402 else {
403 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
404 Py_TYPE(arg)->tp_name);
405 return NULL;
406 }
407
408 size = 0;
409 result = PyList_New(0);
410 if (!result)
411 return NULL;
412
413 while ((n = get_line(self, &output)) != 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +0000414 line = PyBytes_FromStringAndSize(output, n);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000415 if (!line)
416 goto on_error;
417 if (PyList_Append(result, line) == -1) {
418 Py_DECREF(line);
419 goto on_error;
420 }
421 Py_DECREF(line);
422 size += n;
423 if (maxsize > 0 && size >= maxsize)
424 break;
425 }
426 return result;
427
428 on_error:
429 Py_DECREF(result);
430 return NULL;
431}
432
433PyDoc_STRVAR(readinto_doc,
Alexandre Vassalotti10dfc1e2008-05-08 01:34:41 +0000434"readinto(bytearray) -> int. Read up to len(b) bytes into b.\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000435"\n"
436"Returns number of bytes read (0 for EOF), or None if the object\n"
437"is set not to block as has no data to read.");
438
439static PyObject *
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200440bytesio_readinto(bytesio *self, PyObject *arg)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000441{
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200442 Py_buffer buffer;
Benjamin Petersonfa735552010-11-20 17:24:04 +0000443 Py_ssize_t len, n;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000444
445 CHECK_CLOSED(self);
446
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200447 if (!PyArg_Parse(arg, "w*", &buffer))
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000448 return NULL;
449
Benjamin Petersonfa735552010-11-20 17:24:04 +0000450 /* adjust invalid sizes */
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200451 len = buffer.len;
Benjamin Petersonfa735552010-11-20 17:24:04 +0000452 n = self->string_size - self->pos;
453 if (len > n) {
454 len = n;
455 if (len < 0)
456 len = 0;
457 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000458
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200459 memcpy(buffer.buf, self->buf + self->pos, len);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000460 assert(self->pos + len < PY_SSIZE_T_MAX);
461 assert(len >= 0);
462 self->pos += len;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200463 PyBuffer_Release(&buffer);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000464
465 return PyLong_FromSsize_t(len);
466}
467
468PyDoc_STRVAR(truncate_doc,
469"truncate([size]) -> int. Truncate the file to at most size bytes.\n"
470"\n"
471"Size defaults to the current file position, as returned by tell().\n"
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000472"The current file position is unchanged. Returns the new size.\n");
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000473
474static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000475bytesio_truncate(bytesio *self, PyObject *args)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000476{
477 Py_ssize_t size;
478 PyObject *arg = Py_None;
479
480 CHECK_CLOSED(self);
Antoine Pitrou972ee132010-09-06 18:48:21 +0000481 CHECK_EXPORTS(self);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000482
483 if (!PyArg_ParseTuple(args, "|O:truncate", &arg))
484 return NULL;
485
486 if (PyLong_Check(arg)) {
487 size = PyLong_AsSsize_t(arg);
Benjamin Petersona8a93042008-09-30 02:18:09 +0000488 if (size == -1 && PyErr_Occurred())
489 return NULL;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000490 }
491 else if (arg == Py_None) {
492 /* Truncate to current position if no argument is passed. */
493 size = self->pos;
494 }
495 else {
496 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
497 Py_TYPE(arg)->tp_name);
498 return NULL;
499 }
500
501 if (size < 0) {
502 PyErr_Format(PyExc_ValueError,
503 "negative size value %zd", size);
504 return NULL;
505 }
506
507 if (size < self->string_size) {
508 self->string_size = size;
509 if (resize_buffer(self, size) < 0)
510 return NULL;
511 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000512
513 return PyLong_FromSsize_t(size);
514}
515
516static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000517bytesio_iternext(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000518{
519 char *next;
520 Py_ssize_t n;
521
522 CHECK_CLOSED(self);
523
524 n = get_line(self, &next);
525
526 if (!next || n == 0)
527 return NULL;
528
Christian Heimes72b710a2008-05-26 13:28:38 +0000529 return PyBytes_FromStringAndSize(next, n);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000530}
531
532PyDoc_STRVAR(seek_doc,
Martin Panterdb4220e2015-09-11 03:58:30 +0000533"seek(pos[, whence]) -> int. Change stream position.\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000534"\n"
535"Seek to byte offset pos relative to position indicated by whence:\n"
536" 0 Start of stream (the default). pos should be >= 0;\n"
537" 1 Current position - pos may be negative;\n"
538" 2 End of stream - pos usually negative.\n"
539"Returns the new absolute position.");
540
541static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000542bytesio_seek(bytesio *self, PyObject *args)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000543{
544 Py_ssize_t pos;
545 int mode = 0;
546
547 CHECK_CLOSED(self);
548
549 if (!PyArg_ParseTuple(args, "n|i:seek", &pos, &mode))
550 return NULL;
551
552 if (pos < 0 && mode == 0) {
553 PyErr_Format(PyExc_ValueError,
554 "negative seek value %zd", pos);
555 return NULL;
556 }
557
558 /* mode 0: offset relative to beginning of the string.
559 mode 1: offset relative to current position.
560 mode 2: offset relative the end of the string. */
561 if (mode == 1) {
562 if (pos > PY_SSIZE_T_MAX - self->pos) {
563 PyErr_SetString(PyExc_OverflowError,
564 "new position too large");
565 return NULL;
566 }
567 pos += self->pos;
568 }
569 else if (mode == 2) {
570 if (pos > PY_SSIZE_T_MAX - self->string_size) {
571 PyErr_SetString(PyExc_OverflowError,
572 "new position too large");
573 return NULL;
574 }
575 pos += self->string_size;
576 }
577 else if (mode != 0) {
578 PyErr_Format(PyExc_ValueError,
579 "invalid whence (%i, should be 0, 1 or 2)", mode);
580 return NULL;
581 }
582
583 if (pos < 0)
584 pos = 0;
585 self->pos = pos;
586
587 return PyLong_FromSsize_t(self->pos);
588}
589
590PyDoc_STRVAR(write_doc,
Alexandre Vassalotti10dfc1e2008-05-08 01:34:41 +0000591"write(bytes) -> int. Write bytes to file.\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000592"\n"
593"Return the number of bytes written.");
594
595static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000596bytesio_write(bytesio *self, PyObject *obj)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000597{
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000598 Py_ssize_t n = 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000599 Py_buffer buf;
600 PyObject *result = NULL;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000601
602 CHECK_CLOSED(self);
Antoine Pitrou972ee132010-09-06 18:48:21 +0000603 CHECK_EXPORTS(self);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000604
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000605 if (PyObject_GetBuffer(obj, &buf, PyBUF_CONTIG_RO) < 0)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000606 return NULL;
607
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000608 if (buf.len != 0)
609 n = write_bytes(self, buf.buf, buf.len);
610 if (n >= 0)
611 result = PyLong_FromSsize_t(n);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000612
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000613 PyBuffer_Release(&buf);
614 return result;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000615}
616
617PyDoc_STRVAR(writelines_doc,
Serhiy Storchakab817b772015-04-10 02:18:44 +0300618"writelines(lines) -> None. Write bytes objects to the file.\n"
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000619"\n"
Serhiy Storchakab817b772015-04-10 02:18:44 +0300620"Note that newlines are not added. The argument can be any iterable\n"
621"object producing bytes objects. This is equivalent to calling write() for\n"
622"each bytes object.");
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000623
624static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000625bytesio_writelines(bytesio *self, PyObject *v)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000626{
627 PyObject *it, *item;
628 PyObject *ret;
629
630 CHECK_CLOSED(self);
631
632 it = PyObject_GetIter(v);
633 if (it == NULL)
634 return NULL;
635
636 while ((item = PyIter_Next(it)) != NULL) {
637 ret = bytesio_write(self, item);
638 Py_DECREF(item);
639 if (ret == NULL) {
640 Py_DECREF(it);
641 return NULL;
642 }
643 Py_DECREF(ret);
644 }
645 Py_DECREF(it);
646
647 /* See if PyIter_Next failed */
648 if (PyErr_Occurred())
649 return NULL;
650
651 Py_RETURN_NONE;
652}
653
654PyDoc_STRVAR(close_doc,
655"close() -> None. Disable all I/O operations.");
656
657static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000658bytesio_close(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000659{
Serhiy Storchakac057c382015-02-03 02:00:18 +0200660 CHECK_EXPORTS(self);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000661 if (self->buf != NULL) {
662 PyMem_Free(self->buf);
663 self->buf = NULL;
664 }
665 Py_RETURN_NONE;
666}
667
Alexandre Vassalotticf76e1a2009-07-22 03:24:36 +0000668/* Pickling support.
669
670 Note that only pickle protocol 2 and onward are supported since we use
671 extended __reduce__ API of PEP 307 to make BytesIO instances picklable.
672
673 Providing support for protocol < 2 would require the __reduce_ex__ method
674 which is notably long-winded when defined properly.
675
676 For BytesIO, the implementation would similar to one coded for
677 object.__reduce_ex__, but slightly less general. To be more specific, we
678 could call bytesio_getstate directly and avoid checking for the presence of
679 a fallback __reduce__ method. However, we would still need a __newobj__
680 function to use the efficient instance representation of PEP 307.
681 */
682
683static PyObject *
684bytesio_getstate(bytesio *self)
685{
686 PyObject *initvalue = bytesio_getvalue(self);
687 PyObject *dict;
688 PyObject *state;
689
690 if (initvalue == NULL)
691 return NULL;
692 if (self->dict == NULL) {
693 Py_INCREF(Py_None);
694 dict = Py_None;
695 }
696 else {
697 dict = PyDict_Copy(self->dict);
Stefan Krah96efdd42012-09-08 11:12:33 +0200698 if (dict == NULL) {
699 Py_DECREF(initvalue);
Alexandre Vassalotticf76e1a2009-07-22 03:24:36 +0000700 return NULL;
Stefan Krah96efdd42012-09-08 11:12:33 +0200701 }
Alexandre Vassalotticf76e1a2009-07-22 03:24:36 +0000702 }
703
704 state = Py_BuildValue("(OnN)", initvalue, self->pos, dict);
705 Py_DECREF(initvalue);
706 return state;
707}
708
709static PyObject *
710bytesio_setstate(bytesio *self, PyObject *state)
711{
712 PyObject *result;
713 PyObject *position_obj;
714 PyObject *dict;
715 Py_ssize_t pos;
716
717 assert(state != NULL);
718
719 /* We allow the state tuple to be longer than 3, because we may need
720 someday to extend the object's state without breaking
721 backward-compatibility. */
722 if (!PyTuple_Check(state) || Py_SIZE(state) < 3) {
723 PyErr_Format(PyExc_TypeError,
724 "%.200s.__setstate__ argument should be 3-tuple, got %.200s",
725 Py_TYPE(self)->tp_name, Py_TYPE(state)->tp_name);
726 return NULL;
727 }
Antoine Pitrou972ee132010-09-06 18:48:21 +0000728 CHECK_EXPORTS(self);
Alexandre Vassalotticf76e1a2009-07-22 03:24:36 +0000729 /* Reset the object to its default state. This is only needed to handle
730 the case of repeated calls to __setstate__. */
731 self->string_size = 0;
732 self->pos = 0;
733
734 /* Set the value of the internal buffer. If state[0] does not support the
735 buffer protocol, bytesio_write will raise the appropriate TypeError. */
736 result = bytesio_write(self, PyTuple_GET_ITEM(state, 0));
737 if (result == NULL)
738 return NULL;
739 Py_DECREF(result);
740
741 /* Set carefully the position value. Alternatively, we could use the seek
742 method instead of modifying self->pos directly to better protect the
743 object internal state against errneous (or malicious) inputs. */
744 position_obj = PyTuple_GET_ITEM(state, 1);
745 if (!PyLong_Check(position_obj)) {
746 PyErr_Format(PyExc_TypeError,
747 "second item of state must be an integer, not %.200s",
748 Py_TYPE(position_obj)->tp_name);
749 return NULL;
750 }
751 pos = PyLong_AsSsize_t(position_obj);
752 if (pos == -1 && PyErr_Occurred())
753 return NULL;
754 if (pos < 0) {
755 PyErr_SetString(PyExc_ValueError,
756 "position value cannot be negative");
757 return NULL;
758 }
759 self->pos = pos;
760
761 /* Set the dictionary of the instance variables. */
762 dict = PyTuple_GET_ITEM(state, 2);
763 if (dict != Py_None) {
764 if (!PyDict_Check(dict)) {
765 PyErr_Format(PyExc_TypeError,
766 "third item of state should be a dict, got a %.200s",
767 Py_TYPE(dict)->tp_name);
768 return NULL;
769 }
770 if (self->dict) {
771 /* Alternatively, we could replace the internal dictionary
772 completely. However, it seems more practical to just update it. */
773 if (PyDict_Update(self->dict, dict) < 0)
774 return NULL;
775 }
776 else {
777 Py_INCREF(dict);
778 self->dict = dict;
779 }
780 }
781
782 Py_RETURN_NONE;
783}
784
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000785static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000786bytesio_dealloc(bytesio *self)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000787{
Alexandre Vassalottifc477042009-07-22 02:24:49 +0000788 _PyObject_GC_UNTRACK(self);
Antoine Pitrou972ee132010-09-06 18:48:21 +0000789 if (self->exports > 0) {
790 PyErr_SetString(PyExc_SystemError,
791 "deallocated BytesIO object has exported buffers");
792 PyErr_Print();
793 }
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000794 if (self->buf != NULL) {
795 PyMem_Free(self->buf);
796 self->buf = NULL;
797 }
Alexandre Vassalottifc477042009-07-22 02:24:49 +0000798 Py_CLEAR(self->dict);
799 if (self->weakreflist != NULL)
800 PyObject_ClearWeakRefs((PyObject *) self);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000801 Py_TYPE(self)->tp_free(self);
802}
803
804static PyObject *
805bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
806{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000807 bytesio *self;
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000808
809 assert(type != NULL && type->tp_alloc != NULL);
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000810 self = (bytesio *)type->tp_alloc(type, 0);
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000811 if (self == NULL)
812 return NULL;
813
Alexandre Vassalotticf76e1a2009-07-22 03:24:36 +0000814 /* tp_alloc initializes all the fields to zero. So we don't have to
815 initialize them here. */
816
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000817 self->buf = (char *)PyMem_Malloc(0);
818 if (self->buf == NULL) {
819 Py_DECREF(self);
820 return PyErr_NoMemory();
821 }
822
823 return (PyObject *)self;
824}
825
826static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000827bytesio_init(bytesio *self, PyObject *args, PyObject *kwds)
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000828{
Alexandre Vassalottiba5c7432009-08-04 23:19:13 +0000829 char *kwlist[] = {"initial_bytes", NULL};
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000830 PyObject *initvalue = NULL;
831
Alexandre Vassalottiba5c7432009-08-04 23:19:13 +0000832 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:BytesIO", kwlist,
833 &initvalue))
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000834 return -1;
835
836 /* In case, __init__ is called multiple times. */
837 self->string_size = 0;
838 self->pos = 0;
839
840 if (initvalue && initvalue != Py_None) {
841 PyObject *res;
842 res = bytesio_write(self, initvalue);
843 if (res == NULL)
844 return -1;
845 Py_DECREF(res);
846 self->pos = 0;
847 }
848
849 return 0;
850}
851
Antoine Pitrou8f328d02012-07-30 00:01:06 +0200852static PyObject *
853bytesio_sizeof(bytesio *self, void *unused)
854{
855 Py_ssize_t res;
856
857 res = sizeof(bytesio);
858 if (self->buf)
859 res += self->buf_size;
860 return PyLong_FromSsize_t(res);
861}
862
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000863static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000864bytesio_traverse(bytesio *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000865{
866 Py_VISIT(self->dict);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000867 return 0;
868}
869
870static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000871bytesio_clear(bytesio *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000872{
873 Py_CLEAR(self->dict);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000874 return 0;
875}
876
877
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000878static PyGetSetDef bytesio_getsetlist[] = {
879 {"closed", (getter)bytesio_get_closed, NULL,
880 "True if the file is closed."},
Benjamin Peterson1fea3212009-04-19 03:15:20 +0000881 {NULL}, /* sentinel */
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000882};
883
884static struct PyMethodDef bytesio_methods[] = {
Antoine Pitrou1d857452012-09-05 20:11:49 +0200885 {"readable", (PyCFunction)return_not_closed, METH_NOARGS, readable_doc},
886 {"seekable", (PyCFunction)return_not_closed, METH_NOARGS, seekable_doc},
887 {"writable", (PyCFunction)return_not_closed, METH_NOARGS, writable_doc},
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000888 {"close", (PyCFunction)bytesio_close, METH_NOARGS, close_doc},
889 {"flush", (PyCFunction)bytesio_flush, METH_NOARGS, flush_doc},
890 {"isatty", (PyCFunction)bytesio_isatty, METH_NOARGS, isatty_doc},
891 {"tell", (PyCFunction)bytesio_tell, METH_NOARGS, tell_doc},
892 {"write", (PyCFunction)bytesio_write, METH_O, write_doc},
893 {"writelines", (PyCFunction)bytesio_writelines, METH_O, writelines_doc},
894 {"read1", (PyCFunction)bytesio_read1, METH_O, read1_doc},
895 {"readinto", (PyCFunction)bytesio_readinto, METH_O, readinto_doc},
896 {"readline", (PyCFunction)bytesio_readline, METH_VARARGS, readline_doc},
897 {"readlines", (PyCFunction)bytesio_readlines, METH_VARARGS, readlines_doc},
898 {"read", (PyCFunction)bytesio_read, METH_VARARGS, read_doc},
Antoine Pitrou972ee132010-09-06 18:48:21 +0000899 {"getbuffer", (PyCFunction)bytesio_getbuffer, METH_NOARGS, getbuffer_doc},
Antoine Pitroud5c3f6c2010-09-02 19:48:07 +0000900 {"getvalue", (PyCFunction)bytesio_getvalue, METH_NOARGS, getval_doc},
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000901 {"seek", (PyCFunction)bytesio_seek, METH_VARARGS, seek_doc},
902 {"truncate", (PyCFunction)bytesio_truncate, METH_VARARGS, truncate_doc},
Alexandre Vassalotticf76e1a2009-07-22 03:24:36 +0000903 {"__getstate__", (PyCFunction)bytesio_getstate, METH_NOARGS, NULL},
904 {"__setstate__", (PyCFunction)bytesio_setstate, METH_O, NULL},
Antoine Pitrou8f328d02012-07-30 00:01:06 +0200905 {"__sizeof__", (PyCFunction)bytesio_sizeof, METH_NOARGS, NULL},
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000906 {NULL, NULL} /* sentinel */
907};
908
909PyDoc_STRVAR(bytesio_doc,
910"BytesIO([buffer]) -> object\n"
911"\n"
912"Create a buffered I/O implementation using an in-memory bytes\n"
913"buffer, ready for reading and writing.");
914
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000915PyTypeObject PyBytesIO_Type = {
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000916 PyVarObject_HEAD_INIT(NULL, 0)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000917 "_io.BytesIO", /*tp_name*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000918 sizeof(bytesio), /*tp_basicsize*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000919 0, /*tp_itemsize*/
920 (destructor)bytesio_dealloc, /*tp_dealloc*/
921 0, /*tp_print*/
922 0, /*tp_getattr*/
923 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000924 0, /*tp_reserved*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000925 0, /*tp_repr*/
926 0, /*tp_as_number*/
927 0, /*tp_as_sequence*/
928 0, /*tp_as_mapping*/
929 0, /*tp_hash*/
930 0, /*tp_call*/
931 0, /*tp_str*/
932 0, /*tp_getattro*/
933 0, /*tp_setattro*/
934 0, /*tp_as_buffer*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000935 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
936 Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000937 bytesio_doc, /*tp_doc*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000938 (traverseproc)bytesio_traverse, /*tp_traverse*/
939 (inquiry)bytesio_clear, /*tp_clear*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000940 0, /*tp_richcompare*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000941 offsetof(bytesio, weakreflist), /*tp_weaklistoffset*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000942 PyObject_SelfIter, /*tp_iter*/
943 (iternextfunc)bytesio_iternext, /*tp_iternext*/
944 bytesio_methods, /*tp_methods*/
945 0, /*tp_members*/
946 bytesio_getsetlist, /*tp_getset*/
947 0, /*tp_base*/
948 0, /*tp_dict*/
949 0, /*tp_descr_get*/
950 0, /*tp_descr_set*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000951 offsetof(bytesio, dict), /*tp_dictoffset*/
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000952 (initproc)bytesio_init, /*tp_init*/
953 0, /*tp_alloc*/
954 bytesio_new, /*tp_new*/
955};
Antoine Pitrou972ee132010-09-06 18:48:21 +0000956
957
958/*
959 * Implementation of the small intermediate object used by getbuffer().
960 * getbuffer() returns a memoryview over this object, which should make it
961 * invisible from Python code.
962 */
963
964static int
965bytesiobuf_getbuffer(bytesiobuf *obj, Py_buffer *view, int flags)
966{
967 int ret;
Antoine Pitrou972ee132010-09-06 18:48:21 +0000968 bytesio *b = (bytesio *) obj->source;
969 if (view == NULL) {
970 b->exports++;
971 return 0;
972 }
Antoine Pitrou972ee132010-09-06 18:48:21 +0000973 ret = PyBuffer_FillInfo(view, (PyObject*)obj, b->buf, b->string_size,
974 0, flags);
975 if (ret >= 0) {
976 b->exports++;
977 }
978 return ret;
979}
980
981static void
982bytesiobuf_releasebuffer(bytesiobuf *obj, Py_buffer *view)
983{
984 bytesio *b = (bytesio *) obj->source;
985 b->exports--;
986}
987
988static int
989bytesiobuf_traverse(bytesiobuf *self, visitproc visit, void *arg)
990{
991 Py_VISIT(self->source);
992 return 0;
993}
994
995static void
996bytesiobuf_dealloc(bytesiobuf *self)
997{
998 Py_CLEAR(self->source);
999 Py_TYPE(self)->tp_free(self);
1000}
1001
1002static PyBufferProcs bytesiobuf_as_buffer = {
1003 (getbufferproc) bytesiobuf_getbuffer,
1004 (releasebufferproc) bytesiobuf_releasebuffer,
1005};
1006
1007PyTypeObject _PyBytesIOBuffer_Type = {
1008 PyVarObject_HEAD_INIT(NULL, 0)
1009 "_io._BytesIOBuffer", /*tp_name*/
1010 sizeof(bytesiobuf), /*tp_basicsize*/
1011 0, /*tp_itemsize*/
1012 (destructor)bytesiobuf_dealloc, /*tp_dealloc*/
1013 0, /*tp_print*/
1014 0, /*tp_getattr*/
1015 0, /*tp_setattr*/
1016 0, /*tp_reserved*/
1017 0, /*tp_repr*/
1018 0, /*tp_as_number*/
1019 0, /*tp_as_sequence*/
1020 0, /*tp_as_mapping*/
1021 0, /*tp_hash*/
1022 0, /*tp_call*/
1023 0, /*tp_str*/
1024 0, /*tp_getattro*/
1025 0, /*tp_setattro*/
1026 &bytesiobuf_as_buffer, /*tp_as_buffer*/
1027 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1028 0, /*tp_doc*/
1029 (traverseproc)bytesiobuf_traverse, /*tp_traverse*/
1030 0, /*tp_clear*/
1031 0, /*tp_richcompare*/
1032 0, /*tp_weaklistoffset*/
1033 0, /*tp_iter*/
1034 0, /*tp_iternext*/
1035 0, /*tp_methods*/
1036 0, /*tp_members*/
1037 0, /*tp_getset*/
1038 0, /*tp_base*/
1039 0, /*tp_dict*/
1040 0, /*tp_descr_get*/
1041 0, /*tp_descr_set*/
1042 0, /*tp_dictoffset*/
1043 0, /*tp_init*/
1044 0, /*tp_alloc*/
1045 0, /*tp_new*/
1046};