Squashed commit of the following:

commit 41d339c46763bbe26123e1e6504b6e32290e33e1
Author: Cheukting <cheukting.ho@gmail.com>
Date:   Thu Jun 23 17:01:04 2022 +0800

    test in all py versions

commit b3572a5a12672228c3276fc8c8e05980dfb7888a
Author: Cheukting <cheukting.ho@gmail.com>
Date:   Thu Jun 23 16:41:06 2022 +0800

    add test

commit 7166a2a51e4f99046b028b663c193d8b558c7fd4
Author: Cheukting <cheukting.ho@gmail.com>
Date:   Thu Jun 23 16:00:07 2022 +0800

    update changelog

commit b958c73d489157f0c0d4e46425083a5e2e2bc851
Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date:   Thu Jun 23 07:50:52 2022 +0000

    [pre-commit.ci] auto fixes from pre-commit.com hooks

    for more information, see https://pre-commit.ci

commit ea7f376c6ca37c40c83df0e4a1cfaaedb34bae91
Author: Cheukting <cheukting.ho@gmail.com>
Date:   Thu Jun 23 15:48:21 2022 +0800

    Fix MyPy

commit 97469beb1da40257e9a061a5e19548546c9312c4
Author: Cheukting <cheukting.ho@gmail.com>
Date:   Thu Jun 23 15:03:48 2022 +0800

    fix if ExceptionGroup not exist

commit 84e553642cd69b4d499231d733df91ebfa84c7ad
Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date:   Thu Jun 23 03:43:27 2022 +0000

    [pre-commit.ci] auto fixes from pre-commit.com hooks

    for more information, see https://pre-commit.ci

commit 76bbef449b88bbd74fb5cca3b5293337a624ef03
Author: Cheukting <cheukting.ho@gmail.com>
Date:   Thu Jun 23 11:40:41 2022 +0800

    adding changelog

commit db82bebc5a4969e2083adcd97bdfd2a63bb17d98
Author: Cheukting <cheukting.ho@gmail.com>
Date:   Thu Jun 23 11:33:10 2022 +0800

    fall back to native when handeling to exception groups
This commit is contained in:
jakkdl 2022-08-11 14:41:17 +02:00
parent cc0092b9d8
commit 078b112138
4 changed files with 58 additions and 4 deletions

View File

@ -0,0 +1 @@
Showing inner exceptions by forcing native display in ``ExceptionGroups`` even when using display options other than ``--tb=native``. A temporary step before full implementation of pytest-native display for inner exceptions in ``ExceptionGroups``.

View File

@ -56,6 +56,18 @@ if TYPE_CHECKING:
_TracebackStyle = Literal["long", "short", "line", "no", "native", "value", "auto"]
ExceptionGroupTypes: tuple = () # type: ignore
try:
ExceptionGroupTypes += (ExceptionGroup,) # type: ignore
except NameError:
pass # Is missing for `python<3.10`
try:
import exceptiongroup
ExceptionGroupTypes += (exceptiongroup.ExceptionGroup,)
except ModuleNotFoundError:
pass # No backport is installed
class Code:
"""Wrapper around Python code objects."""
@ -923,11 +935,20 @@ class FormattedExcinfo:
seen: Set[int] = set()
while e is not None and id(e) not in seen:
seen.add(id(e))
if excinfo_:
reprtraceback = self.repr_traceback(excinfo_)
reprcrash: Optional[ReprFileLocation] = (
excinfo_._getreprcrash() if self.style != "value" else None
if isinstance(e, ExceptionGroupTypes):
reprtraceback: Union[
ReprTracebackNative, ReprTraceback
] = ReprTracebackNative(
traceback.format_exception(
type(excinfo.value),
excinfo.value,
excinfo.traceback[0]._rawentry,
)
)
reprcrash: Optional[ReprFileLocation] = None
elif excinfo_:
reprtraceback = self.repr_traceback(excinfo_)
reprcrash = excinfo_._getreprcrash() if self.style != "value" else None
else:
# Fallback to native repr if the exception doesn't have a traceback:
# ExceptionInfo objects require a full traceback to work.

View File

@ -1470,3 +1470,34 @@ def test_no_recursion_index_on_recursion_error():
with pytest.raises(RuntimeError) as excinfo:
RecursionDepthError().trigger
assert "maximum recursion" in str(excinfo.getrepr())
def test_exceptiongroup(pytester: Pytester) -> None:
pytester.makepyfile(
"""
def f(): raise ValueError("From f()")
def g(): raise RuntimeError("From g()")
def main():
excs = []
for callback in [f, g]:
try:
callback()
except Exception as err:
excs.append(err)
if excs:
raise ExceptionGroup("Oops", excs)
def test():
main()
"""
)
result = pytester.runpytest()
assert result.ret != 0
match = [
r" | ExceptionGroup: Oops (2 sub-exceptions)",
r" | ValueError: From f()",
r" | RuntimeError: From g()",
]
result.stdout.re_match_lines(match)

View File

@ -46,6 +46,7 @@ setenv =
extras = testing
deps =
doctesting: PyYAML
exceptiongroup: exceptiongroup>=1.0.0
numpy: numpy>=1.19.4
pexpect: pexpect>=4.8.0
pluggymain: pluggy @ git+https://github.com/pytest-dev/pluggy.git