]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Define `httpclient.HTTPError.__repr__`
authorBen Darnell <ben@bendarnell.com>
Sun, 14 Feb 2016 02:15:34 +0000 (21:15 -0500)
committerBen Darnell <ben@bendarnell.com>
Sun, 14 Feb 2016 02:15:34 +0000 (21:15 -0500)
This avoids stack overflow in the default `__repr__`.

Fixes #1624

tornado/httpclient.py
tornado/test/httpclient_test.py

index 90c78119b7ca2af7c98fbb7af9a3c711857cee19..32ce1e0b6867161766f2b197f77e5c5624395054 100644 (file)
@@ -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.
index e1496e9b2ed5d36de1bb683afded7eda6bef373c..9e8e62c6f90a4e390e8c5eae742d6e238f3a943a 100644 (file)
@@ -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")