Skip to content

Add support for hexadecimal integer literals in templates #1170

@amykyta3

Description

@amykyta3

Since Jinja expression syntax closely mimics Python, I was hoping to use hexadecimal integer literals on some of my templates. In my case, representing values in hexadecimal form is more natural and converting to decimal would cause confusion.

Please add support for the rest of Python's integer literal representations: https://docs.python.org/3/reference/lexical_analysis.html#integer-literals

Expected Behavior

For example:

import jinja2
print(jinja2.Template("Number = {{0x10}}").render())

I would expect it to output:

'Number = 16'

Actual Behavior

Instead, I get a traceback from the Jinja2 lexer:

jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got 'x10'

It seems that only simple integer literals are supported.

Template Code

Number = {{0x10}}

Full Traceback

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/amykyta3/.local/lib/python3.7/site-packages/jinja2/environment.py", line 1031, in __new__
    return env.from_string(source, template_class=cls)
  File "/home/amykyta3/.local/lib/python3.7/site-packages/jinja2/environment.py", line 941, in from_string
    return cls.from_code(self, self.compile(source), globals, None)
  File "/home/amykyta3/.local/lib/python3.7/site-packages/jinja2/environment.py", line 638, in compile
    self.handle_exception(source=source_hint)
  File "/home/amykyta3/.local/lib/python3.7/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/home/amykyta3/.local/lib/python3.7/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<unknown>", line 1, in template
jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got 'x10'

Your Environment

  • Python version: 3.7
  • Jinja version: 2.11.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions