Skip to content

fix: keep generated template code within CPython's bracket limit - #318

Merged
JuroOravec merged 2 commits into
mainfrom
fix/deep-generated-code
Oct 24, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
fix/deep-generated-code

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

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 with SyntaxError: too many nested parentheses (<string>, line 2). The compiler printed the whole node tree as one body = [...] 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:

  • A template within that budget prints exactly as before. The 135 locked compile tests in tests/tag_compiler*.rs pass with no edits.
  • For a deeper template, the printer moves some subtrees into local variables _n1, _n2, ... that earlier statements assign. It picks them from the leaves up, which moves the fewest subtrees the bracket limit needs.
  • Python builds a nested expression from the inside out and left to right, and some node constructors raise (ComponentNode rejects 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.
  • The printer walks the tree without recursion, so printing a deeper template needs no more Rust stack. A final check returns an internal error if a statement ever passes 200 brackets.

No other language binding changes, because their compile is still a stub. The LangImpl::compile doc now says the tree nests as deep as the template, and the compiler.rs module docs describe the moved-out form under "Deep templates".

Tests

  • Rust (src/lang/python_printer_tests.rs):
    • The moved-out form is locked for nested <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.
    • Six shapes at depths up to 600 and budgets 100 and 7 are checked for four things: every statement stays within the budget; putting the locals back gives the single-statement output of the original printer; Python runs the constructors in the same order; and output within the budget is byte-identical.
  • Python (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.
  • Proof that the order checks work: with the order rule turned off, the Rust order check fails, and 54 of the Python checks fail.
  • Fast gate: scripts/check.py --profile fast passed 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 and cargo fmt were rerun on it. mypy --platform linux passes.

Not in this PR

At about 330 nested logic tags, rendering raises RecursionError in 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

JuroOravec and others added 2 commits October 24, 2026 15:57
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant