summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2012-09-14 15:30:31 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2012-09-14 15:30:31 (GMT)
commit9b1c84b5861bf259e709b56b7a11824b114801c9 (patch)
tree43c814ceb026d8aff9246d158f9e238956497b6d
parent8429b6784bd7447055c7880e1b84954cd27bd0a3 (diff)
parent1e7ee9dfa0cc5007da1cbc3331b799584af8b680 (diff)
downloadcpython-9b1c84b5861bf259e709b56b7a11824b114801c9.zip
cpython-9b1c84b5861bf259e709b56b7a11824b114801c9.tar.gz
cpython-9b1c84b5861bf259e709b56b7a11824b114801c9.tar.bz2
Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Patch by Alessandro Moura.
-rw-r--r--Lib/socket.py15
-rw-r--r--Lib/test/test_socket.py11
-rw-r--r--Misc/NEWS4
3 files changed, 28 insertions, 2 deletions
diff --git a/Lib/socket.py b/Lib/socket.py
index 1378b0f..d4f1b65 100644
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -324,12 +324,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.
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 7716d33..d7c9a31 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -1245,6 +1245,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 test_pickle(self):
sock = socket.socket()
with sock:
diff --git a/Misc/NEWS b/Misc/NEWS
index d72a9d5..990f4d6 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,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 #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