summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlex Mykyta <amykyta3@users.noreply.github.com>2020-03-10 20:57:28 -0700
committerDavid Lord <davidism@gmail.com>2021-04-05 10:35:57 -0700
commit2e0a3da2bc8b764dc8cb5aec390cc7ac7dacd8ae (patch)
treea387deadf03ef2ebd43bb22e3ad16de776294ca5 /src
parent1eb871fdc32cb90db54700281dd6f4b7aa9a4cad (diff)
downloadjinja2-2e0a3da2bc8b764dc8cb5aec390cc7ac7dacd8ae.tar.gz
parse hex, octal, and binary integer literals
Diffstat (limited to 'src')
-rw-r--r--src/jinja2/lexer.py19
1 files changed, 17 insertions, 2 deletions
diff --git a/src/jinja2/lexer.py b/src/jinja2/lexer.py
index 082a051..d992f0d 100644
--- a/src/jinja2/lexer.py
+++ b/src/jinja2/lexer.py
@@ -23,7 +23,22 @@ newline_re = re.compile(r"(\r\n|\r|\n)")
string_re = re.compile(
r"('([^'\\]*(?:\\.[^'\\]*)*)'" r'|"([^"\\]*(?:\\.[^"\\]*)*)")', re.S
)
-integer_re = re.compile(r"(\d+_)*\d+")
+integer_re = re.compile(
+ r"""
+ (
+ 0b(_?[0-1])+ # binary
+ |
+ 0o(_?[0-7])+ # octal
+ |
+ 0x(_?[\da-f])+ # hex
+ |
+ [1-9](_?\d)* # decimal
+ |
+ 0(_?0)* # decimal zero
+ )
+ """,
+ re.IGNORECASE | re.VERBOSE,
+)
float_re = re.compile(
r"""
(?<!\.) # doesn't start with a .
@@ -613,7 +628,7 @@ class Lexer:
msg = str(e).split(":")[-1].strip()
raise TemplateSyntaxError(msg, lineno, name, filename)
elif token == TOKEN_INTEGER:
- value = int(value.replace("_", ""))
+ value = int(value.replace("_", ""), 0)
elif token == TOKEN_FLOAT:
# remove all "_" first to support more Python versions
value = literal_eval(value.replace("_", ""))