Make nested-trans-block exceptions nicer
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Aarni Koskela · committed by David Lord · parent 7167953 · Browse files · 3 files·+28 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+28 added−3 removed
- tests:+13 added−0 removed
- other:+15 added−3 removed
Provenance
- Commit
19a55db- Tree
fdb7c8a- Author
Aarni Koskela <akx@iki.fi>- Committer
David Lord <davidism@gmail.com>- Parent
7167953
3 files changed+28 added−3 removed
modified:src/jinja2/ext.py+13 added-3 removed
493 unmodified lines494 buf.append(f"%({name})s")495 parser.stream.expect("variable_end")496 elif parser.stream.current.type == "block_begin":497 next(parser.stream)498+ block_name = (499+ parser.stream.current.value498- if parser.stream.current.test("name:endtrans"):500+ if parser.stream.current.type == "name"501+ else None502+ )503+ if block_name == "endtrans":504 break500- elif parser.stream.current.test("name:pluralize"):505+ elif block_name == "pluralize":506 if allow_pluralize:507 break508 parser.fail(509 "a translatable section can have only one pluralize section"510 )511+ elif block_name == "trans":512+ parser.fail(513+ "trans blocks can't be nested; did you mean `endtrans`?"514+ )515 parser.fail(507- "control structures in translatable sections are not allowed"516+ f"control structures in translatable sections are not allowed; "517+ f"saw `{block_name}`"518 )519 elif parser.stream.eos:520 parser.fail("unclosed translation block")521 else:348 unmodified linesmodified:tests/test_ext.py+13 added
5 unmodified lines6 from jinja2 import DictLoader7 from jinja2 import Environment8 from jinja2 import nodes9 from jinja2 import pass_context10+from jinja2 import TemplateSyntaxError11 from jinja2.exceptions import TemplateAssertionError12 from jinja2.ext import Extension13 from jinja2.lexer import count_newlines14 from jinja2.lexer import Token453 unmodified lines468 (2, "pgettext", ("babel", "Hello World"), []),469 (3, "npgettext", ("babel", "%(users)s user", "%(users)s users", None), []),470 ]471 472+ def test_nested_trans_error(self):473+ s = "{% trans %}foo{% trans %}{% endtrans %}"474+ with pytest.raises(TemplateSyntaxError) as excinfo:475+ i18n_env.from_string(s)476+ assert "trans blocks can't be nested" in str(excinfo.value)477 478+ def test_trans_block_error(self):479+ s = "{% trans %}foo{% wibble bar %}{% endwibble %}{% endtrans %}"480+ with pytest.raises(TemplateSyntaxError) as excinfo:481+ i18n_env.from_string(s)482+ assert "saw `wibble`" in str(excinfo.value)483+484+485 class TestScope:486 def test_basic_scope_behavior(self):487 # This is what the old with statement compiled down to488 class ScopeExt(Extension):251 unmodified linesmodified:CHANGES.rst+2 added
6 unmodified lines7 8 - Fix compiler error when checking if required blocks in parent templates are9 empty. :pr:`1858`10 - ``xmlattr`` filter does not allow keys with spaces. GHSA-h5c8-rqwp-cp9511+- Make error messages stemming from invalid nesting of ``{% trans %}`` blocks12+ more helpful. :pr:`1916`13 14 15 Version 3.1.216 -------------971 unmodified lines