Version 2.10.2
--------------
-_Unreleased_
+Unreleased
+
+- Fix Python 3.7 deprecation warnings.
+- Using ``range`` in the sandboxed environment uses ``xrange`` on
+ Python 2 to avoid memory use. #933
-- Fix Python 3.7 deprecation warnings.
Version 2.10.1
--------------
import operator
from jinja2.environment import Environment
from jinja2.exceptions import SecurityError
-from jinja2._compat import string_types, PY2, abc
+from jinja2._compat import string_types, PY2, abc, range_type
from jinja2.utils import Markup
from markupsafe import EscapeFormatter
"""A range that can't generate ranges with a length of more than
MAX_RANGE items.
"""
- rng = range(*args)
+ rng = range_type(*args)
+
if len(rng) > MAX_RANGE:
- raise OverflowError('range too big, maximum size for range is %d' %
- MAX_RANGE)
+ raise OverflowError(
+ "Range too big. The sandbox blocks ranges larger than"
+ " MAX_RANGE (%d)." % MAX_RANGE
+ )
+
return rng
c.next()
assert c.current == 2
c.reset()
- assert c.current == 1
+ assert c.current == 1
def test_expressions(self, env):
expr = env.compile_expression("foo")
t = env.from_string('{{ foo }}')
assert t.render(foo='<foo>') == '<foo>'
+ def test_sandbox_max_range(self, env):
+ from jinja2.sandbox import SandboxedEnvironment, MAX_RANGE
+
+ env = SandboxedEnvironment()
+ t = env.from_string("{% for item in range(total) %}{{ item }}{% endfor %}")
+
+ with pytest.raises(OverflowError):
+ t.render(total=MAX_RANGE + 1)
+
@pytest.mark.api
@pytest.mark.meta