Skip to content
Myr

Merge pull request #1171 from amykyta3/feature/base-integer-literals

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

Add support for hex, octal, and binary integer literals

authored by David Lord · committed by GitHub · parents 1eb871f, 2e0a3da · Browse files · 3 files+26 added−2 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+26 added−2 removed

  • tests:+7 added−0 removed
  • other:+19 added−2 removed

3 files+26 added−2 removed

modified:src/jinja2/lexer.py+17 added-2 removed
21 unmodified lines
22newline_re = re.compile(r"(\r\n|\r|\n)")23string_re = re.compile(24 r"('([^'\\]*(?:\\.[^'\\]*)*)'" r'|"([^"\\]*(?:\\.[^"\\]*)*)")', re.S25)26integer_re = re.compile(r"(\d+_)*\d+")26integer_re = re.compile(27 r"""28 (29 0b(_?[0-1])+ # binary30 |31 0o(_?[0-7])+ # octal32 |33 0x(_?[\da-f])+ # hex34 |35 [1-9](_?\d)* # decimal36 |37 0(_?0)* # decimal zero38 )39 """,40 re.IGNORECASE | re.VERBOSE,41)42float_re = re.compile(43 r"""44 (?<!\.) # doesn't start with a .45 (\d+_)*\d+ # digits, possibly _ separated
581 unmodified lines
627 except Exception as e:628 msg = str(e).split(":")[-1].strip()629 raise TemplateSyntaxError(msg, lineno, name, filename)630 elif token == TOKEN_INTEGER:616 value = int(value.replace("_", ""))631 value = int(value.replace("_", ""), 0)632 elif token == TOKEN_FLOAT:633 # remove all "_" first to support more Python versions634 value = literal_eval(value.replace("_", ""))635 elif token == TOKEN_OPERATOR:
181 unmodified lines
modified:tests/test_lexnparse.py+7 added
410 unmodified lines
411 ("2.5e100", "2.5e+100"),412 ("2.5e+100", "2.5e+100"),413 ("25.6e-10", "2.56e-09"),414 ("1_2.3_4e5_6", "1.234e+57"),415 ("0", "0"),416 ("0_00", "0"),417 ("0b1001_1111", "159"),418 ("0o123", "83"),419 ("0o1_23", "83"),420 ("0x123abc", "1194684"),421 ("0x12_3abc", "1194684"),422 ),423 )424 def test_numeric_literal(self, env, value, expect):425 t = env.from_string(f"{{{{ {value} }}}}")
605 unmodified lines
modified:CHANGES.rst+2 added
48 unmodified lines
49- Support ``pgettext`` and ``npgettext`` (message contexts) in i18n50 extension. :issue:`441`51- The ``|indent`` filter's ``width`` argument can be a string to52 indent by. :pr:`1167`53- The parser understands hex, octal, and binary integer literals.54 :issue:`1170`555657Version 2.11.358--------------
780 unmodified lines