2020-10-19 15:47:35 +08:00
|
|
|
import importlib
|
2020-04-29 21:45:47 +08:00
|
|
|
import io
|
2016-04-20 17:07:34 +08:00
|
|
|
import operator
|
2017-12-27 11:47:26 +08:00
|
|
|
import os
|
2019-06-03 06:31:29 +08:00
|
|
|
import queue
|
2017-12-27 11:47:26 +08:00
|
|
|
import sys
|
2018-08-11 23:54:48 +08:00
|
|
|
import textwrap
|
2020-12-15 19:02:32 +08:00
|
|
|
from pathlib import Path
|
2020-09-03 18:14:32 +08:00
|
|
|
from typing import Any
|
|
|
|
from typing import Dict
|
2020-05-01 19:40:17 +08:00
|
|
|
from typing import Tuple
|
2020-10-03 03:09:56 +08:00
|
|
|
from typing import TYPE_CHECKING
|
2019-11-16 05:02:55 +08:00
|
|
|
from typing import Union
|
2018-10-25 15:01:29 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
import py
|
|
|
|
|
2018-10-25 15:01:29 +08:00
|
|
|
import _pytest
|
|
|
|
import pytest
|
|
|
|
from _pytest._code.code import ExceptionChainRepr
|
|
|
|
from _pytest._code.code import ExceptionInfo
|
|
|
|
from _pytest._code.code import FormattedExcinfo
|
2020-01-23 19:45:38 +08:00
|
|
|
from _pytest._io import TerminalWriter
|
2020-12-15 19:02:32 +08:00
|
|
|
from _pytest.pathlib import import_path
|
2020-01-22 03:06:14 +08:00
|
|
|
from _pytest.pytester import LineMatcher
|
2020-12-15 19:02:32 +08:00
|
|
|
from _pytest.pytester import Pytester
|
|
|
|
|
2018-10-25 15:01:29 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from _pytest._code.code import _TracebackStyle
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2018-10-09 02:12:55 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def limited_recursion_depth():
|
|
|
|
before = sys.getrecursionlimit()
|
2018-10-09 03:57:45 +08:00
|
|
|
sys.setrecursionlimit(150)
|
2018-10-09 02:12:55 +08:00
|
|
|
yield
|
|
|
|
sys.setrecursionlimit(before)
|
|
|
|
|
|
|
|
|
2019-07-14 16:36:33 +08:00
|
|
|
def test_excinfo_simple() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
try:
|
|
|
|
raise ValueError
|
|
|
|
except ValueError:
|
2018-11-22 19:20:14 +08:00
|
|
|
info = _pytest._code.ExceptionInfo.from_current()
|
2015-11-27 22:43:01 +08:00
|
|
|
assert info.type == ValueError
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_excinfo_from_exc_info_simple() -> None:
|
2019-07-14 16:36:33 +08:00
|
|
|
try:
|
|
|
|
raise ValueError
|
|
|
|
except ValueError as e:
|
2020-05-01 19:40:17 +08:00
|
|
|
assert e.__traceback__ is not None
|
2019-07-14 16:36:33 +08:00
|
|
|
info = _pytest._code.ExceptionInfo.from_exc_info((type(e), e, e.__traceback__))
|
|
|
|
assert info.type == ValueError
|
|
|
|
|
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_excinfo_getstatement():
|
|
|
|
def g():
|
|
|
|
raise ValueError
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def f():
|
|
|
|
g()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
try:
|
|
|
|
f()
|
|
|
|
except ValueError:
|
2018-11-22 19:20:14 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo.from_current()
|
2018-05-23 22:48:46 +08:00
|
|
|
linenumbers = [
|
2019-11-15 21:54:56 +08:00
|
|
|
f.__code__.co_firstlineno - 1 + 4,
|
|
|
|
f.__code__.co_firstlineno - 1 + 1,
|
|
|
|
g.__code__.co_firstlineno - 1 + 1,
|
2018-05-23 22:48:46 +08:00
|
|
|
]
|
2017-11-04 23:17:20 +08:00
|
|
|
values = list(excinfo.traceback)
|
|
|
|
foundlinenumbers = [x.lineno for x in values]
|
2015-11-27 22:43:01 +08:00
|
|
|
assert foundlinenumbers == linenumbers
|
2017-07-17 07:25:09 +08:00
|
|
|
# for x in info:
|
2015-11-27 22:43:01 +08:00
|
|
|
# print "%s:%d %s" %(x.path.relto(root), x.lineno, x.statement)
|
2017-07-17 07:25:09 +08:00
|
|
|
# xxx
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
# testchain for getentries test below
|
2017-07-17 07:25:09 +08:00
|
|
|
|
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def f():
|
|
|
|
#
|
|
|
|
raise ValueError
|
|
|
|
#
|
2017-07-17 07:25:09 +08:00
|
|
|
|
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def g():
|
|
|
|
#
|
|
|
|
__tracebackhide__ = True
|
|
|
|
f()
|
|
|
|
#
|
2017-07-17 07:25:09 +08:00
|
|
|
|
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def h():
|
|
|
|
#
|
|
|
|
g()
|
|
|
|
#
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestTraceback_f_g_h:
|
2015-11-27 22:43:01 +08:00
|
|
|
def setup_method(self, method):
|
|
|
|
try:
|
|
|
|
h()
|
|
|
|
except ValueError:
|
2018-11-22 19:20:14 +08:00
|
|
|
self.excinfo = _pytest._code.ExceptionInfo.from_current()
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def test_traceback_entries(self):
|
|
|
|
tb = self.excinfo.traceback
|
|
|
|
entries = list(tb)
|
2017-07-17 07:25:09 +08:00
|
|
|
assert len(tb) == 4 # maybe fragile test
|
|
|
|
assert len(entries) == 4 # maybe fragile test
|
2018-05-23 22:48:46 +08:00
|
|
|
names = ["f", "g", "h"]
|
2015-11-27 22:43:01 +08:00
|
|
|
for entry in entries:
|
|
|
|
try:
|
|
|
|
names.remove(entry.frame.code.name)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
assert not names
|
|
|
|
|
|
|
|
def test_traceback_entry_getsource(self):
|
|
|
|
tb = self.excinfo.traceback
|
2017-07-17 07:25:08 +08:00
|
|
|
s = str(tb[-1].getsource())
|
2015-11-27 22:43:01 +08:00
|
|
|
assert s.startswith("def f():")
|
|
|
|
assert s.endswith("raise ValueError")
|
|
|
|
|
|
|
|
def test_traceback_entry_getsource_in_construct(self):
|
2020-07-02 01:20:12 +08:00
|
|
|
def xyz():
|
|
|
|
try:
|
|
|
|
raise ValueError
|
|
|
|
except somenoname: # type: ignore[name-defined] # noqa: F821
|
2020-07-04 17:12:52 +08:00
|
|
|
pass # pragma: no cover
|
2020-07-02 01:20:12 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
try:
|
2020-07-02 01:20:12 +08:00
|
|
|
xyz()
|
2015-11-27 22:43:01 +08:00
|
|
|
except NameError:
|
2020-07-02 01:20:12 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo.from_current()
|
|
|
|
else:
|
|
|
|
assert False, "did not raise NameError"
|
|
|
|
|
|
|
|
tb = excinfo.traceback
|
|
|
|
source = tb[-1].getsource()
|
|
|
|
assert source is not None
|
|
|
|
assert source.deindent().lines == [
|
|
|
|
"def xyz():",
|
|
|
|
" try:",
|
|
|
|
" raise ValueError",
|
|
|
|
" except somenoname: # type: ignore[name-defined] # noqa: F821",
|
|
|
|
]
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def test_traceback_cut(self):
|
2020-10-27 22:07:03 +08:00
|
|
|
co = _pytest._code.Code.from_function(f)
|
2015-11-27 22:43:01 +08:00
|
|
|
path, firstlineno = co.path, co.firstlineno
|
|
|
|
traceback = self.excinfo.traceback
|
|
|
|
newtraceback = traceback.cut(path=path, firstlineno=firstlineno)
|
|
|
|
assert len(newtraceback) == 1
|
2017-07-17 07:25:08 +08:00
|
|
|
newtraceback = traceback.cut(path=path, lineno=firstlineno + 2)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert len(newtraceback) == 1
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_traceback_cut_excludepath(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile("def f(): raise ValueError")
|
2018-11-23 02:05:10 +08:00
|
|
|
with pytest.raises(ValueError) as excinfo:
|
2020-12-15 19:02:32 +08:00
|
|
|
import_path(p).f() # type: ignore[attr-defined]
|
2015-11-27 22:43:01 +08:00
|
|
|
basedir = py.path.local(pytest.__file__).dirpath()
|
|
|
|
newtraceback = excinfo.traceback.cut(excludepath=basedir)
|
|
|
|
for x in newtraceback:
|
2018-05-23 22:48:46 +08:00
|
|
|
if hasattr(x, "path"):
|
2015-11-27 22:43:01 +08:00
|
|
|
assert not py.path.local(x.path).relto(basedir)
|
|
|
|
assert newtraceback[-1].frame.code.path == p
|
|
|
|
|
|
|
|
def test_traceback_filter(self):
|
|
|
|
traceback = self.excinfo.traceback
|
|
|
|
ntraceback = traceback.filter()
|
|
|
|
assert len(ntraceback) == len(traceback) - 1
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"tracebackhide, matching",
|
|
|
|
[
|
|
|
|
(lambda info: True, True),
|
|
|
|
(lambda info: False, False),
|
|
|
|
(operator.methodcaller("errisinstance", ValueError), True),
|
|
|
|
(operator.methodcaller("errisinstance", IndexError), False),
|
|
|
|
],
|
|
|
|
)
|
2016-04-20 16:25:33 +08:00
|
|
|
def test_traceback_filter_selective(self, tracebackhide, matching):
|
|
|
|
def f():
|
|
|
|
#
|
|
|
|
raise ValueError
|
|
|
|
#
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2016-04-20 16:25:33 +08:00
|
|
|
def g():
|
|
|
|
#
|
|
|
|
__tracebackhide__ = tracebackhide
|
|
|
|
f()
|
|
|
|
#
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2016-04-20 16:25:33 +08:00
|
|
|
def h():
|
|
|
|
#
|
|
|
|
g()
|
|
|
|
#
|
|
|
|
|
|
|
|
excinfo = pytest.raises(ValueError, h)
|
|
|
|
traceback = excinfo.traceback
|
|
|
|
ntraceback = traceback.filter()
|
2020-10-03 04:16:22 +08:00
|
|
|
print(f"old: {traceback!r}")
|
|
|
|
print(f"new: {ntraceback!r}")
|
2016-04-20 16:25:33 +08:00
|
|
|
|
|
|
|
if matching:
|
|
|
|
assert len(ntraceback) == len(traceback) - 2
|
|
|
|
else:
|
|
|
|
# -1 because of the __tracebackhide__ in pytest.raises
|
|
|
|
assert len(ntraceback) == len(traceback) - 1
|
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_traceback_recursion_index(self):
|
|
|
|
def f(n):
|
|
|
|
if n < 10:
|
|
|
|
n += 1
|
|
|
|
f(n)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(RuntimeError, f, 8)
|
|
|
|
traceback = excinfo.traceback
|
|
|
|
recindex = traceback.recursionindex()
|
|
|
|
assert recindex == 3
|
|
|
|
|
|
|
|
def test_traceback_only_specific_recursion_errors(self, monkeypatch):
|
|
|
|
def f(n):
|
|
|
|
if n == 0:
|
|
|
|
raise RuntimeError("hello")
|
2017-07-17 07:25:08 +08:00
|
|
|
f(n - 1)
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2018-10-09 02:12:55 +08:00
|
|
|
excinfo = pytest.raises(RuntimeError, f, 25)
|
2015-11-27 22:43:01 +08:00
|
|
|
monkeypatch.delattr(excinfo.traceback.__class__, "recursionindex")
|
|
|
|
repr = excinfo.getrepr()
|
|
|
|
assert "RuntimeError: hello" in str(repr.reprcrash)
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_traceback_no_recursion_index(self) -> None:
|
|
|
|
def do_stuff() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
raise RuntimeError
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def reraise_me() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
import sys
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
exc, val, tb = sys.exc_info()
|
2019-11-16 05:02:55 +08:00
|
|
|
assert val is not None
|
2019-06-03 06:32:00 +08:00
|
|
|
raise val.with_traceback(tb)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def f(n: int) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
try:
|
|
|
|
do_stuff()
|
2020-04-25 03:15:45 +08:00
|
|
|
except BaseException:
|
2015-11-27 22:43:01 +08:00
|
|
|
reraise_me()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(RuntimeError, f, 8)
|
2019-11-16 05:02:55 +08:00
|
|
|
assert excinfo is not None
|
2015-11-27 22:43:01 +08:00
|
|
|
traceback = excinfo.traceback
|
|
|
|
recindex = traceback.recursionindex()
|
|
|
|
assert recindex is None
|
|
|
|
|
|
|
|
def test_traceback_messy_recursion(self):
|
2017-07-17 07:25:09 +08:00
|
|
|
# XXX: simplified locally testable version
|
2018-05-23 22:48:46 +08:00
|
|
|
decorator = pytest.importorskip("decorator").decorator
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def log(f, *k, **kw):
|
2020-10-03 04:16:22 +08:00
|
|
|
print(f"{k} {kw}")
|
2015-11-27 22:43:01 +08:00
|
|
|
f(*k, **kw)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
log = decorator(log)
|
|
|
|
|
|
|
|
def fail():
|
2018-05-23 22:48:46 +08:00
|
|
|
raise ValueError("")
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
fail = log(log(fail))
|
|
|
|
|
|
|
|
excinfo = pytest.raises(ValueError, fail)
|
|
|
|
assert excinfo.traceback.recursionindex() is None
|
|
|
|
|
|
|
|
def test_traceback_getcrashentry(self):
|
|
|
|
def i():
|
|
|
|
__tracebackhide__ = True
|
|
|
|
raise ValueError
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def h():
|
|
|
|
i()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def g():
|
|
|
|
__tracebackhide__ = True
|
|
|
|
h()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def f():
|
|
|
|
g()
|
|
|
|
|
|
|
|
excinfo = pytest.raises(ValueError, f)
|
|
|
|
tb = excinfo.traceback
|
|
|
|
entry = tb.getcrashentry()
|
2020-10-27 22:07:03 +08:00
|
|
|
co = _pytest._code.Code.from_function(h)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert entry.frame.code.path == co.path
|
|
|
|
assert entry.lineno == co.firstlineno + 1
|
2018-05-23 22:48:46 +08:00
|
|
|
assert entry.frame.code.name == "h"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def test_traceback_getcrashentry_empty(self):
|
|
|
|
def g():
|
|
|
|
__tracebackhide__ = True
|
|
|
|
raise ValueError
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def f():
|
|
|
|
__tracebackhide__ = True
|
|
|
|
g()
|
|
|
|
|
|
|
|
excinfo = pytest.raises(ValueError, f)
|
|
|
|
tb = excinfo.traceback
|
|
|
|
entry = tb.getcrashentry()
|
2020-10-27 22:07:03 +08:00
|
|
|
co = _pytest._code.Code.from_function(g)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert entry.frame.code.path == co.path
|
|
|
|
assert entry.lineno == co.firstlineno + 2
|
2018-05-23 22:48:46 +08:00
|
|
|
assert entry.frame.code.name == "g"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_excinfo_exconly():
|
|
|
|
excinfo = pytest.raises(ValueError, h)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert excinfo.exconly().startswith("ValueError")
|
2018-11-23 02:05:10 +08:00
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
|
|
raise ValueError("hello\nworld")
|
2015-11-27 22:43:01 +08:00
|
|
|
msg = excinfo.exconly(tryshort=True)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert msg.startswith("ValueError")
|
2015-11-27 22:43:01 +08:00
|
|
|
assert msg.endswith("world")
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_excinfo_repr_str() -> None:
|
|
|
|
excinfo1 = pytest.raises(ValueError, h)
|
|
|
|
assert repr(excinfo1) == "<ExceptionInfo ValueError() tblen=4>"
|
|
|
|
assert str(excinfo1) == "<ExceptionInfo ValueError() tblen=4>"
|
2019-10-09 11:16:27 +08:00
|
|
|
|
|
|
|
class CustomException(Exception):
|
|
|
|
def __repr__(self):
|
|
|
|
return "custom_repr"
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def raises() -> None:
|
2019-10-09 11:16:27 +08:00
|
|
|
raise CustomException()
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
excinfo2 = pytest.raises(CustomException, raises)
|
|
|
|
assert repr(excinfo2) == "<ExceptionInfo custom_repr tblen=2>"
|
|
|
|
assert str(excinfo2) == "<ExceptionInfo custom_repr tblen=2>"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_excinfo_for_later() -> None:
|
|
|
|
e = ExceptionInfo[BaseException].for_later()
|
2018-11-22 19:20:14 +08:00
|
|
|
assert "for raises" in repr(e)
|
|
|
|
assert "for raises" in str(e)
|
|
|
|
|
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_excinfo_errisinstance():
|
|
|
|
excinfo = pytest.raises(ValueError, h)
|
|
|
|
assert excinfo.errisinstance(ValueError)
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_excinfo_no_sourcecode():
|
|
|
|
try:
|
2017-07-18 08:16:14 +08:00
|
|
|
exec("raise ValueError()")
|
2015-11-27 22:43:01 +08:00
|
|
|
except ValueError:
|
2018-11-22 19:20:14 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo.from_current()
|
2015-11-27 22:43:01 +08:00
|
|
|
s = str(excinfo.traceback[-1])
|
2017-10-10 13:54:56 +08:00
|
|
|
assert s == " File '<string>':1 in <module>\n ???\n"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_excinfo_no_python_sourcecode(tmpdir):
|
2017-07-17 07:25:09 +08:00
|
|
|
# XXX: simplified locally testable version
|
2018-05-23 22:48:46 +08:00
|
|
|
tmpdir.join("test.txt").write("{{ h()}}:")
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
jinja2 = pytest.importorskip("jinja2")
|
2015-11-27 22:43:01 +08:00
|
|
|
loader = jinja2.FileSystemLoader(str(tmpdir))
|
|
|
|
env = jinja2.Environment(loader=loader)
|
2018-05-23 22:48:46 +08:00
|
|
|
template = env.get_template("test.txt")
|
|
|
|
excinfo = pytest.raises(ValueError, template.render, h=h)
|
2015-11-27 22:43:01 +08:00
|
|
|
for item in excinfo.traceback:
|
2017-07-17 07:25:09 +08:00
|
|
|
print(item) # XXX: for some reason jinja.Template.render is printed in full
|
2019-08-01 21:09:14 +08:00
|
|
|
item.source # shouldn't fail
|
2020-06-21 05:34:41 +08:00
|
|
|
if isinstance(item.path, py.path.local) and item.path.basename == "test.txt":
|
2018-05-23 22:48:46 +08:00
|
|
|
assert str(item.source) == "{{ h()}}:"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_entrysource_Queue_example():
|
|
|
|
try:
|
|
|
|
queue.Queue().get(timeout=0.001)
|
|
|
|
except queue.Empty:
|
2018-11-22 19:20:14 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo.from_current()
|
2015-11-27 22:43:01 +08:00
|
|
|
entry = excinfo.traceback[-1]
|
|
|
|
source = entry.getsource()
|
|
|
|
assert source is not None
|
|
|
|
s = str(source).strip()
|
|
|
|
assert s.startswith("def get")
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_codepath_Queue_example():
|
|
|
|
try:
|
|
|
|
queue.Queue().get(timeout=0.001)
|
|
|
|
except queue.Empty:
|
2018-11-22 19:20:14 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo.from_current()
|
2015-11-27 22:43:01 +08:00
|
|
|
entry = excinfo.traceback[-1]
|
|
|
|
path = entry.path
|
|
|
|
assert isinstance(path, py.path.local)
|
|
|
|
assert path.basename.lower() == "queue.py"
|
|
|
|
assert path.check()
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2016-04-02 23:24:08 +08:00
|
|
|
def test_match_succeeds():
|
|
|
|
with pytest.raises(ZeroDivisionError) as excinfo:
|
2017-03-17 10:07:03 +08:00
|
|
|
0 // 0
|
2018-05-23 22:48:46 +08:00
|
|
|
excinfo.match(r".*zero.*")
|
2016-04-02 23:24:08 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_match_raises_error(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-04-02 23:24:08 +08:00
|
|
|
import pytest
|
|
|
|
def test_division_zero():
|
|
|
|
with pytest.raises(ZeroDivisionError) as excinfo:
|
|
|
|
0 / 0
|
|
|
|
excinfo.match(r'[123]+')
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2016-04-02 23:24:08 +08:00
|
|
|
assert result.ret != 0
|
2020-02-21 23:41:57 +08:00
|
|
|
|
2020-07-16 03:26:47 +08:00
|
|
|
exc_msg = "Regex pattern '[[]123[]]+' does not match 'division by zero'."
|
2020-10-03 04:16:22 +08:00
|
|
|
result.stdout.fnmatch_lines([f"E * AssertionError: {exc_msg}"])
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*__tracebackhide__ = True*")
|
2019-09-02 01:17:02 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--fulltrace")
|
2019-09-02 01:17:02 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
result.stdout.fnmatch_lines(
|
2020-10-03 04:16:22 +08:00
|
|
|
["*__tracebackhide__ = True*", f"E * AssertionError: {exc_msg}"]
|
2019-09-02 01:17:02 +08:00
|
|
|
)
|
2016-04-02 23:24:08 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestFormattedExcinfo:
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
2020-12-15 19:02:32 +08:00
|
|
|
def importasmod(self, tmp_path: Path, _sys_snapshot):
|
2015-11-27 22:43:01 +08:00
|
|
|
def importasmod(source):
|
2018-08-24 00:06:17 +08:00
|
|
|
source = textwrap.dedent(source)
|
2020-12-15 19:02:32 +08:00
|
|
|
modpath = tmp_path.joinpath("mod.py")
|
|
|
|
tmp_path.joinpath("__init__.py").touch()
|
|
|
|
modpath.write_text(source)
|
2020-10-19 15:47:35 +08:00
|
|
|
importlib.invalidate_caches()
|
2020-12-15 19:02:32 +08:00
|
|
|
return import_path(modpath)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
return importasmod
|
|
|
|
|
|
|
|
def test_repr_source(self):
|
|
|
|
pr = FormattedExcinfo()
|
2018-05-23 22:48:46 +08:00
|
|
|
source = _pytest._code.Source(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2015-11-27 22:43:01 +08:00
|
|
|
def f(x):
|
|
|
|
pass
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
).strip()
|
2015-11-27 22:43:01 +08:00
|
|
|
pr.flow_marker = "|"
|
|
|
|
lines = pr.get_source(source, 0)
|
|
|
|
assert len(lines) == 2
|
|
|
|
assert lines[0] == "| def f(x):"
|
|
|
|
assert lines[1] == " pass"
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_repr_source_excinfo(self) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Check if indentation is right."""
|
2020-07-02 01:20:12 +08:00
|
|
|
try:
|
|
|
|
|
|
|
|
def f():
|
|
|
|
1 / 0
|
|
|
|
|
|
|
|
f()
|
|
|
|
|
|
|
|
except BaseException:
|
|
|
|
excinfo = _pytest._code.ExceptionInfo.from_current()
|
|
|
|
else:
|
2020-07-04 17:12:52 +08:00
|
|
|
assert False, "did not raise"
|
2020-07-02 01:20:12 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
pr = FormattedExcinfo()
|
|
|
|
source = pr._getentrysource(excinfo.traceback[-1])
|
2020-05-01 19:40:17 +08:00
|
|
|
assert source is not None
|
2015-11-27 22:43:01 +08:00
|
|
|
lines = pr.get_source(source, 1, excinfo)
|
2020-07-02 01:20:12 +08:00
|
|
|
for line in lines:
|
|
|
|
print(line)
|
|
|
|
assert lines == [
|
|
|
|
" def f():",
|
|
|
|
"> 1 / 0",
|
|
|
|
"E ZeroDivisionError: division by zero",
|
|
|
|
]
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def test_repr_source_not_existing(self):
|
|
|
|
pr = FormattedExcinfo()
|
|
|
|
co = compile("raise ValueError()", "", "exec")
|
|
|
|
try:
|
2017-07-18 08:16:14 +08:00
|
|
|
exec(co)
|
2015-11-27 22:43:01 +08:00
|
|
|
except ValueError:
|
2018-11-22 19:20:14 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo.from_current()
|
2015-11-27 22:43:01 +08:00
|
|
|
repr = pr.repr_excinfo(excinfo)
|
|
|
|
assert repr.reprtraceback.reprentries[1].lines[0] == "> ???"
|
2019-05-28 07:31:52 +08:00
|
|
|
assert repr.chain[0][0].reprentries[1].lines[0] == "> ???"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def test_repr_many_line_source_not_existing(self):
|
|
|
|
pr = FormattedExcinfo()
|
2018-05-23 22:48:46 +08:00
|
|
|
co = compile(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
a = 1
|
|
|
|
raise ValueError()
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
"",
|
|
|
|
"exec",
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
try:
|
2017-07-18 08:16:14 +08:00
|
|
|
exec(co)
|
2015-11-27 22:43:01 +08:00
|
|
|
except ValueError:
|
2018-11-22 19:20:14 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo.from_current()
|
2015-11-27 22:43:01 +08:00
|
|
|
repr = pr.repr_excinfo(excinfo)
|
|
|
|
assert repr.reprtraceback.reprentries[1].lines[0] == "> ???"
|
2019-05-28 07:31:52 +08:00
|
|
|
assert repr.chain[0][0].reprentries[1].lines[0] == "> ???"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2019-11-16 18:53:49 +08:00
|
|
|
def test_repr_source_failing_fullsource(self, monkeypatch) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
pr = FormattedExcinfo()
|
|
|
|
|
2019-11-16 18:53:49 +08:00
|
|
|
try:
|
|
|
|
1 / 0
|
|
|
|
except ZeroDivisionError:
|
|
|
|
excinfo = ExceptionInfo.from_current()
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2019-11-16 18:53:49 +08:00
|
|
|
with monkeypatch.context() as m:
|
|
|
|
m.setattr(_pytest._code.Code, "fullsource", property(lambda self: None))
|
|
|
|
repr = pr.repr_excinfo(excinfo)
|
2016-03-20 06:02:17 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
assert repr.reprtraceback.reprentries[0].lines[0] == "> ???"
|
2019-05-28 07:31:52 +08:00
|
|
|
assert repr.chain[0][0].reprentries[0].lines[0] == "> ???"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_repr_local(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
p = FormattedExcinfo(showlocals=True)
|
2018-05-23 22:48:46 +08:00
|
|
|
loc = {"y": 5, "z": 7, "x": 3, "@x": 2, "__builtins__": {}}
|
2015-11-27 22:43:01 +08:00
|
|
|
reprlocals = p.repr_locals(loc)
|
2020-05-01 19:40:17 +08:00
|
|
|
assert reprlocals is not None
|
2015-11-27 22:43:01 +08:00
|
|
|
assert reprlocals.lines
|
2018-05-23 22:48:46 +08:00
|
|
|
assert reprlocals.lines[0] == "__builtins__ = <builtins>"
|
|
|
|
assert reprlocals.lines[1] == "x = 3"
|
|
|
|
assert reprlocals.lines[2] == "y = 5"
|
|
|
|
assert reprlocals.lines[3] == "z = 7"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_repr_local_with_error(self) -> None:
|
2019-04-19 23:54:21 +08:00
|
|
|
class ObjWithErrorInRepr:
|
|
|
|
def __repr__(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
p = FormattedExcinfo(showlocals=True, truncate_locals=False)
|
|
|
|
loc = {"x": ObjWithErrorInRepr(), "__builtins__": {}}
|
|
|
|
reprlocals = p.repr_locals(loc)
|
2020-05-01 19:40:17 +08:00
|
|
|
assert reprlocals is not None
|
2019-04-19 23:54:21 +08:00
|
|
|
assert reprlocals.lines
|
|
|
|
assert reprlocals.lines[0] == "__builtins__ = <builtins>"
|
2019-10-23 12:59:18 +08:00
|
|
|
assert "[NotImplementedError() raised in repr()]" in reprlocals.lines[1]
|
2019-04-19 23:54:21 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_repr_local_with_exception_in_class_property(self) -> None:
|
2019-04-19 23:54:21 +08:00
|
|
|
class ExceptionWithBrokenClass(Exception):
|
2019-07-08 15:04:19 +08:00
|
|
|
# Type ignored because it's bypassed intentionally.
|
|
|
|
@property # type: ignore
|
2019-04-19 23:54:21 +08:00
|
|
|
def __class__(self):
|
|
|
|
raise TypeError("boom!")
|
|
|
|
|
|
|
|
class ObjWithErrorInRepr:
|
|
|
|
def __repr__(self):
|
|
|
|
raise ExceptionWithBrokenClass()
|
|
|
|
|
|
|
|
p = FormattedExcinfo(showlocals=True, truncate_locals=False)
|
|
|
|
loc = {"x": ObjWithErrorInRepr(), "__builtins__": {}}
|
|
|
|
reprlocals = p.repr_locals(loc)
|
2020-05-01 19:40:17 +08:00
|
|
|
assert reprlocals is not None
|
2019-04-19 23:54:21 +08:00
|
|
|
assert reprlocals.lines
|
|
|
|
assert reprlocals.lines[0] == "__builtins__ = <builtins>"
|
2019-10-23 12:59:18 +08:00
|
|
|
assert "[ExceptionWithBrokenClass() raised in repr()]" in reprlocals.lines[1]
|
2019-04-19 23:54:21 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_repr_local_truncated(self) -> None:
|
2018-07-14 22:05:50 +08:00
|
|
|
loc = {"l": [i for i in range(10)]}
|
|
|
|
p = FormattedExcinfo(showlocals=True)
|
|
|
|
truncated_reprlocals = p.repr_locals(loc)
|
2020-05-01 19:40:17 +08:00
|
|
|
assert truncated_reprlocals is not None
|
2018-07-14 22:05:50 +08:00
|
|
|
assert truncated_reprlocals.lines
|
|
|
|
assert truncated_reprlocals.lines[0] == "l = [0, 1, 2, 3, 4, 5, ...]"
|
|
|
|
|
|
|
|
q = FormattedExcinfo(showlocals=True, truncate_locals=False)
|
|
|
|
full_reprlocals = q.repr_locals(loc)
|
2020-05-01 19:40:17 +08:00
|
|
|
assert full_reprlocals is not None
|
2018-07-14 22:05:50 +08:00
|
|
|
assert full_reprlocals.lines
|
|
|
|
assert full_reprlocals.lines[0] == "l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_repr_tracebackentry_lines(self, importasmod) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def func1():
|
|
|
|
raise ValueError("hello\\nworld")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(ValueError, mod.func1)
|
|
|
|
excinfo.traceback = excinfo.traceback.filter()
|
|
|
|
p = FormattedExcinfo()
|
|
|
|
reprtb = p.repr_traceback_entry(excinfo.traceback[-1])
|
|
|
|
|
|
|
|
# test as intermittent entry
|
|
|
|
lines = reprtb.lines
|
2018-05-23 22:48:46 +08:00
|
|
|
assert lines[0] == " def func1():"
|
2015-11-27 22:43:01 +08:00
|
|
|
assert lines[1] == '> raise ValueError("hello\\nworld")'
|
|
|
|
|
|
|
|
# test as last entry
|
|
|
|
p = FormattedExcinfo(showlocals=True)
|
|
|
|
repr_entry = p.repr_traceback_entry(excinfo.traceback[-1], excinfo)
|
|
|
|
lines = repr_entry.lines
|
2018-05-23 22:48:46 +08:00
|
|
|
assert lines[0] == " def func1():"
|
2015-11-27 22:43:01 +08:00
|
|
|
assert lines[1] == '> raise ValueError("hello\\nworld")'
|
2018-05-23 22:48:46 +08:00
|
|
|
assert lines[2] == "E ValueError: hello"
|
|
|
|
assert lines[3] == "E world"
|
2015-11-27 22:43:01 +08:00
|
|
|
assert not lines[4:]
|
|
|
|
|
|
|
|
loc = repr_entry.reprfileloc
|
2020-05-01 19:40:17 +08:00
|
|
|
assert loc is not None
|
2015-11-27 22:43:01 +08:00
|
|
|
assert loc.path == mod.__file__
|
|
|
|
assert loc.lineno == 3
|
2017-07-17 07:25:09 +08:00
|
|
|
# assert loc.message == "ValueError: hello"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_repr_tracebackentry_lines2(self, importasmod, tw_mock) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def func1(m, x, y, z):
|
|
|
|
raise ValueError("hello\\nworld")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-07-17 07:25:08 +08:00
|
|
|
excinfo = pytest.raises(ValueError, mod.func1, "m" * 90, 5, 13, "z" * 120)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo.traceback = excinfo.traceback.filter()
|
|
|
|
entry = excinfo.traceback[-1]
|
|
|
|
p = FormattedExcinfo(funcargs=True)
|
|
|
|
reprfuncargs = p.repr_args(entry)
|
2020-05-01 19:40:17 +08:00
|
|
|
assert reprfuncargs is not None
|
2018-05-23 22:48:46 +08:00
|
|
|
assert reprfuncargs.args[0] == ("m", repr("m" * 90))
|
|
|
|
assert reprfuncargs.args[1] == ("x", "5")
|
|
|
|
assert reprfuncargs.args[2] == ("y", "13")
|
|
|
|
assert reprfuncargs.args[3] == ("z", repr("z" * 120))
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
p = FormattedExcinfo(funcargs=True)
|
|
|
|
repr_entry = p.repr_traceback_entry(entry)
|
2020-05-01 19:40:17 +08:00
|
|
|
assert repr_entry.reprfuncargs is not None
|
2015-11-27 22:43:01 +08:00
|
|
|
assert repr_entry.reprfuncargs.args == reprfuncargs.args
|
2019-08-26 22:32:57 +08:00
|
|
|
repr_entry.toterminal(tw_mock)
|
|
|
|
assert tw_mock.lines[0] == "m = " + repr("m" * 90)
|
|
|
|
assert tw_mock.lines[1] == "x = 5, y = 13"
|
|
|
|
assert tw_mock.lines[2] == "z = " + repr("z" * 120)
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_repr_tracebackentry_lines_var_kw_args(self, importasmod, tw_mock) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def func1(x, *y, **z):
|
|
|
|
raise ValueError("hello\\nworld")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
excinfo = pytest.raises(ValueError, mod.func1, "a", "b", c="d")
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo.traceback = excinfo.traceback.filter()
|
|
|
|
entry = excinfo.traceback[-1]
|
|
|
|
p = FormattedExcinfo(funcargs=True)
|
|
|
|
reprfuncargs = p.repr_args(entry)
|
2020-05-01 19:40:17 +08:00
|
|
|
assert reprfuncargs is not None
|
2018-05-23 22:48:46 +08:00
|
|
|
assert reprfuncargs.args[0] == ("x", repr("a"))
|
|
|
|
assert reprfuncargs.args[1] == ("y", repr(("b",)))
|
|
|
|
assert reprfuncargs.args[2] == ("z", repr({"c": "d"}))
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
p = FormattedExcinfo(funcargs=True)
|
|
|
|
repr_entry = p.repr_traceback_entry(entry)
|
2020-05-01 19:40:17 +08:00
|
|
|
assert repr_entry.reprfuncargs
|
2015-11-27 22:43:01 +08:00
|
|
|
assert repr_entry.reprfuncargs.args == reprfuncargs.args
|
2019-08-26 22:32:57 +08:00
|
|
|
repr_entry.toterminal(tw_mock)
|
|
|
|
assert tw_mock.lines[0] == "x = 'a', y = ('b',), z = {'c': 'd'}"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_repr_tracebackentry_short(self, importasmod) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def func1():
|
|
|
|
raise ValueError("hello")
|
|
|
|
def entry():
|
|
|
|
func1()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(ValueError, mod.entry)
|
|
|
|
p = FormattedExcinfo(style="short")
|
|
|
|
reprtb = p.repr_traceback_entry(excinfo.traceback[-2])
|
|
|
|
lines = reprtb.lines
|
2020-12-15 19:02:32 +08:00
|
|
|
basename = Path(mod.__file__).name
|
2018-05-23 22:48:46 +08:00
|
|
|
assert lines[0] == " func1()"
|
2020-05-01 19:40:17 +08:00
|
|
|
assert reprtb.reprfileloc is not None
|
2015-11-27 22:43:01 +08:00
|
|
|
assert basename in str(reprtb.reprfileloc.path)
|
|
|
|
assert reprtb.reprfileloc.lineno == 5
|
|
|
|
|
|
|
|
# test last entry
|
|
|
|
p = FormattedExcinfo(style="short")
|
|
|
|
reprtb = p.repr_traceback_entry(excinfo.traceback[-1], excinfo)
|
|
|
|
lines = reprtb.lines
|
|
|
|
assert lines[0] == ' raise ValueError("hello")'
|
2018-05-23 22:48:46 +08:00
|
|
|
assert lines[1] == "E ValueError: hello"
|
2020-05-01 19:40:17 +08:00
|
|
|
assert reprtb.reprfileloc is not None
|
2015-11-27 22:43:01 +08:00
|
|
|
assert basename in str(reprtb.reprfileloc.path)
|
|
|
|
assert reprtb.reprfileloc.lineno == 3
|
|
|
|
|
|
|
|
def test_repr_tracebackentry_no(self, importasmod):
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def func1():
|
|
|
|
raise ValueError("hello")
|
|
|
|
def entry():
|
|
|
|
func1()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(ValueError, mod.entry)
|
|
|
|
p = FormattedExcinfo(style="no")
|
|
|
|
p.repr_traceback_entry(excinfo.traceback[-2])
|
|
|
|
|
|
|
|
p = FormattedExcinfo(style="no")
|
|
|
|
reprentry = p.repr_traceback_entry(excinfo.traceback[-1], excinfo)
|
|
|
|
lines = reprentry.lines
|
2018-05-23 22:48:46 +08:00
|
|
|
assert lines[0] == "E ValueError: hello"
|
2015-11-27 22:43:01 +08:00
|
|
|
assert not lines[1:]
|
|
|
|
|
|
|
|
def test_repr_traceback_tbfilter(self, importasmod):
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def f(x):
|
|
|
|
raise ValueError(x)
|
|
|
|
def entry():
|
|
|
|
f(0)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(ValueError, mod.entry)
|
|
|
|
p = FormattedExcinfo(tbfilter=True)
|
|
|
|
reprtb = p.repr_traceback(excinfo)
|
|
|
|
assert len(reprtb.reprentries) == 2
|
|
|
|
p = FormattedExcinfo(tbfilter=False)
|
|
|
|
reprtb = p.repr_traceback(excinfo)
|
|
|
|
assert len(reprtb.reprentries) == 3
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_traceback_short_no_source(self, importasmod, monkeypatch) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def func1():
|
|
|
|
raise ValueError("hello")
|
|
|
|
def entry():
|
|
|
|
func1()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(ValueError, mod.entry)
|
|
|
|
from _pytest._code.code import Code
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
monkeypatch.setattr(Code, "path", "bogus")
|
2015-11-27 22:43:01 +08:00
|
|
|
p = FormattedExcinfo(style="short")
|
|
|
|
reprtb = p.repr_traceback_entry(excinfo.traceback[-2])
|
|
|
|
lines = reprtb.lines
|
|
|
|
last_p = FormattedExcinfo(style="short")
|
|
|
|
last_reprtb = last_p.repr_traceback_entry(excinfo.traceback[-1], excinfo)
|
|
|
|
last_lines = last_reprtb.lines
|
|
|
|
monkeypatch.undo()
|
2018-05-23 22:48:46 +08:00
|
|
|
assert lines[0] == " func1()"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
assert last_lines[0] == ' raise ValueError("hello")'
|
2018-05-23 22:48:46 +08:00
|
|
|
assert last_lines[1] == "E ValueError: hello"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_repr_traceback_and_excinfo(self, importasmod) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def f(x):
|
|
|
|
raise ValueError(x)
|
|
|
|
def entry():
|
|
|
|
f(0)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(ValueError, mod.entry)
|
|
|
|
|
2020-10-06 09:13:05 +08:00
|
|
|
styles: Tuple[_TracebackStyle, ...] = ("long", "short")
|
2020-05-01 19:40:17 +08:00
|
|
|
for style in styles:
|
2015-11-27 22:43:01 +08:00
|
|
|
p = FormattedExcinfo(style=style)
|
|
|
|
reprtb = p.repr_traceback(excinfo)
|
|
|
|
assert len(reprtb.reprentries) == 2
|
|
|
|
assert reprtb.style == style
|
|
|
|
assert not reprtb.extraline
|
|
|
|
repr = p.repr_excinfo(excinfo)
|
|
|
|
assert repr.reprtraceback
|
|
|
|
assert len(repr.reprtraceback.reprentries) == len(reprtb.reprentries)
|
2019-05-28 07:31:52 +08:00
|
|
|
|
|
|
|
assert repr.chain[0][0]
|
|
|
|
assert len(repr.chain[0][0].reprentries) == len(reprtb.reprentries)
|
2020-05-01 19:40:17 +08:00
|
|
|
assert repr.reprcrash is not None
|
2015-11-27 22:43:01 +08:00
|
|
|
assert repr.reprcrash.path.endswith("mod.py")
|
|
|
|
assert repr.reprcrash.message == "ValueError: 0"
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_repr_traceback_with_invalid_cwd(self, importasmod, monkeypatch) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def f(x):
|
|
|
|
raise ValueError(x)
|
|
|
|
def entry():
|
|
|
|
f(0)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(ValueError, mod.entry)
|
|
|
|
|
2020-01-22 03:06:14 +08:00
|
|
|
p = FormattedExcinfo(abspath=False)
|
|
|
|
|
|
|
|
raised = 0
|
|
|
|
|
|
|
|
orig_getcwd = os.getcwd
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def raiseos():
|
2020-01-22 03:06:14 +08:00
|
|
|
nonlocal raised
|
2020-05-01 19:40:17 +08:00
|
|
|
upframe = sys._getframe().f_back
|
|
|
|
assert upframe is not None
|
|
|
|
if upframe.f_code.co_name == "checked_call":
|
2020-01-22 03:06:14 +08:00
|
|
|
# Only raise with expected calls, but not via e.g. inspect for
|
|
|
|
# py38-windows.
|
|
|
|
raised += 1
|
|
|
|
raise OSError(2, "custom_oserror")
|
|
|
|
return orig_getcwd()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setattr(os, "getcwd", raiseos)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert p._makepath(__file__) == __file__
|
2020-01-22 03:06:14 +08:00
|
|
|
assert raised == 1
|
|
|
|
repr_tb = p.repr_traceback(excinfo)
|
|
|
|
|
|
|
|
matcher = LineMatcher(str(repr_tb).splitlines())
|
|
|
|
matcher.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"def entry():",
|
|
|
|
"> f(0)",
|
|
|
|
"",
|
2020-10-03 04:16:22 +08:00
|
|
|
f"{mod.__file__}:5: ",
|
2020-01-22 03:06:14 +08:00
|
|
|
"_ _ *",
|
|
|
|
"",
|
|
|
|
" def f(x):",
|
|
|
|
"> raise ValueError(x)",
|
|
|
|
"E ValueError: 0",
|
|
|
|
"",
|
2020-10-03 04:16:22 +08:00
|
|
|
f"{mod.__file__}:3: ValueError",
|
2020-01-22 03:06:14 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
assert raised == 3
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2019-08-26 22:32:57 +08:00
|
|
|
def test_repr_excinfo_addouterr(self, importasmod, tw_mock):
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def entry():
|
|
|
|
raise ValueError()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(ValueError, mod.entry)
|
|
|
|
repr = excinfo.getrepr()
|
|
|
|
repr.addsection("title", "content")
|
2019-08-26 22:32:57 +08:00
|
|
|
repr.toterminal(tw_mock)
|
|
|
|
assert tw_mock.lines[-1] == "content"
|
|
|
|
assert tw_mock.lines[-2] == ("-", "title")
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_repr_excinfo_reprcrash(self, importasmod) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def entry():
|
|
|
|
raise ValueError()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(ValueError, mod.entry)
|
|
|
|
repr = excinfo.getrepr()
|
2020-05-01 19:40:17 +08:00
|
|
|
assert repr.reprcrash is not None
|
2015-11-27 22:43:01 +08:00
|
|
|
assert repr.reprcrash.path.endswith("mod.py")
|
|
|
|
assert repr.reprcrash.lineno == 3
|
|
|
|
assert repr.reprcrash.message == "ValueError"
|
|
|
|
assert str(repr.reprcrash).endswith("mod.py:3: ValueError")
|
|
|
|
|
|
|
|
def test_repr_traceback_recursion(self, importasmod):
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def rec2(x):
|
|
|
|
return rec1(x+1)
|
|
|
|
def rec1(x):
|
|
|
|
return rec2(x-1)
|
|
|
|
def entry():
|
|
|
|
rec1(42)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(RuntimeError, mod.entry)
|
|
|
|
|
|
|
|
for style in ("short", "long", "no"):
|
|
|
|
p = FormattedExcinfo(style="short")
|
|
|
|
reprtb = p.repr_traceback(excinfo)
|
|
|
|
assert reprtb.extraline == "!!! Recursion detected (same locals & position)"
|
|
|
|
assert str(reprtb)
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_reprexcinfo_getrepr(self, importasmod) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def f(x):
|
|
|
|
raise ValueError(x)
|
|
|
|
def entry():
|
|
|
|
f(0)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(ValueError, mod.entry)
|
|
|
|
|
2020-10-06 09:13:05 +08:00
|
|
|
styles: Tuple[_TracebackStyle, ...] = ("short", "long", "no")
|
2020-05-01 19:40:17 +08:00
|
|
|
for style in styles:
|
2015-11-27 22:43:01 +08:00
|
|
|
for showlocals in (True, False):
|
|
|
|
repr = excinfo.getrepr(style=style, showlocals=showlocals)
|
|
|
|
assert repr.reprtraceback.style == style
|
2019-05-28 07:31:52 +08:00
|
|
|
|
|
|
|
assert isinstance(repr, ExceptionChainRepr)
|
2020-05-01 19:40:17 +08:00
|
|
|
for r in repr.chain:
|
|
|
|
assert r[0].style == style
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def test_reprexcinfo_unicode(self):
|
|
|
|
from _pytest._code.code import TerminalRepr
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
class MyRepr(TerminalRepr):
|
2020-01-23 19:45:38 +08:00
|
|
|
def toterminal(self, tw: TerminalWriter) -> None:
|
2019-06-03 06:32:00 +08:00
|
|
|
tw.line("я")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
x = str(MyRepr())
|
|
|
|
assert x == "я"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2019-08-26 22:32:57 +08:00
|
|
|
def test_toterminal_long(self, importasmod, tw_mock):
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def g(x):
|
|
|
|
raise ValueError(x)
|
|
|
|
def f():
|
|
|
|
g(3)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(ValueError, mod.f)
|
|
|
|
excinfo.traceback = excinfo.traceback.filter()
|
|
|
|
repr = excinfo.getrepr()
|
2019-08-26 22:32:57 +08:00
|
|
|
repr.toterminal(tw_mock)
|
|
|
|
assert tw_mock.lines[0] == ""
|
|
|
|
tw_mock.lines.pop(0)
|
|
|
|
assert tw_mock.lines[0] == " def f():"
|
|
|
|
assert tw_mock.lines[1] == "> g(3)"
|
|
|
|
assert tw_mock.lines[2] == ""
|
|
|
|
line = tw_mock.get_write_msg(3)
|
2016-07-30 17:12:35 +08:00
|
|
|
assert line.endswith("mod.py")
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[4] == (":5: ")
|
|
|
|
assert tw_mock.lines[5] == ("_ ", None)
|
|
|
|
assert tw_mock.lines[6] == ""
|
|
|
|
assert tw_mock.lines[7] == " def g(x):"
|
|
|
|
assert tw_mock.lines[8] == "> raise ValueError(x)"
|
|
|
|
assert tw_mock.lines[9] == "E ValueError: 3"
|
|
|
|
assert tw_mock.lines[10] == ""
|
|
|
|
line = tw_mock.get_write_msg(11)
|
2016-07-30 17:12:35 +08:00
|
|
|
assert line.endswith("mod.py")
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[12] == ":3: ValueError"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_toterminal_long_missing_source(
|
|
|
|
self, importasmod, tmp_path: Path, tw_mock
|
|
|
|
) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def g(x):
|
|
|
|
raise ValueError(x)
|
|
|
|
def f():
|
|
|
|
g(3)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(ValueError, mod.f)
|
2020-12-15 19:02:32 +08:00
|
|
|
tmp_path.joinpath("mod.py").unlink()
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo.traceback = excinfo.traceback.filter()
|
|
|
|
repr = excinfo.getrepr()
|
2019-08-26 22:32:57 +08:00
|
|
|
repr.toterminal(tw_mock)
|
|
|
|
assert tw_mock.lines[0] == ""
|
|
|
|
tw_mock.lines.pop(0)
|
|
|
|
assert tw_mock.lines[0] == "> ???"
|
|
|
|
assert tw_mock.lines[1] == ""
|
|
|
|
line = tw_mock.get_write_msg(2)
|
2016-07-30 17:12:35 +08:00
|
|
|
assert line.endswith("mod.py")
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[3] == ":5: "
|
|
|
|
assert tw_mock.lines[4] == ("_ ", None)
|
|
|
|
assert tw_mock.lines[5] == ""
|
|
|
|
assert tw_mock.lines[6] == "> ???"
|
|
|
|
assert tw_mock.lines[7] == "E ValueError: 3"
|
|
|
|
assert tw_mock.lines[8] == ""
|
|
|
|
line = tw_mock.get_write_msg(9)
|
2016-07-30 17:12:35 +08:00
|
|
|
assert line.endswith("mod.py")
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[10] == ":3: ValueError"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_toterminal_long_incomplete_source(
|
|
|
|
self, importasmod, tmp_path: Path, tw_mock
|
|
|
|
) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def g(x):
|
|
|
|
raise ValueError(x)
|
|
|
|
def f():
|
|
|
|
g(3)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(ValueError, mod.f)
|
2020-12-15 19:02:32 +08:00
|
|
|
tmp_path.joinpath("mod.py").write_text("asdf")
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo.traceback = excinfo.traceback.filter()
|
|
|
|
repr = excinfo.getrepr()
|
2019-08-26 22:32:57 +08:00
|
|
|
repr.toterminal(tw_mock)
|
|
|
|
assert tw_mock.lines[0] == ""
|
|
|
|
tw_mock.lines.pop(0)
|
|
|
|
assert tw_mock.lines[0] == "> ???"
|
|
|
|
assert tw_mock.lines[1] == ""
|
|
|
|
line = tw_mock.get_write_msg(2)
|
2016-07-30 17:12:35 +08:00
|
|
|
assert line.endswith("mod.py")
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[3] == ":5: "
|
|
|
|
assert tw_mock.lines[4] == ("_ ", None)
|
|
|
|
assert tw_mock.lines[5] == ""
|
|
|
|
assert tw_mock.lines[6] == "> ???"
|
|
|
|
assert tw_mock.lines[7] == "E ValueError: 3"
|
|
|
|
assert tw_mock.lines[8] == ""
|
|
|
|
line = tw_mock.get_write_msg(9)
|
2016-07-30 17:12:35 +08:00
|
|
|
assert line.endswith("mod.py")
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[10] == ":3: ValueError"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2019-08-26 22:32:57 +08:00
|
|
|
def test_toterminal_long_filenames(self, importasmod, tw_mock):
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def f():
|
|
|
|
raise ValueError()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(ValueError, mod.f)
|
|
|
|
path = py.path.local(mod.__file__)
|
|
|
|
old = path.dirpath().chdir()
|
|
|
|
try:
|
|
|
|
repr = excinfo.getrepr(abspath=False)
|
2019-08-26 22:32:57 +08:00
|
|
|
repr.toterminal(tw_mock)
|
2015-11-27 22:43:01 +08:00
|
|
|
x = py.path.local().bestrelpath(path)
|
|
|
|
if len(x) < len(str(path)):
|
2019-08-26 22:32:57 +08:00
|
|
|
msg = tw_mock.get_write_msg(-2)
|
2016-07-30 17:12:35 +08:00
|
|
|
assert msg == "mod.py"
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[-1] == ":3: ValueError"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
repr = excinfo.getrepr(abspath=True)
|
2019-08-26 22:32:57 +08:00
|
|
|
repr.toterminal(tw_mock)
|
|
|
|
msg = tw_mock.get_write_msg(-2)
|
2016-07-30 17:12:35 +08:00
|
|
|
assert msg == path
|
2019-08-26 22:32:57 +08:00
|
|
|
line = tw_mock.lines[-1]
|
2016-07-30 17:12:35 +08:00
|
|
|
assert line == ":3: ValueError"
|
2015-11-27 22:43:01 +08:00
|
|
|
finally:
|
|
|
|
old.chdir()
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"reproptions",
|
|
|
|
[
|
2020-09-03 18:14:32 +08:00
|
|
|
pytest.param(
|
|
|
|
{
|
|
|
|
"style": style,
|
|
|
|
"showlocals": showlocals,
|
|
|
|
"funcargs": funcargs,
|
|
|
|
"tbfilter": tbfilter,
|
|
|
|
},
|
|
|
|
id="style={},showlocals={},funcargs={},tbfilter={}".format(
|
|
|
|
style, showlocals, funcargs, tbfilter
|
|
|
|
),
|
|
|
|
)
|
|
|
|
for style in ["long", "short", "line", "no", "native", "value", "auto"]
|
2018-05-23 22:48:46 +08:00
|
|
|
for showlocals in (True, False)
|
|
|
|
for tbfilter in (True, False)
|
|
|
|
for funcargs in (True, False)
|
|
|
|
],
|
|
|
|
)
|
2020-09-03 18:14:32 +08:00
|
|
|
def test_format_excinfo(self, reproptions: Dict[str, Any]) -> None:
|
|
|
|
def bar():
|
|
|
|
assert False, "some error"
|
|
|
|
|
|
|
|
def foo():
|
|
|
|
bar()
|
|
|
|
|
|
|
|
# using inline functions as opposed to importasmod so we get source code lines
|
|
|
|
# in the tracebacks (otherwise getinspect doesn't find the source code).
|
|
|
|
with pytest.raises(AssertionError) as excinfo:
|
|
|
|
foo()
|
2020-04-29 21:45:47 +08:00
|
|
|
file = io.StringIO()
|
|
|
|
tw = TerminalWriter(file=file)
|
2015-11-27 22:43:01 +08:00
|
|
|
repr = excinfo.getrepr(**reproptions)
|
|
|
|
repr.toterminal(tw)
|
2020-04-29 21:45:47 +08:00
|
|
|
assert file.getvalue()
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2019-08-26 22:32:57 +08:00
|
|
|
def test_traceback_repr_style(self, importasmod, tw_mock):
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def f():
|
|
|
|
g()
|
|
|
|
def g():
|
|
|
|
h()
|
|
|
|
def h():
|
|
|
|
i()
|
|
|
|
def i():
|
|
|
|
raise ValueError()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(ValueError, mod.f)
|
|
|
|
excinfo.traceback = excinfo.traceback.filter()
|
|
|
|
excinfo.traceback[1].set_repr_style("short")
|
|
|
|
excinfo.traceback[2].set_repr_style("short")
|
|
|
|
r = excinfo.getrepr(style="long")
|
2019-08-26 22:32:57 +08:00
|
|
|
r.toterminal(tw_mock)
|
|
|
|
for line in tw_mock.lines:
|
2017-07-18 08:16:14 +08:00
|
|
|
print(line)
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[0] == ""
|
|
|
|
assert tw_mock.lines[1] == " def f():"
|
|
|
|
assert tw_mock.lines[2] == "> g()"
|
|
|
|
assert tw_mock.lines[3] == ""
|
|
|
|
msg = tw_mock.get_write_msg(4)
|
2016-07-30 17:12:35 +08:00
|
|
|
assert msg.endswith("mod.py")
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[5] == ":3: "
|
|
|
|
assert tw_mock.lines[6] == ("_ ", None)
|
|
|
|
tw_mock.get_write_msg(7)
|
|
|
|
assert tw_mock.lines[8].endswith("in g")
|
|
|
|
assert tw_mock.lines[9] == " h()"
|
|
|
|
tw_mock.get_write_msg(10)
|
|
|
|
assert tw_mock.lines[11].endswith("in h")
|
|
|
|
assert tw_mock.lines[12] == " i()"
|
|
|
|
assert tw_mock.lines[13] == ("_ ", None)
|
|
|
|
assert tw_mock.lines[14] == ""
|
|
|
|
assert tw_mock.lines[15] == " def i():"
|
|
|
|
assert tw_mock.lines[16] == "> raise ValueError()"
|
|
|
|
assert tw_mock.lines[17] == "E ValueError"
|
|
|
|
assert tw_mock.lines[18] == ""
|
|
|
|
msg = tw_mock.get_write_msg(19)
|
2016-07-30 17:12:35 +08:00
|
|
|
msg.endswith("mod.py")
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[20] == ":9: ValueError"
|
2016-03-20 06:02:17 +08:00
|
|
|
|
2019-08-26 22:32:57 +08:00
|
|
|
def test_exc_chain_repr(self, importasmod, tw_mock):
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2016-03-20 06:02:17 +08:00
|
|
|
class Err(Exception):
|
|
|
|
pass
|
|
|
|
def f():
|
|
|
|
try:
|
|
|
|
g()
|
|
|
|
except Exception as e:
|
|
|
|
raise Err() from e
|
|
|
|
finally:
|
|
|
|
h()
|
|
|
|
def g():
|
|
|
|
raise ValueError()
|
|
|
|
|
|
|
|
def h():
|
|
|
|
raise AttributeError()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-03-20 06:02:17 +08:00
|
|
|
excinfo = pytest.raises(AttributeError, mod.f)
|
|
|
|
r = excinfo.getrepr(style="long")
|
2019-08-26 22:32:57 +08:00
|
|
|
r.toterminal(tw_mock)
|
|
|
|
for line in tw_mock.lines:
|
2017-07-18 08:16:14 +08:00
|
|
|
print(line)
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[0] == ""
|
|
|
|
assert tw_mock.lines[1] == " def f():"
|
|
|
|
assert tw_mock.lines[2] == " try:"
|
|
|
|
assert tw_mock.lines[3] == "> g()"
|
|
|
|
assert tw_mock.lines[4] == ""
|
|
|
|
line = tw_mock.get_write_msg(5)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert line.endswith("mod.py")
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[6] == ":6: "
|
|
|
|
assert tw_mock.lines[7] == ("_ ", None)
|
|
|
|
assert tw_mock.lines[8] == ""
|
|
|
|
assert tw_mock.lines[9] == " def g():"
|
|
|
|
assert tw_mock.lines[10] == "> raise ValueError()"
|
|
|
|
assert tw_mock.lines[11] == "E ValueError"
|
|
|
|
assert tw_mock.lines[12] == ""
|
|
|
|
line = tw_mock.get_write_msg(13)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert line.endswith("mod.py")
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[14] == ":12: ValueError"
|
|
|
|
assert tw_mock.lines[15] == ""
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
2019-08-26 22:32:57 +08:00
|
|
|
tw_mock.lines[16]
|
2018-05-23 22:48:46 +08:00
|
|
|
== "The above exception was the direct cause of the following exception:"
|
|
|
|
)
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[17] == ""
|
|
|
|
assert tw_mock.lines[18] == " def f():"
|
|
|
|
assert tw_mock.lines[19] == " try:"
|
|
|
|
assert tw_mock.lines[20] == " g()"
|
|
|
|
assert tw_mock.lines[21] == " except Exception as e:"
|
|
|
|
assert tw_mock.lines[22] == "> raise Err() from e"
|
|
|
|
assert tw_mock.lines[23] == "E test_exc_chain_repr0.mod.Err"
|
|
|
|
assert tw_mock.lines[24] == ""
|
|
|
|
line = tw_mock.get_write_msg(25)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert line.endswith("mod.py")
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[26] == ":8: Err"
|
|
|
|
assert tw_mock.lines[27] == ""
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
2019-08-26 22:32:57 +08:00
|
|
|
tw_mock.lines[28]
|
2018-05-23 22:48:46 +08:00
|
|
|
== "During handling of the above exception, another exception occurred:"
|
|
|
|
)
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[29] == ""
|
|
|
|
assert tw_mock.lines[30] == " def f():"
|
|
|
|
assert tw_mock.lines[31] == " try:"
|
|
|
|
assert tw_mock.lines[32] == " g()"
|
|
|
|
assert tw_mock.lines[33] == " except Exception as e:"
|
|
|
|
assert tw_mock.lines[34] == " raise Err() from e"
|
|
|
|
assert tw_mock.lines[35] == " finally:"
|
|
|
|
assert tw_mock.lines[36] == "> h()"
|
|
|
|
assert tw_mock.lines[37] == ""
|
|
|
|
line = tw_mock.get_write_msg(38)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert line.endswith("mod.py")
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[39] == ":10: "
|
|
|
|
assert tw_mock.lines[40] == ("_ ", None)
|
|
|
|
assert tw_mock.lines[41] == ""
|
|
|
|
assert tw_mock.lines[42] == " def h():"
|
|
|
|
assert tw_mock.lines[43] == "> raise AttributeError()"
|
|
|
|
assert tw_mock.lines[44] == "E AttributeError"
|
|
|
|
assert tw_mock.lines[45] == ""
|
|
|
|
line = tw_mock.get_write_msg(46)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert line.endswith("mod.py")
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[47] == ":15: AttributeError"
|
2016-05-14 01:36:13 +08:00
|
|
|
|
2018-10-12 20:37:54 +08:00
|
|
|
@pytest.mark.parametrize("mode", ["from_none", "explicit_suppress"])
|
2019-08-26 22:32:57 +08:00
|
|
|
def test_exc_repr_chain_suppression(self, importasmod, mode, tw_mock):
|
2018-10-12 20:37:54 +08:00
|
|
|
"""Check that exc repr does not show chained exceptions in Python 3.
|
|
|
|
- When the exception is raised with "from None"
|
|
|
|
- Explicitly suppressed with "chain=False" to ExceptionInfo.getrepr().
|
|
|
|
"""
|
|
|
|
raise_suffix = " from None" if mode == "from_none" else ""
|
2018-05-23 22:48:46 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2017-07-29 17:39:17 +08:00
|
|
|
def f():
|
|
|
|
try:
|
|
|
|
g()
|
|
|
|
except Exception:
|
2018-10-12 20:37:54 +08:00
|
|
|
raise AttributeError(){raise_suffix}
|
2017-07-29 17:39:17 +08:00
|
|
|
def g():
|
|
|
|
raise ValueError()
|
2018-10-12 20:37:54 +08:00
|
|
|
""".format(
|
|
|
|
raise_suffix=raise_suffix
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2017-07-29 17:39:17 +08:00
|
|
|
excinfo = pytest.raises(AttributeError, mod.f)
|
2018-10-12 20:37:54 +08:00
|
|
|
r = excinfo.getrepr(style="long", chain=mode != "explicit_suppress")
|
2019-08-26 22:32:57 +08:00
|
|
|
r.toterminal(tw_mock)
|
|
|
|
for line in tw_mock.lines:
|
2017-07-29 17:39:17 +08:00
|
|
|
print(line)
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[0] == ""
|
|
|
|
assert tw_mock.lines[1] == " def f():"
|
|
|
|
assert tw_mock.lines[2] == " try:"
|
|
|
|
assert tw_mock.lines[3] == " g()"
|
|
|
|
assert tw_mock.lines[4] == " except Exception:"
|
|
|
|
assert tw_mock.lines[5] == "> raise AttributeError(){}".format(
|
2018-10-12 20:37:54 +08:00
|
|
|
raise_suffix
|
|
|
|
)
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[6] == "E AttributeError"
|
|
|
|
assert tw_mock.lines[7] == ""
|
|
|
|
line = tw_mock.get_write_msg(8)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert line.endswith("mod.py")
|
2019-08-26 22:32:57 +08:00
|
|
|
assert tw_mock.lines[9] == ":6: AttributeError"
|
|
|
|
assert len(tw_mock.lines) == 10
|
2017-07-29 17:39:17 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"reason, description",
|
|
|
|
[
|
2019-11-07 19:25:46 +08:00
|
|
|
pytest.param(
|
2018-05-23 22:48:46 +08:00
|
|
|
"cause",
|
|
|
|
"The above exception was the direct cause of the following exception:",
|
2019-11-07 19:25:46 +08:00
|
|
|
id="cause",
|
2018-05-23 22:48:46 +08:00
|
|
|
),
|
2019-11-07 19:25:46 +08:00
|
|
|
pytest.param(
|
2018-05-23 22:48:46 +08:00
|
|
|
"context",
|
|
|
|
"During handling of the above exception, another exception occurred:",
|
2019-11-07 19:25:46 +08:00
|
|
|
id="context",
|
2018-05-23 22:48:46 +08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2016-10-28 07:15:05 +08:00
|
|
|
def test_exc_chain_repr_without_traceback(self, importasmod, reason, description):
|
|
|
|
"""
|
|
|
|
Handle representation of exception chains where one of the exceptions doesn't have a
|
|
|
|
real traceback, such as those raised in a subprocess submitted by the multiprocessing
|
|
|
|
module (#1984).
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
exc_handling_code = " from e" if reason == "cause" else ""
|
|
|
|
mod = importasmod(
|
|
|
|
"""
|
2016-10-28 07:15:05 +08:00
|
|
|
def f():
|
|
|
|
try:
|
|
|
|
g()
|
|
|
|
except Exception as e:
|
|
|
|
raise RuntimeError('runtime problem'){exc_handling_code}
|
|
|
|
def g():
|
|
|
|
raise ValueError('invalid value')
|
2018-05-23 22:48:46 +08:00
|
|
|
""".format(
|
|
|
|
exc_handling_code=exc_handling_code
|
|
|
|
)
|
|
|
|
)
|
2016-10-28 07:15:05 +08:00
|
|
|
|
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
|
|
mod.f()
|
|
|
|
|
|
|
|
# emulate the issue described in #1984
|
2018-05-23 22:48:46 +08:00
|
|
|
attr = "__%s__" % reason
|
2016-10-28 07:15:05 +08:00
|
|
|
getattr(excinfo.value, attr).__traceback__ = None
|
|
|
|
|
|
|
|
r = excinfo.getrepr()
|
2020-04-29 21:45:47 +08:00
|
|
|
file = io.StringIO()
|
|
|
|
tw = TerminalWriter(file=file)
|
2016-10-28 07:15:05 +08:00
|
|
|
tw.hasmarkup = False
|
|
|
|
r.toterminal(tw)
|
|
|
|
|
2020-04-29 21:45:47 +08:00
|
|
|
matcher = LineMatcher(file.getvalue().splitlines())
|
2018-05-23 22:48:46 +08:00
|
|
|
matcher.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"ValueError: invalid value",
|
|
|
|
description,
|
|
|
|
"* except Exception as e:",
|
|
|
|
"> * raise RuntimeError('runtime problem')" + exc_handling_code,
|
|
|
|
"E *RuntimeError: runtime problem",
|
|
|
|
]
|
|
|
|
)
|
2016-10-28 07:15:05 +08:00
|
|
|
|
2019-08-26 22:32:57 +08:00
|
|
|
def test_exc_chain_repr_cycle(self, importasmod, tw_mock):
|
2018-08-11 23:54:48 +08:00
|
|
|
mod = importasmod(
|
|
|
|
"""
|
|
|
|
class Err(Exception):
|
|
|
|
pass
|
|
|
|
def fail():
|
|
|
|
return 0 / 0
|
|
|
|
def reraise():
|
|
|
|
try:
|
|
|
|
fail()
|
|
|
|
except ZeroDivisionError as e:
|
|
|
|
raise Err() from e
|
|
|
|
def unreraise():
|
|
|
|
try:
|
|
|
|
reraise()
|
|
|
|
except Err as e:
|
|
|
|
raise e.__cause__
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
excinfo = pytest.raises(ZeroDivisionError, mod.unreraise)
|
|
|
|
r = excinfo.getrepr(style="short")
|
2019-08-26 22:32:57 +08:00
|
|
|
r.toterminal(tw_mock)
|
|
|
|
out = "\n".join(line for line in tw_mock.lines if isinstance(line, str))
|
2018-08-11 23:54:48 +08:00
|
|
|
expected_out = textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
:13: in unreraise
|
|
|
|
reraise()
|
|
|
|
:10: in reraise
|
|
|
|
raise Err() from e
|
|
|
|
E test_exc_chain_repr_cycle0.mod.Err
|
|
|
|
|
|
|
|
During handling of the above exception, another exception occurred:
|
|
|
|
:15: in unreraise
|
|
|
|
raise e.__cause__
|
|
|
|
:8: in reraise
|
|
|
|
fail()
|
|
|
|
:5: in fail
|
|
|
|
return 0 / 0
|
|
|
|
E ZeroDivisionError: division by zero"""
|
|
|
|
)
|
|
|
|
assert out == expected_out
|
|
|
|
|
2020-09-12 07:44:24 +08:00
|
|
|
def test_exec_type_error_filter(self, importasmod):
|
|
|
|
"""See #7742"""
|
|
|
|
mod = importasmod(
|
|
|
|
"""\
|
|
|
|
def f():
|
|
|
|
exec("a = 1", {}, [])
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
mod.f()
|
|
|
|
# previously crashed with `AttributeError: list has no attribute get`
|
|
|
|
excinfo.traceback.filter()
|
|
|
|
|
2016-04-08 22:24:12 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("style", ["short", "long"])
|
|
|
|
@pytest.mark.parametrize("encoding", [None, "utf8", "utf16"])
|
|
|
|
def test_repr_traceback_with_unicode(style, encoding):
|
2019-11-16 05:02:55 +08:00
|
|
|
if encoding is None:
|
2020-10-06 09:13:05 +08:00
|
|
|
msg: Union[str, bytes] = "☹"
|
2019-11-16 05:02:55 +08:00
|
|
|
else:
|
|
|
|
msg = "☹".encode(encoding)
|
2016-04-08 22:24:12 +08:00
|
|
|
try:
|
|
|
|
raise RuntimeError(msg)
|
|
|
|
except RuntimeError:
|
2018-11-22 19:20:14 +08:00
|
|
|
e_info = ExceptionInfo.from_current()
|
2016-04-08 22:24:12 +08:00
|
|
|
formatter = FormattedExcinfo(style=style)
|
|
|
|
repr_traceback = formatter.repr_traceback(e_info)
|
|
|
|
assert repr_traceback is not None
|
|
|
|
|
2016-06-07 21:43:05 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_cwd_deleted(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2020-12-15 19:02:32 +08:00
|
|
|
import os
|
|
|
|
|
|
|
|
def test(tmp_path):
|
|
|
|
os.chdir(tmp_path)
|
|
|
|
tmp_path.unlink()
|
2016-06-07 21:43:05 +08:00
|
|
|
assert False
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 failed in *"])
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*INTERNALERROR*")
|
|
|
|
result.stderr.no_fnmatch_line("*INTERNALERROR*")
|
2017-06-06 23:41:15 +08:00
|
|
|
|
|
|
|
|
2018-10-09 02:12:55 +08:00
|
|
|
@pytest.mark.usefixtures("limited_recursion_depth")
|
2017-06-06 23:41:15 +08:00
|
|
|
def test_exception_repr_extraction_error_on_recursion():
|
|
|
|
"""
|
|
|
|
Ensure we can properly detect a recursion error even
|
2018-05-13 18:06:09 +08:00
|
|
|
if some locals raise error on comparison (#2459).
|
2017-06-06 23:41:15 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class numpy_like:
|
2017-06-06 23:41:15 +08:00
|
|
|
def __eq__(self, other):
|
|
|
|
if type(other) is numpy_like:
|
2018-05-23 22:48:46 +08:00
|
|
|
raise ValueError(
|
|
|
|
"The truth value of an array "
|
|
|
|
"with more than one element is ambiguous."
|
|
|
|
)
|
2017-06-06 23:41:15 +08:00
|
|
|
|
|
|
|
def a(x):
|
|
|
|
return b(numpy_like())
|
|
|
|
|
|
|
|
def b(x):
|
|
|
|
return a(numpy_like())
|
|
|
|
|
2018-10-09 02:47:06 +08:00
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
2017-06-06 23:41:15 +08:00
|
|
|
a(numpy_like())
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2018-10-09 02:12:55 +08:00
|
|
|
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*",
|
|
|
|
]
|
|
|
|
)
|
2017-06-10 05:47:07 +08:00
|
|
|
|
|
|
|
|
2018-10-09 02:12:55 +08:00
|
|
|
@pytest.mark.usefixtures("limited_recursion_depth")
|
2017-06-10 05:47:07 +08:00
|
|
|
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).
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class RecursionDepthError:
|
2018-10-09 02:12:55 +08:00
|
|
|
def __getattr__(self, attr):
|
|
|
|
return getattr(self, "_" + attr)
|
2017-06-10 05:47:07 +08:00
|
|
|
|
2018-10-09 02:47:06 +08:00
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
2017-06-10 05:47:07 +08:00
|
|
|
RecursionDepthError().trigger
|
2018-10-09 02:12:55 +08:00
|
|
|
assert "maximum recursion" in str(excinfo.getrepr())
|