+++ /dev/null
-/*\r
- An implementation of the I/O abstract base classes hierarchy\r
- as defined by PEP 3116 - "New I/O"\r
- \r
- Classes defined here: IOBase, RawIOBase.\r
- \r
- Written by Amaury Forgeot d'Arc and Antoine Pitrou\r
-*/\r
-\r
-\r
-#define PY_SSIZE_T_CLEAN\r
-#include "Python.h"\r
-#include "structmember.h"\r
-#include "_iomodule.h"\r
-\r
-/*\r
- * IOBase class, an abstract class\r
- */\r
-\r
-typedef struct {\r
- PyObject_HEAD\r
- \r
- PyObject *dict;\r
- PyObject *weakreflist;\r
-} iobase;\r
-\r
-PyDoc_STRVAR(iobase_doc,\r
- "The abstract base class for all I/O classes, acting on streams of\n"\r
- "bytes. There is no public constructor.\n"\r
- "\n"\r
- "This class provides dummy implementations for many methods that\n"\r
- "derived classes can override selectively; the default implementations\n"\r
- "represent a file that cannot be read, written or seeked.\n"\r
- "\n"\r
- "Even though IOBase does not declare read, readinto, or write because\n"\r
- "their signatures will vary, implementations and clients should\n"\r
- "consider those methods part of the interface. Also, implementations\n"\r
- "may raise a IOError when operations they do not support are called.\n"\r
- "\n"\r
- "The basic type used for binary data read from or written to a file is\n"\r
- "bytes. bytearrays are accepted too, and in some cases (such as\n"\r
- "readinto) needed. Text I/O classes work with str data.\n"\r
- "\n"\r
- "Note that calling any method (even inquiries) on a closed stream is\n"\r
- "undefined. Implementations may raise IOError in this case.\n"\r
- "\n"\r
- "IOBase (and its subclasses) support the iterator protocol, meaning\n"\r
- "that an IOBase object can be iterated over yielding the lines in a\n"\r
- "stream.\n"\r
- "\n"\r
- "IOBase also supports the :keyword:`with` statement. In this example,\n"\r
- "fp is closed after the suite of the with statement is complete:\n"\r
- "\n"\r
- "with open('spam.txt', 'r') as fp:\n"\r
- " fp.write('Spam and eggs!')\n");\r
-\r
-/* Use this macro whenever you want to check the internal `closed` status\r
- of the IOBase object rather than the virtual `closed` attribute as returned\r
- by whatever subclass. */\r
-\r
-#define IS_CLOSED(self) \\r
- PyObject_HasAttrString(self, "__IOBase_closed")\r
-\r
-/* Internal methods */\r
-static PyObject *\r
-iobase_unsupported(const char *message)\r
-{\r
- PyErr_SetString(_PyIO_unsupported_operation, message);\r
- return NULL;\r
-}\r
-\r
-/* Positionning */\r
-\r
-PyDoc_STRVAR(iobase_seek_doc,\r
- "Change stream position.\n"\r
- "\n"\r
- "Change the stream position to byte offset offset. offset is\n"\r
- "interpreted relative to the position indicated by whence. Values\n"\r
- "for whence are:\n"\r
- "\n"\r
- "* 0 -- start of stream (the default); offset should be zero or positive\n"\r
- "* 1 -- current stream position; offset may be negative\n"\r
- "* 2 -- end of stream; offset is usually negative\n"\r
- "\n"\r
- "Return the new absolute position.");\r
-\r
-static PyObject *\r
-iobase_seek(PyObject *self, PyObject *args)\r
-{\r
- return iobase_unsupported("seek");\r
-}\r
-\r
-PyDoc_STRVAR(iobase_tell_doc,\r
- "Return current stream position.");\r
-\r
-static PyObject *\r
-iobase_tell(PyObject *self, PyObject *args)\r
-{\r
- return PyObject_CallMethod(self, "seek", "ii", 0, 1);\r
-}\r
-\r
-PyDoc_STRVAR(iobase_truncate_doc,\r
- "Truncate file to size bytes.\n"\r
- "\n"\r
- "File pointer is left unchanged. Size defaults to the current IO\n"\r
- "position as reported by tell(). Returns the new size.");\r
-\r
-static PyObject *\r
-iobase_truncate(PyObject *self, PyObject *args)\r
-{\r
- return iobase_unsupported("truncate");\r
-}\r
-\r
-/* Flush and close methods */\r
-\r
-PyDoc_STRVAR(iobase_flush_doc,\r
- "Flush write buffers, if applicable.\n"\r
- "\n"\r
- "This is not implemented for read-only and non-blocking streams.\n");\r
-\r
-static PyObject *\r
-iobase_flush(PyObject *self, PyObject *args)\r
-{\r
- /* XXX Should this return the number of bytes written??? */\r
- if (IS_CLOSED(self)) {\r
- PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");\r
- return NULL;\r
- }\r
- Py_RETURN_NONE;\r
-}\r
-\r
-PyDoc_STRVAR(iobase_close_doc,\r
- "Flush and close the IO object.\n"\r
- "\n"\r
- "This method has no effect if the file is already closed.\n");\r
-\r
-static int\r
-iobase_closed(PyObject *self)\r
-{\r
- PyObject *res;\r
- int closed;\r
- /* This gets the derived attribute, which is *not* __IOBase_closed\r
- in most cases! */\r
- res = PyObject_GetAttr(self, _PyIO_str_closed);\r
- if (res == NULL)\r
- return 0;\r
- closed = PyObject_IsTrue(res);\r
- Py_DECREF(res);\r
- return closed;\r
-}\r
-\r
-static PyObject *\r
-iobase_closed_get(PyObject *self, void *context)\r
-{\r
- return PyBool_FromLong(IS_CLOSED(self));\r
-}\r
-\r
-PyObject *\r
-_PyIOBase_check_closed(PyObject *self, PyObject *args)\r
-{\r
- if (iobase_closed(self)) {\r
- PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");\r
- return NULL;\r
- }\r
- if (args == Py_True)\r
- return Py_None;\r
- else\r
- Py_RETURN_NONE;\r
-}\r
-\r
-/* XXX: IOBase thinks it has to maintain its own internal state in\r
- `__IOBase_closed` and call flush() by itself, but it is redundant with\r
- whatever behaviour a non-trivial derived class will implement. */\r
-\r
-static PyObject *\r
-iobase_close(PyObject *self, PyObject *args)\r
-{\r
- PyObject *res;\r
-\r
- if (IS_CLOSED(self))\r
- Py_RETURN_NONE;\r
-\r
- res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);\r
- PyObject_SetAttrString(self, "__IOBase_closed", Py_True);\r
- if (res == NULL) {\r
- return NULL;\r
- }\r
- Py_XDECREF(res);\r
- Py_RETURN_NONE;\r
-}\r
-\r
-/* Finalization and garbage collection support */\r
-\r
-int\r
-_PyIOBase_finalize(PyObject *self)\r
-{\r
- PyObject *res;\r
- PyObject *tp, *v, *tb;\r
- int closed = 1;\r
- int is_zombie;\r
-\r
- /* If _PyIOBase_finalize() is called from a destructor, we need to\r
- resurrect the object as calling close() can invoke arbitrary code. */\r
- is_zombie = (Py_REFCNT(self) == 0);\r
- if (is_zombie) {\r
- ++Py_REFCNT(self);\r
- }\r
- PyErr_Fetch(&tp, &v, &tb);\r
- /* If `closed` doesn't exist or can't be evaluated as bool, then the\r
- object is probably in an unusable state, so ignore. */\r
- res = PyObject_GetAttr(self, _PyIO_str_closed);\r
- if (res == NULL)\r
- PyErr_Clear();\r
- else {\r
- closed = PyObject_IsTrue(res);\r
- Py_DECREF(res);\r
- if (closed == -1)\r
- PyErr_Clear();\r
- }\r
- if (closed == 0) {\r
- res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_close,\r
- NULL);\r
- /* Silencing I/O errors is bad, but printing spurious tracebacks is\r
- equally as bad, and potentially more frequent (because of\r
- shutdown issues). */\r
- if (res == NULL)\r
- PyErr_Clear();\r
- else\r
- Py_DECREF(res);\r
- }\r
- PyErr_Restore(tp, v, tb);\r
- if (is_zombie) {\r
- if (--Py_REFCNT(self) != 0) {\r
- /* The object lives again. The following code is taken from\r
- slot_tp_del in typeobject.c. */\r
- Py_ssize_t refcnt = Py_REFCNT(self);\r
- _Py_NewReference(self);\r
- Py_REFCNT(self) = refcnt;\r
- /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so\r
- * we need to undo that. */\r
- _Py_DEC_REFTOTAL;\r
- /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object\r
- * chain, so no more to do there.\r
- * If COUNT_ALLOCS, the original decref bumped tp_frees, and\r
- * _Py_NewReference bumped tp_allocs: both of those need to be\r
- * undone.\r
- */\r
-#ifdef COUNT_ALLOCS\r
- --Py_TYPE(self)->tp_frees;\r
- --Py_TYPE(self)->tp_allocs;\r
-#endif\r
- return -1;\r
- }\r
- }\r
- return 0;\r
-}\r
-\r
-static int\r
-iobase_traverse(iobase *self, visitproc visit, void *arg)\r
-{\r
- Py_VISIT(self->dict);\r
- return 0;\r
-}\r
-\r
-static int\r
-iobase_clear(iobase *self)\r
-{\r
- if (_PyIOBase_finalize((PyObject *) self) < 0)\r
- return -1;\r
- Py_CLEAR(self->dict);\r
- return 0;\r
-}\r
-\r
-/* Destructor */\r
-\r
-static void\r
-iobase_dealloc(iobase *self)\r
-{\r
- /* NOTE: since IOBaseObject has its own dict, Python-defined attributes\r
- are still available here for close() to use.\r
- However, if the derived class declares a __slots__, those slots are\r
- already gone.\r
- */\r
- if (_PyIOBase_finalize((PyObject *) self) < 0) {\r
- /* When called from a heap type's dealloc, the type will be\r
- decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */\r
- if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE))\r
- Py_INCREF(Py_TYPE(self));\r
- return;\r
- }\r
- _PyObject_GC_UNTRACK(self);\r
- if (self->weakreflist != NULL)\r
- PyObject_ClearWeakRefs((PyObject *) self);\r
- Py_CLEAR(self->dict);\r
- Py_TYPE(self)->tp_free((PyObject *) self);\r
-}\r
-\r
-/* Inquiry methods */\r
-\r
-PyDoc_STRVAR(iobase_seekable_doc,\r
- "Return whether object supports random access.\n"\r
- "\n"\r
- "If False, seek(), tell() and truncate() will raise IOError.\n"\r
- "This method may need to do a test seek().");\r
-\r
-static PyObject *\r
-iobase_seekable(PyObject *self, PyObject *args)\r
-{\r
- Py_RETURN_FALSE;\r
-}\r
-\r
-PyObject *\r
-_PyIOBase_check_seekable(PyObject *self, PyObject *args)\r
-{\r
- PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_seekable, NULL);\r
- if (res == NULL)\r
- return NULL;\r
- if (res != Py_True) {\r
- Py_CLEAR(res);\r
- PyErr_SetString(PyExc_IOError, "File or stream is not seekable.");\r
- return NULL;\r
- }\r
- if (args == Py_True) {\r
- Py_DECREF(res);\r
- }\r
- return res;\r
-}\r
-\r
-PyDoc_STRVAR(iobase_readable_doc,\r
- "Return whether object was opened for reading.\n"\r
- "\n"\r
- "If False, read() will raise IOError.");\r
-\r
-static PyObject *\r
-iobase_readable(PyObject *self, PyObject *args)\r
-{\r
- Py_RETURN_FALSE;\r
-}\r
-\r
-/* May be called with any object */\r
-PyObject *\r
-_PyIOBase_check_readable(PyObject *self, PyObject *args)\r
-{\r
- PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_readable, NULL);\r
- if (res == NULL)\r
- return NULL;\r
- if (res != Py_True) {\r
- Py_CLEAR(res);\r
- PyErr_SetString(PyExc_IOError, "File or stream is not readable.");\r
- return NULL;\r
- }\r
- if (args == Py_True) {\r
- Py_DECREF(res);\r
- }\r
- return res;\r
-}\r
-\r
-PyDoc_STRVAR(iobase_writable_doc,\r
- "Return whether object was opened for writing.\n"\r
- "\n"\r
- "If False, read() will raise IOError.");\r
-\r
-static PyObject *\r
-iobase_writable(PyObject *self, PyObject *args)\r
-{\r
- Py_RETURN_FALSE;\r
-}\r
-\r
-/* May be called with any object */\r
-PyObject *\r
-_PyIOBase_check_writable(PyObject *self, PyObject *args)\r
-{\r
- PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_writable, NULL);\r
- if (res == NULL)\r
- return NULL;\r
- if (res != Py_True) {\r
- Py_CLEAR(res);\r
- PyErr_SetString(PyExc_IOError, "File or stream is not writable.");\r
- return NULL;\r
- }\r
- if (args == Py_True) {\r
- Py_DECREF(res);\r
- }\r
- return res;\r
-}\r
-\r
-/* Context manager */\r
-\r
-static PyObject *\r
-iobase_enter(PyObject *self, PyObject *args)\r
-{\r
- if (_PyIOBase_check_closed(self, Py_True) == NULL)\r
- return NULL;\r
-\r
- Py_INCREF(self);\r
- return self;\r
-}\r
-\r
-static PyObject *\r
-iobase_exit(PyObject *self, PyObject *args)\r
-{\r
- return PyObject_CallMethodObjArgs(self, _PyIO_str_close, NULL);\r
-}\r
-\r
-/* Lower-level APIs */\r
-\r
-/* XXX Should these be present even if unimplemented? */\r
-\r
-PyDoc_STRVAR(iobase_fileno_doc,\r
- "Returns underlying file descriptor if one exists.\n"\r
- "\n"\r
- "An IOError is raised if the IO object does not use a file descriptor.\n");\r
-\r
-static PyObject *\r
-iobase_fileno(PyObject *self, PyObject *args)\r
-{\r
- return iobase_unsupported("fileno");\r
-}\r
-\r
-PyDoc_STRVAR(iobase_isatty_doc,\r
- "Return whether this is an 'interactive' stream.\n"\r
- "\n"\r
- "Return False if it can't be determined.\n");\r
-\r
-static PyObject *\r
-iobase_isatty(PyObject *self, PyObject *args)\r
-{\r
- if (_PyIOBase_check_closed(self, Py_True) == NULL)\r
- return NULL;\r
- Py_RETURN_FALSE;\r
-}\r
-\r
-/* Readline(s) and writelines */\r
-\r
-PyDoc_STRVAR(iobase_readline_doc,\r
- "Read and return a line from the stream.\n"\r
- "\n"\r
- "If limit is specified, at most limit bytes will be read.\n"\r
- "\n"\r
- "The line terminator is always b'\n' for binary files; for text\n"\r
- "files, the newlines argument to open can be used to select the line\n"\r
- "terminator(s) recognized.\n");\r
-\r
-static PyObject *\r
-iobase_readline(PyObject *self, PyObject *args)\r
-{\r
- /* For backwards compatibility, a (slowish) readline(). */\r
-\r
- Py_ssize_t limit = -1;\r
- int has_peek = 0;\r
- PyObject *buffer, *result;\r
- Py_ssize_t old_size = -1;\r
-\r
- if (!PyArg_ParseTuple(args, "|O&:readline", &_PyIO_ConvertSsize_t, &limit)) {\r
- return NULL;\r
- }\r
-\r
- if (PyObject_HasAttrString(self, "peek"))\r
- has_peek = 1;\r
-\r
- buffer = PyByteArray_FromStringAndSize(NULL, 0);\r
- if (buffer == NULL)\r
- return NULL;\r
-\r
- while (limit < 0 || Py_SIZE(buffer) < limit) {\r
- Py_ssize_t nreadahead = 1;\r
- PyObject *b;\r
-\r
- if (has_peek) {\r
- PyObject *readahead = PyObject_CallMethod(self, "peek", "i", 1);\r
- if (readahead == NULL)\r
- goto fail;\r
- if (!PyBytes_Check(readahead)) {\r
- PyErr_Format(PyExc_IOError,\r
- "peek() should have returned a bytes object, "\r
- "not '%.200s'", Py_TYPE(readahead)->tp_name);\r
- Py_DECREF(readahead);\r
- goto fail;\r
- }\r
- if (PyBytes_GET_SIZE(readahead) > 0) {\r
- Py_ssize_t n = 0;\r
- const char *buf = PyBytes_AS_STRING(readahead);\r
- if (limit >= 0) {\r
- do {\r
- if (n >= PyBytes_GET_SIZE(readahead) || n >= limit)\r
- break;\r
- if (buf[n++] == '\n')\r
- break;\r
- } while (1);\r
- }\r
- else {\r
- do {\r
- if (n >= PyBytes_GET_SIZE(readahead))\r
- break;\r
- if (buf[n++] == '\n')\r
- break;\r
- } while (1);\r
- }\r
- nreadahead = n;\r
- }\r
- Py_DECREF(readahead);\r
- }\r
-\r
- b = PyObject_CallMethod(self, "read", "n", nreadahead);\r
- if (b == NULL)\r
- goto fail;\r
- if (!PyBytes_Check(b)) {\r
- PyErr_Format(PyExc_IOError,\r
- "read() should have returned a bytes object, "\r
- "not '%.200s'", Py_TYPE(b)->tp_name);\r
- Py_DECREF(b);\r
- goto fail;\r
- }\r
- if (PyBytes_GET_SIZE(b) == 0) {\r
- Py_DECREF(b);\r
- break;\r
- }\r
-\r
- old_size = PyByteArray_GET_SIZE(buffer);\r
- PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b));\r
- memcpy(PyByteArray_AS_STRING(buffer) + old_size,\r
- PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b));\r
-\r
- Py_DECREF(b);\r
-\r
- if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n')\r
- break;\r
- }\r
-\r
- result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer),\r
- PyByteArray_GET_SIZE(buffer));\r
- Py_DECREF(buffer);\r
- return result;\r
- fail:\r
- Py_DECREF(buffer);\r
- return NULL;\r
-}\r
-\r
-static PyObject *\r
-iobase_iter(PyObject *self)\r
-{\r
- if (_PyIOBase_check_closed(self, Py_True) == NULL)\r
- return NULL;\r
-\r
- Py_INCREF(self);\r
- return self;\r
-}\r
-\r
-static PyObject *\r
-iobase_iternext(PyObject *self)\r
-{\r
- PyObject *line = PyObject_CallMethodObjArgs(self, _PyIO_str_readline, NULL);\r
-\r
- if (line == NULL)\r
- return NULL;\r
-\r
- if (PyObject_Size(line) == 0) {\r
- Py_DECREF(line);\r
- return NULL;\r
- }\r
-\r
- return line;\r
-}\r
-\r
-PyDoc_STRVAR(iobase_readlines_doc,\r
- "Return a list of lines from the stream.\n"\r
- "\n"\r
- "hint can be specified to control the number of lines read: no more\n"\r
- "lines will be read if the total size (in bytes/characters) of all\n"\r
- "lines so far exceeds hint.");\r
-\r
-static PyObject *\r
-iobase_readlines(PyObject *self, PyObject *args)\r
-{\r
- Py_ssize_t hint = -1, length = 0;\r
- PyObject *result;\r
-\r
- if (!PyArg_ParseTuple(args, "|O&:readlines", &_PyIO_ConvertSsize_t, &hint)) {\r
- return NULL;\r
- }\r
-\r
- result = PyList_New(0);\r
- if (result == NULL)\r
- return NULL;\r
-\r
- if (hint <= 0) {\r
- /* XXX special-casing this made sense in the Python version in order\r
- to remove the bytecode interpretation overhead, but it could\r
- probably be removed here. */\r
- PyObject *ret = PyObject_CallMethod(result, "extend", "O", self);\r
- if (ret == NULL) {\r
- Py_DECREF(result);\r
- return NULL;\r
- }\r
- Py_DECREF(ret);\r
- return result;\r
- }\r
-\r
- while (1) {\r
- PyObject *line = PyIter_Next(self);\r
- if (line == NULL) {\r
- if (PyErr_Occurred()) {\r
- Py_DECREF(result);\r
- return NULL;\r
- }\r
- else\r
- break; /* StopIteration raised */\r
- }\r
-\r
- if (PyList_Append(result, line) < 0) {\r
- Py_DECREF(line);\r
- Py_DECREF(result);\r
- return NULL;\r
- }\r
- length += PyObject_Size(line);\r
- Py_DECREF(line);\r
-\r
- if (length > hint)\r
- break;\r
- }\r
- return result;\r
-}\r
-\r
-static PyObject *\r
-iobase_writelines(PyObject *self, PyObject *args)\r
-{\r
- PyObject *lines, *iter, *res;\r
-\r
- if (!PyArg_ParseTuple(args, "O:writelines", &lines)) {\r
- return NULL;\r
- }\r
-\r
- if (_PyIOBase_check_closed(self, Py_True) == NULL)\r
- return NULL;\r
-\r
- iter = PyObject_GetIter(lines);\r
- if (iter == NULL)\r
- return NULL;\r
-\r
- while (1) {\r
- PyObject *line = PyIter_Next(iter);\r
- if (line == NULL) {\r
- if (PyErr_Occurred()) {\r
- Py_DECREF(iter);\r
- return NULL;\r
- }\r
- else\r
- break; /* Stop Iteration */\r
- }\r
-\r
- res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL);\r
- Py_DECREF(line);\r
- if (res == NULL) {\r
- Py_DECREF(iter);\r
- return NULL;\r
- }\r
- Py_DECREF(res);\r
- }\r
- Py_DECREF(iter);\r
- Py_RETURN_NONE;\r
-}\r
-\r
-static PyMethodDef iobase_methods[] = {\r
- {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc},\r
- {"tell", iobase_tell, METH_NOARGS, iobase_tell_doc},\r
- {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc},\r
- {"flush", iobase_flush, METH_NOARGS, iobase_flush_doc},\r
- {"close", iobase_close, METH_NOARGS, iobase_close_doc},\r
-\r
- {"seekable", iobase_seekable, METH_NOARGS, iobase_seekable_doc},\r
- {"readable", iobase_readable, METH_NOARGS, iobase_readable_doc},\r
- {"writable", iobase_writable, METH_NOARGS, iobase_writable_doc},\r
-\r
- {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS},\r
- {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS},\r
- {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS},\r
- {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS},\r
-\r
- {"fileno", iobase_fileno, METH_NOARGS, iobase_fileno_doc},\r
- {"isatty", iobase_isatty, METH_NOARGS, iobase_isatty_doc},\r
-\r
- {"__enter__", iobase_enter, METH_NOARGS},\r
- {"__exit__", iobase_exit, METH_VARARGS},\r
-\r
- {"readline", iobase_readline, METH_VARARGS, iobase_readline_doc},\r
- {"readlines", iobase_readlines, METH_VARARGS, iobase_readlines_doc},\r
- {"writelines", iobase_writelines, METH_VARARGS},\r
-\r
- {NULL, NULL}\r
-};\r
-\r
-static PyGetSetDef iobase_getset[] = {\r
- {"closed", (getter)iobase_closed_get, NULL, NULL},\r
- {NULL}\r
-};\r
-\r
-\r
-PyTypeObject PyIOBase_Type = {\r
- PyVarObject_HEAD_INIT(NULL, 0)\r
- "_io._IOBase", /*tp_name*/\r
- sizeof(iobase), /*tp_basicsize*/\r
- 0, /*tp_itemsize*/\r
- (destructor)iobase_dealloc, /*tp_dealloc*/\r
- 0, /*tp_print*/\r
- 0, /*tp_getattr*/\r
- 0, /*tp_setattr*/\r
- 0, /*tp_compare */\r
- 0, /*tp_repr*/\r
- 0, /*tp_as_number*/\r
- 0, /*tp_as_sequence*/\r
- 0, /*tp_as_mapping*/\r
- 0, /*tp_hash */\r
- 0, /*tp_call*/\r
- 0, /*tp_str*/\r
- 0, /*tp_getattro*/\r
- 0, /*tp_setattro*/\r
- 0, /*tp_as_buffer*/\r
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE\r
- | Py_TPFLAGS_HAVE_GC, /*tp_flags*/\r
- iobase_doc, /* tp_doc */\r
- (traverseproc)iobase_traverse, /* tp_traverse */\r
- (inquiry)iobase_clear, /* tp_clear */\r
- 0, /* tp_richcompare */\r
- offsetof(iobase, weakreflist), /* tp_weaklistoffset */\r
- iobase_iter, /* tp_iter */\r
- iobase_iternext, /* tp_iternext */\r
- iobase_methods, /* tp_methods */\r
- 0, /* tp_members */\r
- iobase_getset, /* tp_getset */\r
- 0, /* tp_base */\r
- 0, /* tp_dict */\r
- 0, /* tp_descr_get */\r
- 0, /* tp_descr_set */\r
- offsetof(iobase, dict), /* tp_dictoffset */\r
- 0, /* tp_init */\r
- 0, /* tp_alloc */\r
- PyType_GenericNew, /* tp_new */\r
-};\r
-\r
-\r
-/*\r
- * RawIOBase class, Inherits from IOBase.\r
- */\r
-PyDoc_STRVAR(rawiobase_doc,\r
- "Base class for raw binary I/O.");\r
-\r
-/*\r
- * The read() method is implemented by calling readinto(); derived classes\r
- * that want to support read() only need to implement readinto() as a\r
- * primitive operation. In general, readinto() can be more efficient than\r
- * read().\r
- *\r
- * (It would be tempting to also provide an implementation of readinto() in\r
- * terms of read(), in case the latter is a more suitable primitive operation,\r
- * but that would lead to nasty recursion in case a subclass doesn't implement\r
- * either.)\r
-*/\r
-\r
-static PyObject *\r
-rawiobase_read(PyObject *self, PyObject *args)\r
-{\r
- Py_ssize_t n = -1;\r
- PyObject *b, *res;\r
-\r
- if (!PyArg_ParseTuple(args, "|n:read", &n)) {\r
- return NULL;\r
- }\r
-\r
- if (n < 0)\r
- return PyObject_CallMethod(self, "readall", NULL);\r
-\r
- /* TODO: allocate a bytes object directly instead and manually construct\r
- a writable memoryview pointing to it. */\r
- b = PyByteArray_FromStringAndSize(NULL, n);\r
- if (b == NULL)\r
- return NULL;\r
-\r
- res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL);\r
- if (res == NULL || res == Py_None) {\r
- Py_DECREF(b);\r
- return res;\r
- }\r
-\r
- n = PyNumber_AsSsize_t(res, PyExc_ValueError);\r
- Py_DECREF(res);\r
- if (n == -1 && PyErr_Occurred()) {\r
- Py_DECREF(b);\r
- return NULL;\r
- }\r
-\r
- res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);\r
- Py_DECREF(b);\r
- return res;\r
-}\r
-\r
-\r
-PyDoc_STRVAR(rawiobase_readall_doc,\r
- "Read until EOF, using multiple read() call.");\r
-\r
-static PyObject *\r
-rawiobase_readall(PyObject *self, PyObject *args)\r
-{\r
- int r;\r
- PyObject *chunks = PyList_New(0);\r
- PyObject *result;\r
- \r
- if (chunks == NULL)\r
- return NULL;\r
-\r
- while (1) {\r
- PyObject *data = PyObject_CallMethod(self, "read",\r
- "i", DEFAULT_BUFFER_SIZE);\r
- if (!data) {\r
- Py_DECREF(chunks);\r
- return NULL;\r
- }\r
- if (data == Py_None) {\r
- if (PyList_GET_SIZE(chunks) == 0) {\r
- Py_DECREF(chunks);\r
- return data;\r
- }\r
- Py_DECREF(data);\r
- break;\r
- }\r
- if (!PyBytes_Check(data)) {\r
- Py_DECREF(chunks);\r
- Py_DECREF(data);\r
- PyErr_SetString(PyExc_TypeError, "read() should return bytes");\r
- return NULL;\r
- }\r
- if (PyBytes_GET_SIZE(data) == 0) {\r
- /* EOF */\r
- Py_DECREF(data);\r
- break;\r
- }\r
- r = PyList_Append(chunks, data);\r
- Py_DECREF(data);\r
- if (r < 0) {\r
- Py_DECREF(chunks);\r
- return NULL;\r
- }\r
- }\r
- result = _PyBytes_Join(_PyIO_empty_bytes, chunks);\r
- Py_DECREF(chunks);\r
- return result;\r
-}\r
-\r
-static PyMethodDef rawiobase_methods[] = {\r
- {"read", rawiobase_read, METH_VARARGS},\r
- {"readall", rawiobase_readall, METH_NOARGS, rawiobase_readall_doc},\r
- {NULL, NULL}\r
-};\r
-\r
-PyTypeObject PyRawIOBase_Type = {\r
- PyVarObject_HEAD_INIT(NULL, 0)\r
- "_io._RawIOBase", /*tp_name*/\r
- 0, /*tp_basicsize*/\r
- 0, /*tp_itemsize*/\r
- 0, /*tp_dealloc*/\r
- 0, /*tp_print*/\r
- 0, /*tp_getattr*/\r
- 0, /*tp_setattr*/\r
- 0, /*tp_compare */\r
- 0, /*tp_repr*/\r
- 0, /*tp_as_number*/\r
- 0, /*tp_as_sequence*/\r
- 0, /*tp_as_mapping*/\r
- 0, /*tp_hash */\r
- 0, /*tp_call*/\r
- 0, /*tp_str*/\r
- 0, /*tp_getattro*/\r
- 0, /*tp_setattro*/\r
- 0, /*tp_as_buffer*/\r
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/\r
- rawiobase_doc, /* tp_doc */\r
- 0, /* tp_traverse */\r
- 0, /* tp_clear */\r
- 0, /* tp_richcompare */\r
- 0, /* tp_weaklistoffset */\r
- 0, /* tp_iter */\r
- 0, /* tp_iternext */\r
- rawiobase_methods, /* tp_methods */\r
- 0, /* tp_members */\r
- 0, /* tp_getset */\r
- &PyIOBase_Type, /* tp_base */\r
- 0, /* tp_dict */\r
- 0, /* tp_descr_get */\r
- 0, /* tp_descr_set */\r
- 0, /* tp_dictoffset */\r
- 0, /* tp_init */\r
- 0, /* tp_alloc */\r
- 0, /* tp_new */\r
-};\r