From 028f058370265f86ce36fc1313520c81e63fb9f0 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sat, 7 Jan 2017 14:57:44 +0100 Subject: Add a policy for the ascii literal behavior. Fixes #392 --- jinja2/compiler.py | 2 +- jinja2/defaults.py | 1 + jinja2/lexer.py | 9 --------- jinja2/nodes.py | 11 +++++++++-- 4 files changed, 11 insertions(+), 12 deletions(-) (limited to 'jinja2') diff --git a/jinja2/compiler.py b/jinja2/compiler.py index 65bffe6c..6595d632 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -1340,7 +1340,7 @@ class CodeGenerator(NodeVisitor): self.write(ref) def visit_Const(self, node, frame): - val = node.value + val = node.as_const(frame.eval_ctx) if isinstance(val, float): self.write(str(val)) else: diff --git a/jinja2/defaults.py b/jinja2/defaults.py index 90ccb65f..38c53764 100644 --- a/jinja2/defaults.py +++ b/jinja2/defaults.py @@ -41,6 +41,7 @@ DEFAULT_NAMESPACE = { # default policies DEFAULT_POLICIES = { + 'compiler.ascii_str': True, 'urlize.rel': 'noopener', 'urlize.target': None, 'json.dumps_function': None, diff --git a/jinja2/lexer.py b/jinja2/lexer.py index c8dac214..d2ca32fc 100644 --- a/jinja2/lexer.py +++ b/jinja2/lexer.py @@ -574,15 +574,6 @@ class Lexer(object): except Exception as e: msg = str(e).split(':')[-1].strip() raise TemplateSyntaxError(msg, lineno, name, filename) - # if we can express it as bytestring (ascii only) - # we do that for support of semi broken APIs - # as datetime.datetime.strftime. On python 3 this - # call becomes a noop thanks to 2to3 - if PY2: - try: - value = value.encode('ascii') - except UnicodeError: - pass elif token == 'integer': value = int(value) elif token == 'float': diff --git a/jinja2/nodes.py b/jinja2/nodes.py index 4d62cccb..5e0726a3 100644 --- a/jinja2/nodes.py +++ b/jinja2/nodes.py @@ -17,7 +17,7 @@ import operator from collections import deque from jinja2.utils import Markup -from jinja2._compat import izip, with_metaclass, text_type +from jinja2._compat import izip, with_metaclass, text_type, PY2 #: the types we support for context functions @@ -470,7 +470,14 @@ class Const(Literal): fields = ('value',) def as_const(self, eval_ctx=None): - return self.value + rv = self.value + if PY2 and type(rv) is text_type and \ + self.environment.policies['compiler.ascii_str']: + try: + rv = rv.encode('ascii') + except UnicodeError: + pass + return rv @classmethod def from_untrusted(cls, value, lineno=None, environment=None): -- cgit v1.2.3