Skip to content

Commit

Permalink
break cycle in chained exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed May 12, 2019
1 parent 777500b commit 44e38c2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Unreleased
float values. (:issue:`1511`)
- Update :class:`~middleware.lint.LintMiddleware` to work on Python 3.
(:issue:`1510`)
- The debugger detects cycles in chained exceptions and does not time
out in that case. (:issue:`1536`)


Version 0.15.2
Expand Down
4 changes: 3 additions & 1 deletion src/werkzeug/debug/tbtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,14 @@ def __init__(self, exc_type, exc_value, tb):
self.exception_type = exception_type

self.groups = []
memo = set()
while True:
self.groups.append(Group(exc_type, exc_value, tb))
memo.add(exc_value)
if PY2:
break
exc_value = exc_value.__cause__ or exc_value.__context__
if exc_value is None:
if exc_value is None or exc_value in memo:
break
exc_type = type(exc_value)
tb = exc_value.__traceback__
Expand Down
17 changes: 17 additions & 0 deletions tests/test_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,20 @@ def app(environ, start_response):
assert "The debugger caught an exception in your WSGI application" in r.text
else:
assert r.text == "hello"


@pytest.mark.skipif(PY2, reason="Python 2 doesn't have chained exceptions.")
@pytest.mark.timeout(2)
def test_chained_exception_cycle():
try:
try:
raise ValueError()
except ValueError:
raise TypeError()
except TypeError as e:
# create a cycle and make it available outside the except block
e.__context__.__context__ = error = e

# if cycles aren't broken, this will time out
tb = Traceback(TypeError, error, error.__traceback__)
assert len(tb.groups) == 2
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ skip_missing_interpreters = true
deps =
coverage
pytest
pytest-timeout
pytest-xprocess
requests
requests_unixsocket
Expand Down

0 comments on commit 44e38c2

Please sign in to comment.