Index: httplib.py =================================================================== --- httplib.py (revision 67204) +++ httplib.py (working copy) @@ -616,7 +616,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) @@ -1130,9 +1130,20 @@ 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 + ) + __str__=__repr__ class ImproperConnectionState(HTTPException): pass Index: test/test_httplib.py =================================================================== --- test/test_httplib.py (revision 67204) +++ test/test_httplib.py (working copy) @@ -181,6 +181,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: @@ -193,7 +195,24 @@ 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, i: + self.assertEquals(i.partial, 'Hello\r\n') + print i + 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() + resp.close() + class OfflineTest(TestCase): def test_responses(self): self.assertEquals(httplib.responses[httplib.NOT_FOUND], "Not Found")