Skip to content

Commit

Permalink
Define httpclient.HTTPError.__repr__
Browse files Browse the repository at this point in the history
This avoids stack overflow in the default `__repr__`.

Fixes tornadoweb#1624
  • Loading branch information
bdarnell committed Feb 14, 2016
1 parent e2dac2c commit 22d884c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 6 additions & 0 deletions tornado/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,12 @@ def __init__(self, code, message=None, response=None):
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.
Expand Down
11 changes: 10 additions & 1 deletion tornado/test/httpclient_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,15 @@ def test_copy(self):
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('https://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")

0 comments on commit 22d884c

Please sign in to comment.