Skip to content
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

Improve performance of ~3 of the slowest tests #4090

Merged
merged 3 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 26 additions & 26 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
pytest_version_info = tuple(map(int, pytest.__version__.split(".")[:3]))


@pytest.fixture
def limited_recursion_depth():
before = sys.getrecursionlimit()
sys.setrecursionlimit(100)
yield
sys.setrecursionlimit(before)


class TWMock(object):
WRITE = object()

Expand Down Expand Up @@ -239,7 +247,7 @@ def f(n):
raise RuntimeError("hello")
f(n - 1)

excinfo = pytest.raises(RuntimeError, f, 100)
excinfo = pytest.raises(RuntimeError, f, 25)
monkeypatch.delattr(excinfo.traceback.__class__, "recursionindex")
repr = excinfo.getrepr()
assert "RuntimeError: hello" in str(repr.reprcrash)
Expand Down Expand Up @@ -1341,11 +1349,13 @@ def test(tmpdir):
assert "INTERNALERROR" not in result.stdout.str() + result.stderr.str()


@pytest.mark.usefixtures("limited_recursion_depth")
def test_exception_repr_extraction_error_on_recursion():
"""
Ensure we can properly detect a recursion error even
if some locals raise error on comparison (#2459).
"""
from _pytest.pytester import LineMatcher

class numpy_like(object):
def __eq__(self, other):
Expand All @@ -1361,40 +1371,30 @@ def a(x):
def b(x):
return a(numpy_like())

try:
with pytest.raises(RecursionError) as excinfo:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RecursionError is py35+ (or py36+) only; IIRC you need to use RuntimeError instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dang, I always forget this 😭

a(numpy_like())
except: # noqa
from _pytest._code.code import ExceptionInfo
from _pytest.pytester import LineMatcher

exc_info = ExceptionInfo()

matcher = LineMatcher(str(exc_info.getrepr()).splitlines())
matcher.fnmatch_lines(
[
"!!! Recursion error detected, but an error occurred locating the origin of recursion.",
"*The following exception happened*",
"*ValueError: The truth value of an array*",
]
)
matcher = LineMatcher(str(excinfo.getrepr()).splitlines())
matcher.fnmatch_lines(
[
"!!! Recursion error detected, but an error occurred locating the origin of recursion.",
"*The following exception happened*",
"*ValueError: The truth value of an array*",
]
)


@pytest.mark.usefixtures("limited_recursion_depth")
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)
class RecursionDepthError(object):
def __getattr__(self, attr):
return getattr(self, "_" + attr)

with pytest.raises(RecursionError) as excinfo:
RecursionDepthError().trigger
except: # noqa
from _pytest._code.code import ExceptionInfo

exc_info = ExceptionInfo()
assert "maximum recursion" in str(exc_info.getrepr())
else:
assert 0
assert "maximum recursion" in str(excinfo.getrepr())
2 changes: 1 addition & 1 deletion testing/logging/test_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test2(caplog):
assert 0
"""
)
result = testdir.runpytest_subprocess()
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*log from test1*", "*2 failed in *"])
assert "log from test2" not in result.stdout.str()

Expand Down