Repository navigation
fix: keep generated template code within CPython's bracket limit - #318
Merged
Merged
Conversation
A template with 50 or more nested <c-if> or <c-for> tags failed to render with "SyntaxError: too many nested parentheses", because the compiler printed the whole node tree as one `body = [...]` statement, each logic level adds four nested brackets, and CPython allows at most 200. The Python printer now keeps every statement within 100 nested brackets. Past that, it moves some subtrees into `_nN` locals assigned in earlier statements. Subtrees that are too deep are chosen from the leaves up, so the fewest move for depth, and every earlier sibling that calls a constructor moves too, so Python builds the nodes in the same order. The printer walks the tree without recursion, and a self-check reports a statement past CPython's limit of 200 nested brackets as an internal error. Templates within the budget print exactly as before: the locked compile tests pass unchanged. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Oct 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #272. This is PR B of the plan in #272: it keeps the generated Python code within CPython's bracket limit.
What changes
A template with 50 or more nested
<c-if>or<c-for>tags failed to render withSyntaxError: too many nested parentheses (<string>, line 2). The compiler printed the whole node tree as onebody = [...]statement. Every logic level adds four nested brackets, and CPython allows at most 200 in one statement.The Python printer (
lang/python.rs) now keeps every statement within 100 nested brackets:tests/tag_compiler*.rspass with no edits._n1,_n2, ... that earlier statements assign. It picks them from the leaves up, which moves the fewest subtrees the bracket limit needs.ComponentNoderejects a malformed<c-mark>). So when a subtree moves, every earlier sibling that calls a constructor moves too, and Python still builds the nodes in the same order.No other language binding changes, because their
compileis still a stub. TheLangImpl::compiledoc now says the tree nests as deep as the template, and thecompiler.rsmodule docs describe the moved-out form under "Deep templates".Tests
src/lang/python_printer_tests.rs):<c-if>/<c-else>,<c-for>/<c-empty>, a fill holding a slot, element attributes with#c-key, prepared mode, a constructor before a deep sibling, and 30 nested<c-if>at the real budget.tests/test_deep_templates.py): six representative templates each hold a{{ hole }}with nodes on both sides, and 20 to 120 levels of<c-if cond="show">go around the hole. Each one is checked against the template with no levels added: it must render the same HTML, on the native and Python render paths and in prepared mode, and build its nodes in the same order, compared with source spans.scripts/check.py --profile fastpassed at 2d3dc13ea, with citry_core built from that commit. The final commit only changes comments and changelog wording after that run.cargo test -p citry_template_parser, the new Python tests, ruff andcargo fmtwere rerun on it.mypy --platform linuxpasses.Not in this PR
At about 330 nested logic tags, rendering raises
RecursionErrorin the Python walkers. At about 400 to 1,000 levels, the Rust parser and compiler can crash the process. Later PRs in #272 fix both.🤖 Generated with Claude Code