Skip to content

Commit

Permalink
Merge pull request #618 from jackwilsdon/stricter-from-parsing
Browse files Browse the repository at this point in the history
Add stricter checking to "from ... import ..."
  • Loading branch information
davidism committed Jul 7, 2017
2 parents 2c61df1 + 32afe63 commit a361b74
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -28,10 +28,12 @@ Version 2.10
- Add ``min`` and ``max`` filters. (`#475`_)
- Add tests for all comparison operators: ``eq``, ``ne``, ``lt``, ``le``,
``gt``, ``ge``. (`#665`_)
- ``import`` statement cannot end with a trailing comma. (`#618`_)

.. _#469: https://github.com/pallets/jinja/pull/469
.. _#475: https://github.com/pallets/jinja/pull/475
.. _#478: https://github.com/pallets/jinja/pull/478
.. _#618: https://github.com/pallets/jinja/pull/618
.. _#665: https://github.com/pallets/jinja/pull/665

Version 2.9.6
Expand Down
2 changes: 1 addition & 1 deletion jinja2/parser.py
Expand Up @@ -334,7 +334,7 @@ def parse_context():
if parse_context() or self.stream.current.type != 'comma':
break
else:
break
self.stream.expect('name')
if not hasattr(node, 'with_context'):
node.with_context = False
self.stream.skip_if('comma')
Expand Down
22 changes: 20 additions & 2 deletions tests/test_imports.py
Expand Up @@ -11,7 +11,8 @@
import pytest

from jinja2 import Environment, DictLoader
from jinja2.exceptions import TemplateNotFound, TemplatesNotFound
from jinja2.exceptions import TemplateNotFound, TemplatesNotFound, \
TemplateSyntaxError


@pytest.fixture
Expand Down Expand Up @@ -50,7 +51,24 @@ def test_context_imports(self, test_env):
)
assert t.render(foo=42) == '[42|23]'

def test_trailing_comma(self, test_env):
def test_import_needs_name(self, test_env):
test_env.from_string('{% from "foo" import bar %}')
test_env.from_string('{% from "foo" import bar, baz %}')

with pytest.raises(TemplateSyntaxError):
test_env.from_string('{% from "foo" import %}')

def test_no_trailing_comma(self, test_env):
with pytest.raises(TemplateSyntaxError):
test_env.from_string('{% from "foo" import bar, %}')

with pytest.raises(TemplateSyntaxError):
test_env.from_string('{% from "foo" import bar,, %}')

with pytest.raises(TemplateSyntaxError):
test_env.from_string('{% from "foo" import, %}')

def test_trailing_comma_with_context(self, test_env):
test_env.from_string('{% from "foo" import bar, baz with context %}')
test_env.from_string('{% from "foo" import bar, baz, with context %}')
test_env.from_string('{% from "foo" import bar, with context %}')
Expand Down

0 comments on commit a361b74

Please sign in to comment.