From: CleoQc Date: Tue, 13 Nov 2018 00:43:13 +0000 (-0500) Subject: support underscore in int and float X-Git-Tag: 2.11.0~72^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1fea4b65f9bdc6908b28e27f24c1dc785796b4df;p=thirdparty%2Fjinja.git support underscore in int and float --- diff --git a/docs/templates.rst b/docs/templates.rst index 88df9167..0e6781cf 100644 --- a/docs/templates.rst +++ b/docs/templates.rst @@ -1177,13 +1177,16 @@ for Python objects such as strings and numbers. The following literals exist: arguments to function calls and filters, or just to extend or include a template). -42: - Integers are whole numbers without a decimal part. +42 / 123_456: + Integers are whole numbers without a decimal part. The '_' character + can be used to separate groups for legibility. -42.23 / 42.1e2: +42.23 / 42.1e2 / 123_456.789: Floating point numbers can be written using a '.' as a decimal mark. They can also be written in scientific notation with an upper or - lower case 'e' to indicate the exponent part. + lower case 'e' to indicate the exponent part. The '_' character can + be used to separate groups for legibility, but cannot be used in the + exponent part. ['list', 'of', 'objects']: Everything between two brackets is a list. Lists are useful for storing diff --git a/jinja2/lexer.py b/jinja2/lexer.py index d8812c9b..a387cf92 100644 --- a/jinja2/lexer.py +++ b/jinja2/lexer.py @@ -23,6 +23,8 @@ from jinja2._compat import implements_iterator, intern, iteritems, text_type from jinja2.exceptions import TemplateSyntaxError from jinja2.utils import LRUCache +from ast import literal_eval # to support scientific notation + # cache for the lexers. Exists in order to be able to have multiple # environments with the same lexer _lexer_cache = LRUCache(50) @@ -31,7 +33,7 @@ _lexer_cache = LRUCache(50) whitespace_re = re.compile(r'\s+', re.U) string_re = re.compile(r"('([^'\\]*(?:\\.[^'\\]*)*)'" r'|"([^"\\]*(?:\\.[^"\\]*)*)")', re.S) -integer_re = re.compile(r'\d+') +integer_re = re.compile(r'(\d+_)*(\d+)') try: # check if this Python supports Unicode identifiers @@ -53,7 +55,7 @@ else: del jinja2._identifier del _identifier -float_re = re.compile(r'(?