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

Manage recursion limit preventing RuntimeError #68

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions pyflakes/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ def __init__(self, tree, filename='(none)', builtins=None,
self.scopeStack = [ModuleScope()]
self.exceptHandlers = [()]
self.root = tree
self._recursion_limit = sys.getrecursionlimit()
self.handleChildren(tree)
self.runDeferred(self._deferredFunctions)
# Set _deferredFunctions to None so that deferFunction will fail
Expand Down Expand Up @@ -820,6 +821,15 @@ def on_conditional_branch():
self.report(messages.UndefinedName, node, name)

def handleChildren(self, tree, omit=None):
# The recursion limit needs to be at least double nodeDepth
# as the recursion cycles between handleChildren and handleNode.
# Set it to triple nodeDepth to account for other items on the stack,
# and to reduce the frequency of changes to the limit.
acceptable_recursion_limit = self.nodeDepth * 3
if self._recursion_limit <= acceptable_recursion_limit:
sys.setrecursionlimit(acceptable_recursion_limit)
self._recursion_limit = acceptable_recursion_limit

for node in iter_child_nodes(tree, omit=omit):
self.handleNode(node, tree)

Expand Down
28 changes: 28 additions & 0 deletions pyflakes/test/test_other.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
"""
Tests for various Pyflakes behavior.
"""
import sys

from sys import version_info

from pyflakes import messages as m
from pyflakes.test.harness import TestCase, skip, skipIf

try:
sys.pypy_version_info
PYPY = True
except AttributeError:
PYPY = False


class Test(TestCase):

Expand Down Expand Up @@ -1993,3 +2000,24 @@ def test_raise_notimplemented(self):
self.flakes('''
raise NotImplemented
''', m.RaiseNotImplemented)


class TestMaximumRecursion(TestCase):

def setUp(self):
self._recursionlimit = sys.getrecursionlimit()

def test_recursion_limit(self):
# Using self._recursionlimit * 10 tends to cause CPython to core dump.
# Older PyPy tend to break with lower recusion limits.
if PYPY and version_info < (3, 5):
new_recursion_limit = self._recursionlimit * 2
else:
new_recursion_limit = self._recursionlimit * 6

r = range(new_recursion_limit)
s = 'x = ' + ' + '.join(str(n) for n in r)
self.flakes(s)

def tearDown(self):
sys.setrecursionlimit(self._recursionlimit)