[Python-checkins] r70107 - in python/trunk: Lib/httplib.py Lib/test/test_httplib.py Misc/ACKS Misc/NEWS

benjamin.peterson python-checkins at python.org
Mon Mar 2 23:41:42 CET 2009


Author: benjamin.peterson
Date: Mon Mar  2 23:41:42 2009
New Revision: 70107

Log:
give httplib.IncompleteRead a more sane repr #4308

Modified:
   python/trunk/Lib/httplib.py
   python/trunk/Lib/test/test_httplib.py
   python/trunk/Misc/ACKS
   python/trunk/Misc/NEWS

Modified: python/trunk/Lib/httplib.py
==============================================================================
--- python/trunk/Lib/httplib.py	(original)
+++ python/trunk/Lib/httplib.py	Mon Mar  2 23:41:42 2009
@@ -626,7 +626,7 @@
         while amt > 0:
             chunk = self.fp.read(min(amt, MAXAMOUNT))
             if not chunk:
-                raise IncompleteRead(s)
+                raise IncompleteRead(''.join(s), amt)
             s.append(chunk)
             amt -= len(chunk)
         return ''.join(s)
@@ -1161,9 +1161,18 @@
     pass
 
 class IncompleteRead(HTTPException):
-    def __init__(self, partial):
+    def __init__(self, partial, expected=None):
         self.args = partial,
         self.partial = partial
+        self.expected = expected
+    def __repr__(self):
+        if self.expected is not None:
+            e = ', %i more expected' % self.expected
+        else:
+            e = ''
+        return 'IncompleteRead(%i bytes read%s)' % (len(self.partial), e)
+    def __str__(self):
+        return repr(self)
 
 class ImproperConnectionState(HTTPException):
     pass

Modified: python/trunk/Lib/test/test_httplib.py
==============================================================================
--- python/trunk/Lib/test/test_httplib.py	(original)
+++ python/trunk/Lib/test/test_httplib.py	Mon Mar  2 23:41:42 2009
@@ -185,6 +185,8 @@
                 resp.read()
             except httplib.IncompleteRead, i:
                 self.assertEquals(i.partial, 'hello world')
+                self.assertEqual(repr(i),'IncompleteRead(11 bytes read)')
+                self.assertEqual(str(i),'IncompleteRead(11 bytes read)')
             else:
                 self.fail('IncompleteRead expected')
             finally:
@@ -198,6 +200,23 @@
         self.assertEquals(resp.read(), 'Hello\r\n')
         resp.close()
 
+    def test_incomplete_read(self):
+        sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello\r\n')
+        resp = httplib.HTTPResponse(sock, method="GET")
+        resp.begin()
+        try:
+            resp.read()
+        except httplib.IncompleteRead as i:
+            self.assertEquals(i.partial, 'Hello\r\n')
+            self.assertEqual(repr(i),
+                             "IncompleteRead(7 bytes read, 3 more expected)")
+            self.assertEqual(str(i),
+                             "IncompleteRead(7 bytes read, 3 more expected)")
+        else:
+            self.fail('IncompleteRead expected')
+        finally:
+            resp.close()
+
 
 class OfflineTest(TestCase):
     def test_responses(self):

Modified: python/trunk/Misc/ACKS
==============================================================================
--- python/trunk/Misc/ACKS	(original)
+++ python/trunk/Misc/ACKS	Mon Mar  2 23:41:42 2009
@@ -765,6 +765,7 @@
 Blake Winton
 Jean-Claude Wippler
 Lars Wirzenius
+Chris Withers
 Stefan Witzel
 David Wolever
 Klaus-Juergen Wolf

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Mon Mar  2 23:41:42 2009
@@ -168,6 +168,9 @@
 Library
 -------
 
+- Issue #4308: httplib.IncompleteRead's repr doesn't include all of the data all
+  ready received.
+
 - Issue #5401: Fixed a performance problem in mimetypes when ``from mimetypes
   import guess_extension`` was used.
 


More information about the Python-checkins mailing list