]> granicus.if.org Git - python/commitdiff
Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError...
authorAntoine Pitrou <solipsis@pitrou.net>
Fri, 14 Sep 2012 15:28:10 +0000 (17:28 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Fri, 14 Sep 2012 15:28:10 +0000 (17:28 +0200)
Patch by Alessandro Moura.

Lib/socket.py
Lib/test/test_socket.py
Misc/NEWS

index a93cd11248c7a041c712a06b6ca6c1403de45589..ea56a67d555eddd7d62df9c02154ea9d0ea656d5 100644 (file)
@@ -315,12 +315,23 @@ class SocketIO(io.RawIOBase):
     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.
index cce0d1b6eb4f917ddcc6611ed820b7371bed9224..e2ed21d222b86767fce00ed5072a065275a330a6 100644 (file)
@@ -839,6 +839,17 @@ class GeneralModuleTests(unittest.TestCase):
             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 testListenBacklog0(self):
         srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         srv.bind((HOST, 0))
index 24c6b7121d5d27a32764f797a442e4c5e7454f9b..29e2c1956562292bedac7b682caf4f4ec3efd91c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -120,6 +120,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #15842: the SocketIO.{readable,writable,seekable} methods now
+  raise ValueError when the file-like object is closed.  Patch by Alessandro
+  Moura.
+
 - Issue #15881: Fixed atexit hook in multiprocessing.  Original patch
   by Chris McDonough.