Skip to content

Commit

Permalink
Catch RecursionError in raise_if_nothing_inferred (#1705)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Jul 11, 2022
1 parent 7c0305e commit bb4b66a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Release date: TBA

Closes #1700

* Fixed crash with recursion limits during inference.

Closes #1646

What's New in astroid 2.12.1?
=============================
Release date: 2022-07-10
Expand Down
4 changes: 4 additions & 0 deletions astroid/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ def raise_if_nothing_inferred(func, instance, args, kwargs):
raise InferenceError(
"StopIteration raised without any error information."
) from error
except RecursionError as error:
raise InferenceError(
f"RecursionError raised with limit {sys.getrecursionlimit()}."
) from error

yield from generator

Expand Down
20 changes: 19 additions & 1 deletion tests/unittest_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import sys
import textwrap
import unittest
from unittest import mock

import pytest

from astroid import MANAGER, Instance, bases, nodes, parse, test_utils
from astroid.builder import AstroidBuilder, extract_node
from astroid.builder import AstroidBuilder, _extract_single_node, extract_node
from astroid.const import PY38_PLUS
from astroid.context import InferenceContext
from astroid.exceptions import InferenceError
Expand Down Expand Up @@ -420,5 +421,22 @@ def test_max_inferred_for_complicated_class_hierarchy() -> None:
assert super_node.getattr("__init__", context=context)[0] == Uninferable


@mock.patch(
"astroid.nodes.ImportFrom._infer",
side_effect=RecursionError,
)
def test_recursion_during_inference(mocked) -> None:
"""Check that we don't crash if we hit the recursion limit during inference."""
node: nodes.Call = _extract_single_node(
"""
from module import something
something()
"""
)
with pytest.raises(InferenceError) as error:
next(node.infer())
assert error.value.message.startswith("RecursionError raised")


if __name__ == "__main__":
unittest.main()

0 comments on commit bb4b66a

Please sign in to comment.