Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

safe_range can consume all available memory on Python 2 #933

alekseyev opened this issue Dec 13, 2018 · 1 comment


Copy link

commented Dec 13, 2018

safe_range has previously used xrange to get the length of a sequence.

    rng = xrange(*args)
    if len(rng) > MAX_RANGE:
        raise OverflowError('range too big, maximum size for range is %d' %

However since porting to Python 3 it uses range which behaves differently in Python 2 and 3. On Python 2 it will try to generate the whole sequence in memory before measuring the length -- which can cause an out of memory error.

Expected Behavior

safe_range(100000000000) should raise an OverflowError

Actual Behavior

safe_range(100000000000) attempts to generate a huge list in memory

Template Code

{%for n in range(100000000000) %}{{n}}{% endfor %}

Your Environment

  • Python version: 2.7
  • Jinja version: 2.10

This comment has been minimized.

Copy link

commented Feb 18, 2019

Looks like this was an oversight on my part when I pulled in some Python 3 changes. This should be changed to use xrange again on 2.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
3 participants
You can’t perform that action at this time.