﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28721	"Cannot use the variable name ""inf"" in templates"	Fraser Nevett	Levi Payne	"Trying to render a template variable named **inf** does not work:

{{{#!python
>>> from django.template import Template, Context
>>> Template('{{ inf }}').render(Context({'inf': 'xxx'}))
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""/tmp/venv/lib/python2.7/site-packages/django/template/base.py"", line 191, in __init__
    self.nodelist = self.compile_nodelist()
  File ""/tmp/venv/lib/python2.7/site-packages/django/template/base.py"", line 233, in compile_nodelist
    e.template_debug = self.get_exception_info(e, e.token)
AttributeError: 'exceptions.OverflowError' object has no attribute 'token'
}}}

It also fails if the variable is undefined in the context:
{{{#!python
>>> Template('{{ inf }}').render(Context())
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""/tmp/venv/lib/python2.7/site-packages/django/template/base.py"", line 191, in __init__
    self.nodelist = self.compile_nodelist()
  File ""/tmp/venv/lib/python2.7/site-packages/django/template/base.py"", line 233, in compile_nodelist
    e.template_debug = self.get_exception_info(e, e.token)
AttributeError: 'exceptions.OverflowError' object has no attribute 'token'
}}}

This is happening because `'inf'` is used in Python to represent infinity:

{{{#!python
>>> float('inf')
inf
}}}


The problem appears to be with [https://github.com/django/django/blob/1.11.6/django/template/base.py#L809-L819 this bit of the code]:
{{{#!python
            # First try to treat this variable as a number.
            #
            # Note that this could cause an OverflowError here that we're not
            # catching. Since this should only happen at compile time, that's
            # probably OK.
            self.literal = float(var)

            # So it's a float... is it an int? If the original value contained a
            # dot or an ""e"" then it was a float, not an int.
            if '.' not in var and 'e' not in var.lower():
                self.literal = int(self.literal)
}}}

It successfully converts the string `'inf'` to a float with value of infinity, but then trying to convert this float to an int causes the OverflowError:

{{{#!python
>>> int(float('inf'))
Traceback (most recent call last):
  File ""<stdin>"", line 1, in <module>
OverflowError: cannot convert float infinity to integer
}}}

Python also supports floats of `'-inf'` and `'nan'`. Using `'-inf'` as a template variable name doesn't work because it isn't a valid variable name. Using `'nan'` as a template variable name works OK:

{{{#!python
>>> Template('{{ nan }}').render(Context({'nan': 'xxx'}))
u'xxx'
}}}

I think the fix is to allow variables named **inf**, rather than to support using **inf** as a float literal within templates."	Bug	closed	Template system	1.11	Normal	fixed			Accepted	1	0	0	1	0	0
