Skip to content
Myr

Make nested-trans-block exceptions nicer (#1918)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by GitHub · parents 7167953, 19a55db · 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

3 files+28 added−3 removed

modified:src/jinja2/ext.py+13 added-3 removed
493 unmodified lines
494 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 lines
modified:tests/test_ext.py+13 added
5 unmodified lines
6from jinja2 import DictLoader7from jinja2 import Environment8from jinja2 import nodes9from jinja2 import pass_context10from jinja2 import TemplateSyntaxError11from jinja2.exceptions import TemplateAssertionError12from jinja2.ext import Extension13from jinja2.lexer import count_newlines14from jinja2.lexer import Token
453 unmodified lines
468 (2, "pgettext", ("babel", "Hello World"), []),469 (3, "npgettext", ("babel", "%(users)s user", "%(users)s users", None), []),470 ]471472 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)477478 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)483484485class TestScope:486 def test_basic_scope_behavior(self):487 # This is what the old with statement compiled down to488 class ScopeExt(Extension):
251 unmodified lines
modified:CHANGES.rst+2 added
6 unmodified lines
78- 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`131415Version 3.1.216-------------
971 unmodified lines