Skip to content
Merged
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
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ What's New in astroid 4.0.0?
============================
Release date: TBA

* Prevent crash when parsing deeply nested parentheses causing MemoryError in python's built-in ast.

Closes #2643

* Fix crash when inferring namedtuple with invalid field name looking like f-string formatting.

Closes #2519
Expand Down
2 changes: 1 addition & 1 deletion astroid/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def _data_build(
node, parser_module = _parse_string(
data, type_comments=True, modname=modname
)
except (TypeError, ValueError, SyntaxError) as exc:
except (TypeError, ValueError, SyntaxError, MemoryError) as exc:
raise AstroidSyntaxError(
"Parsing Python code failed:\n{error}",
source=data,
Expand Down
15 changes: 14 additions & 1 deletion tests/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt

import platform
import sys
import textwrap
import unittest
Expand All @@ -13,7 +14,7 @@
from astroid.builder import AstroidBuilder, _extract_single_node, extract_node
from astroid.const import PY312_PLUS
from astroid.context import InferenceContext
from astroid.exceptions import InferenceError
from astroid.exceptions import AstroidSyntaxError, InferenceError
from astroid.manager import AstroidManager
from astroid.raw_building import build_module
from astroid.util import Uninferable
Expand Down Expand Up @@ -561,3 +562,15 @@ def test_regression_infer_namedtuple_invalid_fieldname_error() -> None:
node = extract_node(code)
inferred = next(node.infer())
assert inferred.value == Uninferable


def test_regression_parse_deeply_nested_parentheses() -> None:
"""Regression test for issue #2643."""
with pytest.raises(AstroidSyntaxError, match="Parsing Python code failed:") as ctx:
extract_node(
"A=((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((c,j=t"
)
expected = (
SyntaxError if platform.python_implementation() == "PyPy" else MemoryError
)
assert isinstance(ctx.value.error, expected)
Loading