Skip to content

Commit 1327f6b

Browse files
authored
[fix] Fix parsing deeply nested parentheses causes MemoryError (#2809)
1 parent f7c8b4c commit 1327f6b

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-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+
* Prevent crash when parsing deeply nested parentheses causing MemoryError in python's built-in ast.
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: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
33
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
44

5+
import platform
56
import sys
67
import textwrap
78
import unittest
@@ -13,7 +14,7 @@
1314
from astroid.builder import AstroidBuilder, _extract_single_node, extract_node
1415
from astroid.const import PY312_PLUS
1516
from astroid.context import InferenceContext
16-
from astroid.exceptions import InferenceError
17+
from astroid.exceptions import AstroidSyntaxError, InferenceError
1718
from astroid.manager import AstroidManager
1819
from astroid.raw_building import build_module
1920
from astroid.util import Uninferable
@@ -561,3 +562,15 @@ def test_regression_infer_namedtuple_invalid_fieldname_error() -> None:
561562
node = extract_node(code)
562563
inferred = next(node.infer())
563564
assert inferred.value == Uninferable
565+
566+
567+
def test_regression_parse_deeply_nested_parentheses() -> None:
568+
"""Regression test for issue #2643."""
569+
with pytest.raises(AstroidSyntaxError, match="Parsing Python code failed:") as ctx:
570+
extract_node(
571+
"A=((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((c,j=t"
572+
)
573+
expected = (
574+
SyntaxError if platform.python_implementation() == "PyPy" else MemoryError
575+
)
576+
assert isinstance(ctx.value.error, expected)

0 commit comments

Comments
 (0)