diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2012-09-05 20:13:48 +0200 |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2012-09-05 20:13:48 +0200 |
commit | 2a7f4e1757c5146a5f86f70ae69b91d99d707e3f (patch) | |
tree | 34c5f2e691444701fdd33d166fb36666420cf2a2 /Modules/_io | |
parent | 73c00e0a6fd92d7c38ae7fd73f35753d69464524 (diff) | |
parent | 0424dd1cb2615c3afca7407ad40866c3aae2af45 (diff) | |
download | cpython-2a7f4e1757c5146a5f86f70ae69b91d99d707e3f.tar.gz |
Issue #15841: The readable(), writable() and seekable() methods of BytesIO
and StringIO objects now raise ValueError when the object has been closed.
Patch by Alessandro Moura.
Diffstat (limited to 'Modules/_io')
-rw-r--r-- | Modules/_io/bytesio.c | 20 | ||||
-rw-r--r-- | Modules/_io/stringio.c | 19 |
2 files changed, 31 insertions, 8 deletions
diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c index 3d027e238d..20863af7c3 100644 --- a/Modules/_io/bytesio.c +++ b/Modules/_io/bytesio.c @@ -121,7 +121,7 @@ resize_buffer(bytesio *self, size_t size) } /* Internal routine for writing a string of bytes to the buffer of a BytesIO - object. Returns the number of bytes wrote, or -1 on error. */ + object. Returns the number of bytes written, or -1 on error. */ static Py_ssize_t write_bytes(bytesio *self, const char *bytes, Py_ssize_t len) { @@ -171,10 +171,20 @@ bytesio_get_closed(bytesio *self) } } +PyDoc_STRVAR(readable_doc, +"readable() -> bool. Returns True if the IO object can be read."); + +PyDoc_STRVAR(writable_doc, +"writable() -> bool. Returns True if the IO object can be written."); + +PyDoc_STRVAR(seekable_doc, +"seekable() -> bool. Returns True if the IO object can be seeked."); + /* Generic getter for the writable, readable and seekable properties */ static PyObject * -return_true(bytesio *self) +return_not_closed(bytesio *self) { + CHECK_CLOSED(self); Py_RETURN_TRUE; } @@ -867,9 +877,9 @@ static PyGetSetDef bytesio_getsetlist[] = { }; static struct PyMethodDef bytesio_methods[] = { - {"readable", (PyCFunction)return_true, METH_NOARGS, NULL}, - {"seekable", (PyCFunction)return_true, METH_NOARGS, NULL}, - {"writable", (PyCFunction)return_true, METH_NOARGS, NULL}, + {"readable", (PyCFunction)return_not_closed, METH_NOARGS, readable_doc}, + {"seekable", (PyCFunction)return_not_closed, METH_NOARGS, seekable_doc}, + {"writable", (PyCFunction)return_not_closed, METH_NOARGS, writable_doc}, {"close", (PyCFunction)bytesio_close, METH_NOARGS, close_doc}, {"flush", (PyCFunction)bytesio_flush, METH_NOARGS, flush_doc}, {"isatty", (PyCFunction)bytesio_isatty, METH_NOARGS, isatty_doc}, diff --git a/Modules/_io/stringio.c b/Modules/_io/stringio.c index a1c31c0856..9d73884f73 100644 --- a/Modules/_io/stringio.c +++ b/Modules/_io/stringio.c @@ -760,10 +760,21 @@ stringio_init(stringio *self, PyObject *args, PyObject *kwds) } /* Properties and pseudo-properties */ + +PyDoc_STRVAR(stringio_readable_doc, +"readable() -> bool. Returns True if the IO object can be read."); + +PyDoc_STRVAR(stringio_writable_doc, +"writable() -> bool. Returns True if the IO object can be written."); + +PyDoc_STRVAR(stringio_seekable_doc, +"seekable() -> bool. Returns True if the IO object can be seeked."); + static PyObject * stringio_seekable(stringio *self, PyObject *args) { CHECK_INITIALIZED(self); + CHECK_CLOSED(self); Py_RETURN_TRUE; } @@ -771,6 +782,7 @@ static PyObject * stringio_readable(stringio *self, PyObject *args) { CHECK_INITIALIZED(self); + CHECK_CLOSED(self); Py_RETURN_TRUE; } @@ -778,6 +790,7 @@ static PyObject * stringio_writable(stringio *self, PyObject *args) { CHECK_INITIALIZED(self); + CHECK_CLOSED(self); Py_RETURN_TRUE; } @@ -956,9 +969,9 @@ static struct PyMethodDef stringio_methods[] = { {"seek", (PyCFunction)stringio_seek, METH_VARARGS, stringio_seek_doc}, {"write", (PyCFunction)stringio_write, METH_O, stringio_write_doc}, - {"seekable", (PyCFunction)stringio_seekable, METH_NOARGS}, - {"readable", (PyCFunction)stringio_readable, METH_NOARGS}, - {"writable", (PyCFunction)stringio_writable, METH_NOARGS}, + {"seekable", (PyCFunction)stringio_seekable, METH_NOARGS, stringio_seekable_doc}, + {"readable", (PyCFunction)stringio_readable, METH_NOARGS, stringio_readable_doc}, + {"writable", (PyCFunction)stringio_writable, METH_NOARGS, stringio_writable_doc}, {"__getstate__", (PyCFunction)stringio_getstate, METH_NOARGS}, {"__setstate__", (PyCFunction)stringio_setstate, METH_O}, |