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

RecursionError in InterestingOrigin.from_exception with recursive __context__ #4115

Closed
jakkdl opened this issue Sep 25, 2024 · 2 comments · Fixed by #4119
Closed

RecursionError in InterestingOrigin.from_exception with recursive __context__ #4115

jakkdl opened this issue Sep 25, 2024 · 2 comments · Fixed by #4119
Labels
bug something is clearly wrong here

Comments

@jakkdl
Copy link
Contributor

jakkdl commented Sep 25, 2024

def test_recursive_exception():
    @given(st.data())
    def test_function(data):
        try:
            raise ExceptionGroup("", [ValueError()])
        except ExceptionGroup as eg:
            raise eg.exceptions[0] from None
    test_function()

causes an infinite recursion in InterestingOrigin.from_exception

def from_exception(cls, exception: BaseException, /) -> "InterestingOrigin":
filename, lineno = None, None
if tb := get_trimmed_traceback(exception):
filename, lineno, *_ = traceback.extract_tb(tb)[-1]
return cls(
type(exception),
filename,
lineno,
# Note that if __cause__ is set it is always equal to __context__, explicitly
# to support introspection when debugging, so we can use that unconditionally.
cls.from_exception(exception.__context__) if exception.__context__ else (),
# We distinguish exception groups by the inner exceptions, as for __context__
(
tuple(map(cls.from_exception, exception.exceptions))
if isinstance(exception, BaseExceptionGroup)
else ()
),
)

Due to exception.__context__.exceptions[0] is exception.
You can also do it without exceptiongroups, but idk if this would ever be encountered in the wild:

def test_recursive_exception2():
    @given(st.data())
    def test_function(data):
        k = ValueError()
        k.__context__ = k
        raise k
    test_function()

Encountered during development of #4110

@Zac-HD Zac-HD added the bug something is clearly wrong here label Sep 25, 2024
@Zac-HD
Copy link
Member

Zac-HD commented Sep 25, 2024

We should clearly fix both of these - thanks for the report!

@jakkdl
Copy link
Contributor Author

jakkdl commented Sep 27, 2024

Since I wanted to reraise a group exception in #4110 I added a quick fix in the latest commit: 14be202#diff-790b2cc456c4882f3785f65d06add0d5ed8e472a5c48b9abcbfd56eb23fb952a

idk what you want the long-term fix to be, but I feel like this should have some standard-ish solution used in pytest and others that parses __context__.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something is clearly wrong here
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants