Skip to content

Fix three crashes in the parser - #81

Open
hilman2 wants to merge 1 commit into
CheetahTemplate3:masterfrom
hilman2:fix-parser-crashes
Open

Fix three crashes in the parser#81
hilman2 wants to merge 1 commit into
CheetahTemplate3:masterfrom
hilman2:fix-parser-crashes

Conversation

@hilman2

@hilman2 hilman2 commented Aug 26, 2026

Copy link
Copy Markdown

Three templates that abort compilation on master:

#set $x = c'a\nb'          ParseError: malformed \N character escape
#set $x = c''              TypeError: expected string or bytes-like object, got 'NoneType'
#def outer                 UnboundLocalError: 'useSearchList_orig'
#block inner: hi
#end def

The first two come from transformToken: it uppercases the whole string literal before eval, and it returns None when the string is empty. The uppercased value is only tested for emptiness, so the call has no purpose.

The third is _eatSingleLineDef binding useSearchList_orig in one branch and reading it under a different condition.

Escapes inside c'...' stay uninterpreted, since the string is read character by character from the source. This only stops the crash.

Three regression tests in SyntaxAndOutput.py. Full suite passes on 2.7, 3.6 and 3.12, flake8 clean.

`transformToken` uppercased the whole string literal before evaluating
it. That turns a newline escape into `\N`, an invalid escape, so every
`c'...'` string containing a backslash aborted compilation. The result
is only tested for emptiness, so the call had no purpose.

The same method returned `None` for an empty string. Callers append
the result unconditionally, so `c''` crashed with a TypeError.

`_eatSingleLineDef` bound `useSearchList_orig` in the closure branch
only but read it whenever `isNestedDef` was true. A single-line
`#block` inside a `#def` takes the other branch with `isNestedDef`
true, so it raised UnboundLocalError. The restore now follows the same
condition as the assignment.

Escapes inside `c'...'` strings stay uninterpreted; the string is read
character by character from the source. This only stops the crash.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.

2 participants