]> granicus.if.org Git - python/commitdiff
give httplib.IncompleteRead a more sane repr #4308
authorBenjamin Peterson <benjamin@python.org>
Mon, 2 Mar 2009 22:41:42 +0000 (22:41 +0000)
committerBenjamin Peterson <benjamin@python.org>
Mon, 2 Mar 2009 22:41:42 +0000 (22:41 +0000)
Lib/httplib.py
Lib/test/test_httplib.py
Misc/ACKS
Misc/NEWS

index 85e49abd28803404d7e2081a982ec9a0837160ba..2e749eabb6d1b045ccdbdce42d0fdabade08a5ae 100644 (file)
@@ -626,7 +626,7 @@ class HTTPResponse:
         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 @@ class UnimplementedFileMode(HTTPException):
     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
index c8c0648e51352d900fc86dc26b22edd2816ea54e..54a2b0ea6dcf131a7b9117e2f50197612c655ce1 100644 (file)
@@ -185,6 +185,8 @@ class BasicTest(TestCase):
                 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 @@ class BasicTest(TestCase):
         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):
index 2438d1e3e9727c429881238667906c1567a035e3..426cd9a336c5e8c23b7fe513ba913881daee84d5 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -765,6 +765,7 @@ Dik Winter
 Blake Winton
 Jean-Claude Wippler
 Lars Wirzenius
+Chris Withers
 Stefan Witzel
 David Wolever
 Klaus-Juergen Wolf
index a9a249a309eeabb0475ab046e7c82405033f0b62..8c524bba16ebceb30d9aafb4c1b6bfa84f758b92 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -168,6 +168,9 @@ Core and Builtins
 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.