Skip to content

Commit e47e3b8

Browse files
committed
[fix] Fix parsing deeply nested parentheses causing MemoryError
1 parent f7c8b4c commit e47e3b8

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ What's New in astroid 4.0.0?
77
============================
88
Release date: TBA
99

10+
* Fix parsing deeply nested parentheses causing MemoryError
11+
12+
Closes #2643
13+
1014
* Fix crash when inferring namedtuple with invalid field name looking like f-string formatting.
1115

1216
Closes #2519

astroid/builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def _data_build(
181181
node, parser_module = _parse_string(
182182
data, type_comments=True, modname=modname
183183
)
184-
except (TypeError, ValueError, SyntaxError) as exc:
184+
except (TypeError, ValueError, SyntaxError, MemoryError) as exc:
185185
raise AstroidSyntaxError(
186186
"Parsing Python code failed:\n{error}",
187187
source=data,

tests/test_regrtest.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from astroid.builder import AstroidBuilder, _extract_single_node, extract_node
1414
from astroid.const import PY312_PLUS
1515
from astroid.context import InferenceContext
16-
from astroid.exceptions import InferenceError
16+
from astroid.exceptions import AstroidSyntaxError, InferenceError
1717
from astroid.manager import AstroidManager
1818
from astroid.raw_building import build_module
1919
from astroid.util import Uninferable
@@ -561,3 +561,12 @@ def test_regression_infer_namedtuple_invalid_fieldname_error() -> None:
561561
node = extract_node(code)
562562
inferred = next(node.infer())
563563
assert inferred.value == Uninferable
564+
565+
566+
def test_regression_parse_deeply_nested_parentheses() -> None:
567+
"""Regression test for issue #2643."""
568+
with pytest.raises(AstroidSyntaxError, match="Parsing Python code failed:") as ctx:
569+
extract_node(
570+
"A=((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((c,j=t"
571+
)
572+
assert isinstance(ctx.value.error, MemoryError)

0 commit comments

Comments
 (0)