[Python-checkins] r88622 - in python/branches/py3k: Lib/socket.py Lib/test/test_socket.py Misc/NEWS

antoine.pitrou python-checkins at python.org
Sat Feb 26 00:07:44 CET 2011


Author: antoine.pitrou
Date: Sat Feb 26 00:07:44 2011
New Revision: 88622

Log:
Issue #7322: Trying to read from a socket's file-like object after a timeout
occurred now raises an error instead of silently losing data.
Patch by Ross Lagerwall.



Modified:
   python/branches/py3k/Lib/socket.py
   python/branches/py3k/Lib/test/test_socket.py
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Lib/socket.py
==============================================================================
--- python/branches/py3k/Lib/socket.py	(original)
+++ python/branches/py3k/Lib/socket.py	Sat Feb 26 00:07:44 2011
@@ -257,6 +257,7 @@
         self._mode = mode
         self._reading = "r" in mode
         self._writing = "w" in mode
+        self._timeout_occurred = False
 
     def readinto(self, b):
         """Read up to len(b) bytes into the writable buffer *b* and return
@@ -268,9 +269,14 @@
         """
         self._checkClosed()
         self._checkReadable()
+        if self._timeout_occurred:
+            raise IOError("cannot read from timed out object")
         while True:
             try:
                 return self._sock.recv_into(b)
+            except timeout:
+                self._timeout_occurred = True
+                raise
             except error as e:
                 n = e.args[0]
                 if n == EINTR:

Modified: python/branches/py3k/Lib/test/test_socket.py
==============================================================================
--- python/branches/py3k/Lib/test/test_socket.py	(original)
+++ python/branches/py3k/Lib/test/test_socket.py	Sat Feb 26 00:07:44 2011
@@ -1109,6 +1109,23 @@
         self.write_file = None
         SocketConnectedTest.clientTearDown(self)
 
+    def testReadAfterTimeout(self):
+        # Issue #7322: A file object must disallow further reads
+        # after a timeout has occurred.
+        self.cli_conn.settimeout(1)
+        self.read_file.read(3)
+        # First read raises a timeout
+        self.assertRaises(socket.timeout, self.read_file.read, 1)
+        # Second read is disallowed
+        with self.assertRaises(IOError) as ctx:
+            self.read_file.read(1)
+        self.assertIn("cannot read from timed out object", str(ctx.exception))
+
+    def _testReadAfterTimeout(self):
+        self.write_file.write(self.write_msg[0:3])
+        self.write_file.flush()
+        self.serv_finished.wait()
+
     def testSmallRead(self):
         # Performing small file read test
         first_seg = self.read_file.read(len(self.read_msg)-3)

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Sat Feb 26 00:07:44 2011
@@ -35,6 +35,9 @@
 Library
 -------
 
+- Issue #7322: Trying to read from a socket's file-like object after a timeout
+  occurred now raises an error instead of silently losing data.
+
 - Issue 11291: poplib.POP no longer suppresses errors on quit().
 
 - Issue 11177: asyncore's create_socket() arguments can now be omitted.


More information about the Python-checkins mailing list