Manual py3k backport: [svn r74158] Issue #6218: Make io.BytesIO and io.StringIO pickl...
[python.git] / Modules / _io / bufferedio.c
blob040f3bf9e76d0e272a80815d86d8da01bc68ec8c
1 /*
2 An implementation of Buffered I/O as defined by PEP 3116 - "New I/O"
4 Classes defined here: BufferedIOBase, BufferedReader, BufferedWriter,
5 BufferedRandom.
7 Written by Amaury Forgeot d'Arc and Antoine Pitrou
8 */
10 #define PY_SSIZE_T_CLEAN
11 #include "Python.h"
12 #include "structmember.h"
13 #include "pythread.h"
14 #include "_iomodule.h"
17 * BufferedIOBase class, inherits from IOBase.
19 PyDoc_STRVAR(bufferediobase_doc,
20 "Base class for buffered IO objects.\n"
21 "\n"
22 "The main difference with RawIOBase is that the read() method\n"
23 "supports omitting the size argument, and does not have a default\n"
24 "implementation that defers to readinto().\n"
25 "\n"
26 "In addition, read(), readinto() and write() may raise\n"
27 "BlockingIOError if the underlying raw stream is in non-blocking\n"
28 "mode and not ready; unlike their raw counterparts, they will never\n"
29 "return None.\n"
30 "\n"
31 "A typical implementation should not inherit from a RawIOBase\n"
32 "implementation, but wrap one.\n"
35 static PyObject *
36 bufferediobase_readinto(PyObject *self, PyObject *args)
38 Py_buffer buf;
39 Py_ssize_t len;
40 PyObject *data;
42 if (!PyArg_ParseTuple(args, "w*:readinto", &buf)) {
43 return NULL;
46 data = PyObject_CallMethod(self, "read", "n", buf.len);
47 if (data == NULL)
48 goto error;
50 if (!PyBytes_Check(data)) {
51 Py_DECREF(data);
52 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
53 goto error;
56 len = Py_SIZE(data);
57 memcpy(buf.buf, PyBytes_AS_STRING(data), len);
59 PyBuffer_Release(&buf);
60 Py_DECREF(data);
62 return PyLong_FromSsize_t(len);
64 error:
65 PyBuffer_Release(&buf);
66 return NULL;
69 static PyObject *
70 bufferediobase_unsupported(const char *message)
72 PyErr_SetString(_PyIO_unsupported_operation, message);
73 return NULL;
76 PyDoc_STRVAR(bufferediobase_detach_doc,
77 "Disconnect this buffer from its underlying raw stream and return it.\n"
78 "\n"
79 "After the raw stream has been detached, the buffer is in an unusable\n"
80 "state.\n");
82 static PyObject *
83 bufferediobase_detach(PyObject *self)
85 return bufferediobase_unsupported("detach");
88 PyDoc_STRVAR(bufferediobase_read_doc,
89 "Read and return up to n bytes.\n"
90 "\n"
91 "If the argument is omitted, None, or negative, reads and\n"
92 "returns all data until EOF.\n"
93 "\n"
94 "If the argument is positive, and the underlying raw stream is\n"
95 "not 'interactive', multiple raw reads may be issued to satisfy\n"
96 "the byte count (unless EOF is reached first). But for\n"
97 "interactive raw streams (as well as sockets and pipes), at most\n"
98 "one raw read will be issued, and a short result does not imply\n"
99 "that EOF is imminent.\n"
100 "\n"
101 "Returns an empty bytes object on EOF.\n"
102 "\n"
103 "Returns None if the underlying raw stream was open in non-blocking\n"
104 "mode and no data is available at the moment.\n");
106 static PyObject *
107 bufferediobase_read(PyObject *self, PyObject *args)
109 return bufferediobase_unsupported("read");
112 PyDoc_STRVAR(bufferediobase_read1_doc,
113 "Read and return up to n bytes, with at most one read() call\n"
114 "to the underlying raw stream. A short result does not imply\n"
115 "that EOF is imminent.\n"
116 "\n"
117 "Returns an empty bytes object on EOF.\n");
119 static PyObject *
120 bufferediobase_read1(PyObject *self, PyObject *args)
122 return bufferediobase_unsupported("read1");
125 PyDoc_STRVAR(bufferediobase_write_doc,
126 "Write the given buffer to the IO stream.\n"
127 "\n"
128 "Returns the number of bytes written, which is never less than\n"
129 "len(b).\n"
130 "\n"
131 "Raises BlockingIOError if the buffer is full and the\n"
132 "underlying raw stream cannot accept more data at the moment.\n");
134 static PyObject *
135 bufferediobase_write(PyObject *self, PyObject *args)
137 return bufferediobase_unsupported("write");
141 static PyMethodDef bufferediobase_methods[] = {
142 {"detach", (PyCFunction)bufferediobase_detach, METH_NOARGS, bufferediobase_detach_doc},
143 {"read", bufferediobase_read, METH_VARARGS, bufferediobase_read_doc},
144 {"read1", bufferediobase_read1, METH_VARARGS, bufferediobase_read1_doc},
145 {"readinto", bufferediobase_readinto, METH_VARARGS, NULL},
146 {"write", bufferediobase_write, METH_VARARGS, bufferediobase_write_doc},
147 {NULL, NULL}
150 PyTypeObject PyBufferedIOBase_Type = {
151 PyVarObject_HEAD_INIT(NULL, 0)
152 "_io._BufferedIOBase", /*tp_name*/
153 0, /*tp_basicsize*/
154 0, /*tp_itemsize*/
155 0, /*tp_dealloc*/
156 0, /*tp_print*/
157 0, /*tp_getattr*/
158 0, /*tp_setattr*/
159 0, /*tp_compare */
160 0, /*tp_repr*/
161 0, /*tp_as_number*/
162 0, /*tp_as_sequence*/
163 0, /*tp_as_mapping*/
164 0, /*tp_hash */
165 0, /*tp_call*/
166 0, /*tp_str*/
167 0, /*tp_getattro*/
168 0, /*tp_setattro*/
169 0, /*tp_as_buffer*/
170 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
171 bufferediobase_doc, /* tp_doc */
172 0, /* tp_traverse */
173 0, /* tp_clear */
174 0, /* tp_richcompare */
175 0, /* tp_weaklistoffset */
176 0, /* tp_iter */
177 0, /* tp_iternext */
178 bufferediobase_methods, /* tp_methods */
179 0, /* tp_members */
180 0, /* tp_getset */
181 &PyIOBase_Type, /* tp_base */
182 0, /* tp_dict */
183 0, /* tp_descr_get */
184 0, /* tp_descr_set */
185 0, /* tp_dictoffset */
186 0, /* tp_init */
187 0, /* tp_alloc */
188 0, /* tp_new */
192 typedef struct {
193 PyObject_HEAD
195 PyObject *raw;
196 int ok; /* Initialized? */
197 int detached;
198 int readable;
199 int writable;
201 /* True if this is a vanilla Buffered object (rather than a user derived
202 class) *and* the raw stream is a vanilla FileIO object. */
203 int fast_closed_checks;
205 /* Absolute position inside the raw stream (-1 if unknown). */
206 Py_off_t abs_pos;
208 /* A static buffer of size `buffer_size` */
209 char *buffer;
210 /* Current logical position in the buffer. */
211 Py_off_t pos;
212 /* Position of the raw stream in the buffer. */
213 Py_off_t raw_pos;
215 /* Just after the last buffered byte in the buffer, or -1 if the buffer
216 isn't ready for reading. */
217 Py_off_t read_end;
219 /* Just after the last byte actually written */
220 Py_off_t write_pos;
221 /* Just after the last byte waiting to be written, or -1 if the buffer
222 isn't ready for writing. */
223 Py_off_t write_end;
225 #ifdef WITH_THREAD
226 PyThread_type_lock lock;
227 #endif
229 Py_ssize_t buffer_size;
230 Py_ssize_t buffer_mask;
232 PyObject *dict;
233 PyObject *weakreflist;
234 } buffered;
237 Implementation notes:
239 * BufferedReader, BufferedWriter and BufferedRandom try to share most
240 methods (this is helped by the members `readable` and `writable`, which
241 are initialized in the respective constructors)
242 * They also share a single buffer for reading and writing. This enables
243 interleaved reads and writes without flushing. It also makes the logic
244 a bit trickier to get right.
245 * The absolute position of the raw stream is cached, if possible, in the
246 `abs_pos` member. It must be updated every time an operation is done
247 on the raw stream. If not sure, it can be reinitialized by calling
248 _buffered_raw_tell(), which queries the raw stream (_buffered_raw_seek()
249 also does it). To read it, use RAW_TELL().
250 * Three helpers, _bufferedreader_raw_read, _bufferedwriter_raw_write and
251 _bufferedwriter_flush_unlocked do a lot of useful housekeeping.
253 NOTE: we should try to maintain block alignment of reads and writes to the
254 raw stream (according to the buffer size), but for now it is only done
255 in read() and friends.
259 /* These macros protect the buffered object against concurrent operations. */
261 #ifdef WITH_THREAD
262 #define ENTER_BUFFERED(self) \
263 Py_BEGIN_ALLOW_THREADS \
264 PyThread_acquire_lock(self->lock, 1); \
265 Py_END_ALLOW_THREADS
267 #define LEAVE_BUFFERED(self) \
268 PyThread_release_lock(self->lock);
269 #else
270 #define ENTER_BUFFERED(self)
271 #define LEAVE_BUFFERED(self)
272 #endif
274 #define CHECK_INITIALIZED(self) \
275 if (self->ok <= 0) { \
276 if (self->detached) { \
277 PyErr_SetString(PyExc_ValueError, \
278 "raw stream has been detached"); \
279 } else { \
280 PyErr_SetString(PyExc_ValueError, \
281 "I/O operation on uninitialized object"); \
283 return NULL; \
286 #define CHECK_INITIALIZED_INT(self) \
287 if (self->ok <= 0) { \
288 if (self->detached) { \
289 PyErr_SetString(PyExc_ValueError, \
290 "raw stream has been detached"); \
291 } else { \
292 PyErr_SetString(PyExc_ValueError, \
293 "I/O operation on uninitialized object"); \
295 return -1; \
298 #define IS_CLOSED(self) \
299 (self->fast_closed_checks \
300 ? _PyFileIO_closed(self->raw) \
301 : buffered_closed(self))
303 #define CHECK_CLOSED(self, error_msg) \
304 if (IS_CLOSED(self)) { \
305 PyErr_SetString(PyExc_ValueError, error_msg); \
306 return NULL; \
310 #define VALID_READ_BUFFER(self) \
311 (self->readable && self->read_end != -1)
313 #define VALID_WRITE_BUFFER(self) \
314 (self->writable && self->write_end != -1)
316 #define ADJUST_POSITION(self, _new_pos) \
317 do { \
318 self->pos = _new_pos; \
319 if (VALID_READ_BUFFER(self) && self->read_end < self->pos) \
320 self->read_end = self->pos; \
321 } while(0)
323 #define READAHEAD(self) \
324 ((self->readable && VALID_READ_BUFFER(self)) \
325 ? (self->read_end - self->pos) : 0)
327 #define RAW_OFFSET(self) \
328 (((VALID_READ_BUFFER(self) || VALID_WRITE_BUFFER(self)) \
329 && self->raw_pos >= 0) ? self->raw_pos - self->pos : 0)
331 #define RAW_TELL(self) \
332 (self->abs_pos != -1 ? self->abs_pos : _buffered_raw_tell(self))
334 #define MINUS_LAST_BLOCK(self, size) \
335 (self->buffer_mask ? \
336 (size & ~self->buffer_mask) : \
337 (self->buffer_size * (size / self->buffer_size)))
340 static void
341 buffered_dealloc(buffered *self)
343 if (self->ok && _PyIOBase_finalize((PyObject *) self) < 0)
344 return;
345 _PyObject_GC_UNTRACK(self);
346 self->ok = 0;
347 if (self->weakreflist != NULL)
348 PyObject_ClearWeakRefs((PyObject *)self);
349 Py_CLEAR(self->raw);
350 if (self->buffer) {
351 PyMem_Free(self->buffer);
352 self->buffer = NULL;
354 #ifdef WITH_THREAD
355 if (self->lock) {
356 PyThread_free_lock(self->lock);
357 self->lock = NULL;
359 #endif
360 Py_CLEAR(self->dict);
361 Py_TYPE(self)->tp_free((PyObject *)self);
364 static int
365 buffered_traverse(buffered *self, visitproc visit, void *arg)
367 Py_VISIT(self->raw);
368 Py_VISIT(self->dict);
369 return 0;
372 static int
373 buffered_clear(buffered *self)
375 if (self->ok && _PyIOBase_finalize((PyObject *) self) < 0)
376 return -1;
377 self->ok = 0;
378 Py_CLEAR(self->raw);
379 Py_CLEAR(self->dict);
380 return 0;
384 * _BufferedIOMixin methods
385 * This is not a class, just a collection of methods that will be reused
386 * by BufferedReader and BufferedWriter
389 /* Flush and close */
391 static PyObject *
392 buffered_simple_flush(buffered *self, PyObject *args)
394 CHECK_INITIALIZED(self)
395 return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_flush, NULL);
398 static int
399 buffered_closed(buffered *self)
401 int closed;
402 PyObject *res;
403 CHECK_INITIALIZED_INT(self)
404 res = PyObject_GetAttr(self->raw, _PyIO_str_closed);
405 if (res == NULL)
406 return -1;
407 closed = PyObject_IsTrue(res);
408 Py_DECREF(res);
409 return closed;
412 static PyObject *
413 buffered_closed_get(buffered *self, void *context)
415 CHECK_INITIALIZED(self)
416 return PyObject_GetAttr(self->raw, _PyIO_str_closed);
419 static PyObject *
420 buffered_close(buffered *self, PyObject *args)
422 PyObject *res = NULL;
423 int r;
425 CHECK_INITIALIZED(self)
426 ENTER_BUFFERED(self)
428 r = buffered_closed(self);
429 if (r < 0)
430 goto end;
431 if (r > 0) {
432 res = Py_None;
433 Py_INCREF(res);
434 goto end;
436 /* flush() will most probably re-take the lock, so drop it first */
437 LEAVE_BUFFERED(self)
438 res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL);
439 ENTER_BUFFERED(self)
440 if (res == NULL) {
441 /* If flush() fails, just give up */
442 if (PyErr_ExceptionMatches(PyExc_IOError))
443 PyErr_Clear();
444 else
445 goto end;
447 Py_XDECREF(res);
449 res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_close, NULL);
451 end:
452 LEAVE_BUFFERED(self)
453 return res;
456 /* detach */
458 static PyObject *
459 buffered_detach(buffered *self, PyObject *args)
461 PyObject *raw, *res;
462 CHECK_INITIALIZED(self)
463 res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL);
464 if (res == NULL)
465 return NULL;
466 Py_DECREF(res);
467 raw = self->raw;
468 self->raw = NULL;
469 self->detached = 1;
470 self->ok = 0;
471 return raw;
474 /* Inquiries */
476 static PyObject *
477 buffered_seekable(buffered *self, PyObject *args)
479 CHECK_INITIALIZED(self)
480 return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_seekable, NULL);
483 static PyObject *
484 buffered_readable(buffered *self, PyObject *args)
486 CHECK_INITIALIZED(self)
487 return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_readable, NULL);
490 static PyObject *
491 buffered_writable(buffered *self, PyObject *args)
493 CHECK_INITIALIZED(self)
494 return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_writable, NULL);
497 static PyObject *
498 buffered_name_get(buffered *self, void *context)
500 CHECK_INITIALIZED(self)
501 return PyObject_GetAttrString(self->raw, "name");
504 static PyObject *
505 buffered_mode_get(buffered *self, void *context)
507 CHECK_INITIALIZED(self)
508 return PyObject_GetAttrString(self->raw, "mode");
511 /* Lower-level APIs */
513 static PyObject *
514 buffered_fileno(buffered *self, PyObject *args)
516 CHECK_INITIALIZED(self)
517 return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_fileno, NULL);
520 static PyObject *
521 buffered_isatty(buffered *self, PyObject *args)
523 CHECK_INITIALIZED(self)
524 return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_isatty, NULL);
528 /* Forward decls */
529 static PyObject *
530 _bufferedwriter_flush_unlocked(buffered *, int);
531 static Py_ssize_t
532 _bufferedreader_fill_buffer(buffered *self);
533 static void
534 _bufferedreader_reset_buf(buffered *self);
535 static void
536 _bufferedwriter_reset_buf(buffered *self);
537 static PyObject *
538 _bufferedreader_peek_unlocked(buffered *self, Py_ssize_t);
539 static PyObject *
540 _bufferedreader_read_all(buffered *self);
541 static PyObject *
542 _bufferedreader_read_fast(buffered *self, Py_ssize_t);
543 static PyObject *
544 _bufferedreader_read_generic(buffered *self, Py_ssize_t);
548 * Helpers
551 /* Returns the address of the `written` member if a BlockingIOError was
552 raised, NULL otherwise. The error is always re-raised. */
553 static Py_ssize_t *
554 _buffered_check_blocking_error(void)
556 PyObject *t, *v, *tb;
557 PyBlockingIOErrorObject *err;
559 PyErr_Fetch(&t, &v, &tb);
560 if (v == NULL || !PyErr_GivenExceptionMatches(v, PyExc_BlockingIOError)) {
561 PyErr_Restore(t, v, tb);
562 return NULL;
564 err = (PyBlockingIOErrorObject *) v;
565 /* TODO: sanity check (err->written >= 0) */
566 PyErr_Restore(t, v, tb);
567 return &err->written;
570 static Py_off_t
571 _buffered_raw_tell(buffered *self)
573 Py_off_t n;
574 PyObject *res;
575 res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_tell, NULL);
576 if (res == NULL)
577 return -1;
578 n = PyNumber_AsOff_t(res, PyExc_ValueError);
579 Py_DECREF(res);
580 if (n < 0) {
581 if (!PyErr_Occurred())
582 PyErr_Format(PyExc_IOError,
583 "Raw stream returned invalid position %zd", n);
584 return -1;
586 self->abs_pos = n;
587 return n;
590 static Py_off_t
591 _buffered_raw_seek(buffered *self, Py_off_t target, int whence)
593 PyObject *res, *posobj, *whenceobj;
594 Py_off_t n;
596 posobj = PyLong_FromOff_t(target);
597 if (posobj == NULL)
598 return -1;
599 whenceobj = PyLong_FromLong(whence);
600 if (whenceobj == NULL) {
601 Py_DECREF(posobj);
602 return -1;
604 res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_seek,
605 posobj, whenceobj, NULL);
606 Py_DECREF(posobj);
607 Py_DECREF(whenceobj);
608 if (res == NULL)
609 return -1;
610 n = PyNumber_AsOff_t(res, PyExc_ValueError);
611 Py_DECREF(res);
612 if (n < 0) {
613 if (!PyErr_Occurred())
614 PyErr_Format(PyExc_IOError,
615 "Raw stream returned invalid position %zd", n);
616 return -1;
618 self->abs_pos = n;
619 return n;
622 static int
623 _buffered_init(buffered *self)
625 Py_ssize_t n;
626 if (self->buffer_size <= 0) {
627 PyErr_SetString(PyExc_ValueError,
628 "buffer size must be strictly positive");
629 return -1;
631 if (self->buffer)
632 PyMem_Free(self->buffer);
633 self->buffer = PyMem_Malloc(self->buffer_size);
634 if (self->buffer == NULL) {
635 PyErr_NoMemory();
636 return -1;
638 #ifdef WITH_THREAD
639 self->lock = PyThread_allocate_lock();
640 if (self->lock == NULL) {
641 PyErr_SetString(PyExc_RuntimeError, "can't allocate read lock");
642 return -1;
644 #endif
645 /* Find out whether buffer_size is a power of 2 */
646 /* XXX is this optimization useful? */
647 for (n = self->buffer_size - 1; n & 1; n >>= 1)
649 if (n == 0)
650 self->buffer_mask = self->buffer_size - 1;
651 else
652 self->buffer_mask = 0;
653 if (_buffered_raw_tell(self) == -1)
654 PyErr_Clear();
655 return 0;
659 * Shared methods and wrappers
662 static PyObject *
663 buffered_flush(buffered *self, PyObject *args)
665 PyObject *res;
667 CHECK_INITIALIZED(self)
668 CHECK_CLOSED(self, "flush of closed file")
670 ENTER_BUFFERED(self)
671 res = _bufferedwriter_flush_unlocked(self, 0);
672 if (res != NULL && self->readable) {
673 /* Rewind the raw stream so that its position corresponds to
674 the current logical position. */
675 Py_off_t n;
676 n = _buffered_raw_seek(self, -RAW_OFFSET(self), 1);
677 if (n == -1)
678 Py_CLEAR(res);
679 _bufferedreader_reset_buf(self);
681 LEAVE_BUFFERED(self)
683 return res;
686 static PyObject *
687 buffered_peek(buffered *self, PyObject *args)
689 Py_ssize_t n = 0;
690 PyObject *res = NULL;
692 CHECK_INITIALIZED(self)
693 if (!PyArg_ParseTuple(args, "|n:peek", &n)) {
694 return NULL;
697 ENTER_BUFFERED(self)
699 if (self->writable) {
700 res = _bufferedwriter_flush_unlocked(self, 1);
701 if (res == NULL)
702 goto end;
703 Py_CLEAR(res);
705 res = _bufferedreader_peek_unlocked(self, n);
707 end:
708 LEAVE_BUFFERED(self)
709 return res;
712 static PyObject *
713 buffered_read(buffered *self, PyObject *args)
715 Py_ssize_t n = -1;
716 PyObject *res;
718 CHECK_INITIALIZED(self)
719 if (!PyArg_ParseTuple(args, "|n:read", &n)) {
720 return NULL;
722 if (n < -1) {
723 PyErr_SetString(PyExc_ValueError,
724 "read length must be positive or -1");
725 return NULL;
728 CHECK_CLOSED(self, "read of closed file")
730 if (n == -1) {
731 /* The number of bytes is unspecified, read until the end of stream */
732 ENTER_BUFFERED(self)
733 res = _bufferedreader_read_all(self);
734 LEAVE_BUFFERED(self)
736 else {
737 res = _bufferedreader_read_fast(self, n);
738 if (res == Py_None) {
739 Py_DECREF(res);
740 ENTER_BUFFERED(self)
741 res = _bufferedreader_read_generic(self, n);
742 LEAVE_BUFFERED(self)
746 return res;
749 static PyObject *
750 buffered_read1(buffered *self, PyObject *args)
752 Py_ssize_t n, have, r;
753 PyObject *res = NULL;
755 CHECK_INITIALIZED(self)
756 if (!PyArg_ParseTuple(args, "n:read1", &n)) {
757 return NULL;
760 if (n < 0) {
761 PyErr_SetString(PyExc_ValueError,
762 "read length must be positive");
763 return NULL;
765 if (n == 0)
766 return PyBytes_FromStringAndSize(NULL, 0);
768 ENTER_BUFFERED(self)
770 if (self->writable) {
771 res = _bufferedwriter_flush_unlocked(self, 1);
772 if (res == NULL)
773 goto end;
774 Py_CLEAR(res);
777 /* Return up to n bytes. If at least one byte is buffered, we
778 only return buffered bytes. Otherwise, we do one raw read. */
780 /* XXX: this mimicks the io.py implementation but is probably wrong.
781 If we need to read from the raw stream, then we could actually read
782 all `n` bytes asked by the caller (and possibly more, so as to fill
783 our buffer for the next reads). */
785 have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
786 if (have > 0) {
787 if (n > have)
788 n = have;
789 res = PyBytes_FromStringAndSize(self->buffer + self->pos, n);
790 if (res == NULL)
791 goto end;
792 self->pos += n;
793 goto end;
796 /* Fill the buffer from the raw stream, and copy it to the result. */
797 _bufferedreader_reset_buf(self);
798 r = _bufferedreader_fill_buffer(self);
799 if (r == -1)
800 goto end;
801 if (r == -2)
802 r = 0;
803 if (n > r)
804 n = r;
805 res = PyBytes_FromStringAndSize(self->buffer, n);
806 if (res == NULL)
807 goto end;
808 self->pos = n;
810 end:
811 LEAVE_BUFFERED(self)
812 return res;
815 static PyObject *
816 buffered_readinto(buffered *self, PyObject *args)
818 PyObject *res = NULL;
820 CHECK_INITIALIZED(self)
822 /* TODO: use raw.readinto() instead! */
823 if (self->writable) {
824 ENTER_BUFFERED(self)
825 res = _bufferedwriter_flush_unlocked(self, 0);
826 LEAVE_BUFFERED(self)
827 if (res == NULL)
828 goto end;
829 Py_DECREF(res);
831 res = bufferediobase_readinto((PyObject *)self, args);
833 end:
834 return res;
837 static PyObject *
838 _buffered_readline(buffered *self, Py_ssize_t limit)
840 PyObject *res = NULL;
841 PyObject *chunks = NULL;
842 Py_ssize_t n, written = 0;
843 const char *start, *s, *end;
845 CHECK_CLOSED(self, "readline of closed file")
847 /* First, try to find a line in the buffer. This can run unlocked because
848 the calls to the C API are simple enough that they can't trigger
849 any thread switch. */
850 n = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
851 if (limit >= 0 && n > limit)
852 n = limit;
853 start = self->buffer + self->pos;
854 s = memchr(start, '\n', n);
855 if (s != NULL) {
856 res = PyBytes_FromStringAndSize(start, s - start + 1);
857 if (res != NULL)
858 self->pos += s - start + 1;
859 goto end_unlocked;
861 if (n == limit) {
862 res = PyBytes_FromStringAndSize(start, n);
863 if (res != NULL)
864 self->pos += n;
865 goto end_unlocked;
868 ENTER_BUFFERED(self)
870 /* Now we try to get some more from the raw stream */
871 if (self->writable) {
872 res = _bufferedwriter_flush_unlocked(self, 1);
873 if (res == NULL)
874 goto end;
875 Py_CLEAR(res);
877 chunks = PyList_New(0);
878 if (chunks == NULL)
879 goto end;
880 if (n > 0) {
881 res = PyBytes_FromStringAndSize(start, n);
882 if (res == NULL)
883 goto end;
884 if (PyList_Append(chunks, res) < 0) {
885 Py_CLEAR(res);
886 goto end;
888 Py_CLEAR(res);
889 written += n;
890 if (limit >= 0)
891 limit -= n;
894 for (;;) {
895 _bufferedreader_reset_buf(self);
896 n = _bufferedreader_fill_buffer(self);
897 if (n == -1)
898 goto end;
899 if (n <= 0)
900 break;
901 if (limit >= 0 && n > limit)
902 n = limit;
903 start = self->buffer;
904 end = start + n;
905 s = start;
906 while (s < end) {
907 if (*s++ == '\n') {
908 res = PyBytes_FromStringAndSize(start, s - start);
909 if (res == NULL)
910 goto end;
911 self->pos = s - start;
912 goto found;
915 res = PyBytes_FromStringAndSize(start, n);
916 if (res == NULL)
917 goto end;
918 if (n == limit) {
919 self->pos = n;
920 break;
922 if (PyList_Append(chunks, res) < 0) {
923 Py_CLEAR(res);
924 goto end;
926 Py_CLEAR(res);
927 written += n;
928 if (limit >= 0)
929 limit -= n;
931 found:
932 if (res != NULL && PyList_Append(chunks, res) < 0) {
933 Py_CLEAR(res);
934 goto end;
936 Py_CLEAR(res);
937 res = _PyBytes_Join(_PyIO_empty_bytes, chunks);
939 end:
940 LEAVE_BUFFERED(self)
941 end_unlocked:
942 Py_XDECREF(chunks);
943 return res;
946 static PyObject *
947 buffered_readline(buffered *self, PyObject *args)
949 PyObject *limitobj = NULL;
950 Py_ssize_t limit = -1;
952 CHECK_INITIALIZED(self)
954 if (!PyArg_ParseTuple(args, "|O:readline", &limitobj)) {
955 return NULL;
957 if (limitobj) {
958 if (!PyNumber_Check(limitobj)) {
959 PyErr_Format(PyExc_TypeError,
960 "integer argument expected, got '%.200s'",
961 Py_TYPE(limitobj)->tp_name);
962 return NULL;
964 limit = PyNumber_AsSsize_t(limitobj, PyExc_OverflowError);
965 if (limit == -1 && PyErr_Occurred())
966 return NULL;
968 return _buffered_readline(self, limit);
972 static PyObject *
973 buffered_tell(buffered *self, PyObject *args)
975 Py_off_t pos;
977 CHECK_INITIALIZED(self)
978 pos = _buffered_raw_tell(self);
979 if (pos == -1)
980 return NULL;
981 pos -= RAW_OFFSET(self);
982 /* TODO: sanity check (pos >= 0) */
983 return PyLong_FromOff_t(pos);
986 static PyObject *
987 buffered_seek(buffered *self, PyObject *args)
989 Py_off_t target, n;
990 int whence = 0;
991 PyObject *targetobj, *res = NULL;
993 CHECK_INITIALIZED(self)
994 if (!PyArg_ParseTuple(args, "O|i:seek", &targetobj, &whence)) {
995 return NULL;
997 if (whence < 0 || whence > 2) {
998 PyErr_Format(PyExc_ValueError,
999 "whence must be between 0 and 2, not %d", whence);
1000 return NULL;
1003 CHECK_CLOSED(self, "seek of closed file")
1005 target = PyNumber_AsOff_t(targetobj, PyExc_ValueError);
1006 if (target == -1 && PyErr_Occurred())
1007 return NULL;
1009 if (whence != 2 && self->readable) {
1010 Py_off_t current, avail;
1011 /* Check if seeking leaves us inside the current buffer,
1012 so as to return quickly if possible. Also, we needn't take the
1013 lock in this fast path.
1014 Don't know how to do that when whence == 2, though. */
1015 /* NOTE: RAW_TELL() can release the GIL but the object is in a stable
1016 state at this point. */
1017 current = RAW_TELL(self);
1018 avail = READAHEAD(self);
1019 if (avail > 0) {
1020 Py_off_t offset;
1021 if (whence == 0)
1022 offset = target - (current - RAW_OFFSET(self));
1023 else
1024 offset = target;
1025 if (offset >= -self->pos && offset <= avail) {
1026 self->pos += offset;
1027 return PyLong_FromOff_t(current - avail + offset);
1032 ENTER_BUFFERED(self)
1034 /* Fallback: invoke raw seek() method and clear buffer */
1035 if (self->writable) {
1036 res = _bufferedwriter_flush_unlocked(self, 0);
1037 if (res == NULL)
1038 goto end;
1039 Py_CLEAR(res);
1040 _bufferedwriter_reset_buf(self);
1043 /* TODO: align on block boundary and read buffer if needed? */
1044 if (whence == 1)
1045 target -= RAW_OFFSET(self);
1046 n = _buffered_raw_seek(self, target, whence);
1047 if (n == -1)
1048 goto end;
1049 self->raw_pos = -1;
1050 res = PyLong_FromOff_t(n);
1051 if (res != NULL && self->readable)
1052 _bufferedreader_reset_buf(self);
1054 end:
1055 LEAVE_BUFFERED(self)
1056 return res;
1059 static PyObject *
1060 buffered_truncate(buffered *self, PyObject *args)
1062 PyObject *pos = Py_None;
1063 PyObject *res = NULL;
1065 CHECK_INITIALIZED(self)
1066 if (!PyArg_ParseTuple(args, "|O:truncate", &pos)) {
1067 return NULL;
1070 ENTER_BUFFERED(self)
1072 if (self->writable) {
1073 res = _bufferedwriter_flush_unlocked(self, 0);
1074 if (res == NULL)
1075 goto end;
1076 Py_CLEAR(res);
1078 if (self->readable) {
1079 if (pos == Py_None) {
1080 /* Rewind the raw stream so that its position corresponds to
1081 the current logical position. */
1082 if (_buffered_raw_seek(self, -RAW_OFFSET(self), 1) == -1)
1083 goto end;
1085 _bufferedreader_reset_buf(self);
1087 res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_truncate, pos, NULL);
1088 if (res == NULL)
1089 goto end;
1090 /* Reset cached position */
1091 if (_buffered_raw_tell(self) == -1)
1092 PyErr_Clear();
1094 end:
1095 LEAVE_BUFFERED(self)
1096 return res;
1099 static PyObject *
1100 buffered_iternext(buffered *self)
1102 PyObject *line;
1103 PyTypeObject *tp;
1105 CHECK_INITIALIZED(self);
1107 tp = Py_TYPE(self);
1108 if (tp == &PyBufferedReader_Type ||
1109 tp == &PyBufferedRandom_Type) {
1110 /* Skip method call overhead for speed */
1111 line = _buffered_readline(self, -1);
1113 else {
1114 line = PyObject_CallMethodObjArgs((PyObject *)self,
1115 _PyIO_str_readline, NULL);
1116 if (line && !PyBytes_Check(line)) {
1117 PyErr_Format(PyExc_IOError,
1118 "readline() should have returned a bytes object, "
1119 "not '%.200s'", Py_TYPE(line)->tp_name);
1120 Py_DECREF(line);
1121 return NULL;
1125 if (line == NULL)
1126 return NULL;
1128 if (PyBytes_GET_SIZE(line) == 0) {
1129 /* Reached EOF or would have blocked */
1130 Py_DECREF(line);
1131 return NULL;
1134 return line;
1137 static PyObject *
1138 buffered_repr(buffered *self)
1140 PyObject *nameobj, *res;
1142 nameobj = PyObject_GetAttrString((PyObject *) self, "name");
1143 if (nameobj == NULL) {
1144 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1145 PyErr_Clear();
1146 else
1147 return NULL;
1148 res = PyString_FromFormat("<%s>", Py_TYPE(self)->tp_name);
1150 else {
1151 PyObject *repr = PyObject_Repr(nameobj);
1152 Py_DECREF(nameobj);
1153 if (repr == NULL)
1154 return NULL;
1155 res = PyString_FromFormat("<%s name=%s>",
1156 Py_TYPE(self)->tp_name,
1157 PyString_AS_STRING(repr));
1158 Py_DECREF(repr);
1160 return res;
1164 * class BufferedReader
1167 PyDoc_STRVAR(bufferedreader_doc,
1168 "Create a new buffered reader using the given readable raw IO object.");
1170 static void _bufferedreader_reset_buf(buffered *self)
1172 self->read_end = -1;
1175 static int
1176 bufferedreader_init(buffered *self, PyObject *args, PyObject *kwds)
1178 char *kwlist[] = {"raw", "buffer_size", NULL};
1179 Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
1180 PyObject *raw;
1182 self->ok = 0;
1183 self->detached = 0;
1185 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:BufferedReader", kwlist,
1186 &raw, &buffer_size)) {
1187 return -1;
1190 if (_PyIOBase_check_readable(raw, Py_True) == NULL)
1191 return -1;
1193 Py_CLEAR(self->raw);
1194 Py_INCREF(raw);
1195 self->raw = raw;
1196 self->buffer_size = buffer_size;
1197 self->readable = 1;
1198 self->writable = 0;
1200 if (_buffered_init(self) < 0)
1201 return -1;
1202 _bufferedreader_reset_buf(self);
1204 self->fast_closed_checks = (Py_TYPE(self) == &PyBufferedReader_Type &&
1205 Py_TYPE(raw) == &PyFileIO_Type);
1207 self->ok = 1;
1208 return 0;
1211 static Py_ssize_t
1212 _bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len)
1214 Py_buffer buf;
1215 PyObject *memobj, *res;
1216 Py_ssize_t n;
1217 /* NOTE: the buffer needn't be released as its object is NULL. */
1218 if (PyBuffer_FillInfo(&buf, NULL, start, len, 0, PyBUF_CONTIG) == -1)
1219 return -1;
1220 memobj = PyMemoryView_FromBuffer(&buf);
1221 if (memobj == NULL)
1222 return -1;
1223 res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_readinto, memobj, NULL);
1224 Py_DECREF(memobj);
1225 if (res == NULL)
1226 return -1;
1227 if (res == Py_None) {
1228 /* Non-blocking stream would have blocked. Special return code! */
1229 Py_DECREF(res);
1230 return -2;
1232 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
1233 Py_DECREF(res);
1234 if (n < 0 || n > len) {
1235 PyErr_Format(PyExc_IOError,
1236 "raw readinto() returned invalid length %zd "
1237 "(should have been between 0 and %zd)", n, len);
1238 return -1;
1240 if (n > 0 && self->abs_pos != -1)
1241 self->abs_pos += n;
1242 return n;
1245 static Py_ssize_t
1246 _bufferedreader_fill_buffer(buffered *self)
1248 Py_ssize_t start, len, n;
1249 if (VALID_READ_BUFFER(self))
1250 start = Py_SAFE_DOWNCAST(self->read_end, Py_off_t, Py_ssize_t);
1251 else
1252 start = 0;
1253 len = self->buffer_size - start;
1254 n = _bufferedreader_raw_read(self, self->buffer + start, len);
1255 if (n <= 0)
1256 return n;
1257 self->read_end = start + n;
1258 self->raw_pos = start + n;
1259 return n;
1262 static PyObject *
1263 _bufferedreader_read_all(buffered *self)
1265 Py_ssize_t current_size;
1266 PyObject *res, *data = NULL;
1267 PyObject *chunks = PyList_New(0);
1269 if (chunks == NULL)
1270 return NULL;
1272 /* First copy what we have in the current buffer. */
1273 current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
1274 if (current_size) {
1275 data = PyBytes_FromStringAndSize(
1276 self->buffer + self->pos, current_size);
1277 if (data == NULL) {
1278 Py_DECREF(chunks);
1279 return NULL;
1282 _bufferedreader_reset_buf(self);
1283 /* We're going past the buffer's bounds, flush it */
1284 if (self->writable) {
1285 res = _bufferedwriter_flush_unlocked(self, 1);
1286 if (res == NULL) {
1287 Py_DECREF(chunks);
1288 return NULL;
1290 Py_CLEAR(res);
1292 while (1) {
1293 if (data) {
1294 if (PyList_Append(chunks, data) < 0) {
1295 Py_DECREF(data);
1296 Py_DECREF(chunks);
1297 return NULL;
1299 Py_DECREF(data);
1302 /* Read until EOF or until read() would block. */
1303 data = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_read, NULL);
1304 if (data == NULL) {
1305 Py_DECREF(chunks);
1306 return NULL;
1308 if (data != Py_None && !PyBytes_Check(data)) {
1309 Py_DECREF(data);
1310 Py_DECREF(chunks);
1311 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
1312 return NULL;
1314 if (data == Py_None || PyBytes_GET_SIZE(data) == 0) {
1315 if (current_size == 0) {
1316 Py_DECREF(chunks);
1317 return data;
1319 else {
1320 res = _PyBytes_Join(_PyIO_empty_bytes, chunks);
1321 Py_DECREF(data);
1322 Py_DECREF(chunks);
1323 return res;
1326 current_size += PyBytes_GET_SIZE(data);
1327 if (self->abs_pos != -1)
1328 self->abs_pos += PyBytes_GET_SIZE(data);
1332 /* Read n bytes from the buffer if it can, otherwise return None.
1333 This function is simple enough that it can run unlocked. */
1334 static PyObject *
1335 _bufferedreader_read_fast(buffered *self, Py_ssize_t n)
1337 Py_ssize_t current_size;
1339 current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
1340 if (n <= current_size) {
1341 /* Fast path: the data to read is fully buffered. */
1342 PyObject *res = PyBytes_FromStringAndSize(self->buffer + self->pos, n);
1343 if (res != NULL)
1344 self->pos += n;
1345 return res;
1347 Py_RETURN_NONE;
1350 /* Generic read function: read from the stream until enough bytes are read,
1351 * or until an EOF occurs or until read() would block.
1353 static PyObject *
1354 _bufferedreader_read_generic(buffered *self, Py_ssize_t n)
1356 PyObject *res = NULL;
1357 Py_ssize_t current_size, remaining, written;
1358 char *out;
1360 current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
1361 if (n <= current_size)
1362 return _bufferedreader_read_fast(self, n);
1364 res = PyBytes_FromStringAndSize(NULL, n);
1365 if (res == NULL)
1366 goto error;
1367 out = PyBytes_AS_STRING(res);
1368 remaining = n;
1369 written = 0;
1370 if (current_size > 0) {
1371 memcpy(out, self->buffer + self->pos, current_size);
1372 remaining -= current_size;
1373 written += current_size;
1375 _bufferedreader_reset_buf(self);
1376 while (remaining > 0) {
1377 /* We want to read a whole block at the end into buffer.
1378 If we had readv() we could do this in one pass. */
1379 Py_ssize_t r = MINUS_LAST_BLOCK(self, remaining);
1380 if (r == 0)
1381 break;
1382 r = _bufferedreader_raw_read(self, out + written, r);
1383 if (r == -1)
1384 goto error;
1385 if (r == 0 || r == -2) {
1386 /* EOF occurred or read() would block. */
1387 if (r == 0 || written > 0) {
1388 if (_PyBytes_Resize(&res, written))
1389 goto error;
1390 return res;
1392 Py_DECREF(res);
1393 Py_INCREF(Py_None);
1394 return Py_None;
1396 remaining -= r;
1397 written += r;
1399 assert(remaining <= self->buffer_size);
1400 self->pos = 0;
1401 self->raw_pos = 0;
1402 self->read_end = 0;
1403 while (self->read_end < self->buffer_size) {
1404 Py_ssize_t r = _bufferedreader_fill_buffer(self);
1405 if (r == -1)
1406 goto error;
1407 if (r == 0 || r == -2) {
1408 /* EOF occurred or read() would block. */
1409 if (r == 0 || written > 0) {
1410 if (_PyBytes_Resize(&res, written))
1411 goto error;
1412 return res;
1414 Py_DECREF(res);
1415 Py_INCREF(Py_None);
1416 return Py_None;
1418 if (remaining > r) {
1419 memcpy(out + written, self->buffer + self->pos, r);
1420 written += r;
1421 self->pos += r;
1422 remaining -= r;
1424 else if (remaining > 0) {
1425 memcpy(out + written, self->buffer + self->pos, remaining);
1426 written += remaining;
1427 self->pos += remaining;
1428 remaining = 0;
1430 if (remaining == 0)
1431 break;
1434 return res;
1436 error:
1437 Py_XDECREF(res);
1438 return NULL;
1441 static PyObject *
1442 _bufferedreader_peek_unlocked(buffered *self, Py_ssize_t n)
1444 Py_ssize_t have, r;
1446 have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
1447 /* Constraints:
1448 1. we don't want to advance the file position.
1449 2. we don't want to lose block alignment, so we can't shift the buffer
1450 to make some place.
1451 Therefore, we either return `have` bytes (if > 0), or a full buffer.
1453 if (have > 0) {
1454 return PyBytes_FromStringAndSize(self->buffer + self->pos, have);
1457 /* Fill the buffer from the raw stream, and copy it to the result. */
1458 _bufferedreader_reset_buf(self);
1459 r = _bufferedreader_fill_buffer(self);
1460 if (r == -1)
1461 return NULL;
1462 if (r == -2)
1463 r = 0;
1464 self->pos = 0;
1465 return PyBytes_FromStringAndSize(self->buffer, r);
1468 static PyMethodDef bufferedreader_methods[] = {
1469 /* BufferedIOMixin methods */
1470 {"detach", (PyCFunction)buffered_detach, METH_NOARGS},
1471 {"flush", (PyCFunction)buffered_simple_flush, METH_NOARGS},
1472 {"close", (PyCFunction)buffered_close, METH_NOARGS},
1473 {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS},
1474 {"readable", (PyCFunction)buffered_readable, METH_NOARGS},
1475 {"writable", (PyCFunction)buffered_writable, METH_NOARGS},
1476 {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS},
1477 {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS},
1479 {"read", (PyCFunction)buffered_read, METH_VARARGS},
1480 {"peek", (PyCFunction)buffered_peek, METH_VARARGS},
1481 {"read1", (PyCFunction)buffered_read1, METH_VARARGS},
1482 {"readline", (PyCFunction)buffered_readline, METH_VARARGS},
1483 {"seek", (PyCFunction)buffered_seek, METH_VARARGS},
1484 {"tell", (PyCFunction)buffered_tell, METH_NOARGS},
1485 {"truncate", (PyCFunction)buffered_truncate, METH_VARARGS},
1486 {NULL, NULL}
1489 static PyMemberDef bufferedreader_members[] = {
1490 {"raw", T_OBJECT, offsetof(buffered, raw), 0},
1491 {NULL}
1494 static PyGetSetDef bufferedreader_getset[] = {
1495 {"closed", (getter)buffered_closed_get, NULL, NULL},
1496 {"name", (getter)buffered_name_get, NULL, NULL},
1497 {"mode", (getter)buffered_mode_get, NULL, NULL},
1498 {NULL}
1502 PyTypeObject PyBufferedReader_Type = {
1503 PyVarObject_HEAD_INIT(NULL, 0)
1504 "_io.BufferedReader", /*tp_name*/
1505 sizeof(buffered), /*tp_basicsize*/
1506 0, /*tp_itemsize*/
1507 (destructor)buffered_dealloc, /*tp_dealloc*/
1508 0, /*tp_print*/
1509 0, /*tp_getattr*/
1510 0, /*tp_setattr*/
1511 0, /*tp_compare */
1512 (reprfunc)buffered_repr, /*tp_repr*/
1513 0, /*tp_as_number*/
1514 0, /*tp_as_sequence*/
1515 0, /*tp_as_mapping*/
1516 0, /*tp_hash */
1517 0, /*tp_call*/
1518 0, /*tp_str*/
1519 0, /*tp_getattro*/
1520 0, /*tp_setattro*/
1521 0, /*tp_as_buffer*/
1522 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
1523 | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1524 bufferedreader_doc, /* tp_doc */
1525 (traverseproc)buffered_traverse, /* tp_traverse */
1526 (inquiry)buffered_clear, /* tp_clear */
1527 0, /* tp_richcompare */
1528 offsetof(buffered, weakreflist), /*tp_weaklistoffset*/
1529 0, /* tp_iter */
1530 (iternextfunc)buffered_iternext, /* tp_iternext */
1531 bufferedreader_methods, /* tp_methods */
1532 bufferedreader_members, /* tp_members */
1533 bufferedreader_getset, /* tp_getset */
1534 0, /* tp_base */
1535 0, /* tp_dict */
1536 0, /* tp_descr_get */
1537 0, /* tp_descr_set */
1538 offsetof(buffered, dict), /* tp_dictoffset */
1539 (initproc)bufferedreader_init, /* tp_init */
1540 0, /* tp_alloc */
1541 PyType_GenericNew, /* tp_new */
1546 static int
1547 complain_about_max_buffer_size(void)
1549 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1550 "max_buffer_size is deprecated", 1) < 0)
1551 return 0;
1552 return 1;
1556 * class BufferedWriter
1558 PyDoc_STRVAR(bufferedwriter_doc,
1559 "A buffer for a writeable sequential RawIO object.\n"
1560 "\n"
1561 "The constructor creates a BufferedWriter for the given writeable raw\n"
1562 "stream. If the buffer_size is not given, it defaults to\n"
1563 "DEFAULT_BUFFER_SIZE. max_buffer_size isn't used anymore.\n"
1566 static void
1567 _bufferedwriter_reset_buf(buffered *self)
1569 self->write_pos = 0;
1570 self->write_end = -1;
1573 static int
1574 bufferedwriter_init(buffered *self, PyObject *args, PyObject *kwds)
1576 /* TODO: properly deprecate max_buffer_size */
1577 char *kwlist[] = {"raw", "buffer_size", "max_buffer_size", NULL};
1578 Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
1579 Py_ssize_t max_buffer_size = -234;
1580 PyObject *raw;
1582 self->ok = 0;
1583 self->detached = 0;
1585 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|nn:BufferedReader", kwlist,
1586 &raw, &buffer_size, &max_buffer_size)) {
1587 return -1;
1590 if (max_buffer_size != -234 && !complain_about_max_buffer_size())
1591 return -1;
1593 if (_PyIOBase_check_writable(raw, Py_True) == NULL)
1594 return -1;
1596 Py_CLEAR(self->raw);
1597 Py_INCREF(raw);
1598 self->raw = raw;
1599 self->readable = 0;
1600 self->writable = 1;
1602 self->buffer_size = buffer_size;
1603 if (_buffered_init(self) < 0)
1604 return -1;
1605 _bufferedwriter_reset_buf(self);
1606 self->pos = 0;
1608 self->fast_closed_checks = (Py_TYPE(self) == &PyBufferedWriter_Type &&
1609 Py_TYPE(raw) == &PyFileIO_Type);
1611 self->ok = 1;
1612 return 0;
1615 static Py_ssize_t
1616 _bufferedwriter_raw_write(buffered *self, char *start, Py_ssize_t len)
1618 Py_buffer buf;
1619 PyObject *memobj, *res;
1620 Py_ssize_t n;
1621 /* NOTE: the buffer needn't be released as its object is NULL. */
1622 if (PyBuffer_FillInfo(&buf, NULL, start, len, 1, PyBUF_CONTIG_RO) == -1)
1623 return -1;
1624 memobj = PyMemoryView_FromBuffer(&buf);
1625 if (memobj == NULL)
1626 return -1;
1627 res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_write, memobj, NULL);
1628 Py_DECREF(memobj);
1629 if (res == NULL)
1630 return -1;
1631 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
1632 Py_DECREF(res);
1633 if (n < 0 || n > len) {
1634 PyErr_Format(PyExc_IOError,
1635 "raw write() returned invalid length %zd "
1636 "(should have been between 0 and %zd)", n, len);
1637 return -1;
1639 if (n > 0 && self->abs_pos != -1)
1640 self->abs_pos += n;
1641 return n;
1644 /* `restore_pos` is 1 if we need to restore the raw stream position at
1645 the end, 0 otherwise. */
1646 static PyObject *
1647 _bufferedwriter_flush_unlocked(buffered *self, int restore_pos)
1649 Py_ssize_t written = 0;
1650 Py_off_t n, rewind;
1652 if (!VALID_WRITE_BUFFER(self) || self->write_pos == self->write_end)
1653 goto end;
1654 /* First, rewind */
1655 rewind = RAW_OFFSET(self) + (self->pos - self->write_pos);
1656 if (rewind != 0) {
1657 n = _buffered_raw_seek(self, -rewind, 1);
1658 if (n < 0) {
1659 goto error;
1661 self->raw_pos -= rewind;
1663 while (self->write_pos < self->write_end) {
1664 n = _bufferedwriter_raw_write(self,
1665 self->buffer + self->write_pos,
1666 Py_SAFE_DOWNCAST(self->write_end - self->write_pos,
1667 Py_off_t, Py_ssize_t));
1668 if (n == -1) {
1669 Py_ssize_t *w = _buffered_check_blocking_error();
1670 if (w == NULL)
1671 goto error;
1672 self->write_pos += *w;
1673 self->raw_pos = self->write_pos;
1674 written += *w;
1675 *w = written;
1676 /* Already re-raised */
1677 goto error;
1679 self->write_pos += n;
1680 self->raw_pos = self->write_pos;
1681 written += Py_SAFE_DOWNCAST(n, Py_off_t, Py_ssize_t);
1684 if (restore_pos) {
1685 Py_off_t forward = rewind - written;
1686 if (forward != 0) {
1687 n = _buffered_raw_seek(self, forward, 1);
1688 if (n < 0) {
1689 goto error;
1691 self->raw_pos += forward;
1694 _bufferedwriter_reset_buf(self);
1696 end:
1697 Py_RETURN_NONE;
1699 error:
1700 return NULL;
1703 static PyObject *
1704 bufferedwriter_write(buffered *self, PyObject *args)
1706 PyObject *res = NULL;
1707 Py_buffer buf;
1708 Py_ssize_t written, avail, remaining;
1709 Py_off_t offset;
1711 CHECK_INITIALIZED(self)
1712 if (!PyArg_ParseTuple(args, "s*:write", &buf)) {
1713 return NULL;
1716 if (IS_CLOSED(self)) {
1717 PyErr_SetString(PyExc_ValueError, "write to closed file");
1718 PyBuffer_Release(&buf);
1719 return NULL;
1722 ENTER_BUFFERED(self)
1724 /* Fast path: the data to write can be fully buffered. */
1725 if (!VALID_READ_BUFFER(self) && !VALID_WRITE_BUFFER(self)) {
1726 self->pos = 0;
1727 self->raw_pos = 0;
1729 avail = Py_SAFE_DOWNCAST(self->buffer_size - self->pos, Py_off_t, Py_ssize_t);
1730 if (buf.len <= avail) {
1731 memcpy(self->buffer + self->pos, buf.buf, buf.len);
1732 if (!VALID_WRITE_BUFFER(self)) {
1733 self->write_pos = self->pos;
1735 ADJUST_POSITION(self, self->pos + buf.len);
1736 if (self->pos > self->write_end)
1737 self->write_end = self->pos;
1738 written = buf.len;
1739 goto end;
1742 /* First write the current buffer */
1743 res = _bufferedwriter_flush_unlocked(self, 0);
1744 if (res == NULL) {
1745 Py_ssize_t *w = _buffered_check_blocking_error();
1746 if (w == NULL)
1747 goto error;
1748 if (self->readable)
1749 _bufferedreader_reset_buf(self);
1750 /* Make some place by shifting the buffer. */
1751 assert(VALID_WRITE_BUFFER(self));
1752 memmove(self->buffer, self->buffer + self->write_pos,
1753 Py_SAFE_DOWNCAST(self->write_end - self->write_pos,
1754 Py_off_t, Py_ssize_t));
1755 self->write_end -= self->write_pos;
1756 self->raw_pos -= self->write_pos;
1757 self->pos -= self->write_pos;
1758 self->write_pos = 0;
1759 avail = Py_SAFE_DOWNCAST(self->buffer_size - self->write_end,
1760 Py_off_t, Py_ssize_t);
1761 if (buf.len <= avail) {
1762 /* Everything can be buffered */
1763 PyErr_Clear();
1764 memcpy(self->buffer + self->write_end, buf.buf, buf.len);
1765 self->write_end += buf.len;
1766 written = buf.len;
1767 goto end;
1769 /* Buffer as much as possible. */
1770 memcpy(self->buffer + self->write_end, buf.buf, avail);
1771 self->write_end += avail;
1772 /* Already re-raised */
1773 *w = avail;
1774 goto error;
1776 Py_CLEAR(res);
1778 /* Adjust the raw stream position if it is away from the logical stream
1779 position. This happens if the read buffer has been filled but not
1780 modified (and therefore _bufferedwriter_flush_unlocked() didn't rewind
1781 the raw stream by itself).
1782 Fixes issue #6629.
1784 offset = RAW_OFFSET(self);
1785 if (offset != 0) {
1786 if (_buffered_raw_seek(self, -offset, 1) < 0)
1787 goto error;
1788 self->raw_pos -= offset;
1791 /* Then write buf itself. At this point the buffer has been emptied. */
1792 remaining = buf.len;
1793 written = 0;
1794 while (remaining > self->buffer_size) {
1795 Py_ssize_t n = _bufferedwriter_raw_write(
1796 self, (char *) buf.buf + written, buf.len - written);
1797 if (n == -1) {
1798 Py_ssize_t *w = _buffered_check_blocking_error();
1799 if (w == NULL)
1800 goto error;
1801 written += *w;
1802 remaining -= *w;
1803 if (remaining > self->buffer_size) {
1804 /* Can't buffer everything, still buffer as much as possible */
1805 memcpy(self->buffer,
1806 (char *) buf.buf + written, self->buffer_size);
1807 self->raw_pos = 0;
1808 ADJUST_POSITION(self, self->buffer_size);
1809 self->write_end = self->buffer_size;
1810 *w = written + self->buffer_size;
1811 /* Already re-raised */
1812 goto error;
1814 PyErr_Clear();
1815 break;
1817 written += n;
1818 remaining -= n;
1820 if (self->readable)
1821 _bufferedreader_reset_buf(self);
1822 if (remaining > 0) {
1823 memcpy(self->buffer, (char *) buf.buf + written, remaining);
1824 written += remaining;
1826 self->write_pos = 0;
1827 /* TODO: sanity check (remaining >= 0) */
1828 self->write_end = remaining;
1829 ADJUST_POSITION(self, remaining);
1830 self->raw_pos = 0;
1832 end:
1833 res = PyLong_FromSsize_t(written);
1835 error:
1836 LEAVE_BUFFERED(self)
1837 PyBuffer_Release(&buf);
1838 return res;
1841 static PyMethodDef bufferedwriter_methods[] = {
1842 /* BufferedIOMixin methods */
1843 {"close", (PyCFunction)buffered_close, METH_NOARGS},
1844 {"detach", (PyCFunction)buffered_detach, METH_NOARGS},
1845 {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS},
1846 {"readable", (PyCFunction)buffered_readable, METH_NOARGS},
1847 {"writable", (PyCFunction)buffered_writable, METH_NOARGS},
1848 {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS},
1849 {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS},
1851 {"write", (PyCFunction)bufferedwriter_write, METH_VARARGS},
1852 {"truncate", (PyCFunction)buffered_truncate, METH_VARARGS},
1853 {"flush", (PyCFunction)buffered_flush, METH_NOARGS},
1854 {"seek", (PyCFunction)buffered_seek, METH_VARARGS},
1855 {"tell", (PyCFunction)buffered_tell, METH_NOARGS},
1856 {NULL, NULL}
1859 static PyMemberDef bufferedwriter_members[] = {
1860 {"raw", T_OBJECT, offsetof(buffered, raw), 0},
1861 {NULL}
1864 static PyGetSetDef bufferedwriter_getset[] = {
1865 {"closed", (getter)buffered_closed_get, NULL, NULL},
1866 {"name", (getter)buffered_name_get, NULL, NULL},
1867 {"mode", (getter)buffered_mode_get, NULL, NULL},
1868 {NULL}
1872 PyTypeObject PyBufferedWriter_Type = {
1873 PyVarObject_HEAD_INIT(NULL, 0)
1874 "_io.BufferedWriter", /*tp_name*/
1875 sizeof(buffered), /*tp_basicsize*/
1876 0, /*tp_itemsize*/
1877 (destructor)buffered_dealloc, /*tp_dealloc*/
1878 0, /*tp_print*/
1879 0, /*tp_getattr*/
1880 0, /*tp_setattr*/
1881 0, /*tp_compare */
1882 (reprfunc)buffered_repr, /*tp_repr*/
1883 0, /*tp_as_number*/
1884 0, /*tp_as_sequence*/
1885 0, /*tp_as_mapping*/
1886 0, /*tp_hash */
1887 0, /*tp_call*/
1888 0, /*tp_str*/
1889 0, /*tp_getattro*/
1890 0, /*tp_setattro*/
1891 0, /*tp_as_buffer*/
1892 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
1893 | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1894 bufferedwriter_doc, /* tp_doc */
1895 (traverseproc)buffered_traverse, /* tp_traverse */
1896 (inquiry)buffered_clear, /* tp_clear */
1897 0, /* tp_richcompare */
1898 offsetof(buffered, weakreflist), /*tp_weaklistoffset*/
1899 0, /* tp_iter */
1900 0, /* tp_iternext */
1901 bufferedwriter_methods, /* tp_methods */
1902 bufferedwriter_members, /* tp_members */
1903 bufferedwriter_getset, /* tp_getset */
1904 0, /* tp_base */
1905 0, /* tp_dict */
1906 0, /* tp_descr_get */
1907 0, /* tp_descr_set */
1908 offsetof(buffered, dict), /* tp_dictoffset */
1909 (initproc)bufferedwriter_init, /* tp_init */
1910 0, /* tp_alloc */
1911 PyType_GenericNew, /* tp_new */
1917 * BufferedRWPair
1920 PyDoc_STRVAR(bufferedrwpair_doc,
1921 "A buffered reader and writer object together.\n"
1922 "\n"
1923 "A buffered reader object and buffered writer object put together to\n"
1924 "form a sequential IO object that can read and write. This is typically\n"
1925 "used with a socket or two-way pipe.\n"
1926 "\n"
1927 "reader and writer are RawIOBase objects that are readable and\n"
1928 "writeable respectively. If the buffer_size is omitted it defaults to\n"
1929 "DEFAULT_BUFFER_SIZE.\n"
1932 /* XXX The usefulness of this (compared to having two separate IO objects) is
1933 * questionable.
1936 typedef struct {
1937 PyObject_HEAD
1938 buffered *reader;
1939 buffered *writer;
1940 PyObject *dict;
1941 PyObject *weakreflist;
1942 } rwpair;
1944 static int
1945 bufferedrwpair_init(rwpair *self, PyObject *args, PyObject *kwds)
1947 PyObject *reader, *writer;
1948 Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
1949 Py_ssize_t max_buffer_size = -234;
1951 if (!PyArg_ParseTuple(args, "OO|nn:BufferedRWPair", &reader, &writer,
1952 &buffer_size, &max_buffer_size)) {
1953 return -1;
1956 if (max_buffer_size != -234 && !complain_about_max_buffer_size())
1957 return -1;
1959 if (_PyIOBase_check_readable(reader, Py_True) == NULL)
1960 return -1;
1961 if (_PyIOBase_check_writable(writer, Py_True) == NULL)
1962 return -1;
1964 self->reader = (buffered *) PyObject_CallFunction(
1965 (PyObject *) &PyBufferedReader_Type, "On", reader, buffer_size);
1966 if (self->reader == NULL)
1967 return -1;
1969 self->writer = (buffered *) PyObject_CallFunction(
1970 (PyObject *) &PyBufferedWriter_Type, "On", writer, buffer_size);
1971 if (self->writer == NULL) {
1972 Py_CLEAR(self->reader);
1973 return -1;
1976 return 0;
1979 static int
1980 bufferedrwpair_traverse(rwpair *self, visitproc visit, void *arg)
1982 Py_VISIT(self->dict);
1983 return 0;
1986 static int
1987 bufferedrwpair_clear(rwpair *self)
1989 Py_CLEAR(self->reader);
1990 Py_CLEAR(self->writer);
1991 Py_CLEAR(self->dict);
1992 return 0;
1995 static void
1996 bufferedrwpair_dealloc(rwpair *self)
1998 _PyObject_GC_UNTRACK(self);
1999 Py_CLEAR(self->reader);
2000 Py_CLEAR(self->writer);
2001 Py_CLEAR(self->dict);
2002 Py_TYPE(self)->tp_free((PyObject *) self);
2005 static PyObject *
2006 _forward_call(buffered *self, const char *name, PyObject *args)
2008 PyObject *func = PyObject_GetAttrString((PyObject *)self, name);
2009 PyObject *ret;
2011 if (func == NULL) {
2012 PyErr_SetString(PyExc_AttributeError, name);
2013 return NULL;
2016 ret = PyObject_CallObject(func, args);
2017 Py_DECREF(func);
2018 return ret;
2021 static PyObject *
2022 bufferedrwpair_read(rwpair *self, PyObject *args)
2024 return _forward_call(self->reader, "read", args);
2027 static PyObject *
2028 bufferedrwpair_peek(rwpair *self, PyObject *args)
2030 return _forward_call(self->reader, "peek", args);
2033 static PyObject *
2034 bufferedrwpair_read1(rwpair *self, PyObject *args)
2036 return _forward_call(self->reader, "read1", args);
2039 static PyObject *
2040 bufferedrwpair_readinto(rwpair *self, PyObject *args)
2042 return _forward_call(self->reader, "readinto", args);
2045 static PyObject *
2046 bufferedrwpair_write(rwpair *self, PyObject *args)
2048 return _forward_call(self->writer, "write", args);
2051 static PyObject *
2052 bufferedrwpair_flush(rwpair *self, PyObject *args)
2054 return _forward_call(self->writer, "flush", args);
2057 static PyObject *
2058 bufferedrwpair_readable(rwpair *self, PyObject *args)
2060 return _forward_call(self->reader, "readable", args);
2063 static PyObject *
2064 bufferedrwpair_writable(rwpair *self, PyObject *args)
2066 return _forward_call(self->writer, "writable", args);
2069 static PyObject *
2070 bufferedrwpair_close(rwpair *self, PyObject *args)
2072 PyObject *ret = _forward_call(self->writer, "close", args);
2073 if (ret == NULL)
2074 return NULL;
2075 Py_DECREF(ret);
2077 return _forward_call(self->reader, "close", args);
2080 static PyObject *
2081 bufferedrwpair_isatty(rwpair *self, PyObject *args)
2083 PyObject *ret = _forward_call(self->writer, "isatty", args);
2085 if (ret != Py_False) {
2086 /* either True or exception */
2087 return ret;
2089 Py_DECREF(ret);
2091 return _forward_call(self->reader, "isatty", args);
2094 static PyObject *
2095 bufferedrwpair_closed_get(rwpair *self, void *context)
2097 return PyObject_GetAttr((PyObject *) self->writer, _PyIO_str_closed);
2100 static PyMethodDef bufferedrwpair_methods[] = {
2101 {"read", (PyCFunction)bufferedrwpair_read, METH_VARARGS},
2102 {"peek", (PyCFunction)bufferedrwpair_peek, METH_VARARGS},
2103 {"read1", (PyCFunction)bufferedrwpair_read1, METH_VARARGS},
2104 {"readinto", (PyCFunction)bufferedrwpair_readinto, METH_VARARGS},
2106 {"write", (PyCFunction)bufferedrwpair_write, METH_VARARGS},
2107 {"flush", (PyCFunction)bufferedrwpair_flush, METH_NOARGS},
2109 {"readable", (PyCFunction)bufferedrwpair_readable, METH_NOARGS},
2110 {"writable", (PyCFunction)bufferedrwpair_writable, METH_NOARGS},
2112 {"close", (PyCFunction)bufferedrwpair_close, METH_NOARGS},
2113 {"isatty", (PyCFunction)bufferedrwpair_isatty, METH_NOARGS},
2115 {NULL, NULL}
2118 static PyGetSetDef bufferedrwpair_getset[] = {
2119 {"closed", (getter)bufferedrwpair_closed_get, NULL, NULL},
2120 {NULL}
2123 PyTypeObject PyBufferedRWPair_Type = {
2124 PyVarObject_HEAD_INIT(NULL, 0)
2125 "_io.BufferedRWPair", /*tp_name*/
2126 sizeof(rwpair), /*tp_basicsize*/
2127 0, /*tp_itemsize*/
2128 (destructor)bufferedrwpair_dealloc, /*tp_dealloc*/
2129 0, /*tp_print*/
2130 0, /*tp_getattr*/
2131 0, /*tp_setattr*/
2132 0, /*tp_compare */
2133 0, /*tp_repr*/
2134 0, /*tp_as_number*/
2135 0, /*tp_as_sequence*/
2136 0, /*tp_as_mapping*/
2137 0, /*tp_hash */
2138 0, /*tp_call*/
2139 0, /*tp_str*/
2140 0, /*tp_getattro*/
2141 0, /*tp_setattro*/
2142 0, /*tp_as_buffer*/
2143 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
2144 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2145 bufferedrwpair_doc, /* tp_doc */
2146 (traverseproc)bufferedrwpair_traverse, /* tp_traverse */
2147 (inquiry)bufferedrwpair_clear, /* tp_clear */
2148 0, /* tp_richcompare */
2149 offsetof(rwpair, weakreflist), /*tp_weaklistoffset*/
2150 0, /* tp_iter */
2151 0, /* tp_iternext */
2152 bufferedrwpair_methods, /* tp_methods */
2153 0, /* tp_members */
2154 bufferedrwpair_getset, /* tp_getset */
2155 0, /* tp_base */
2156 0, /* tp_dict */
2157 0, /* tp_descr_get */
2158 0, /* tp_descr_set */
2159 offsetof(rwpair, dict), /* tp_dictoffset */
2160 (initproc)bufferedrwpair_init, /* tp_init */
2161 0, /* tp_alloc */
2162 PyType_GenericNew, /* tp_new */
2168 * BufferedRandom
2171 PyDoc_STRVAR(bufferedrandom_doc,
2172 "A buffered interface to random access streams.\n"
2173 "\n"
2174 "The constructor creates a reader and writer for a seekable stream,\n"
2175 "raw, given in the first argument. If the buffer_size is omitted it\n"
2176 "defaults to DEFAULT_BUFFER_SIZE. max_buffer_size isn't used anymore.\n"
2179 static int
2180 bufferedrandom_init(buffered *self, PyObject *args, PyObject *kwds)
2182 char *kwlist[] = {"raw", "buffer_size", "max_buffer_size", NULL};
2183 Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
2184 Py_ssize_t max_buffer_size = -234;
2185 PyObject *raw;
2187 self->ok = 0;
2188 self->detached = 0;
2190 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|nn:BufferedReader", kwlist,
2191 &raw, &buffer_size, &max_buffer_size)) {
2192 return -1;
2195 if (max_buffer_size != -234 && !complain_about_max_buffer_size())
2196 return -1;
2198 if (_PyIOBase_check_seekable(raw, Py_True) == NULL)
2199 return -1;
2200 if (_PyIOBase_check_readable(raw, Py_True) == NULL)
2201 return -1;
2202 if (_PyIOBase_check_writable(raw, Py_True) == NULL)
2203 return -1;
2205 Py_CLEAR(self->raw);
2206 Py_INCREF(raw);
2207 self->raw = raw;
2208 self->buffer_size = buffer_size;
2209 self->readable = 1;
2210 self->writable = 1;
2212 if (_buffered_init(self) < 0)
2213 return -1;
2214 _bufferedreader_reset_buf(self);
2215 _bufferedwriter_reset_buf(self);
2216 self->pos = 0;
2218 self->fast_closed_checks = (Py_TYPE(self) == &PyBufferedRandom_Type &&
2219 Py_TYPE(raw) == &PyFileIO_Type);
2221 self->ok = 1;
2222 return 0;
2225 static PyMethodDef bufferedrandom_methods[] = {
2226 /* BufferedIOMixin methods */
2227 {"close", (PyCFunction)buffered_close, METH_NOARGS},
2228 {"detach", (PyCFunction)buffered_detach, METH_NOARGS},
2229 {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS},
2230 {"readable", (PyCFunction)buffered_readable, METH_NOARGS},
2231 {"writable", (PyCFunction)buffered_writable, METH_NOARGS},
2232 {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS},
2233 {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS},
2235 {"flush", (PyCFunction)buffered_flush, METH_NOARGS},
2237 {"seek", (PyCFunction)buffered_seek, METH_VARARGS},
2238 {"tell", (PyCFunction)buffered_tell, METH_NOARGS},
2239 {"truncate", (PyCFunction)buffered_truncate, METH_VARARGS},
2240 {"read", (PyCFunction)buffered_read, METH_VARARGS},
2241 {"read1", (PyCFunction)buffered_read1, METH_VARARGS},
2242 {"readinto", (PyCFunction)buffered_readinto, METH_VARARGS},
2243 {"readline", (PyCFunction)buffered_readline, METH_VARARGS},
2244 {"peek", (PyCFunction)buffered_peek, METH_VARARGS},
2245 {"write", (PyCFunction)bufferedwriter_write, METH_VARARGS},
2246 {NULL, NULL}
2249 static PyMemberDef bufferedrandom_members[] = {
2250 {"raw", T_OBJECT, offsetof(buffered, raw), 0},
2251 {NULL}
2254 static PyGetSetDef bufferedrandom_getset[] = {
2255 {"closed", (getter)buffered_closed_get, NULL, NULL},
2256 {"name", (getter)buffered_name_get, NULL, NULL},
2257 {"mode", (getter)buffered_mode_get, NULL, NULL},
2258 {NULL}
2262 PyTypeObject PyBufferedRandom_Type = {
2263 PyVarObject_HEAD_INIT(NULL, 0)
2264 "_io.BufferedRandom", /*tp_name*/
2265 sizeof(buffered), /*tp_basicsize*/
2266 0, /*tp_itemsize*/
2267 (destructor)buffered_dealloc, /*tp_dealloc*/
2268 0, /*tp_print*/
2269 0, /*tp_getattr*/
2270 0, /*tp_setattr*/
2271 0, /*tp_compare */
2272 (reprfunc)buffered_repr, /*tp_repr*/
2273 0, /*tp_as_number*/
2274 0, /*tp_as_sequence*/
2275 0, /*tp_as_mapping*/
2276 0, /*tp_hash */
2277 0, /*tp_call*/
2278 0, /*tp_str*/
2279 0, /*tp_getattro*/
2280 0, /*tp_setattro*/
2281 0, /*tp_as_buffer*/
2282 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
2283 | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2284 bufferedrandom_doc, /* tp_doc */
2285 (traverseproc)buffered_traverse, /* tp_traverse */
2286 (inquiry)buffered_clear, /* tp_clear */
2287 0, /* tp_richcompare */
2288 offsetof(buffered, weakreflist), /*tp_weaklistoffset*/
2289 0, /* tp_iter */
2290 (iternextfunc)buffered_iternext, /* tp_iternext */
2291 bufferedrandom_methods, /* tp_methods */
2292 bufferedrandom_members, /* tp_members */
2293 bufferedrandom_getset, /* tp_getset */
2294 0, /* tp_base */
2295 0, /*tp_dict*/
2296 0, /* tp_descr_get */
2297 0, /* tp_descr_set */
2298 offsetof(buffered, dict), /*tp_dictoffset*/
2299 (initproc)bufferedrandom_init, /* tp_init */
2300 0, /* tp_alloc */
2301 PyType_GenericNew, /* tp_new */