[rejected AI] ignore invalid escape sequence warning when unescaping string literals (#1156) - #2264
Closed
amasen02 wants to merge 1 commit into
Closed
Conversation
pallets#1156) When template strings contain unrecognized escape sequences (e.g. '\d', '\w'), Python emits a DeprecationWarning (or SyntaxWarning in Python 3.12+) when calling decode('unicode-escape'). Under -Werror, this warning is raised as an exception and caught by Jinja as a TemplateSyntaxError. Ignore DeprecationWarning and SyntaxWarning during decode('unicode-escape') in string unescaping so templates with raw regex/escape patterns parse cleanly under strict warning filters. Fixes pallets#1156
Member
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.
fixes #1156
Problem
When string literals in templates contain unrecognized escape sequences (such as regex patterns like
"\d","\w","\s"), Python'sdecode("unicode-escape")emits aDeprecationWarning(orSyntaxWarningin Python 3.12+).Under strict warning configurations such as
python -Werroror test suites with warning filters enabled, this warning is converted into an exception during decoding. Jinja's lexer catches this inexcept Exception as e:and raises aTemplateSyntaxError: invalid escape sequence '\d').Solution
Wrap the
decode("unicode-escape")call insideLexer.tokeniterin a warning filter context that ignoresDeprecationWarningandSyntaxWarning. This permits template string literals containing regex patterns or unrecognized escape sequences to be unescaped without raising warnings or failing when-Werroris active.Verification
test_unrecognized_escape_sequenceintests/test_lexnparse.pyverifying template strings with\d \w \sparse and render properly without raising warnings.-Werrorwith pytest: 912/912 tests passed 100% green.ruff check src testsandruff format --check src tests: all clean.CHANGES.rst.