Skip to content
Myr

sandbox uses xrange on Python 2

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

authored by Prakhar Bhandari · committed by David Lord · parent e74640b · Browse files · 3 files+23 added−7 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+23 added−7 removed

  • tests:+10 added−1 removed
  • other:+13 added−6 removed

3 files+23 added−7 removed

modified:jinja2/sandbox.py+8 added-4 removed
14 unmodified lines
15import types16import operator17from jinja2.environment import Environment18from jinja2.exceptions import SecurityError19from jinja2._compat import string_types, PY2, abc19from jinja2._compat import string_types, PY2, abc, range_type20from jinja2.utils import Markup2122from markupsafe import EscapeFormatter23from string import Formatter
121 unmodified lines
145def safe_range(*args):146 """A range that can't generate ranges with a length of more than147 MAX_RANGE items.148 """149 rng = range(*args)149 rng = range_type(*args)150151 if len(rng) > MAX_RANGE:151 raise OverflowError('range too big, maximum size for range is %d' %152 MAX_RANGE)152 raise OverflowError(153 "Range too big. The sandbox blocks ranges larger than"154 " MAX_RANGE (%d)." % MAX_RANGE155 )156157 return rng158159160def unsafe(f):
328 unmodified lines
modified:tests/test_api.py+10 added-1 removed
66 unmodified lines
67 assert c.next() == item68 c.next()69 assert c.current == 270 c.reset()71 assert c.current == 1 71 assert c.current == 17273 def test_expressions(self, env):74 expr = env.compile_expression("foo")75 assert expr() is None
27 unmodified lines
103 assert t.render(foo='<foo>') == '&lt;foo&gt;'104 t = env.from_string('{{ foo }}')105 assert t.render(foo='<foo>') == '<foo>'106107 def test_sandbox_max_range(self, env):108 from jinja2.sandbox import SandboxedEnvironment, MAX_RANGE109110 env = SandboxedEnvironment()111 t = env.from_string("{% for item in range(total) %}{{ item }}{% endfor %}")112113 with pytest.raises(OverflowError):114 t.render(total=MAX_RANGE + 1)115116117@pytest.mark.api118@pytest.mark.meta119class TestMeta(object):120
229 unmodified lines
modified:CHANGES.rst+5 added-2 removed
3 unmodified lines
45Version 2.10.26--------------78_Unreleased_8Unreleased910- Fix Python 3.7 deprecation warnings.10- Fix Python 3.7 deprecation warnings.11- Using ``range`` in the sandboxed environment uses ``xrange`` on12 Python 2 to avoid memory use. #933131415Version 2.10.116--------------1718Released 2019-04-06
598 unmodified lines