Skip to content

Commit 313d292

Browse files
authored
fix: raise ParseError instead of AssertionError on unmatched '}' (#604)
An unmatched '}' (e.g. parser.parse('}')) reached an internal assertion (c_parser.py:127: assert len(self._scope_stack) > 1) rather than returning through the usual ParseError path. Callers that catch ParseError to handle parse failures would not catch the AssertionError. Replace the bare assert with an explicit ParseError so malformed input travels the documented error path. Fixes #603.
1 parent 89c9f3d commit 313d292

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

pycparser/c_parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ def _push_scope(self) -> None:
124124
self._scope_stack.append(dict())
125125

126126
def _pop_scope(self) -> None:
127-
assert len(self._scope_stack) > 1
127+
if len(self._scope_stack) <= 1:
128+
raise ParseError("Unmatched '}'")
128129
self._scope_stack.pop()
129130

130131
def _add_typedef_name(self, name: str, coord: Optional[Coord]) -> None:

tests/test_c_parser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3783,3 +3783,11 @@ def test_case_empty_statement(self):
37833783

37843784
# ~ unittest.TextTestRunner(verbosity=2).run(suite)
37853785
unittest.main()
3786+
3787+
class TestUnmatchedRbrace(unittest.TestCase):
3788+
"""Regression for #603: unmatched '}' raises ParseError, not AssertionError."""
3789+
3790+
def test_unmatched_rbrace_raises_parse_error(self):
3791+
parser = c_parser.CParser()
3792+
with self.assertRaises(ParseError):
3793+
parser.parse("}", filename="test.c")

0 commit comments

Comments
 (0)