From: Ben Darnell Date: Sun, 14 Feb 2016 02:15:34 +0000 (-0500) Subject: Define `httpclient.HTTPError.__repr__` X-Git-Tag: v4.4.0b1~51 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=22d884c5b94fed3b602bef6798cb732b4c916421;p=thirdparty%2Ftornado.git Define `httpclient.HTTPError.__repr__` This avoids stack overflow in the default `__repr__`. Fixes #1624 --- diff --git a/tornado/httpclient.py b/tornado/httpclient.py index 90c78119b..32ce1e0b6 100644 --- a/tornado/httpclient.py +++ b/tornado/httpclient.py @@ -615,6 +615,12 @@ class HTTPError(Exception): def __str__(self): return "HTTP %d: %s" % (self.code, self.message) + # There is a cyclic reference between self and self.response, + # which breaks the default __repr__ implementation. + # (especially on pypy, which doesn't have the same recursion + # detection as cpython). + __repr__ = __str__ + class _RequestProxy(object): """Combines an object with a dictionary of defaults. diff --git a/tornado/test/httpclient_test.py b/tornado/test/httpclient_test.py index e1496e9b2..9e8e62c6f 100644 --- a/tornado/test/httpclient_test.py +++ b/tornado/test/httpclient_test.py @@ -655,6 +655,15 @@ class HTTPErrorTestCase(unittest.TestCase): self.assertIsNot(e, e2) self.assertEqual(e.code, e2.code) - def test_str(self): + def test_plain_error(self): e = HTTPError(403) self.assertEqual(str(e), "HTTP 403: Forbidden") + self.assertEqual(repr(e), "HTTP 403: Forbidden") + + def test_error_with_response(self): + resp = HTTPResponse(HTTPRequest('http://example.com/'), 403) + with self.assertRaises(HTTPError) as cm: + resp.rethrow() + e = cm.exception + self.assertEqual(str(e), "HTTP 403: Forbidden") + self.assertEqual(repr(e), "HTTP 403: Forbidden")