fix typo
[python.git] / Modules / _io / iobase.c
blob38080f7f8a9725b1de6c60ee6d883bb3ca3b6b35
1 /*
2 An implementation of the I/O abstract base classes hierarchy
3 as defined by PEP 3116 - "New I/O"
5 Classes defined here: IOBase, RawIOBase.
7 Written by Amaury Forgeot d'Arc and Antoine Pitrou
8 */
11 #define PY_SSIZE_T_CLEAN
12 #include "Python.h"
13 #include "structmember.h"
14 #include "_iomodule.h"
17 * IOBase class, an abstract class
20 typedef struct {
21 PyObject_HEAD
23 PyObject *dict;
24 PyObject *weakreflist;
25 } iobase;
27 PyDoc_STRVAR(iobase_doc,
28 "The abstract base class for all I/O classes, acting on streams of\n"
29 "bytes. There is no public constructor.\n"
30 "\n"
31 "This class provides dummy implementations for many methods that\n"
32 "derived classes can override selectively; the default implementations\n"
33 "represent a file that cannot be read, written or seeked.\n"
34 "\n"
35 "Even though IOBase does not declare read, readinto, or write because\n"
36 "their signatures will vary, implementations and clients should\n"
37 "consider those methods part of the interface. Also, implementations\n"
38 "may raise a IOError when operations they do not support are called.\n"
39 "\n"
40 "The basic type used for binary data read from or written to a file is\n"
41 "bytes. bytearrays are accepted too, and in some cases (such as\n"
42 "readinto) needed. Text I/O classes work with str data.\n"
43 "\n"
44 "Note that calling any method (even inquiries) on a closed stream is\n"
45 "undefined. Implementations may raise IOError in this case.\n"
46 "\n"
47 "IOBase (and its subclasses) support the iterator protocol, meaning\n"
48 "that an IOBase object can be iterated over yielding the lines in a\n"
49 "stream.\n"
50 "\n"
51 "IOBase also supports the :keyword:`with` statement. In this example,\n"
52 "fp is closed after the suite of the with statment is complete:\n"
53 "\n"
54 "with open('spam.txt', 'r') as fp:\n"
55 " fp.write('Spam and eggs!')\n");
57 /* Use this macro whenever you want to check the internal `closed` status
58 of the IOBase object rather than the virtual `closed` attribute as returned
59 by whatever subclass. */
61 #define IS_CLOSED(self) \
62 PyObject_HasAttrString(self, "__IOBase_closed")
64 /* Internal methods */
65 static PyObject *
66 iobase_unsupported(const char *message)
68 PyErr_SetString(_PyIO_unsupported_operation, message);
69 return NULL;
72 /* Positionning */
74 PyDoc_STRVAR(iobase_seek_doc,
75 "Change stream position.\n"
76 "\n"
77 "Change the stream position to byte offset offset. offset is\n"
78 "interpreted relative to the position indicated by whence. Values\n"
79 "for whence are:\n"
80 "\n"
81 "* 0 -- start of stream (the default); offset should be zero or positive\n"
82 "* 1 -- current stream position; offset may be negative\n"
83 "* 2 -- end of stream; offset is usually negative\n"
84 "\n"
85 "Return the new absolute position.");
87 static PyObject *
88 iobase_seek(PyObject *self, PyObject *args)
90 return iobase_unsupported("seek");
93 PyDoc_STRVAR(iobase_tell_doc,
94 "Return current stream position.");
96 static PyObject *
97 iobase_tell(PyObject *self, PyObject *args)
99 return PyObject_CallMethod(self, "seek", "ii", 0, 1);
102 PyDoc_STRVAR(iobase_truncate_doc,
103 "Truncate file to size bytes.\n"
104 "\n"
105 "Size defaults to the current IO position as reported by tell(). Return\n"
106 "the new size.");
108 static PyObject *
109 iobase_truncate(PyObject *self, PyObject *args)
111 return iobase_unsupported("truncate");
114 /* Flush and close methods */
116 PyDoc_STRVAR(iobase_flush_doc,
117 "Flush write buffers, if applicable.\n"
118 "\n"
119 "This is not implemented for read-only and non-blocking streams.\n");
121 static PyObject *
122 iobase_flush(PyObject *self, PyObject *args)
124 /* XXX Should this return the number of bytes written??? */
125 if (IS_CLOSED(self)) {
126 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
127 return NULL;
129 Py_RETURN_NONE;
132 PyDoc_STRVAR(iobase_close_doc,
133 "Flush and close the IO object.\n"
134 "\n"
135 "This method has no effect if the file is already closed.\n");
137 static int
138 iobase_closed(PyObject *self)
140 PyObject *res;
141 int closed;
142 /* This gets the derived attribute, which is *not* __IOBase_closed
143 in most cases! */
144 res = PyObject_GetAttr(self, _PyIO_str_closed);
145 if (res == NULL)
146 return 0;
147 closed = PyObject_IsTrue(res);
148 Py_DECREF(res);
149 return closed;
152 static PyObject *
153 iobase_closed_get(PyObject *self, void *context)
155 return PyBool_FromLong(IS_CLOSED(self));
158 PyObject *
159 _PyIOBase_check_closed(PyObject *self, PyObject *args)
161 if (iobase_closed(self)) {
162 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
163 return NULL;
165 if (args == Py_True)
166 return Py_None;
167 else
168 Py_RETURN_NONE;
171 /* XXX: IOBase thinks it has to maintain its own internal state in
172 `__IOBase_closed` and call flush() by itself, but it is redundant with
173 whatever behaviour a non-trivial derived class will implement. */
175 static PyObject *
176 iobase_close(PyObject *self, PyObject *args)
178 PyObject *res;
180 if (IS_CLOSED(self))
181 Py_RETURN_NONE;
183 res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);
184 PyObject_SetAttrString(self, "__IOBase_closed", Py_True);
185 if (res == NULL) {
186 /* If flush() fails, just give up */
187 if (PyErr_ExceptionMatches(PyExc_IOError))
188 PyErr_Clear();
189 else
190 return NULL;
192 Py_XDECREF(res);
193 Py_RETURN_NONE;
196 /* Finalization and garbage collection support */
199 _PyIOBase_finalize(PyObject *self)
201 PyObject *res;
202 PyObject *tp, *v, *tb;
203 int closed = 1;
204 int is_zombie;
206 /* If _PyIOBase_finalize() is called from a destructor, we need to
207 resurrect the object as calling close() can invoke arbitrary code. */
208 is_zombie = (Py_REFCNT(self) == 0);
209 if (is_zombie) {
210 ++Py_REFCNT(self);
212 PyErr_Fetch(&tp, &v, &tb);
213 /* If `closed` doesn't exist or can't be evaluated as bool, then the
214 object is probably in an unusable state, so ignore. */
215 res = PyObject_GetAttr(self, _PyIO_str_closed);
216 if (res == NULL)
217 PyErr_Clear();
218 else {
219 closed = PyObject_IsTrue(res);
220 Py_DECREF(res);
221 if (closed == -1)
222 PyErr_Clear();
224 if (closed == 0) {
225 res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_close,
226 NULL);
227 /* Silencing I/O errors is bad, but printing spurious tracebacks is
228 equally as bad, and potentially more frequent (because of
229 shutdown issues). */
230 if (res == NULL)
231 PyErr_Clear();
232 else
233 Py_DECREF(res);
235 PyErr_Restore(tp, v, tb);
236 if (is_zombie) {
237 if (--Py_REFCNT(self) != 0) {
238 /* The object lives again. The following code is taken from
239 slot_tp_del in typeobject.c. */
240 Py_ssize_t refcnt = Py_REFCNT(self);
241 _Py_NewReference(self);
242 Py_REFCNT(self) = refcnt;
243 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
244 * we need to undo that. */
245 _Py_DEC_REFTOTAL;
246 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
247 * chain, so no more to do there.
248 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
249 * _Py_NewReference bumped tp_allocs: both of those need to be
250 * undone.
252 #ifdef COUNT_ALLOCS
253 --Py_TYPE(self)->tp_frees;
254 --Py_TYPE(self)->tp_allocs;
255 #endif
256 return -1;
259 return 0;
262 static int
263 iobase_traverse(iobase *self, visitproc visit, void *arg)
265 Py_VISIT(self->dict);
266 return 0;
269 static int
270 iobase_clear(iobase *self)
272 if (_PyIOBase_finalize((PyObject *) self) < 0)
273 return -1;
274 Py_CLEAR(self->dict);
275 return 0;
278 /* Destructor */
280 static void
281 iobase_dealloc(iobase *self)
283 /* NOTE: since IOBaseObject has its own dict, Python-defined attributes
284 are still available here for close() to use.
285 However, if the derived class declares a __slots__, those slots are
286 already gone.
288 if (_PyIOBase_finalize((PyObject *) self) < 0) {
289 /* When called from a heap type's dealloc, the type will be
290 decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */
291 if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE))
292 Py_INCREF(Py_TYPE(self));
293 return;
295 _PyObject_GC_UNTRACK(self);
296 if (self->weakreflist != NULL)
297 PyObject_ClearWeakRefs((PyObject *) self);
298 Py_CLEAR(self->dict);
299 Py_TYPE(self)->tp_free((PyObject *) self);
302 /* Inquiry methods */
304 PyDoc_STRVAR(iobase_seekable_doc,
305 "Return whether object supports random access.\n"
306 "\n"
307 "If False, seek(), tell() and truncate() will raise IOError.\n"
308 "This method may need to do a test seek().");
310 static PyObject *
311 iobase_seekable(PyObject *self, PyObject *args)
313 Py_RETURN_FALSE;
316 PyObject *
317 _PyIOBase_check_seekable(PyObject *self, PyObject *args)
319 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_seekable, NULL);
320 if (res == NULL)
321 return NULL;
322 if (res != Py_True) {
323 Py_CLEAR(res);
324 PyErr_SetString(PyExc_IOError, "File or stream is not seekable.");
325 return NULL;
327 if (args == Py_True) {
328 Py_DECREF(res);
330 return res;
333 PyDoc_STRVAR(iobase_readable_doc,
334 "Return whether object was opened for reading.\n"
335 "\n"
336 "If False, read() will raise IOError.");
338 static PyObject *
339 iobase_readable(PyObject *self, PyObject *args)
341 Py_RETURN_FALSE;
344 /* May be called with any object */
345 PyObject *
346 _PyIOBase_check_readable(PyObject *self, PyObject *args)
348 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_readable, NULL);
349 if (res == NULL)
350 return NULL;
351 if (res != Py_True) {
352 Py_CLEAR(res);
353 PyErr_SetString(PyExc_IOError, "File or stream is not readable.");
354 return NULL;
356 if (args == Py_True) {
357 Py_DECREF(res);
359 return res;
362 PyDoc_STRVAR(iobase_writable_doc,
363 "Return whether object was opened for writing.\n"
364 "\n"
365 "If False, read() will raise IOError.");
367 static PyObject *
368 iobase_writable(PyObject *self, PyObject *args)
370 Py_RETURN_FALSE;
373 /* May be called with any object */
374 PyObject *
375 _PyIOBase_check_writable(PyObject *self, PyObject *args)
377 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_writable, NULL);
378 if (res == NULL)
379 return NULL;
380 if (res != Py_True) {
381 Py_CLEAR(res);
382 PyErr_SetString(PyExc_IOError, "File or stream is not writable.");
383 return NULL;
385 if (args == Py_True) {
386 Py_DECREF(res);
388 return res;
391 /* Context manager */
393 static PyObject *
394 iobase_enter(PyObject *self, PyObject *args)
396 if (_PyIOBase_check_closed(self, Py_True) == NULL)
397 return NULL;
399 Py_INCREF(self);
400 return self;
403 static PyObject *
404 iobase_exit(PyObject *self, PyObject *args)
406 return PyObject_CallMethodObjArgs(self, _PyIO_str_close, NULL);
409 /* Lower-level APIs */
411 /* XXX Should these be present even if unimplemented? */
413 PyDoc_STRVAR(iobase_fileno_doc,
414 "Returns underlying file descriptor if one exists.\n"
415 "\n"
416 "An IOError is raised if the IO object does not use a file descriptor.\n");
418 static PyObject *
419 iobase_fileno(PyObject *self, PyObject *args)
421 return iobase_unsupported("fileno");
424 PyDoc_STRVAR(iobase_isatty_doc,
425 "Return whether this is an 'interactive' stream.\n"
426 "\n"
427 "Return False if it can't be determined.\n");
429 static PyObject *
430 iobase_isatty(PyObject *self, PyObject *args)
432 if (_PyIOBase_check_closed(self, Py_True) == NULL)
433 return NULL;
434 Py_RETURN_FALSE;
437 /* Readline(s) and writelines */
439 PyDoc_STRVAR(iobase_readline_doc,
440 "Read and return a line from the stream.\n"
441 "\n"
442 "If limit is specified, at most limit bytes will be read.\n"
443 "\n"
444 "The line terminator is always b'\n' for binary files; for text\n"
445 "files, the newlines argument to open can be used to select the line\n"
446 "terminator(s) recognized.\n");
448 static PyObject *
449 iobase_readline(PyObject *self, PyObject *args)
451 /* For backwards compatibility, a (slowish) readline(). */
453 Py_ssize_t limit = -1;
454 int has_peek = 0;
455 PyObject *buffer, *result;
456 Py_ssize_t old_size = -1;
458 if (!PyArg_ParseTuple(args, "|n:readline", &limit)) {
459 return NULL;
462 if (PyObject_HasAttrString(self, "peek"))
463 has_peek = 1;
465 buffer = PyByteArray_FromStringAndSize(NULL, 0);
466 if (buffer == NULL)
467 return NULL;
469 while (limit < 0 || Py_SIZE(buffer) < limit) {
470 Py_ssize_t nreadahead = 1;
471 PyObject *b;
473 if (has_peek) {
474 PyObject *readahead = PyObject_CallMethod(self, "peek", "i", 1);
475 if (readahead == NULL)
476 goto fail;
477 if (!PyBytes_Check(readahead)) {
478 PyErr_Format(PyExc_IOError,
479 "peek() should have returned a bytes object, "
480 "not '%.200s'", Py_TYPE(readahead)->tp_name);
481 Py_DECREF(readahead);
482 goto fail;
484 if (PyBytes_GET_SIZE(readahead) > 0) {
485 Py_ssize_t n = 0;
486 const char *buf = PyBytes_AS_STRING(readahead);
487 if (limit >= 0) {
488 do {
489 if (n >= PyBytes_GET_SIZE(readahead) || n >= limit)
490 break;
491 if (buf[n++] == '\n')
492 break;
493 } while (1);
495 else {
496 do {
497 if (n >= PyBytes_GET_SIZE(readahead))
498 break;
499 if (buf[n++] == '\n')
500 break;
501 } while (1);
503 nreadahead = n;
505 Py_DECREF(readahead);
508 b = PyObject_CallMethod(self, "read", "n", nreadahead);
509 if (b == NULL)
510 goto fail;
511 if (!PyBytes_Check(b)) {
512 PyErr_Format(PyExc_IOError,
513 "read() should have returned a bytes object, "
514 "not '%.200s'", Py_TYPE(b)->tp_name);
515 Py_DECREF(b);
516 goto fail;
518 if (PyBytes_GET_SIZE(b) == 0) {
519 Py_DECREF(b);
520 break;
523 old_size = PyByteArray_GET_SIZE(buffer);
524 PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b));
525 memcpy(PyByteArray_AS_STRING(buffer) + old_size,
526 PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b));
528 Py_DECREF(b);
530 if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n')
531 break;
534 result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer),
535 PyByteArray_GET_SIZE(buffer));
536 Py_DECREF(buffer);
537 return result;
538 fail:
539 Py_DECREF(buffer);
540 return NULL;
543 static PyObject *
544 iobase_iter(PyObject *self)
546 if (_PyIOBase_check_closed(self, Py_True) == NULL)
547 return NULL;
549 Py_INCREF(self);
550 return self;
553 static PyObject *
554 iobase_iternext(PyObject *self)
556 PyObject *line = PyObject_CallMethodObjArgs(self, _PyIO_str_readline, NULL);
558 if (line == NULL)
559 return NULL;
561 if (PyObject_Size(line) == 0) {
562 Py_DECREF(line);
563 return NULL;
566 return line;
569 PyDoc_STRVAR(iobase_readlines_doc,
570 "Return a list of lines from the stream.\n"
571 "\n"
572 "hint can be specified to control the number of lines read: no more\n"
573 "lines will be read if the total size (in bytes/characters) of all\n"
574 "lines so far exceeds hint.");
576 static PyObject *
577 iobase_readlines(PyObject *self, PyObject *args)
579 Py_ssize_t hint = -1, length = 0;
580 PyObject *hintobj = Py_None, *result;
582 if (!PyArg_ParseTuple(args, "|O:readlines", &hintobj)) {
583 return NULL;
585 if (hintobj != Py_None) {
586 hint = PyNumber_AsSsize_t(hintobj, PyExc_ValueError);
587 if (hint == -1 && PyErr_Occurred())
588 return NULL;
591 result = PyList_New(0);
592 if (result == NULL)
593 return NULL;
595 if (hint <= 0) {
596 /* XXX special-casing this made sense in the Python version in order
597 to remove the bytecode interpretation overhead, but it could
598 probably be removed here. */
599 PyObject *ret = PyObject_CallMethod(result, "extend", "O", self);
600 if (ret == NULL) {
601 Py_DECREF(result);
602 return NULL;
604 Py_DECREF(ret);
605 return result;
608 while (1) {
609 PyObject *line = PyIter_Next(self);
610 if (line == NULL) {
611 if (PyErr_Occurred()) {
612 Py_DECREF(result);
613 return NULL;
615 else
616 break; /* StopIteration raised */
619 if (PyList_Append(result, line) < 0) {
620 Py_DECREF(line);
621 Py_DECREF(result);
622 return NULL;
624 length += PyObject_Size(line);
625 Py_DECREF(line);
627 if (length > hint)
628 break;
630 return result;
633 static PyObject *
634 iobase_writelines(PyObject *self, PyObject *args)
636 PyObject *lines, *iter, *res;
638 if (!PyArg_ParseTuple(args, "O:writelines", &lines)) {
639 return NULL;
642 if (_PyIOBase_check_closed(self, Py_True) == NULL)
643 return NULL;
645 iter = PyObject_GetIter(lines);
646 if (iter == NULL)
647 return NULL;
649 while (1) {
650 PyObject *line = PyIter_Next(iter);
651 if (line == NULL) {
652 if (PyErr_Occurred()) {
653 Py_DECREF(iter);
654 return NULL;
656 else
657 break; /* Stop Iteration */
660 res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL);
661 Py_DECREF(line);
662 if (res == NULL) {
663 Py_DECREF(iter);
664 return NULL;
666 Py_DECREF(res);
668 Py_DECREF(iter);
669 Py_RETURN_NONE;
672 static PyMethodDef iobase_methods[] = {
673 {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc},
674 {"tell", iobase_tell, METH_NOARGS, iobase_tell_doc},
675 {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc},
676 {"flush", iobase_flush, METH_NOARGS, iobase_flush_doc},
677 {"close", iobase_close, METH_NOARGS, iobase_close_doc},
679 {"seekable", iobase_seekable, METH_NOARGS, iobase_seekable_doc},
680 {"readable", iobase_readable, METH_NOARGS, iobase_readable_doc},
681 {"writable", iobase_writable, METH_NOARGS, iobase_writable_doc},
683 {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS},
684 {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS},
685 {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS},
686 {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS},
688 {"fileno", iobase_fileno, METH_NOARGS, iobase_fileno_doc},
689 {"isatty", iobase_isatty, METH_NOARGS, iobase_isatty_doc},
691 {"__enter__", iobase_enter, METH_NOARGS},
692 {"__exit__", iobase_exit, METH_VARARGS},
694 {"readline", iobase_readline, METH_VARARGS, iobase_readline_doc},
695 {"readlines", iobase_readlines, METH_VARARGS, iobase_readlines_doc},
696 {"writelines", iobase_writelines, METH_VARARGS},
698 {NULL, NULL}
701 static PyGetSetDef iobase_getset[] = {
702 {"closed", (getter)iobase_closed_get, NULL, NULL},
703 {NULL}
707 PyTypeObject PyIOBase_Type = {
708 PyVarObject_HEAD_INIT(NULL, 0)
709 "_io._IOBase", /*tp_name*/
710 sizeof(iobase), /*tp_basicsize*/
711 0, /*tp_itemsize*/
712 (destructor)iobase_dealloc, /*tp_dealloc*/
713 0, /*tp_print*/
714 0, /*tp_getattr*/
715 0, /*tp_setattr*/
716 0, /*tp_compare */
717 0, /*tp_repr*/
718 0, /*tp_as_number*/
719 0, /*tp_as_sequence*/
720 0, /*tp_as_mapping*/
721 0, /*tp_hash */
722 0, /*tp_call*/
723 0, /*tp_str*/
724 0, /*tp_getattro*/
725 0, /*tp_setattro*/
726 0, /*tp_as_buffer*/
727 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
728 | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
729 iobase_doc, /* tp_doc */
730 (traverseproc)iobase_traverse, /* tp_traverse */
731 (inquiry)iobase_clear, /* tp_clear */
732 0, /* tp_richcompare */
733 offsetof(iobase, weakreflist), /* tp_weaklistoffset */
734 iobase_iter, /* tp_iter */
735 iobase_iternext, /* tp_iternext */
736 iobase_methods, /* tp_methods */
737 0, /* tp_members */
738 iobase_getset, /* tp_getset */
739 0, /* tp_base */
740 0, /* tp_dict */
741 0, /* tp_descr_get */
742 0, /* tp_descr_set */
743 offsetof(iobase, dict), /* tp_dictoffset */
744 0, /* tp_init */
745 0, /* tp_alloc */
746 PyType_GenericNew, /* tp_new */
751 * RawIOBase class, Inherits from IOBase.
753 PyDoc_STRVAR(rawiobase_doc,
754 "Base class for raw binary I/O.");
757 * The read() method is implemented by calling readinto(); derived classes
758 * that want to support read() only need to implement readinto() as a
759 * primitive operation. In general, readinto() can be more efficient than
760 * read().
762 * (It would be tempting to also provide an implementation of readinto() in
763 * terms of read(), in case the latter is a more suitable primitive operation,
764 * but that would lead to nasty recursion in case a subclass doesn't implement
765 * either.)
768 static PyObject *
769 rawiobase_read(PyObject *self, PyObject *args)
771 Py_ssize_t n = -1;
772 PyObject *b, *res;
774 if (!PyArg_ParseTuple(args, "|n:read", &n)) {
775 return NULL;
778 if (n < 0)
779 return PyObject_CallMethod(self, "readall", NULL);
781 /* TODO: allocate a bytes object directly instead and manually construct
782 a writable memoryview pointing to it. */
783 b = PyByteArray_FromStringAndSize(NULL, n);
784 if (b == NULL)
785 return NULL;
787 res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL);
788 if (res == NULL) {
789 Py_DECREF(b);
790 return NULL;
793 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
794 Py_DECREF(res);
795 if (n == -1 && PyErr_Occurred()) {
796 Py_DECREF(b);
797 return NULL;
800 res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);
801 Py_DECREF(b);
802 return res;
806 PyDoc_STRVAR(rawiobase_readall_doc,
807 "Read until EOF, using multiple read() call.");
809 static PyObject *
810 rawiobase_readall(PyObject *self, PyObject *args)
812 int r;
813 PyObject *chunks = PyList_New(0);
814 PyObject *result;
816 if (chunks == NULL)
817 return NULL;
819 while (1) {
820 PyObject *data = PyObject_CallMethod(self, "read",
821 "i", DEFAULT_BUFFER_SIZE);
822 if (!data) {
823 Py_DECREF(chunks);
824 return NULL;
826 if (!PyBytes_Check(data)) {
827 Py_DECREF(chunks);
828 Py_DECREF(data);
829 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
830 return NULL;
832 if (PyBytes_GET_SIZE(data) == 0) {
833 /* EOF */
834 Py_DECREF(data);
835 break;
837 r = PyList_Append(chunks, data);
838 Py_DECREF(data);
839 if (r < 0) {
840 Py_DECREF(chunks);
841 return NULL;
844 result = _PyBytes_Join(_PyIO_empty_bytes, chunks);
845 Py_DECREF(chunks);
846 return result;
849 static PyMethodDef rawiobase_methods[] = {
850 {"read", rawiobase_read, METH_VARARGS},
851 {"readall", rawiobase_readall, METH_NOARGS, rawiobase_readall_doc},
852 {NULL, NULL}
855 PyTypeObject PyRawIOBase_Type = {
856 PyVarObject_HEAD_INIT(NULL, 0)
857 "_io._RawIOBase", /*tp_name*/
858 0, /*tp_basicsize*/
859 0, /*tp_itemsize*/
860 0, /*tp_dealloc*/
861 0, /*tp_print*/
862 0, /*tp_getattr*/
863 0, /*tp_setattr*/
864 0, /*tp_compare */
865 0, /*tp_repr*/
866 0, /*tp_as_number*/
867 0, /*tp_as_sequence*/
868 0, /*tp_as_mapping*/
869 0, /*tp_hash */
870 0, /*tp_call*/
871 0, /*tp_str*/
872 0, /*tp_getattro*/
873 0, /*tp_setattro*/
874 0, /*tp_as_buffer*/
875 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
876 rawiobase_doc, /* tp_doc */
877 0, /* tp_traverse */
878 0, /* tp_clear */
879 0, /* tp_richcompare */
880 0, /* tp_weaklistoffset */
881 0, /* tp_iter */
882 0, /* tp_iternext */
883 rawiobase_methods, /* tp_methods */
884 0, /* tp_members */
885 0, /* tp_getset */
886 &PyIOBase_Type, /* tp_base */
887 0, /* tp_dict */
888 0, /* tp_descr_get */
889 0, /* tp_descr_set */
890 0, /* tp_dictoffset */
891 0, /* tp_init */
892 0, /* tp_alloc */
893 0, /* tp_new */