]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #15841: The readable(), writable() and seekable() methods of BytesIO
authorAntoine Pitrou <solipsis@pitrou.net>
Wed, 5 Sep 2012 18:11:49 +0000 (20:11 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Wed, 5 Sep 2012 18:11:49 +0000 (20:11 +0200)
and StringIO objects now raise ValueError when the object has been closed.
Patch by Alessandro Moura.

Lib/_pyio.py
Lib/test/test_memoryio.py
Misc/ACKS
Misc/NEWS
Modules/_io/bytesio.c
Modules/_io/stringio.c

index a2faeb32579b0b8b2b520a3e48316f18801e8e33..2d376d83002db72df196b0f179f8b4c6786c1ddf 100644 (file)
@@ -889,12 +889,18 @@ class BytesIO(BufferedIOBase):
         return pos
 
     def readable(self):
+        if self.closed:
+            raise ValueError("I/O operation on closed file.")
         return True
 
     def writable(self):
+        if self.closed:
+            raise ValueError("I/O operation on closed file.")
         return True
 
     def seekable(self):
+        if self.closed:
+            raise ValueError("I/O operation on closed file.")
         return True
 
 
@@ -1567,6 +1573,8 @@ class TextIOWrapper(TextIOBase):
         return self._buffer
 
     def seekable(self):
+        if self.closed:
+            raise ValueError("I/O operation on closed file.")
         return self._seekable
 
     def readable(self):
index 92480989acaa75bc01a595cdcb8a80b97f3f83fa..7c0c84f0d950457c3c17c5d8efb56067f2f21a8b 100644 (file)
@@ -318,9 +318,9 @@ class MemoryTestMixin:
         self.assertEqual(memio.isatty(), False)
         self.assertEqual(memio.closed, False)
         memio.close()
-        self.assertEqual(memio.writable(), True)
-        self.assertEqual(memio.readable(), True)
-        self.assertEqual(memio.seekable(), True)
+        self.assertRaises(ValueError, memio.writable)
+        self.assertRaises(ValueError, memio.readable)
+        self.assertRaises(ValueError, memio.seekable)
         self.assertRaises(ValueError, memio.isatty)
         self.assertEqual(memio.closed, True)
 
@@ -665,7 +665,6 @@ class CBytesIOTest(PyBytesIOTest):
         check(io.BytesIO(b'a'), basesize + 1 + 1 )
         check(io.BytesIO(b'a' * 1000), basesize + 1000 + 1 )
 
-
 class CStringIOTest(PyStringIOTest):
     ioclass = io.StringIO
     UnsupportedOperation = io.UnsupportedOperation
index f534143cb601d1d047197bf394ca654b6d209fad..5df7962c489ce4ccc8eeb1472de2a29270b737a8 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -658,6 +658,7 @@ Skip Montanaro
 Paul Moore
 Derek Morr
 James A Morrison
+Alessandro Moura
 Pablo Mouzo
 Mher Movsisyan
 Ruslan Mstoi
index 93b679b37ffb0546f7e9c566be4ca36d59887684..3e75f44b856f2cc2ff6b6feb9d8f31077bd8b560 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -115,6 +115,10 @@ Core and Builtins
 Library
 -------
 
+- 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.
+
 - Issue #15509: webbrowser.UnixBrowser no longer passes empty arguments to
   Popen when %action substitutions produce empty strings.
 
index 03f4c7d14aad8136044ace76a3aa350603bfc344..ef951aaa031e31e711388c71dfe73aaefbdb1a21 100644 (file)
@@ -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},
index c9d14b114b181786f582a1840a370ca4b5e62279..1f1fa40b02d7bd4ac642e49e136dfcd5b34d1331 100644 (file)
@@ -650,10 +650,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;
 }
 
@@ -661,6 +672,7 @@ static PyObject *
 stringio_readable(stringio *self, PyObject *args)
 {
     CHECK_INITIALIZED(self);
+    CHECK_CLOSED(self);
     Py_RETURN_TRUE;
 }
 
@@ -668,6 +680,7 @@ static PyObject *
 stringio_writable(stringio *self, PyObject *args)
 {
     CHECK_INITIALIZED(self);
+    CHECK_CLOSED(self);
     Py_RETURN_TRUE;
 }
 
@@ -835,9 +848,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},