Skip to content

Commit fd4291e

Browse files
committed
Support jinja2 3.1
Since Jinja2 3.1, `while_` and `autoescape` have been removed. They've been included in stdlib for a while and didn't need to be included. Unfortuantely we don't pin to any lower or upper bounds of jinja2, so the best/fastest option is to just version detect and include by default if less than 3.1. Fixes #100 Fixes #101
1 parent ac0ccb5 commit fd4291e

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

jinja2cli/cli.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def _load_querystring():
150150
import urllib.parse as urlparse
151151

152152
def _parse_qs(data):
153-
""" Extend urlparse to allow objects in dot syntax.
153+
"""Extend urlparse to allow objects in dot syntax.
154154
155155
>>> _parse_qs('user.first_name=Matt&user.last_name=Robenolt')
156156
{'user': {'first_name': 'Matt', 'last_name': 'Robenolt'}}
@@ -221,7 +221,23 @@ def _parse_env(data):
221221

222222

223223
def render(template_path, data, extensions, strict=False):
224-
from jinja2 import Environment, FileSystemLoader, StrictUndefined
224+
from jinja2 import (
225+
__version__ as jinja_version,
226+
Environment,
227+
FileSystemLoader,
228+
StrictUndefined,
229+
)
230+
231+
# Starting with jinja2 3.1, `with_` and `autoescape` are no longer
232+
# able to be imported, but since they were default, let's stub them back
233+
# in implicitly for older versions.
234+
# We also don't track any lower bounds on jinja2 as a dependency, so
235+
# it's not easily safe to know it's included by default either.
236+
if tuple(jinja_version.split(".", 2)) < ("3", "1"):
237+
for ext in "with_", "autoescape":
238+
ext = "jinja2.ext." + ext
239+
if ext not in extensions:
240+
extensions.append(ext)
225241

226242
env = Environment(
227243
loader=FileSystemLoader(os.path.dirname(template_path)),
@@ -287,7 +303,7 @@ def cli(opts, args):
287303
try:
288304
data = fn(data) or {}
289305
except except_exc:
290-
raise raise_exc(u"%s ..." % data[:60])
306+
raise raise_exc("%s ..." % data[:60])
291307
else:
292308
data = {}
293309

@@ -385,7 +401,7 @@ def main():
385401
help="extra jinja2 extensions to load",
386402
dest="extensions",
387403
action="append",
388-
default=["do", "with_", "autoescape", "loopcontrols"],
404+
default=["do", "loopcontrols"],
389405
)
390406
parser.add_option(
391407
"-D",

0 commit comments

Comments
 (0)