[Python-checkins] cpython (merge 3.2 -> default): Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise

antoine.pitrou python-checkins at python.org
Fri Sep 14 17:34:19 CEST 2012


http://hg.python.org/cpython/rev/3b0e20f71d8a
changeset:   79027:3b0e20f71d8a
parent:      79024:0ed61ee823d8
parent:      79026:fad797916266
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Fri Sep 14 17:30:31 2012 +0200
summary:
  Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Patch by Alessandro Moura.

files:
  Lib/socket.py           |  15 +++++++++++++--
  Lib/test/test_socket.py |  11 +++++++++++
  Misc/NEWS               |   4 ++++
  3 files changed, 28 insertions(+), 2 deletions(-)


diff --git a/Lib/socket.py b/Lib/socket.py
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -324,12 +324,23 @@
     def readable(self):
         """True if the SocketIO is open for reading.
         """
-        return self._reading and not self.closed
+        if self.closed:
+            raise ValueError("I/O operation on closed socket.")
+        return self._reading
 
     def writable(self):
         """True if the SocketIO is open for writing.
         """
-        return self._writing and not self.closed
+        if self.closed:
+            raise ValueError("I/O operation on closed socket.")
+        return self._writing
+
+    def seekable(self):
+        """True if the SocketIO is open for seeking.
+        """
+        if self.closed:
+            raise ValueError("I/O operation on closed socket.")
+        return super().seekable()
 
     def fileno(self):
         """Return the file descriptor of the underlying socket.
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -1245,6 +1245,17 @@
             fp.close()
             self.assertEqual(repr(fp), "<_io.BufferedReader name=-1>")
 
+    def test_unusable_closed_socketio(self):
+        with socket.socket() as sock:
+            fp = sock.makefile("rb", buffering=0)
+            self.assertTrue(fp.readable())
+            self.assertFalse(fp.writable())
+            self.assertFalse(fp.seekable())
+            fp.close()
+            self.assertRaises(ValueError, fp.readable)
+            self.assertRaises(ValueError, fp.writable)
+            self.assertRaises(ValueError, fp.seekable)
+
     def test_pickle(self):
         sock = socket.socket()
         with sock:
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,10 @@
 Library
 -------
 
+- Issue #15842: the SocketIO.{readable,writable,seekable} methods now
+  raise ValueError when the file-like object is closed.  Patch by Alessandro
+  Moura.
+
 - Issue #15882: Change _decimal to accept any coefficient tuple when
   constructing infinities. This is done for backwards compatibility
   with decimal.py: Infinity coefficients are undefined in _decimal

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list