Skip to content

Fix internal error when trying to detect the start of a recursive traceback #2487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions _pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,11 @@ def _truncate_recursive_traceback(self, traceback):
).format(exc_type=type(e).__name__, exc_msg=safe_str(e), max_frames=max_frames, total=len(traceback))
traceback = traceback[:max_frames] + traceback[-max_frames:]
else:
extraline = "!!! Recursion detected (same locals & position)"
traceback = traceback[:recursionindex + 1]
if recursionindex is not None:
extraline = "!!! Recursion detected (same locals & position)"
traceback = traceback[:recursionindex + 1]
else:
extraline = None

return traceback, extraline

Expand Down
1 change: 1 addition & 0 deletions changelog/2486.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix internal error when trying to detect the start of a recursive traceback.
23 changes: 23 additions & 0 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function

import sys
import operator
import _pytest
import py
Expand Down Expand Up @@ -1173,3 +1174,25 @@ def b(x):
'*The following exception happened*',
'*ValueError: The truth value of an array*',
])


def test_no_recursion_index_on_recursion_error():
"""
Ensure that we don't break in case we can't find the recursion index
during a recursion error (#2486).
"""
try:
class RecursionDepthError(object):
def __getattr__(self, attr):
return getattr(self, '_' + attr)

RecursionDepthError().trigger
except:
from _pytest._code.code import ExceptionInfo
exc_info = ExceptionInfo()
if sys.version_info[:2] == (2, 6):
assert "'RecursionDepthError' object has no attribute '___" in str(exc_info.getrepr())
else:
assert 'maximum recursion' in str(exc_info.getrepr())
else:
assert 0