Skip to content
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Drop support for Python 3.8 (Jendrik Seipp, #398).
* Handle `while True` loops without `break` statements (kreathon).
* Add whitelist for `ssl.SSLContext` (tunnelsociety, #392).
* Handle RecursionError when analyzing deeply nested code structures (GitHub Copilot).

# 2.14 (2024-12-08)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@ def test_invalid_cmdline_args():
call_vulture(["vulture/", "--invalid-argument"])
== ExitCode.InvalidCmdlineArguments
)


def test_recursion_error(v):
# Create code with deeply nested binary operations that will
# trigger RecursionError during AST visiting
depth = 500
code = "result = " + " + ".join(["1"] * depth) + "\n"
v.scan(code, filename="test_deep.py")
assert int(v.report()) == ExitCode.InvalidInput
14 changes: 13 additions & 1 deletion vulture/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,16 @@ def handle_syntax_error(e):
self.visit(node)
except SyntaxError as err:
handle_syntax_error(err)
except RecursionError:
self._log(
f"{utils.format_path(filename)}: "
"RecursionError: maximum recursion depth exceeded "
"while analyzing code. "
"This can happen with extremely nested expressions.",
file=sys.stderr,
force=True,
)
self.exit_code = ExitCode.InvalidInput

# Reset the reachability internals for every module to reduce memory
# usage.
Expand Down Expand Up @@ -362,7 +372,9 @@ def report(
else item.get_report(add_size=sort_by_size),
force=True,
)
self.exit_code = ExitCode.DeadCode
# Only set DeadCode if no error has occurred
if self.exit_code == ExitCode.NoDeadCode:
self.exit_code = ExitCode.DeadCode
return self.exit_code

@property
Expand Down