Merge remote-tracking branch 'kadams54/master'
diff --git a/.gitignore b/.gitignore
index 95e76b2..86e7df1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,4 @@
build
dist
.DS_Store
+.tox
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..d919621
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,8 @@
+language: python
+python:
+ - "2.6"
+ - "2.7"
+ - "3.3"
+install:
+ - "python setup.py install"
+script: python setup.py test
diff --git a/AUTHORS b/AUTHORS
index c6cd9ba..943f625 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -29,3 +29,5 @@
- Peter van Dijk (Habbie)
- Stefan Ebner
- Rene Leonhardt
+- Thomas Waldmann
+- Cory Benfield (Lukasa)
diff --git a/CHANGES b/CHANGES
index 9747a31..e4aa398 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,26 @@
Jinja2 Changelog
================
+Version 2.7
+-----------
+(codename to be selected, release date to be announced)
+
+- Choice and prefix loaders now dispatch source and template lookup
+ separately in order to work in combination with module loaders as
+ advertised.
+- Fixed filesizeformat.
+- Added a non-silent option for babel extraction.
+- Added `urlencode` filter that automatically quotes values for
+ URL safe usage with utf-8 as only supported encoding. If applications
+ want to change this encoding they can override the filter.
+- Accessing `last` on the loop context no longer causes the iterator
+ to be consumed into a list.
+- Python requirement changed: 2.6, 2.7 or >= 3.3 are required now,
+ supported by same source code, using the "six" compatibility library.
+- Allow `contextfunction` and other decorators to be applied to `__call__`.
+- Added support for changing from newline to different signs in the `wordwrap`
+ filter.
+
Version 2.6
-----------
(codename Convolution, released on July 24th 2011)
diff --git a/MANIFEST.in b/MANIFEST.in
index aeb66af..63760bb 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -1,4 +1,4 @@
-include MANIFEST.in Makefile CHANGES LICENSE AUTHORS jinja2/_debugsupport.c
+include MANIFEST.in Makefile CHANGES LICENSE AUTHORS
recursive-include docs *
recursive-include custom_fixers *
recursive-include ext *
diff --git a/custom_fixers/__init__.py b/custom_fixers/__init__.py
deleted file mode 100644
index e69de29..0000000
--- a/custom_fixers/__init__.py
+++ /dev/null
diff --git a/custom_fixers/fix_alt_unicode.py b/custom_fixers/fix_alt_unicode.py
deleted file mode 100644
index 96a81c1..0000000
--- a/custom_fixers/fix_alt_unicode.py
+++ /dev/null
@@ -1,13 +0,0 @@
-from lib2to3 import fixer_base
-from lib2to3.fixer_util import Name, BlankLine
-
-
-class FixAltUnicode(fixer_base.BaseFix):
- PATTERN = """
- func=funcdef< 'def' name='__unicode__'
- parameters< '(' NAME ')' > any+ >
- """
-
- def transform(self, node, results):
- name = results['name']
- name.replace(Name('__str__', prefix=name.prefix))
diff --git a/custom_fixers/fix_broken_reraising.py b/custom_fixers/fix_broken_reraising.py
deleted file mode 100644
index fd0ea68..0000000
--- a/custom_fixers/fix_broken_reraising.py
+++ /dev/null
@@ -1,21 +0,0 @@
-from lib2to3 import fixer_base, pytree
-from lib2to3.fixer_util import Name, BlankLine, Name, Attr, ArgList
-
-
-class FixBrokenReraising(fixer_base.BaseFix):
- PATTERN = """
- raise_stmt< 'raise' any ',' val=any ',' tb=any >
- """
-
- # run before the broken 2to3 checker with the same goal
- # tries to rewrite it with a rule that does not work out for jinja
- run_order = 1
-
- def transform(self, node, results):
- tb = results['tb'].clone()
- tb.prefix = ''
- with_tb = Attr(results['val'].clone(), Name('with_traceback')) + \
- [ArgList([tb])]
- new = pytree.Node(self.syms.simple_stmt, [Name("raise")] + with_tb)
- new.prefix = node.prefix
- return new
diff --git a/custom_fixers/fix_xrange2.py b/custom_fixers/fix_xrange2.py
deleted file mode 100644
index 5d35e50..0000000
--- a/custom_fixers/fix_xrange2.py
+++ /dev/null
@@ -1,11 +0,0 @@
-from lib2to3 import fixer_base
-from lib2to3.fixer_util import Name, BlankLine
-
-
-# whyever this is necessary..
-
-class FixXrange2(fixer_base.BaseFix):
- PATTERN = "'xrange'"
-
- def transform(self, node, results):
- node.replace(Name('range', prefix=node.prefix))
diff --git a/docs/api.rst b/docs/api.rst
index 58ffc9f..ae2295f 100644
--- a/docs/api.rst
+++ b/docs/api.rst
@@ -117,7 +117,7 @@
.. autoclass:: Environment([options])
:members: from_string, get_template, select_template,
get_or_select_template, join_path, extend, compile_expression,
- compile_templates, list_templates
+ compile_templates, list_templates, add_extension
.. attribute:: shared
diff --git a/docs/contents.rst.inc b/docs/contents.rst.inc
index 35d6d23..2f5c5e8 100644
--- a/docs/contents.rst.inc
+++ b/docs/contents.rst.inc
@@ -23,7 +23,7 @@
changelog
If you can't find the information you're looking for, have a look at the
-index of try to find it using the search function:
+index or try to find it using the search function:
* :ref:`genindex`
* :ref:`search`
diff --git a/docs/extensions.rst b/docs/extensions.rst
index c6b6ec9..3878d8c 100644
--- a/docs/extensions.rst
+++ b/docs/extensions.rst
@@ -34,7 +34,7 @@
After enabling dummy `_` function that forwards calls to `gettext` is added
to the environment globals. An internationalized application then has to
-provide at least an `gettext` and optoinally a `ngettext` function into the
+provide at least an `gettext` and optionally a `ngettext` function into the
namespace. Either globally or for each rendering.
Environment Methods
@@ -219,7 +219,7 @@
useful, another one would be fragment caching.
When writing extensions you have to keep in mind that you are working with the
-Jinja2 template compiler which does not validate the node tree you are possing
+Jinja2 template compiler which does not validate the node tree you are passing
to it. If the AST is malformed you will get all kinds of compiler or runtime
errors that are horrible to debug. Always make sure you are using the nodes
you create correctly. The API documentation below shows which nodes exist and
diff --git a/docs/faq.rst b/docs/faq.rst
index 2e4be8c..5c80d33 100644
--- a/docs/faq.rst
+++ b/docs/faq.rst
@@ -153,6 +153,18 @@
for Jython or the AppEngine as ctypes is unavailable there and it's not
possible to use the debugsupport extension.
+If you are working in the Google Appengine development server you can
+whitelist the ctypes module to restore the tracebacks. This however won't
+work in production environments::
+
+ import os
+ if os.environ.get('SERVER_SOFTWARE', '').startswith('Dev'):
+ from google.appengine.tools.dev_appserver import HardenedModulesHook
+ HardenedModulesHook._WHITE_LIST_C_MODULES += ['_ctypes', 'gestalt']
+
+Credit for this snippet goes to `Thomas Johansson
+<http://stackoverflow.com/questions/3086091/debug-jinja2-in-google-app-engine/3694434#3694434>`_
+
Why is there no Python 2.3 support?
-----------------------------------
@@ -162,7 +174,7 @@
Python 2.3 support you either have to use `Jinja 1`_ or other templating
engines that still support 2.3.
-My Macros are overriden by something
+My Macros are overridden by something
------------------------------------
In some situations the Jinja scoping appears arbitrary:
diff --git a/docs/index.rst b/docs/index.rst
index 7225e1c..c8964f6 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -28,7 +28,7 @@
.. include:: contents.rst.inc
If you can't find the information you're looking for, have a look at the
-index of try to find it using the search function:
+index or try to find it using the search function:
* :ref:`genindex`
* :ref:`search`
diff --git a/docs/integration.rst b/docs/integration.rst
index 1875711..79dff76 100644
--- a/docs/integration.rst
+++ b/docs/integration.rst
@@ -5,6 +5,9 @@
the `Babel`_ library or your favourite editor for fancy code highlighting.
This is a brief description of whats included.
+Files to help integration are available
+`here. <https://github.com/mitsuhiko/jinja2/tree/master/ext>`_
+
.. _babel-integration:
Babel Integration
@@ -38,6 +41,15 @@
of import paths as `extensions` value. The i18n extension is added
automatically.
+.. versionchanged:: 2.7
+
+ Until 2.7 template syntax errors were always ignored. This was done
+ since many people are dropping non template html files into the
+ templates folder and it would randomly fail. The assumption was that
+ testsuites will catch syntax errors in templates anyways. If you don't
+ want that behavior you can add ``silent=false`` to the settings and
+ exceptions are propagated.
+
.. _mapping file: http://babel.edgewall.org/wiki/Documentation/messages.html#extraction-method-mapping-and-configuration
Pylons
@@ -69,18 +81,18 @@
TextMate
--------
-Inside the `ext` folder of Jinja2 there is a bundle for TextMate that supports
-syntax highlighting for Jinja1 and Jinja2 for text based templates as well as
-HTML. It also contains a few often used snippets.
+Inside the `ext` folder at the root of the Jinja2 project there is a bundle for
+TextMate that supports syntax highlighting for Jinja1 and Jinja2 for text based
+templates as well as HTML. It also contains a few often used snippets.
Vim
---
A syntax plugin for `Vim`_ exists in the Vim-scripts directory as well as the
-ext folder of Jinja2. `The script <http://www.vim.org/scripts/script.php?script_id=1856>`_
-supports Jinja1 and Jinja2. Once installed two file types are available `jinja`
-and `htmljinja`. The first one for text based templates, the latter for HTML
-templates.
+`ext` folder at the root of the Jinja2 project. `The script
+<http://www.vim.org/scripts/script.php?script_id=1856>`_ supports Jinja1 and
+Jinja2. Once installed two file types are available `jinja` and `htmljinja`.
+The first one for text based templates, the latter for HTML templates.
Copy the files into your `syntax` folder.
diff --git a/docs/intro.rst b/docs/intro.rst
index b08947d..60528e3 100644
--- a/docs/intro.rst
+++ b/docs/intro.rst
@@ -2,8 +2,7 @@
============
This is the documentation for the Jinja2 general purpose templating language.
-Jinja2 is a library for Python 2.4 and onwards that is designed to be flexible,
-fast and secure.
+Jinja2 is a library for Python that is designed to be flexible, fast and secure.
If you have any exposure to other text-based template languages, such as Smarty or
Django, you should feel right at home with Jinja2. It's both designer and
@@ -13,15 +12,7 @@
Prerequisites
-------------
-Jinja2 needs at least **Python 2.4** to run. Additionally a working C-compiler
-that can create python extensions should be installed for the debugger if you
-are using Python 2.4.
-
-If you don't have a working C-compiler and you are trying to install the source
-release with the debugsupport you will get a compiler error.
-
-.. _ctypes: http://python.net/crew/theller/ctypes/
-
+Jinja2 works with Python 2.6.x, 2.7.x and >= 3.3.
Installation
------------
@@ -93,19 +84,6 @@
.. _MarkupSafe: http://pypi.python.org/pypi/MarkupSafe
-Enable the debug support Module
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-By default Jinja2 will not compile the debug support module. Enabling this
-will fail if you don't have the Python headers or a working compiler. This
-is often the case if you are installing Jinja2 from a windows machine.
-
-Because the debug support is only necessary for Python 2.4 you will not
-have to do this unless you run 2.4::
-
- sudo python setup.py --with-debugsupport install
-
-
Basic API Usage
---------------
@@ -136,7 +114,7 @@
Experimental Python 3 Support
-----------------------------
-Jinja 2.3 brings experimental support for Python 3. It means that all
+Jinja 2.7 brings experimental support for Python >=3.3. It means that all
unittests pass on the new version, but there might still be small bugs in
there and behavior might be inconsistent. If you notice any bugs, please
provide feedback in the `Jinja bug tracker`_.
diff --git a/docs/jinjaext.py b/docs/jinjaext.py
index 12b5447..8395a55 100644
--- a/docs/jinjaext.py
+++ b/docs/jinjaext.py
@@ -8,6 +8,7 @@
:copyright: Copyright 2008 by Armin Ronacher.
:license: BSD.
"""
+import collections
import os
import re
import inspect
@@ -22,6 +23,7 @@
from pygments.token import Keyword, Name, Comment, String, Error, \
Number, Operator, Generic
from jinja2 import Environment, FileSystemLoader
+from jinja2.utils import next
def parse_rst(state, content_offset, doc):
@@ -110,10 +112,10 @@
def directive(dirname, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
reverse_mapping = {}
- for name, func in mapping.iteritems():
+ for name, func in mapping.items():
reverse_mapping.setdefault(func, []).append(name)
filters = []
- for func, names in reverse_mapping.iteritems():
+ for func, names in reverse_mapping.items():
aliases = sorted(names, key=lambda x: len(x))
name = aliases.pop()
filters.append((name, aliases, func))
@@ -145,9 +147,9 @@
doc.append(p + '.. autoclass:: %s(%s)' % (node.__name__, sig), '')
if node.abstract:
members = []
- for key, name in node.__dict__.iteritems():
+ for key, name in node.__dict__.items():
if not key.startswith('_') and \
- not hasattr(node.__base__, key) and callable(name):
+ not hasattr(node.__base__, key) and isinstance(name, collections.Callable):
members.append(key)
if members:
members.sort()
@@ -169,10 +171,10 @@
titleiter = iter(doctree.traverse(nodes.title))
try:
# skip first title, we are not interested in that one
- titleiter.next()
- title = titleiter.next()
+ next(titleiter)
+ title = next(titleiter)
# and check if there is at least another title
- titleiter.next()
+ next(titleiter)
except StopIteration:
return
tocnode = nodes.section('')
diff --git a/docs/sandbox.rst b/docs/sandbox.rst
index 1302f18..33de4d2 100644
--- a/docs/sandbox.rst
+++ b/docs/sandbox.rst
@@ -42,7 +42,7 @@
are running multiple users on the same server) they can't harm each other
via JavaScript insertions and much more.
- Also the sandbox is only as good as the configuration. We stronly
+ Also the sandbox is only as good as the configuration. We strongly
recommend only passing non-shared resources to the template and use
some sort of whitelisting for attributes.
diff --git a/docs/switching.rst b/docs/switching.rst
index ba3cfb1..c193843 100644
--- a/docs/switching.rst
+++ b/docs/switching.rst
@@ -3,7 +3,7 @@
.. highlight:: html+jinja
-If you have used a different template engine in the past and want to swtich
+If you have used a different template engine in the past and want to switch
to Jinja2 here is a small guide that shows the basic syntatic and semantic
changes between some common, similar text template engines for Python.
diff --git a/docs/templates.rst b/docs/templates.rst
index 48429a5..e121033 100644
--- a/docs/templates.rst
+++ b/docs/templates.rst
@@ -152,10 +152,11 @@
Whitespace Control
------------------
-In the default configuration whitespace is not further modified by the
-template engine, so each whitespace (spaces, tabs, newlines etc.) is returned
-unchanged. If the application configures Jinja to `trim_blocks` the first
-newline after a a template tag is removed automatically (like in PHP).
+In the default configuration, a single trailing newline is stripped if
+present, and whitespace is not further modified by the template engine. Each
+whitespace (spaces, tabs, newlines etc.) is returned unchanged. If the
+application configures Jinja to `trim_blocks` the first newline after a a
+template tag is removed automatically (like in PHP).
But you can also strip whitespace in templates by hand. If you put an minus
sign (``-``) to the start or end of an block (for example a for tag), a
@@ -332,7 +333,7 @@
The filename of the template depends on the template loader. For example the
:class:`FileSystemLoader` allows you to access other templates by giving the
-filename. You can access templates in subdirectories with an slash::
+filename. You can access templates in subdirectories with a slash::
{% extends "layout/default.html" %}
@@ -497,7 +498,21 @@
{% endfor %}
</ul>
-Inside of a for loop block you can access some special variables:
+As variables in templates retain their object properties, it is possible to
+iterate over containers like `dict`::
+
+ <dl>
+ {% for key, value in my_dict.iteritems() %}
+ <dt>{{ key|e }}</dt>
+ <dd>{{ value|e }}</dd>
+ {% endfor %}
+ </dl>
+
+Note however that dictionaries usually are unordered so you might want to
+either pass it as a sorted list to the template or use the `dictsort`
+filter.
+
+Inside of a for-loop block you can access some special variables:
+-----------------------+---------------------------------------------------+
| Variable | Description |
@@ -529,7 +544,7 @@
<li class="{{ loop.cycle('odd', 'even') }}">{{ row }}</li>
{% endfor %}
-With Jinja 2.1 an extra `cycle` helper exists that allows loop-unbound
+Since Jinja 2.1 an extra `cycle` helper exists that allows loop-unbound
cycling. For more information have a look at the :ref:`builtin-globals`.
.. _loop-filtering:
@@ -805,7 +820,7 @@
different templates and get imported from there. This works similar to the
import statements in Python. It's important to know that imports are cached
and imported templates don't have access to the current template variables,
-just the globals by defualt. For more details about context behavior of
+just the globals by default. For more details about context behavior of
imports and includes see :ref:`import-visibility`.
There are two ways to import templates. You can import the complete template
@@ -847,7 +862,7 @@
</dl>
<p>{{ textarea('comment') }}</p>
-Macros and variables starting with one ore more underscores are private and
+Macros and variables starting with one or more underscores are private and
cannot be imported.
.. versionchanged:: 2.4
diff --git a/docs/tricks.rst b/docs/tricks.rst
index 566575e..5427a3e 100644
--- a/docs/tricks.rst
+++ b/docs/tricks.rst
@@ -75,7 +75,7 @@
<ul id="navigation">
{% for href, id, caption in navigation_bar %}
<li{% if id == active_page %} class="active"{% endif
- %}><a href="{{ href|e }}">{{ caption|e }}</a>/li>
+ %}><a href="{{ href|e }}">{{ caption|e }}</a></li>
{% endfor %}
</ul>
...
diff --git a/ext/JinjaTemplates.tmbundle.tar.gz b/ext/JinjaTemplates.tmbundle.tar.gz
deleted file mode 100644
index 65b7422..0000000
--- a/ext/JinjaTemplates.tmbundle.tar.gz
+++ /dev/null
Binary files differ
diff --git a/ext/Vim/jinja.vim b/ext/Vim/jinja.vim
index 894dcc4..503aec6 100644
--- a/ext/Vim/jinja.vim
+++ b/ext/Vim/jinja.vim
@@ -49,14 +49,14 @@
syn keyword jinjaStatement containedin=jinjaTagBlock contained block skipwhite nextgroup=jinjaBlockName
" Variable Names
-syn match jinjaVariable containedin=jinjaVarBlock,jinjaTagBlock,jinjaNested contained skipwhite /[a-zA-Z_][a-zA-Z0-9_]*/
+syn match jinjaVariable containedin=jinjaVarBlock,jinjaTagBlock,jinjaNested contained /[a-zA-Z_][a-zA-Z0-9_]*/
syn keyword jinjaSpecial containedin=jinjaVarBlock,jinjaTagBlock,jinjaNested contained false true none False True None loop super caller varargs kwargs
" Filters
-syn match jinjaOperator "|" containedin=jinjaVarBlock,jinjaTagBlock,jinjaNested contained nextgroup=jinjaFilter
-syn match jinjaFilter contained skipwhite /[a-zA-Z_][a-zA-Z0-9_]*/
-syn match jinjaFunction contained skipwhite /[a-zA-Z_][a-zA-Z0-9_]*/
-syn match jinjaBlockName contained skipwhite /[a-zA-Z_][a-zA-Z0-9_]*/
+syn match jinjaOperator "|" containedin=jinjaVarBlock,jinjaTagBlock,jinjaNested contained skipwhite nextgroup=jinjaFilter
+syn match jinjaFilter contained /[a-zA-Z_][a-zA-Z0-9_]*/
+syn match jinjaFunction contained /[a-zA-Z_][a-zA-Z0-9_]*/
+syn match jinjaBlockName contained /[a-zA-Z_][a-zA-Z0-9_]*/
" Jinja template constants
syn region jinjaString containedin=jinjaVarBlock,jinjaTagBlock,jinjaNested contained start=/"/ skip=/\\"/ end=/"/
@@ -73,7 +73,7 @@
syn region jinjaNested matchgroup=jinjaOperator start="(" end=")" transparent display containedin=jinjaVarBlock,jinjaTagBlock,jinjaNested contained
syn region jinjaNested matchgroup=jinjaOperator start="\[" end="\]" transparent display containedin=jinjaVarBlock,jinjaTagBlock,jinjaNested contained
syn region jinjaNested matchgroup=jinjaOperator start="{" end="}" transparent display containedin=jinjaVarBlock,jinjaTagBlock,jinjaNested contained
-syn region jinjaTagBlock matchgroup=jinjaTagDelim start=/{%-\?/ end=/-\?%}/ skipwhite containedin=ALLBUT,jinjaTagBlock,jinjaVarBlock,jinjaRaw,jinjaString,jinjaNested,jinjaComment
+syn region jinjaTagBlock matchgroup=jinjaTagDelim start=/{%-\?/ end=/-\?%}/ containedin=ALLBUT,jinjaTagBlock,jinjaVarBlock,jinjaRaw,jinjaString,jinjaNested,jinjaComment
syn region jinjaVarBlock matchgroup=jinjaVarDelim start=/{{-\?/ end=/-\?}}/ containedin=ALLBUT,jinjaTagBlock,jinjaVarBlock,jinjaRaw,jinjaString,jinjaNested,jinjaComment
@@ -86,10 +86,10 @@
" Block start keywords. A bit tricker. We only highlight at the start of a
" tag block and only if the name is not followed by a comma or equals sign
" which usually means that we have to deal with an assignment.
-syn match jinjaStatement containedin=jinjaTagBlock contained skipwhite /\({%-\?\s*\)\@<=\<[a-zA-Z_][a-zA-Z0-9_]*\>\(\s*[,=]\)\@!/
+syn match jinjaStatement containedin=jinjaTagBlock contained /\({%-\?\s*\)\@<=\<[a-zA-Z_][a-zA-Z0-9_]*\>\(\s*[,=]\)\@!/
" and context modifiers
-syn match jinjaStatement containedin=jinjaTagBlock contained /\<with\(out\)\?\s\+context\>/ skipwhite
+syn match jinjaStatement containedin=jinjaTagBlock contained /\<with\(out\)\?\s\+context\>/
" Define the default highlighting.
diff --git a/ext/django2jinja/django2jinja.py b/ext/django2jinja/django2jinja.py
index 6d9e76c..ad9627f 100644
--- a/ext/django2jinja/django2jinja.py
+++ b/ext/django2jinja/django2jinja.py
@@ -609,7 +609,7 @@
@node(core_tags.WithNode)
def with_block(writer, node):
writer.warn('with block expanded into set statement. This could cause '
- 'variables following that block to be overriden.', node)
+ 'variables following that block to be overridden.', node)
writer.start_block()
writer.write('set %s = ' % node.name)
writer.node(node.var)
diff --git a/jinja2/_compat.py b/jinja2/_compat.py
new file mode 100644
index 0000000..6318f0b
--- /dev/null
+++ b/jinja2/_compat.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+"""
+ jinja2._compat
+ ~~~~~~~~~~~~~~
+
+ Some py2/py3 compatibility support that is not yet available in
+ "six" 1.3.0.
+ There are bugs open for "six" for all this stuff, so we can remove it
+ again from here as soon as we require a new enough "six" release.
+
+ :copyright: Copyright 2013 by the Jinja team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+# https://bitbucket.org/gutworth/six/issue/25/add-unichr
+try:
+ unichr = unichr # py2
+except NameError:
+ unichr = chr # py3
+
+try:
+ range_type = xrange
+except NameError:
+ range_type = range
diff --git a/jinja2/_debugsupport.c b/jinja2/_debugsupport.c
deleted file mode 100644
index e756d8e..0000000
--- a/jinja2/_debugsupport.c
+++ /dev/null
@@ -1,78 +0,0 @@
-/**
- * jinja2._debugsupport
- * ~~~~~~~~~~~~~~~~~~~~
- *
- * C implementation of `tb_set_next`.
- *
- * :copyright: (c) 2010 by the Jinja Team.
- * :license: BSD.
- */
-
-#include <Python.h>
-
-
-static PyObject*
-tb_set_next(PyObject *self, PyObject *args)
-{
- PyTracebackObject *tb, *old;
- PyObject *next;
-
- if (!PyArg_ParseTuple(args, "O!O:tb_set_next", &PyTraceBack_Type, &tb, &next))
- return NULL;
- if (next == Py_None)
- next = NULL;
- else if (!PyTraceBack_Check(next)) {
- PyErr_SetString(PyExc_TypeError,
- "tb_set_next arg 2 must be traceback or None");
- return NULL;
- }
- else
- Py_INCREF(next);
-
- old = tb->tb_next;
- tb->tb_next = (PyTracebackObject*)next;
- Py_XDECREF(old);
-
- Py_INCREF(Py_None);
- return Py_None;
-}
-
-static PyMethodDef module_methods[] = {
- {"tb_set_next", (PyCFunction)tb_set_next, METH_VARARGS,
- "Set the tb_next member of a traceback object."},
- {NULL, NULL, 0, NULL} /* Sentinel */
-};
-
-
-#if PY_MAJOR_VERSION < 3
-
-#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
-#define PyMODINIT_FUNC void
-#endif
-PyMODINIT_FUNC
-init_debugsupport(void)
-{
- Py_InitModule3("jinja2._debugsupport", module_methods, "");
-}
-
-#else /* Python 3.x module initialization */
-
-static struct PyModuleDef module_definition = {
- PyModuleDef_HEAD_INIT,
- "jinja2._debugsupport",
- NULL,
- -1,
- module_methods,
- NULL,
- NULL,
- NULL,
- NULL
-};
-
-PyMODINIT_FUNC
-PyInit__debugsupport(void)
-{
- return PyModule_Create(&module_definition);
-}
-
-#endif
diff --git a/jinja2/_markupsafe/__init__.py b/jinja2/_markupsafe/__init__.py
index ec7bd57..5ffb57d 100644
--- a/jinja2/_markupsafe/__init__.py
+++ b/jinja2/_markupsafe/__init__.py
@@ -9,8 +9,9 @@
:license: BSD, see LICENSE for more details.
"""
import re
-from itertools import imap
-
+import six
+from six.moves import map
+from jinja2._compat import unichr
__all__ = ['Markup', 'soft_unicode', 'escape', 'escape_silent']
@@ -19,7 +20,7 @@
_entity_re = re.compile(r'&([^;]+);')
-class Markup(unicode):
+class Markup(six.text_type):
r"""Marks a string as being safe for inclusion in HTML/XML output without
needing to be escaped. This implements the `__html__` interface a couple
of frameworks and web applications use. :class:`Markup` is a direct
@@ -68,56 +69,56 @@
if hasattr(base, '__html__'):
base = base.__html__()
if encoding is None:
- return unicode.__new__(cls, base)
- return unicode.__new__(cls, base, encoding, errors)
+ return six.text_type.__new__(cls, base)
+ return six.text_type.__new__(cls, base, encoding, errors)
def __html__(self):
return self
def __add__(self, other):
- if hasattr(other, '__html__') or isinstance(other, basestring):
- return self.__class__(unicode(self) + unicode(escape(other)))
+ if hasattr(other, '__html__') or isinstance(other, six.string_types):
+ return self.__class__(six.text_type(self) + six.text_type(escape(other)))
return NotImplemented
def __radd__(self, other):
- if hasattr(other, '__html__') or isinstance(other, basestring):
- return self.__class__(unicode(escape(other)) + unicode(self))
+ if hasattr(other, '__html__') or isinstance(other, six.string_types):
+ return self.__class__(six.text_type(escape(other)) + six.text_type(self))
return NotImplemented
def __mul__(self, num):
if isinstance(num, (int, long)):
- return self.__class__(unicode.__mul__(self, num))
+ return self.__class__(six.text_type.__mul__(self, num))
return NotImplemented
__rmul__ = __mul__
def __mod__(self, arg):
if isinstance(arg, tuple):
- arg = tuple(imap(_MarkupEscapeHelper, arg))
+ arg = tuple(map(_MarkupEscapeHelper, arg))
else:
arg = _MarkupEscapeHelper(arg)
- return self.__class__(unicode.__mod__(self, arg))
+ return self.__class__(six.text_type.__mod__(self, arg))
def __repr__(self):
return '%s(%s)' % (
self.__class__.__name__,
- unicode.__repr__(self)
+ six.text_type.__repr__(self)
)
def join(self, seq):
- return self.__class__(unicode.join(self, imap(escape, seq)))
- join.__doc__ = unicode.join.__doc__
+ return self.__class__(six.text_type.join(self, map(escape, seq)))
+ join.__doc__ = six.text_type.join.__doc__
def split(self, *args, **kwargs):
- return map(self.__class__, unicode.split(self, *args, **kwargs))
- split.__doc__ = unicode.split.__doc__
+ return map(self.__class__, six.text_type.split(self, *args, **kwargs))
+ split.__doc__ = six.text_type.split.__doc__
def rsplit(self, *args, **kwargs):
- return map(self.__class__, unicode.rsplit(self, *args, **kwargs))
- rsplit.__doc__ = unicode.rsplit.__doc__
+ return map(self.__class__, six.text_type.rsplit(self, *args, **kwargs))
+ rsplit.__doc__ = six.text_type.rsplit.__doc__
def splitlines(self, *args, **kwargs):
- return map(self.__class__, unicode.splitlines(self, *args, **kwargs))
- splitlines.__doc__ = unicode.splitlines.__doc__
+ return map(self.__class__, six.text_type.splitlines(self, *args, **kwargs))
+ splitlines.__doc__ = six.text_type.splitlines.__doc__
def unescape(self):
r"""Unescape markup again into an unicode string. This also resolves
@@ -139,7 +140,7 @@
except ValueError:
pass
return u''
- return _entity_re.sub(handle_match, unicode(self))
+ return _entity_re.sub(handle_match, six.text_type(self))
def striptags(self):
r"""Unescape markup into an unicode string and strip all tags. This
@@ -164,10 +165,10 @@
return rv
def make_wrapper(name):
- orig = getattr(unicode, name)
+ orig = getattr(six.text_type, name)
def func(self, *args, **kwargs):
args = _escape_argspec(list(args), enumerate(args))
- _escape_argspec(kwargs, kwargs.iteritems())
+ _escape_argspec(kwargs, six.iteritems(kwargs))
return self.__class__(orig(self, *args, **kwargs))
func.__name__ = orig.__name__
func.__doc__ = orig.__doc__
@@ -175,21 +176,13 @@
for method in '__getitem__', 'capitalize', \
'title', 'lower', 'upper', 'replace', 'ljust', \
+ 'format', 'partition', 'rpartition', \
'rjust', 'lstrip', 'rstrip', 'center', 'strip', \
'translate', 'expandtabs', 'swapcase', 'zfill':
locals()[method] = make_wrapper(method)
- # new in python 2.5
- if hasattr(unicode, 'partition'):
- partition = make_wrapper('partition'),
- rpartition = make_wrapper('rpartition')
-
- # new in python 2.6
- if hasattr(unicode, 'format'):
- format = make_wrapper('format')
-
# not in python 3
- if hasattr(unicode, '__getslice__'):
+ if hasattr(six.text_type, '__getslice__'):
__getslice__ = make_wrapper('__getslice__')
del method, make_wrapper
@@ -198,7 +191,7 @@
def _escape_argspec(obj, iterable):
"""Helper for various string-wrapped functions."""
for key, value in iterable:
- if hasattr(value, '__html__') or isinstance(value, basestring):
+ if hasattr(value, '__html__') or isinstance(value, six.string_types):
obj[key] = escape(value)
return obj
@@ -211,7 +204,7 @@
__getitem__ = lambda s, x: _MarkupEscapeHelper(s.obj[x])
__str__ = lambda s: str(escape(s.obj))
- __unicode__ = lambda s: unicode(escape(s.obj))
+ __unicode__ = lambda s: six.text_type(escape(s.obj))
__repr__ = lambda s: str(escape(repr(s.obj)))
__int__ = lambda s: int(s.obj)
__float__ = lambda s: float(s.obj)
diff --git a/jinja2/_markupsafe/_bundle.py b/jinja2/_markupsafe/_bundle.py
index e694faf..d23730f 100644
--- a/jinja2/_markupsafe/_bundle.py
+++ b/jinja2/_markupsafe/_bundle.py
@@ -10,6 +10,7 @@
:copyright: Copyright 2010 by the Jinja team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
+from __future__ import print_function
import sys
import os
import re
@@ -25,7 +26,7 @@
def main():
if len(sys.argv) != 2:
- print 'error: only argument is path to markupsafe'
+ print('error: only argument is path to markupsafe')
sys.exit(1)
basedir = os.path.dirname(__file__)
markupdir = sys.argv[1]
diff --git a/jinja2/_markupsafe/_native.py b/jinja2/_markupsafe/_native.py
index 7b95828..389be74 100644
--- a/jinja2/_markupsafe/_native.py
+++ b/jinja2/_markupsafe/_native.py
@@ -9,6 +9,7 @@
:license: BSD, see LICENSE for more details.
"""
from jinja2._markupsafe import Markup
+import six
def escape(s):
@@ -18,7 +19,7 @@
"""
if hasattr(s, '__html__'):
return s.__html__()
- return Markup(unicode(s)
+ return Markup(six.text_type(s)
.replace('&', '&')
.replace('>', '>')
.replace('<', '<')
@@ -40,6 +41,6 @@
"""Make a string unicode if it isn't already. That way a markup
string is not converted back to unicode.
"""
- if not isinstance(s, unicode):
- s = unicode(s)
+ if not isinstance(s, six.text_type):
+ s = six.text_type(s)
return s
diff --git a/jinja2/_markupsafe/tests.py b/jinja2/_markupsafe/tests.py
index c1ce394..449df49 100644
--- a/jinja2/_markupsafe/tests.py
+++ b/jinja2/_markupsafe/tests.py
@@ -1,6 +1,7 @@
import gc
import unittest
from jinja2._markupsafe import Markup, escape, escape_silent
+import six
class MarkupTestCase(unittest.TestCase):
@@ -9,7 +10,7 @@
# adding two strings should escape the unsafe one
unsafe = '<script type="application/x-some-script">alert("foo");</script>'
safe = Markup('<em>username</em>')
- assert unsafe + safe == unicode(escape(unsafe)) + unicode(safe)
+ assert unsafe + safe == six.text_type(escape(unsafe)) + six.text_type(safe)
# string interpolations are safe to use too
assert Markup('<em>%s</em>') % '<bad user>' == \
@@ -55,8 +56,8 @@
def test_markup_leaks(self):
counts = set()
- for count in xrange(20):
- for item in xrange(1000):
+ for count in range(20):
+ for item in range(1000):
escape("foo")
escape("<foo>")
escape(u"foo")
diff --git a/jinja2/_stringdefs.py b/jinja2/_stringdefs.py
index 1161b7f..da5830e 100644
--- a/jinja2/_stringdefs.py
+++ b/jinja2/_stringdefs.py
@@ -13,6 +13,8 @@
:license: BSD, see LICENSE for details.
"""
+from jinja2._compat import unichr
+
Cc = u'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f'
Cf = u'\xad\u0600\u0601\u0602\u0603\u06dd\u070f\u17b4\u17b5\u200b\u200c\u200d\u200e\u200f\u202a\u202b\u202c\u202d\u202e\u2060\u2061\u2062\u2063\u206a\u206b\u206c\u206d\u206e\u206f\ufeff\ufff9\ufffa\ufffb'
diff --git a/jinja2/bccache.py b/jinja2/bccache.py
index 62d9a6b..033ae7a 100644
--- a/jinja2/bccache.py
+++ b/jinja2/bccache.py
@@ -18,7 +18,8 @@
import sys
import marshal
import tempfile
-import cPickle as pickle
+from six.moves import cPickle as pickle
+from six import BytesIO
import fnmatch
try:
from hashlib import sha1
@@ -28,12 +29,10 @@
# marshal works better on 3.x, one hack less required
-if sys.version_info > (3, 0):
- from io import BytesIO
+if sys.version_info[0] >= 3:
marshal_dump = marshal.dump
marshal_load = marshal.load
else:
- from cStringIO import StringIO as BytesIO
def marshal_dump(code, f):
if isinstance(f, file):
diff --git a/jinja2/compiler.py b/jinja2/compiler.py
index b21cb38..59d770f 100644
--- a/jinja2/compiler.py
+++ b/jinja2/compiler.py
@@ -8,14 +8,16 @@
:copyright: (c) 2010 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
-from cStringIO import StringIO
from itertools import chain
from copy import deepcopy
from jinja2 import nodes
from jinja2.nodes import EvalContext
from jinja2.visitor import NodeVisitor
from jinja2.exceptions import TemplateAssertionError
-from jinja2.utils import Markup, concat, escape, is_python_keyword, next
+from jinja2.utils import Markup, concat, escape, is_python_keyword
+from jinja2._compat import range_type
+import six
+from six.moves import cStringIO as StringIO, map
operators = {
@@ -29,14 +31,6 @@
'notin': 'not in'
}
-try:
- exec '(0 if 0 else 0)'
-except SyntaxError:
- have_condexpr = False
-else:
- have_condexpr = True
-
-
# what method to iterate over items do we want to use for dict iteration
# in generated code? on 2.x let's go with iteritems, on 3.x with items
if hasattr(dict, 'iteritems'):
@@ -51,7 +45,11 @@
def f():
if 0: dummy(x)
return f
-unoptimize_before_dead_code = bool(unoptimize_before_dead_code().func_closure)
+
+# The getattr is necessary for pypy which does not set this attribute if
+# no closure is on the function
+unoptimize_before_dead_code = bool(
+ getattr(unoptimize_before_dead_code(), '__closure__', None))
def generate(node, environment, name, filename, stream=None,
@@ -69,8 +67,8 @@
"""Does the node have a safe representation?"""
if value is None or value is NotImplemented or value is Ellipsis:
return True
- if isinstance(value, (bool, int, long, float, complex, basestring,
- xrange, Markup)):
+ if isinstance(value, (bool, int, float, complex, range_type,
+ Markup) + six.string_types):
return True
if isinstance(value, (tuple, list, set, frozenset)):
for item in value:
@@ -78,7 +76,7 @@
return False
return True
elif isinstance(value, dict):
- for key, value in value.iteritems():
+ for key, value in six.iteritems(value):
if not has_safe_repr(key):
return False
if not has_safe_repr(value):
@@ -542,7 +540,7 @@
self.write(', ')
self.visit(kwarg, frame)
if extra_kwargs is not None:
- for key, value in extra_kwargs.iteritems():
+ for key, value in six.iteritems(extra_kwargs):
self.write(', %s=%s' % (key, value))
if node.dyn_args:
self.write(', *')
@@ -558,7 +556,7 @@
self.visit(kwarg.value, frame)
self.write(', ')
if extra_kwargs is not None:
- for key, value in extra_kwargs.iteritems():
+ for key, value in six.iteritems(extra_kwargs):
self.write('%r: %s, ' % (key, value))
if node.dyn_kwargs is not None:
self.write('}, **')
@@ -625,7 +623,7 @@
def pop_scope(self, aliases, frame):
"""Restore all aliases and delete unused variables."""
- for name, alias in aliases.iteritems():
+ for name, alias in six.iteritems(aliases):
self.writeline('l_%s = %s' % (name, alias))
to_delete = set()
for name in frame.identifiers.declared_locally:
@@ -663,16 +661,16 @@
# it without aliasing all the variables.
# this could be fixed in Python 3 where we have the nonlocal
# keyword or if we switch to bytecode generation
- overriden_closure_vars = (
+ overridden_closure_vars = (
func_frame.identifiers.undeclared &
func_frame.identifiers.declared &
(func_frame.identifiers.declared_locally |
func_frame.identifiers.declared_parameter)
)
- if overriden_closure_vars:
+ if overridden_closure_vars:
self.fail('It\'s not possible to set and access variables '
'derived from an outer scope! (affects: %s)' %
- ', '.join(sorted(overriden_closure_vars)), node.lineno)
+ ', '.join(sorted(overridden_closure_vars)), node.lineno)
# remove variables from a closure from the frame's undeclared
# identifiers.
@@ -827,7 +825,7 @@
self.outdent(2 + (not self.has_known_extends))
# at this point we now have the blocks collected and can visit them too.
- for name, block in self.blocks.iteritems():
+ for name, block in six.iteritems(self.blocks):
block_frame = Frame(eval_ctx)
block_frame.inspect(block.body)
block_frame.block = name
@@ -930,7 +928,7 @@
func_name = 'get_or_select_template'
if isinstance(node.template, nodes.Const):
- if isinstance(node.template.value, basestring):
+ if isinstance(node.template.value, six.string_types):
func_name = 'get_template'
elif isinstance(node.template.value, (tuple, list)):
func_name = 'select_template'
@@ -1216,9 +1214,9 @@
return
if self.environment.finalize:
- finalize = lambda x: unicode(self.environment.finalize(x))
+ finalize = lambda x: six.text_type(self.environment.finalize(x))
else:
- finalize = unicode
+ finalize = six.text_type
# if we are inside a frame that requires output checking, we do so
outdent_later = False
@@ -1352,7 +1350,7 @@
public_names = [x for x in assignment_frame.toplevel_assignments
if not x.startswith('_')]
if len(assignment_frame.toplevel_assignments) == 1:
- name = next(iter(assignment_frame.toplevel_assignments))
+ name = six.advance_iterator(iter(assignment_frame.toplevel_assignments))
self.writeline('context.vars[%r] = l_%s' % (name, name))
else:
self.writeline('context.vars.update({')
@@ -1555,22 +1553,13 @@
'expression on %s evaluated to false and '
'no else section was defined.' % self.position(node)))
- if not have_condexpr:
- self.write('((')
- self.visit(node.test, frame)
- self.write(') and (')
- self.visit(node.expr1, frame)
- self.write(',) or (')
- write_expr2()
- self.write(',))[0]')
- else:
- self.write('(')
- self.visit(node.expr1, frame)
- self.write(' if ')
- self.visit(node.test, frame)
- self.write(' else ')
- write_expr2()
- self.write(')')
+ self.write('(')
+ self.visit(node.expr1, frame)
+ self.write(' if ')
+ self.visit(node.test, frame)
+ self.write(' else ')
+ write_expr2()
+ self.write(')')
def visit_Call(self, node, frame, forward_caller=False):
if self.environment.sandboxed:
diff --git a/jinja2/debug.py b/jinja2/debug.py
index 2af2222..31c25cc 100644
--- a/jinja2/debug.py
+++ b/jinja2/debug.py
@@ -15,6 +15,7 @@
from types import TracebackType
from jinja2.utils import CodeType, missing, internal_code
from jinja2.exceptions import TemplateSyntaxError
+import six
# on pypy we can take advantage of transparent proxies
try:
@@ -25,7 +26,7 @@
# how does the raise helper look like?
try:
- exec "raise TypeError, 'foo'"
+ exec("raise TypeError, 'foo'")
except SyntaxError:
raise_helper = 'raise __jinja_exception__[1]'
except TypeError:
@@ -77,7 +78,7 @@
class ProcessedTraceback(object):
- """Holds a Jinja preprocessed traceback for priting or reraising."""
+ """Holds a Jinja preprocessed traceback for printing or reraising."""
def __init__(self, exc_type, exc_value, frames):
assert frames, 'no frames for this traceback?'
@@ -158,7 +159,7 @@
frames = []
# skip some internal frames if wanted
- for x in xrange(initial_skip):
+ for x in range(initial_skip):
if tb is not None:
tb = tb.tb_next
initial_tb = tb
@@ -189,7 +190,7 @@
# reraise it unchanged.
# XXX: can we backup here? when could this happen?
if not frames:
- raise exc_info[0], exc_info[1], exc_info[2]
+ six.reraise(exc_info[0], exc_info[1], exc_info[2])
return ProcessedTraceback(exc_info[0], exc_info[1], frames)
@@ -206,7 +207,7 @@
locals = ctx.get_all()
else:
locals = {}
- for name, value in real_locals.iteritems():
+ for name, value in six.iteritems(real_locals):
if name.startswith('l_') and value is not missing:
locals[name[2:]] = value
@@ -254,7 +255,7 @@
# execute the code and catch the new traceback
try:
- exec code in globals, locals
+ exec(code, globals, locals)
except:
exc_info = sys.exc_info()
new_tb = exc_info[2].tb_next
@@ -330,10 +331,7 @@
tb_set_next = None
if tproxy is None:
try:
- from jinja2._debugsupport import tb_set_next
- except ImportError:
- try:
- tb_set_next = _init_ugly_crap()
- except:
- pass
+ tb_set_next = _init_ugly_crap()
+ except:
+ pass
del _init_ugly_crap
diff --git a/jinja2/defaults.py b/jinja2/defaults.py
index d2d4544..3201271 100644
--- a/jinja2/defaults.py
+++ b/jinja2/defaults.py
@@ -8,6 +8,7 @@
:copyright: (c) 2010 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
+from jinja2._compat import range_type
from jinja2.utils import generate_lorem_ipsum, Cycler, Joiner
@@ -28,7 +29,7 @@
from jinja2.filters import FILTERS as DEFAULT_FILTERS
from jinja2.tests import TESTS as DEFAULT_TESTS
DEFAULT_NAMESPACE = {
- 'range': xrange,
+ 'range': range_type,
'dict': lambda **kw: kw,
'lipsum': generate_lorem_ipsum,
'cycler': Cycler,
diff --git a/jinja2/environment.py b/jinja2/environment.py
index 7a9a59f..58573a2 100644
--- a/jinja2/environment.py
+++ b/jinja2/environment.py
@@ -11,7 +11,11 @@
import os
import sys
from jinja2 import nodes
-from jinja2.defaults import *
+from jinja2.defaults import BLOCK_START_STRING, \
+ BLOCK_END_STRING, VARIABLE_START_STRING, VARIABLE_END_STRING, \
+ COMMENT_START_STRING, COMMENT_END_STRING, LINE_STATEMENT_PREFIX, \
+ LINE_COMMENT_PREFIX, TRIM_BLOCKS, NEWLINE_SEQUENCE, \
+ DEFAULT_FILTERS, DEFAULT_TESTS, DEFAULT_NAMESPACE
from jinja2.lexer import get_lexer, TokenStream
from jinja2.parser import Parser
from jinja2.optimizer import optimize
@@ -21,6 +25,9 @@
TemplatesNotFound
from jinja2.utils import import_string, LRUCache, Markup, missing, \
concat, consume, internalcode, _encode_filename
+import six
+from functools import reduce
+from six.moves import filter, map
# for direct template usage we have up to ten living environments
@@ -67,11 +74,11 @@
def load_extensions(environment, extensions):
"""Load the extensions from the list and bind it to the environment.
- Returns a dict of instanciated environments.
+ Returns a dict of instantiated environments.
"""
result = {}
for extension in extensions:
- if isinstance(extension, basestring):
+ if isinstance(extension, six.string_types):
extension = import_string(extension)
result[extension.identifier] = extension(environment)
return result
@@ -239,7 +246,7 @@
# passed by keyword rather than position. However it's important to
# not change the order of arguments because it's used at least
# internally in those cases:
- # - spontaneus environments (i18n extension and Template)
+ # - spontaneous environments (i18n extension and Template)
# - unittests
# If parameter changes are required only add parameters at the end
# and don't change the arguments (or the defaults!) of the arguments
@@ -292,7 +299,7 @@
yet. This is used by :ref:`extensions <writing-extensions>` to register
callbacks and configuration values without breaking inheritance.
"""
- for key, value in attributes.iteritems():
+ for key, value in six.iteritems(attributes):
if not hasattr(self, key):
setattr(self, key, value)
@@ -323,7 +330,7 @@
rv.overlayed = True
rv.linked_to = self
- for key, value in args.iteritems():
+ for key, value in six.iteritems(args):
if value is not missing:
setattr(rv, key, value)
@@ -333,7 +340,7 @@
rv.cache = copy_cache(self.cache)
rv.extensions = {}
- for key, value in self.extensions.iteritems():
+ for key, value in six.iteritems(self.extensions):
rv.extensions[key] = value.bind(rv)
if extensions is not missing:
rv.extensions.update(load_extensions(rv, extensions))
@@ -352,7 +359,7 @@
try:
return obj[argument]
except (TypeError, LookupError):
- if isinstance(argument, basestring):
+ if isinstance(argument, six.string_types):
try:
attr = str(argument)
except Exception:
@@ -407,7 +414,7 @@
of the extensions to be applied you have to filter source through
the :meth:`preprocess` method.
"""
- source = unicode(source)
+ source = six.text_type(source)
try:
return self.lexer.tokeniter(source, name, filename)
except TemplateSyntaxError:
@@ -420,7 +427,7 @@
because there you usually only want the actual source tokenized.
"""
return reduce(lambda s, e: e.preprocess(s, name, filename),
- self.iter_extensions(), unicode(source))
+ self.iter_extensions(), six.text_type(source))
def _tokenize(self, source, name, filename=None, state=None):
"""Called by the parser to do the preprocessing and filtering
@@ -435,7 +442,7 @@
return stream
def _generate(self, source, name, filename, defer_init=False):
- """Internal hook that can be overriden to hook a different generate
+ """Internal hook that can be overridden to hook a different generate
method in.
.. versionadded:: 2.5
@@ -443,7 +450,7 @@
return generate(source, self, name, filename, defer_init=defer_init)
def _compile(self, source, filename):
- """Internal hook that can be overriden to hook a different compile
+ """Internal hook that can be overridden to hook a different compile
method in.
.. versionadded:: 2.5
@@ -474,7 +481,7 @@
"""
source_hint = None
try:
- if isinstance(source, basestring):
+ if isinstance(source, six.string_types):
source_hint = source
source = self._parse(source, name, filename)
if self.optimized:
@@ -570,10 +577,14 @@
py_header = imp.get_magic() + \
u'\xff\xff\xff\xff'.encode('iso-8859-15')
+ # Python 3.3 added a source filesize to the header
+ if sys.version_info >= (3, 3):
+ py_header += u'\x00\x00\x00\x00'.encode('iso-8859-15')
+
def write_file(filename, data, mode):
if zip:
info = ZipInfo(filename)
- info.external_attr = 0755 << 16L
+ info.external_attr = 0o755 << 16
zip_file.writestr(info, data)
else:
f = open(os.path.join(target, filename), mode)
@@ -597,7 +608,7 @@
source, filename, _ = self.loader.get_source(self, name)
try:
code = self.compile(source, name, filename, True, True)
- except TemplateSyntaxError, e:
+ except TemplateSyntaxError as e:
if not ignore_errors:
raise
log_function('Could not compile "%s": %s' % (name, e))
@@ -667,7 +678,7 @@
if self.exception_handler is not None:
self.exception_handler(traceback)
exc_type, exc_value, tb = traceback.standard_exc_info
- raise exc_type, exc_value, tb
+ six.reraise(exc_type, exc_value, tb)
def join_path(self, template, parent):
"""Join a template with the parent. By default all the lookups are
@@ -754,7 +765,7 @@
.. versionadded:: 2.3
"""
- if isinstance(template_name_or_list, basestring):
+ if isinstance(template_name_or_list, six.string_types):
return self.get_template(template_name_or_list, parent, globals)
elif isinstance(template_name_or_list, Template):
return template_name_or_list
@@ -839,7 +850,7 @@
'environment': environment,
'__file__': code.co_filename
}
- exec code in namespace
+ exec(code, namespace)
rv = cls._from_namespace(environment, namespace, globals)
rv._uptodate = uptodate
return rv
@@ -999,12 +1010,9 @@
return Markup(concat(self._body_stream))
def __str__(self):
- return unicode(self).encode('utf-8')
+ s = self.__unicode__()
+ return s if six.PY3 else s.encode('utf-8')
- # unicode goes after __str__ because we configured 2to3 to rename
- # __unicode__ to __str__. because the 2to3 tree is not designed to
- # remove nodes from it, we leave the above __str__ around and let
- # it override at runtime.
def __unicode__(self):
return concat(self._body_stream)
@@ -1035,7 +1043,7 @@
return rv
-class TemplateStream(object):
+class TemplateStream(six.Iterator):
"""A template stream works pretty much like an ordinary python generator
but it can buffer multiple items to reduce the number of total iterations.
Per default the output is unbuffered which means that for every unbuffered
@@ -1053,15 +1061,15 @@
def dump(self, fp, encoding=None, errors='strict'):
"""Dump the complete stream into a file or file-like object.
Per default unicode strings are written, if you want to encode
- before writing specifiy an `encoding`.
+ before writing specify an `encoding`.
Example usage::
Template('Hello {{ name }}!').stream(name='foo').dump('hello.html')
"""
close = False
- if isinstance(fp, basestring):
- fp = file(fp, 'w')
+ if isinstance(fp, six.string_types):
+ fp = open(fp, encoding is None and 'w' or 'wb')
close = True
try:
if encoding is not None:
@@ -1079,7 +1087,7 @@
def disable_buffering(self):
"""Disable the output buffering."""
- self._next = self._gen.next
+ self._next = lambda: six.next(self._gen)
self.buffered = False
def enable_buffering(self, size=5):
@@ -1107,12 +1115,12 @@
c_size = 0
self.buffered = True
- self._next = generator(self._gen.next).next
+ self._next = lambda: six.next(generator(lambda: six.next(self._gen)))
def __iter__(self):
return self
- def next(self):
+ def __next__(self):
return self._next()
diff --git a/jinja2/exceptions.py b/jinja2/exceptions.py
index 771f6a8..9fe698b 100644
--- a/jinja2/exceptions.py
+++ b/jinja2/exceptions.py
@@ -8,22 +8,34 @@
:copyright: (c) 2010 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
+import sys
+import six
+from six.moves import map
class TemplateError(Exception):
"""Baseclass for all template errors."""
- def __init__(self, message=None):
- if message is not None:
- message = unicode(message).encode('utf-8')
- Exception.__init__(self, message)
-
- @property
- def message(self):
- if self.args:
- message = self.args[0]
+ if sys.version_info[0] < 3:
+ def __init__(self, message=None):
if message is not None:
- return message.decode('utf-8', 'replace')
+ message = six.text_type(message).encode('utf-8')
+ Exception.__init__(self, message)
+
+ @property
+ def message(self):
+ if self.args:
+ message = self.args[0]
+ if message is not None:
+ return message.decode('utf-8', 'replace')
+
+ else:
+ @property
+ def message(self):
+ if self.args:
+ message = self.args[0]
+ if message is not None:
+ return message
class TemplateNotFound(IOError, LookupError, TemplateError):
@@ -62,8 +74,8 @@
def __init__(self, names=(), message=None):
if message is None:
- message = u'non of the templates given were found: ' + \
- u', '.join(map(unicode, names))
+ message = u'none of the templates given were found: ' + \
+ u', '.join(map(six.text_type, names))
TemplateNotFound.__init__(self, names and names[-1] or None, message)
self.templates = list(names)
@@ -83,12 +95,9 @@
self.translated = False
def __str__(self):
- return unicode(self).encode('utf-8')
+ s = self.__unicode__()
+ return s if six.PY3 else s.encode('utf-8')
- # unicode goes after __str__ because we configured 2to3 to rename
- # __unicode__ to __str__. because the 2to3 tree is not designed to
- # remove nodes from it, we leave the above __str__ around and let
- # it override at runtime.
def __unicode__(self):
# for translated errors we only return the message
if self.translated:
diff --git a/jinja2/ext.py b/jinja2/ext.py
index 5ba6efd..495e643 100644
--- a/jinja2/ext.py
+++ b/jinja2/ext.py
@@ -10,13 +10,13 @@
:copyright: (c) 2010 by the Jinja Team.
:license: BSD.
"""
-from collections import deque
from jinja2 import nodes
from jinja2.defaults import *
from jinja2.environment import Environment
-from jinja2.runtime import Undefined, concat
+from jinja2.runtime import concat
from jinja2.exceptions import TemplateAssertionError, TemplateSyntaxError
-from jinja2.utils import contextfunction, import_string, Markup, next
+from jinja2.utils import contextfunction, import_string, Markup
+import six
# the only real useful gettext functions for a Jinja template. Note
@@ -34,7 +34,7 @@
return rv
-class Extension(object):
+class Extension(six.with_metaclass(ExtensionRegistry, object)):
"""Extensions can be used to add extra functionality to the Jinja template
system at the parser level. Custom extensions are bound to an environment
but may not store environment specific data on `self`. The reason for
@@ -205,13 +205,13 @@
self.environment.globals.pop(key, None)
def _extract(self, source, gettext_functions=GETTEXT_FUNCTIONS):
- if isinstance(source, basestring):
+ if isinstance(source, six.string_types):
source = self.environment.parse(source)
return extract_from_ast(source, gettext_functions)
def parse(self, parser):
"""Parse a translatable tag."""
- lineno = next(parser.stream).lineno
+ lineno = six.advance_iterator(parser.stream).lineno
num_called_num = False
# find all the variables referenced. Additionally a variable can be
@@ -235,7 +235,7 @@
# expressions
if parser.stream.current.type == 'assign':
- next(parser.stream)
+ six.advance_iterator(parser.stream)
variables[name.value] = var = parser.parse_expression()
else:
variables[name.value] = var = nodes.Name(name.value, 'load')
@@ -261,7 +261,7 @@
# if we have a pluralize block, we parse that too
if parser.stream.current.test('name:pluralize'):
have_plural = True
- next(parser.stream)
+ six.advance_iterator(parser.stream)
if parser.stream.current.type != 'block_end':
name = parser.stream.expect('name')
if name.value not in variables:
@@ -272,10 +272,10 @@
num_called_num = name.value == 'num'
parser.stream.expect('block_end')
plural_names, plural = self._parse_block(parser, False)
- next(parser.stream)
+ six.advance_iterator(parser.stream)
referenced.update(plural_names)
else:
- next(parser.stream)
+ six.advance_iterator(parser.stream)
# register free names as simple name expressions
for var in referenced:
@@ -300,15 +300,15 @@
while 1:
if parser.stream.current.type == 'data':
buf.append(parser.stream.current.value.replace('%', '%%'))
- next(parser.stream)
+ six.advance_iterator(parser.stream)
elif parser.stream.current.type == 'variable_begin':
- next(parser.stream)
+ six.advance_iterator(parser.stream)
name = parser.stream.expect('name').value
referenced.append(name)
buf.append('%%(%s)s' % name)
parser.stream.expect('variable_end')
elif parser.stream.current.type == 'block_begin':
- next(parser.stream)
+ six.advance_iterator(parser.stream)
if parser.stream.current.test('name:endtrans'):
break
elif parser.stream.current.test('name:pluralize'):
@@ -354,7 +354,7 @@
# enough to handle the variable expansion and autoescape
# handling itself
if self.environment.newstyle_gettext:
- for key, value in variables.iteritems():
+ for key, value in six.iteritems(variables):
# the function adds that later anyways in case num was
# called num, so just skip it.
if num_called_num and key == 'num':
@@ -381,7 +381,7 @@
tags = set(['do'])
def parse(self, parser):
- node = nodes.ExprStmt(lineno=next(parser.stream).lineno)
+ node = nodes.ExprStmt(lineno=six.advance_iterator(parser.stream).lineno)
node.node = parser.parse_tuple()
return node
@@ -391,7 +391,7 @@
tags = set(['break', 'continue'])
def parse(self, parser):
- token = next(parser.stream)
+ token = six.advance_iterator(parser.stream)
if token.value == 'break':
return nodes.Break(lineno=token.lineno)
return nodes.Continue(lineno=token.lineno)
@@ -402,7 +402,7 @@
tags = set(['with'])
def parse(self, parser):
- node = nodes.Scope(lineno=next(parser.stream).lineno)
+ node = nodes.Scope(lineno=six.advance_iterator(parser.stream).lineno)
assignments = []
while parser.stream.current.type != 'block_end':
lineno = parser.stream.current.lineno
@@ -423,7 +423,7 @@
tags = set(['autoescape'])
def parse(self, parser):
- node = nodes.ScopedEvalContextModifier(lineno=next(parser.stream).lineno)
+ node = nodes.ScopedEvalContextModifier(lineno=six.advance_iterator(parser.stream).lineno)
node.options = [
nodes.Keyword('autoescape', parser.parse_expression())
]
@@ -476,7 +476,7 @@
strings = []
for arg in node.args:
if isinstance(arg, nodes.Const) and \
- isinstance(arg.value, basestring):
+ isinstance(arg.value, six.string_types):
strings.append(arg.value)
else:
strings.append(None)
@@ -552,6 +552,10 @@
The `newstyle_gettext` flag can be set to `True` to enable newstyle
gettext calls.
+ .. versionchanged:: 2.7
+ A `silent` option can now be provided. If set to `False` template
+ syntax errors are propagated instead of being ignored.
+
:param fileobj: the file-like object the messages should be extracted from
:param keywords: a list of keywords (i.e. function names) that should be
recognized as translation functions
@@ -571,8 +575,10 @@
extensions.add(InternationalizationExtension)
def getbool(options, key, default=False):
- options.get(key, str(default)).lower() in ('1', 'on', 'yes', 'true')
+ return options.get(key, str(default)).lower() in \
+ ('1', 'on', 'yes', 'true')
+ silent = getbool(options, 'silent', True)
environment = Environment(
options.get('block_start_string', BLOCK_START_STRING),
options.get('block_end_string', BLOCK_END_STRING),
@@ -595,7 +601,9 @@
try:
node = environment.parse(source)
tokens = list(environment.lex(environment.preprocess(source)))
- except TemplateSyntaxError, e:
+ except TemplateSyntaxError as e:
+ if not silent:
+ raise
# skip templates with syntax errors
return
diff --git a/jinja2/filters.py b/jinja2/filters.py
index 1ef47f9..9da1195 100644
--- a/jinja2/filters.py
+++ b/jinja2/filters.py
@@ -12,10 +12,13 @@
import math
from random import choice
from operator import itemgetter
-from itertools import imap, groupby
-from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode
+from itertools import groupby
+from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode, \
+ unicode_urlencode
from jinja2.runtime import Undefined
-from jinja2.exceptions import FilterArgumentError, SecurityError
+from jinja2.exceptions import FilterArgumentError
+import six
+from six.moves import map
_word_re = re.compile(r'\w+(?u)')
@@ -53,7 +56,7 @@
passed object with the rules of the environment. Dots are allowed
to access attributes of attributes.
"""
- if not isinstance(attribute, basestring) or '.' not in attribute:
+ if not isinstance(attribute, six.string_types) or '.' not in attribute:
return lambda x: environment.getitem(x, attribute)
attribute = attribute.split('.')
def attrgetter(item):
@@ -67,7 +70,27 @@
"""Enforce HTML escaping. This will probably double escape variables."""
if hasattr(value, '__html__'):
value = value.__html__()
- return escape(unicode(value))
+ return escape(six.text_type(value))
+
+
+def do_urlencode(value):
+ """Escape strings for use in URLs (uses UTF-8 encoding). It accepts both
+ dictionaries and regular strings as well as pairwise iterables.
+
+ .. versionadded:: 2.7
+ """
+ itemiter = None
+ if isinstance(value, dict):
+ itemiter = six.iteritems(value)
+ elif not isinstance(value, six.string_types):
+ try:
+ itemiter = iter(value)
+ except TypeError:
+ pass
+ if itemiter is None:
+ return unicode_urlencode(value)
+ return u'&'.join(unicode_urlencode(k) + '=' +
+ unicode_urlencode(v) for k, v in itemiter)
@evalcontextfilter
@@ -89,7 +112,7 @@
if count is None:
count = -1
if not eval_ctx.autoescape:
- return unicode(s).replace(unicode(old), unicode(new), count)
+ return six.text_type(s).replace(six.text_type(old), six.text_type(new), count)
if hasattr(old, '__html__') or hasattr(new, '__html__') and \
not hasattr(s, '__html__'):
s = escape(s)
@@ -134,7 +157,7 @@
"""
rv = u' '.join(
u'%s="%s"' % (escape(key), escape(value))
- for key, value in d.iteritems()
+ for key, value in six.iteritems(d)
if value is not None and not isinstance(value, Undefined)
)
if autospace and rv:
@@ -155,7 +178,12 @@
"""Return a titlecased version of the value. I.e. words will start with
uppercase letters, all remaining characters are lowercase.
"""
- return soft_unicode(s).title()
+ rv = []
+ for item in re.compile(r'([-\s]+)(?u)').split(s):
+ if not item:
+ continue
+ rv.append(item[0].upper() + item[1:])
+ return ''.join(rv)
def do_dictsort(value, case_sensitive=False, by='key'):
@@ -168,7 +196,7 @@
{% for item in mydict|dictsort %}
sort the dict by key, case insensitive
- {% for item in mydict|dicsort(true) %}
+ {% for item in mydict|dictsort(true) %}
sort the dict by key, case sensitive
{% for item in mydict|dictsort(false, 'value') %}
@@ -184,7 +212,7 @@
'"key" or "value"')
def sort_func(item):
value = item[pos]
- if isinstance(value, basestring) and not case_sensitive:
+ if isinstance(value, six.string_types) and not case_sensitive:
value = value.lower()
return value
@@ -221,7 +249,7 @@
"""
if not case_sensitive:
def sort_func(item):
- if isinstance(item, basestring):
+ if isinstance(item, six.string_types):
item = item.lower()
return item
else:
@@ -279,11 +307,11 @@
The `attribute` parameter was added.
"""
if attribute is not None:
- value = imap(make_attrgetter(eval_ctx.environment, attribute), value)
+ value = map(make_attrgetter(eval_ctx.environment, attribute), value)
# no automatic escaping? joining is a lot eaiser then
if not eval_ctx.autoescape:
- return unicode(d).join(imap(unicode, value))
+ return six.text_type(d).join(map(six.text_type, value))
# if the delimiter doesn't have an html representation we check
# if any of the items has. If yes we do a coercion to Markup
@@ -294,27 +322,27 @@
if hasattr(item, '__html__'):
do_escape = True
else:
- value[idx] = unicode(item)
+ value[idx] = six.text_type(item)
if do_escape:
d = escape(d)
else:
- d = unicode(d)
+ d = six.text_type(d)
return d.join(value)
# no html involved, to normal joining
- return soft_unicode(d).join(imap(soft_unicode, value))
+ return soft_unicode(d).join(map(soft_unicode, value))
def do_center(value, width=80):
"""Centers the value in a field of a given width."""
- return unicode(value).center(width)
+ return six.text_type(value).center(width)
@environmentfilter
def do_first(environment, seq):
"""Return the first item of a sequence."""
try:
- return iter(seq).next()
+ return six.advance_iterator(iter(seq))
except StopIteration:
return environment.undefined('No first item, sequence was empty.')
@@ -323,7 +351,7 @@
def do_last(environment, seq):
"""Return the last item of a sequence."""
try:
- return iter(reversed(seq)).next()
+ return six.advance_iterator(iter(reversed(seq)))
except StopIteration:
return environment.undefined('No last item, sequence was empty.')
@@ -346,25 +374,25 @@
bytes = float(value)
base = binary and 1024 or 1000
prefixes = [
- (binary and "KiB" or "kB"),
- (binary and "MiB" or "MB"),
- (binary and "GiB" or "GB"),
- (binary and "TiB" or "TB"),
- (binary and "PiB" or "PB"),
- (binary and "EiB" or "EB"),
- (binary and "ZiB" or "ZB"),
- (binary and "YiB" or "YB")
+ (binary and 'KiB' or 'kB'),
+ (binary and 'MiB' or 'MB'),
+ (binary and 'GiB' or 'GB'),
+ (binary and 'TiB' or 'TB'),
+ (binary and 'PiB' or 'PB'),
+ (binary and 'EiB' or 'EB'),
+ (binary and 'ZiB' or 'ZB'),
+ (binary and 'YiB' or 'YB')
]
if bytes == 1:
- return "1 Byte"
+ return '1 Byte'
elif bytes < base:
- return "%d Bytes" % bytes
+ return '%d Bytes' % bytes
else:
for i, prefix in enumerate(prefixes):
- unit = base * base ** (i + 1)
+ unit = base ** (i + 2)
if bytes < unit:
- return "%.1f %s" % ((bytes / unit), prefix)
- return "%.1f %s" % ((bytes / unit), prefix)
+ return '%.1f %s' % ((base * bytes / unit), prefix)
+ return '%.1f %s' % ((base * bytes / unit), prefix)
def do_pprint(value, verbose=False):
@@ -444,15 +472,23 @@
return u' '.join(result)
@environmentfilter
-def do_wordwrap(environment, s, width=79, break_long_words=True):
+def do_wordwrap(environment, s, width=79, break_long_words=True,
+ wrapstring=None):
"""
Return a copy of the string passed to the filter wrapped after
``79`` characters. You can override this default using the first
parameter. If you set the second parameter to `false` Jinja will not
- split words apart if they are longer than `width`.
+ split words apart if they are longer than `width`. By default, the newlines
+ will be the default newlines for the environment, but this can be changed
+ using the wrapstring keyword argument.
+
+ .. versionadded:: 2.7
+ Added support for the `wrapstring` parameter.
"""
+ if not wrapstring:
+ wrapstring = environment.newline_sequence
import textwrap
- return environment.newline_sequence.join(textwrap.wrap(s, width=width, expand_tabs=False,
+ return wrapstring.join(textwrap.wrap(s, width=width, expand_tabs=False,
replace_whitespace=False,
break_long_words=break_long_words))
@@ -513,7 +549,7 @@
"""
if hasattr(value, '__html__'):
value = value.__html__()
- return Markup(unicode(value)).striptags()
+ return Markup(six.text_type(value)).striptags()
def do_slice(value, slices, fill_with=None):
@@ -541,7 +577,7 @@
items_per_slice = length // slices
slices_with_extra = length % slices
offset = 0
- for slice_number in xrange(slices):
+ for slice_number in range(slices):
start = offset + slice_number * items_per_slice
if slice_number < slices_with_extra:
offset += 1
@@ -557,7 +593,7 @@
A filter that batches items. It works pretty much like `slice`
just the other way round. It returns a list of lists with the
given number of items. If you provide a second parameter this
- is used to fill missing items. See this example:
+ is used to fill up missing items. See this example:
.. sourcecode:: html+jinja
@@ -666,7 +702,8 @@
grouper = property(itemgetter(0))
list = property(itemgetter(1))
- def __new__(cls, (key, value)):
+ def __new__(cls, xxx_todo_changeme):
+ (key, value) = xxx_todo_changeme
return tuple.__new__(cls, (key, list(value)))
@@ -687,7 +724,7 @@
attributes. Also the `start` parameter was moved on to the right.
"""
if attribute is not None:
- iterable = imap(make_attrgetter(environment, attribute), iterable)
+ iterable = map(make_attrgetter(environment, attribute), iterable)
return sum(iterable, start)
@@ -707,14 +744,14 @@
def do_mark_unsafe(value):
"""Mark a value as unsafe. This is the reverse operation for :func:`safe`."""
- return unicode(value)
+ return six.text_type(value)
def do_reverse(value):
"""Reverse the object or return an iterator the iterates over it the other
way round.
"""
- if isinstance(value, basestring):
+ if isinstance(value, six.string_types):
return value[::-1]
try:
return reversed(value)
@@ -797,5 +834,6 @@
'round': do_round,
'groupby': do_groupby,
'safe': do_mark_safe,
- 'xmlattr': do_xmlattr
+ 'xmlattr': do_xmlattr,
+ 'urlencode': do_urlencode
}
diff --git a/jinja2/lexer.py b/jinja2/lexer.py
index 0d3f696..bd2e861 100644
--- a/jinja2/lexer.py
+++ b/jinja2/lexer.py
@@ -18,7 +18,8 @@
from operator import itemgetter
from collections import deque
from jinja2.exceptions import TemplateSyntaxError
-from jinja2.utils import LRUCache, next
+from jinja2.utils import LRUCache
+import six
# cache for the lexers. Exists in order to be able to have multiple
@@ -45,6 +46,12 @@
float_re = re.compile(r'(?<!\.)\d+\.\d+')
newline_re = re.compile(r'(\r\n|\r|\n)')
+try:
+ intern = intern # py2
+except NameError:
+ import sys
+ intern = sys.intern # py3
+
# internal the tokens and keep references to them
TOKEN_ADD = intern('add')
TOKEN_ASSIGN = intern('assign')
@@ -126,7 +133,7 @@
';': TOKEN_SEMICOLON
}
-reverse_operators = dict([(v, k) for k, v in operators.iteritems()])
+reverse_operators = dict([(v, k) for k, v in six.iteritems(operators)])
assert len(operators) == len(reverse_operators), 'operators dropped'
operator_re = re.compile('(%s)' % '|'.join(re.escape(x) for x in
sorted(operators, key=lambda x: -len(x))))
@@ -197,7 +204,7 @@
if environment.line_statement_prefix is not None:
rules.append((len(environment.line_statement_prefix), 'linestatement',
- r'^\s*' + e(environment.line_statement_prefix)))
+ r'^[ \t\v]*' + e(environment.line_statement_prefix)))
if environment.line_comment_prefix is not None:
rules.append((len(environment.line_comment_prefix), 'linecomment',
r'(?:^|(?<=\S))[^\S\r\n]*' +
@@ -262,7 +269,7 @@
)
-class TokenStreamIterator(object):
+class TokenStreamIterator(six.Iterator):
"""The iterator for tokenstreams. Iterate over the stream
until the eof token is reached.
"""
@@ -273,35 +280,36 @@
def __iter__(self):
return self
- def next(self):
+ def __next__(self):
token = self.stream.current
if token.type is TOKEN_EOF:
self.stream.close()
raise StopIteration()
- next(self.stream)
+ six.advance_iterator(self.stream)
return token
-class TokenStream(object):
+class TokenStream(six.Iterator):
"""A token stream is an iterable that yields :class:`Token`\s. The
parser however does not iterate over it but calls :meth:`next` to go
one token ahead. The current active token is stored as :attr:`current`.
"""
def __init__(self, generator, name, filename):
- self._next = iter(generator).next
+ self._iter = iter(generator)
self._pushed = deque()
self.name = name
self.filename = filename
self.closed = False
self.current = Token(1, TOKEN_INITIAL, '')
- next(self)
+ six.advance_iterator(self)
def __iter__(self):
return TokenStreamIterator(self)
- def __nonzero__(self):
+ def __bool__(self):
return bool(self._pushed) or self.current.type is not TOKEN_EOF
+ __nonzero__ = __bool__ # py2
eos = property(lambda x: not x, doc="Are we at the end of the stream?")
@@ -311,7 +319,7 @@
def look(self):
"""Look at the next token."""
- old_token = next(self)
+ old_token = six.advance_iterator(self)
result = self.current
self.push(result)
self.current = old_token
@@ -319,28 +327,28 @@
def skip(self, n=1):
"""Got n tokens ahead."""
- for x in xrange(n):
- next(self)
+ for x in range(n):
+ six.advance_iterator(self)
def next_if(self, expr):
"""Perform the token test and return the token if it matched.
Otherwise the return value is `None`.
"""
if self.current.test(expr):
- return next(self)
+ return six.advance_iterator(self)
def skip_if(self, expr):
"""Like :meth:`next_if` but only returns `True` or `False`."""
return self.next_if(expr) is not None
- def next(self):
+ def __next__(self):
"""Go one token ahead and return the old one"""
rv = self.current
if self._pushed:
self.current = self._pushed.popleft()
elif self.current.type is not TOKEN_EOF:
try:
- self.current = self._next()
+ self.current = six.advance_iterator(self._iter)
except StopIteration:
self.close()
return rv
@@ -348,7 +356,7 @@
def close(self):
"""Close the stream."""
self.current = Token(self.current.lineno, TOKEN_EOF, '')
- self._next = None
+ self._iter = None
self.closed = True
def expect(self, expr):
@@ -369,7 +377,7 @@
try:
return self.current
finally:
- next(self)
+ six.advance_iterator(self)
def get_lexer(environment):
@@ -414,7 +422,7 @@
(operator_re, TOKEN_OPERATOR, None)
]
- # assamble the root lexing rule. because "|" is ungreedy
+ # assemble the root lexing rule. because "|" is ungreedy
# we have to sort by length so that the lexer continues working
# as expected when we have parsing rules like <% for block and
# <%= for variables. (if someone wants asp like syntax)
@@ -491,7 +499,7 @@
}
def _normalize_newlines(self, value):
- """Called for strings and template data to normlize it to unicode."""
+ """Called for strings and template data to normalize it to unicode."""
return newline_re.sub(self.newline_sequence, value)
def tokenize(self, source, name=None, filename=None, state=None):
@@ -526,7 +534,7 @@
value = self._normalize_newlines(value[1:-1]) \
.encode('ascii', 'backslashreplace') \
.decode('unicode-escape')
- except Exception, e:
+ 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)
@@ -549,7 +557,7 @@
"""This method tokenizes the text and returns the tokens in a
generator. Use this method if you just want to tokenize a template.
"""
- source = '\n'.join(unicode(source).splitlines())
+ source = '\n'.join(six.text_type(source).splitlines())
pos = 0
lineno = 1
stack = ['root']
@@ -571,7 +579,7 @@
if m is None:
continue
- # we only match blocks and variables if brances / parentheses
+ # we only match blocks and variables if braces / parentheses
# are balanced. continue parsing with the lower rule which
# is the operator rule. do this only if the end tags look
# like operators
@@ -590,7 +598,7 @@
# yield for the current token the first named
# group that matched
elif token == '#bygroup':
- for key, value in m.groupdict().iteritems():
+ for key, value in six.iteritems(m.groupdict()):
if value is not None:
yield lineno, key, value
lineno += value.count('\n')
@@ -647,7 +655,7 @@
stack.pop()
# resolve the new state by group checking
elif new_state == '#bygroup':
- for key, value in m.groupdict().iteritems():
+ for key, value in six.iteritems(m.groupdict()):
if value is not None:
stack.append(key)
break
@@ -669,7 +677,7 @@
# publish new function and start again
pos = pos2
break
- # if loop terminated without break we havn't found a single match
+ # if loop terminated without break we haven't found a single match
# either we are at the end of the file or we have a problem
else:
# end of text
diff --git a/jinja2/loaders.py b/jinja2/loaders.py
index 419a9c8..08fe872 100644
--- a/jinja2/loaders.py
+++ b/jinja2/loaders.py
@@ -13,12 +13,13 @@
import weakref
from types import ModuleType
from os import path
+import six
try:
from hashlib import sha1
except ImportError:
from sha import new as sha1
from jinja2.exceptions import TemplateNotFound
-from jinja2.utils import LRUCache, open_if_exists, internalcode
+from jinja2.utils import open_if_exists, internalcode
def split_template_path(template):
@@ -153,7 +154,7 @@
"""
def __init__(self, searchpath, encoding='utf-8'):
- if isinstance(searchpath, basestring):
+ if isinstance(searchpath, six.string_types):
searchpath = [searchpath]
self.searchpath = list(searchpath)
self.encoding = encoding
@@ -306,7 +307,7 @@
rv = self.load_func(template)
if rv is None:
raise TemplateNotFound(template)
- elif isinstance(rv, basestring):
+ elif isinstance(rv, six.string_types):
return rv, None, None
return rv
@@ -330,12 +331,16 @@
self.mapping = mapping
self.delimiter = delimiter
- def get_source(self, environment, template):
+ def get_loader(self, template):
try:
prefix, name = template.split(self.delimiter, 1)
loader = self.mapping[prefix]
except (ValueError, KeyError):
raise TemplateNotFound(template)
+ return loader, name
+
+ def get_source(self, environment, template):
+ loader, name = self.get_loader(template)
try:
return loader.get_source(environment, name)
except TemplateNotFound:
@@ -343,9 +348,19 @@
# (the one that includes the prefix)
raise TemplateNotFound(template)
+ @internalcode
+ def load(self, environment, name, globals=None):
+ loader, local_name = self.get_loader(name)
+ try:
+ return loader.load(environment, local_name)
+ except TemplateNotFound:
+ # re-raise the exception with the correct fileame here.
+ # (the one that includes the prefix)
+ raise TemplateNotFound(name)
+
def list_templates(self):
result = []
- for prefix, loader in self.mapping.iteritems():
+ for prefix, loader in six.iteritems(self.mapping):
for template in loader.list_templates():
result.append(prefix + self.delimiter + template)
return result
@@ -376,6 +391,15 @@
pass
raise TemplateNotFound(template)
+ @internalcode
+ def load(self, environment, name, globals=None):
+ for loader in self.loaders:
+ try:
+ return loader.load(environment, name, globals)
+ except TemplateNotFound:
+ pass
+ raise TemplateNotFound(name)
+
def list_templates(self):
found = set()
for loader in self.loaders:
@@ -408,7 +432,7 @@
# create a fake module that looks for the templates in the
# path given.
mod = _TemplateModule(package_name)
- if isinstance(path, basestring):
+ if isinstance(path, six.string_types):
path = [path]
else:
path = list(path)
diff --git a/jinja2/meta.py b/jinja2/meta.py
index 3a779a5..26ae0b9 100644
--- a/jinja2/meta.py
+++ b/jinja2/meta.py
@@ -11,7 +11,7 @@
"""
from jinja2 import nodes
from jinja2.compiler import CodeGenerator
-
+import six
class TrackingCodeGenerator(CodeGenerator):
"""We abuse the code generator for introspection."""
@@ -77,7 +77,7 @@
# something const, only yield the strings and ignore
# non-string consts that really just make no sense
if isinstance(template_name, nodes.Const):
- if isinstance(template_name.value, basestring):
+ if isinstance(template_name.value, six.string_types):
yield template_name.value
# something dynamic in there
else:
@@ -87,7 +87,7 @@
yield None
continue
# constant is a basestring, direct template name
- if isinstance(node.template.value, basestring):
+ if isinstance(node.template.value, six.string_types):
yield node.template.value
# a tuple or list (latter *should* not happen) made of consts,
# yield the consts that are strings. We could warn here for
@@ -95,7 +95,7 @@
elif isinstance(node, nodes.Include) and \
isinstance(node.template.value, (tuple, list)):
for template_name in node.template.value:
- if isinstance(template_name, basestring):
+ if isinstance(template_name, six.string_types):
yield template_name
# something else we don't care about, we could warn here
else:
diff --git a/jinja2/nodes.py b/jinja2/nodes.py
index f9da1da..c14f8e3 100644
--- a/jinja2/nodes.py
+++ b/jinja2/nodes.py
@@ -13,9 +13,10 @@
:license: BSD, see LICENSE for more details.
"""
import operator
-from itertools import chain, izip
from collections import deque
from jinja2.utils import Markup, MethodType, FunctionType
+import six
+from six.moves import zip
#: the types we support for context functions
@@ -102,7 +103,7 @@
return ctx
-class Node(object):
+class Node(six.with_metaclass(NodeType, object)):
"""Baseclass for all Jinja2 nodes. There are a number of nodes available
of different types. There are three major types:
@@ -136,13 +137,13 @@
len(self.fields),
len(self.fields) != 1 and 's' or ''
))
- for name, arg in izip(self.fields, fields):
+ for name, arg in zip(self.fields, fields):
setattr(self, name, arg)
for attr in self.attributes:
setattr(self, attr, attributes.pop(attr, None))
if attributes:
raise TypeError('unknown attribute %r' %
- iter(attributes).next())
+ six.advance_iterator(iter(attributes)))
def iter_fields(self, exclude=None, only=None):
"""This method iterates over all fields that are defined and yields
@@ -440,7 +441,7 @@
constant value in the generated code, otherwise it will raise
an `Impossible` exception.
"""
- from compiler import has_safe_repr
+ from .compiler import has_safe_repr
if not has_safe_repr(value):
raise Impossible()
return cls(value, lineno=lineno, environment=environment)
@@ -687,7 +688,7 @@
def as_const(self, eval_ctx=None):
eval_ctx = get_eval_context(self, eval_ctx)
- return ''.join(unicode(x.as_const(eval_ctx)) for x in self.nodes)
+ return ''.join(six.text_type(x.as_const(eval_ctx)) for x in self.nodes)
class Compare(Expr):
diff --git a/jinja2/parser.py b/jinja2/parser.py
index d44229a..953027c 100644
--- a/jinja2/parser.py
+++ b/jinja2/parser.py
@@ -10,8 +10,9 @@
"""
from jinja2 import nodes
from jinja2.exceptions import TemplateSyntaxError, TemplateAssertionError
-from jinja2.utils import next
from jinja2.lexer import describe_token, describe_token_expr
+import six
+from six.moves import map
#: statements that callinto
@@ -162,12 +163,12 @@
self.fail_eof(end_tokens)
if drop_needle:
- next(self.stream)
+ six.advance_iterator(self.stream)
return result
def parse_set(self):
"""Parse an assign statement."""
- lineno = next(self.stream).lineno
+ lineno = six.advance_iterator(self.stream).lineno
target = self.parse_assign_target()
self.stream.expect('assign')
expr = self.parse_tuple()
@@ -185,7 +186,7 @@
test = self.parse_expression()
recursive = self.stream.skip_if('name:recursive')
body = self.parse_statements(('name:endfor', 'name:else'))
- if next(self.stream).value == 'endfor':
+ if six.advance_iterator(self.stream).value == 'endfor':
else_ = []
else:
else_ = self.parse_statements(('name:endfor',), drop_needle=True)
@@ -199,7 +200,7 @@
node.test = self.parse_tuple(with_condexpr=False)
node.body = self.parse_statements(('name:elif', 'name:else',
'name:endif'))
- token = next(self.stream)
+ token = six.advance_iterator(self.stream)
if token.test('name:elif'):
new_node = nodes.If(lineno=self.stream.current.lineno)
node.else_ = [new_node]
@@ -214,7 +215,7 @@
return result
def parse_block(self):
- node = nodes.Block(lineno=next(self.stream).lineno)
+ node = nodes.Block(lineno=six.advance_iterator(self.stream).lineno)
node.name = self.stream.expect('name').value
node.scoped = self.stream.skip_if('name:scoped')
@@ -223,7 +224,7 @@
# raise a nicer error message in that case.
if self.stream.current.type == 'sub':
self.fail('Block names in Jinja have to be valid Python '
- 'identifiers and may not contain hypens, use an '
+ 'identifiers and may not contain hyphens, use an '
'underscore instead.')
node.body = self.parse_statements(('name:endblock',), drop_needle=True)
@@ -231,21 +232,21 @@
return node
def parse_extends(self):
- node = nodes.Extends(lineno=next(self.stream).lineno)
+ node = nodes.Extends(lineno=six.advance_iterator(self.stream).lineno)
node.template = self.parse_expression()
return node
def parse_import_context(self, node, default):
if self.stream.current.test_any('name:with', 'name:without') and \
self.stream.look().test('name:context'):
- node.with_context = next(self.stream).value == 'with'
+ node.with_context = six.advance_iterator(self.stream).value == 'with'
self.stream.skip()
else:
node.with_context = default
return node
def parse_include(self):
- node = nodes.Include(lineno=next(self.stream).lineno)
+ node = nodes.Include(lineno=six.advance_iterator(self.stream).lineno)
node.template = self.parse_expression()
if self.stream.current.test('name:ignore') and \
self.stream.look().test('name:missing'):
@@ -256,14 +257,14 @@
return self.parse_import_context(node, True)
def parse_import(self):
- node = nodes.Import(lineno=next(self.stream).lineno)
+ node = nodes.Import(lineno=six.advance_iterator(self.stream).lineno)
node.template = self.parse_expression()
self.stream.expect('name:as')
node.target = self.parse_assign_target(name_only=True).name
return self.parse_import_context(node, False)
def parse_from(self):
- node = nodes.FromImport(lineno=next(self.stream).lineno)
+ node = nodes.FromImport(lineno=six.advance_iterator(self.stream).lineno)
node.template = self.parse_expression()
self.stream.expect('name:import')
node.names = []
@@ -271,7 +272,7 @@
def parse_context():
if self.stream.current.value in ('with', 'without') and \
self.stream.look().test('name:context'):
- node.with_context = next(self.stream).value == 'with'
+ node.with_context = six.advance_iterator(self.stream).value == 'with'
self.stream.skip()
return True
return False
@@ -316,7 +317,7 @@
self.stream.expect('rparen')
def parse_call_block(self):
- node = nodes.CallBlock(lineno=next(self.stream).lineno)
+ node = nodes.CallBlock(lineno=six.advance_iterator(self.stream).lineno)
if self.stream.current.type == 'lparen':
self.parse_signature(node)
else:
@@ -330,14 +331,14 @@
return node
def parse_filter_block(self):
- node = nodes.FilterBlock(lineno=next(self.stream).lineno)
+ node = nodes.FilterBlock(lineno=six.advance_iterator(self.stream).lineno)
node.filter = self.parse_filter(None, start_inline=True)
node.body = self.parse_statements(('name:endfilter',),
drop_needle=True)
return node
def parse_macro(self):
- node = nodes.Macro(lineno=next(self.stream).lineno)
+ node = nodes.Macro(lineno=six.advance_iterator(self.stream).lineno)
node.name = self.parse_assign_target(name_only=True).name
self.parse_signature(node)
node.body = self.parse_statements(('name:endmacro',),
@@ -345,7 +346,7 @@
return node
def parse_print(self):
- node = nodes.Output(lineno=next(self.stream).lineno)
+ node = nodes.Output(lineno=six.advance_iterator(self.stream).lineno)
node.nodes = []
while self.stream.current.type != 'block_end':
if node.nodes:
@@ -419,7 +420,7 @@
def parse_not(self):
if self.stream.current.test('name:not'):
- lineno = next(self.stream).lineno
+ lineno = six.advance_iterator(self.stream).lineno
return nodes.Not(self.parse_not(), lineno=lineno)
return self.parse_compare()
@@ -430,7 +431,7 @@
while 1:
token_type = self.stream.current.type
if token_type in _compare_operators:
- next(self.stream)
+ six.advance_iterator(self.stream)
ops.append(nodes.Operand(token_type, self.parse_add()))
elif self.stream.skip_if('name:in'):
ops.append(nodes.Operand('in', self.parse_add()))
@@ -449,7 +450,7 @@
lineno = self.stream.current.lineno
left = self.parse_sub()
while self.stream.current.type == 'add':
- next(self.stream)
+ six.advance_iterator(self.stream)
right = self.parse_sub()
left = nodes.Add(left, right, lineno=lineno)
lineno = self.stream.current.lineno
@@ -459,7 +460,7 @@
lineno = self.stream.current.lineno
left = self.parse_concat()
while self.stream.current.type == 'sub':
- next(self.stream)
+ six.advance_iterator(self.stream)
right = self.parse_concat()
left = nodes.Sub(left, right, lineno=lineno)
lineno = self.stream.current.lineno
@@ -469,7 +470,7 @@
lineno = self.stream.current.lineno
args = [self.parse_mul()]
while self.stream.current.type == 'tilde':
- next(self.stream)
+ six.advance_iterator(self.stream)
args.append(self.parse_mul())
if len(args) == 1:
return args[0]
@@ -479,7 +480,7 @@
lineno = self.stream.current.lineno
left = self.parse_div()
while self.stream.current.type == 'mul':
- next(self.stream)
+ six.advance_iterator(self.stream)
right = self.parse_div()
left = nodes.Mul(left, right, lineno=lineno)
lineno = self.stream.current.lineno
@@ -489,7 +490,7 @@
lineno = self.stream.current.lineno
left = self.parse_floordiv()
while self.stream.current.type == 'div':
- next(self.stream)
+ six.advance_iterator(self.stream)
right = self.parse_floordiv()
left = nodes.Div(left, right, lineno=lineno)
lineno = self.stream.current.lineno
@@ -499,7 +500,7 @@
lineno = self.stream.current.lineno
left = self.parse_mod()
while self.stream.current.type == 'floordiv':
- next(self.stream)
+ six.advance_iterator(self.stream)
right = self.parse_mod()
left = nodes.FloorDiv(left, right, lineno=lineno)
lineno = self.stream.current.lineno
@@ -509,7 +510,7 @@
lineno = self.stream.current.lineno
left = self.parse_pow()
while self.stream.current.type == 'mod':
- next(self.stream)
+ six.advance_iterator(self.stream)
right = self.parse_pow()
left = nodes.Mod(left, right, lineno=lineno)
lineno = self.stream.current.lineno
@@ -519,7 +520,7 @@
lineno = self.stream.current.lineno
left = self.parse_unary()
while self.stream.current.type == 'pow':
- next(self.stream)
+ six.advance_iterator(self.stream)
right = self.parse_unary()
left = nodes.Pow(left, right, lineno=lineno)
lineno = self.stream.current.lineno
@@ -529,10 +530,10 @@
token_type = self.stream.current.type
lineno = self.stream.current.lineno
if token_type == 'sub':
- next(self.stream)
+ six.advance_iterator(self.stream)
node = nodes.Neg(self.parse_unary(False), lineno=lineno)
elif token_type == 'add':
- next(self.stream)
+ six.advance_iterator(self.stream)
node = nodes.Pos(self.parse_unary(False), lineno=lineno)
else:
node = self.parse_primary()
@@ -551,20 +552,20 @@
node = nodes.Const(None, lineno=token.lineno)
else:
node = nodes.Name(token.value, 'load', lineno=token.lineno)
- next(self.stream)
+ six.advance_iterator(self.stream)
elif token.type == 'string':
- next(self.stream)
+ six.advance_iterator(self.stream)
buf = [token.value]
lineno = token.lineno
while self.stream.current.type == 'string':
buf.append(self.stream.current.value)
- next(self.stream)
+ six.advance_iterator(self.stream)
node = nodes.Const(''.join(buf), lineno=lineno)
elif token.type in ('integer', 'float'):
- next(self.stream)
+ six.advance_iterator(self.stream)
node = nodes.Const(token.value, lineno=token.lineno)
elif token.type == 'lparen':
- next(self.stream)
+ six.advance_iterator(self.stream)
node = self.parse_tuple(explicit_parentheses=True)
self.stream.expect('rparen')
elif token.type == 'lbracket':
@@ -686,10 +687,10 @@
return node
def parse_subscript(self, node):
- token = next(self.stream)
+ token = six.advance_iterator(self.stream)
if token.type == 'dot':
attr_token = self.stream.current
- next(self.stream)
+ six.advance_iterator(self.stream)
if attr_token.type == 'name':
return nodes.Getattr(node, attr_token.value, 'load',
lineno=token.lineno)
@@ -698,7 +699,6 @@
arg = nodes.Const(attr_token.value, lineno=attr_token.lineno)
return nodes.Getitem(node, arg, 'load', lineno=token.lineno)
if token.type == 'lbracket':
- priority_on_attribute = False
args = []
while self.stream.current.type != 'rbracket':
if args:
@@ -716,13 +716,13 @@
lineno = self.stream.current.lineno
if self.stream.current.type == 'colon':
- next(self.stream)
+ six.advance_iterator(self.stream)
args = [None]
else:
node = self.parse_expression()
if self.stream.current.type != 'colon':
return node
- next(self.stream)
+ six.advance_iterator(self.stream)
args = [node]
if self.stream.current.type == 'colon':
@@ -733,7 +733,7 @@
args.append(None)
if self.stream.current.type == 'colon':
- next(self.stream)
+ six.advance_iterator(self.stream)
if self.stream.current.type not in ('rbracket', 'comma'):
args.append(self.parse_expression())
else:
@@ -763,11 +763,11 @@
break
if self.stream.current.type == 'mul':
ensure(dyn_args is None and dyn_kwargs is None)
- next(self.stream)
+ six.advance_iterator(self.stream)
dyn_args = self.parse_expression()
elif self.stream.current.type == 'pow':
ensure(dyn_kwargs is None)
- next(self.stream)
+ six.advance_iterator(self.stream)
dyn_kwargs = self.parse_expression()
else:
ensure(dyn_args is None and dyn_kwargs is None)
@@ -793,11 +793,11 @@
def parse_filter(self, node, start_inline=False):
while self.stream.current.type == 'pipe' or start_inline:
if not start_inline:
- next(self.stream)
+ six.advance_iterator(self.stream)
token = self.stream.expect('name')
name = token.value
while self.stream.current.type == 'dot':
- next(self.stream)
+ six.advance_iterator(self.stream)
name += '.' + self.stream.expect('name').value
if self.stream.current.type == 'lparen':
args, kwargs, dyn_args, dyn_kwargs = self.parse_call(None)
@@ -811,15 +811,15 @@
return node
def parse_test(self, node):
- token = next(self.stream)
+ token = six.advance_iterator(self.stream)
if self.stream.current.test('name:not'):
- next(self.stream)
+ six.advance_iterator(self.stream)
negated = True
else:
negated = False
name = self.stream.expect('name').value
while self.stream.current.type == 'dot':
- next(self.stream)
+ six.advance_iterator(self.stream)
name += '.' + self.stream.expect('name').value
dyn_args = dyn_kwargs = None
kwargs = []
@@ -862,14 +862,14 @@
if token.value:
add_data(nodes.TemplateData(token.value,
lineno=token.lineno))
- next(self.stream)
+ six.advance_iterator(self.stream)
elif token.type == 'variable_begin':
- next(self.stream)
+ six.advance_iterator(self.stream)
add_data(self.parse_tuple(with_condexpr=True))
self.stream.expect('variable_end')
elif token.type == 'block_begin':
flush_data()
- next(self.stream)
+ six.advance_iterator(self.stream)
if end_tokens is not None and \
self.stream.current.test_any(*end_tokens):
return body
diff --git a/jinja2/runtime.py b/jinja2/runtime.py
index a4a47a2..9742ece 100644
--- a/jinja2/runtime.py
+++ b/jinja2/runtime.py
@@ -8,12 +8,14 @@
:copyright: (c) 2010 by the Jinja Team.
:license: BSD.
"""
-from itertools import chain, imap
+from itertools import chain
from jinja2.nodes import EvalContext, _context_function_types
-from jinja2.utils import Markup, partial, soft_unicode, escape, missing, \
- concat, internalcode, next, object_type_repr
+from jinja2.utils import Markup, soft_unicode, escape, missing, concat, \
+ internalcode, object_type_repr
from jinja2.exceptions import UndefinedError, TemplateRuntimeError, \
TemplateNotFound
+import six
+from six.moves import map
# these variables are exported to the template runtime
@@ -25,16 +27,21 @@
#: the name of the function that is used to convert something into
#: a string. 2to3 will adopt that automatically and the generated
#: code can take advantage of it.
-to_string = unicode
+try:
+ to_string = unicode
+except NameError:
+ to_string = str
#: the identity function. Useful for certain things in the environment
identity = lambda x: x
+_last_iteration = object()
+
def markup_join(seq):
"""Concatenation that escapes if necessary and converts to unicode."""
buf = []
- iterator = imap(soft_unicode, seq)
+ iterator = map(soft_unicode, seq)
for arg in iterator:
buf.append(arg)
if hasattr(arg, '__html__'):
@@ -44,7 +51,7 @@
def unicode_join(seq):
"""Simple args to unicode conversion and concatenation."""
- return concat(imap(unicode, seq))
+ return concat(map(six.text_type, seq))
def new_context(environment, template_name, blocks, vars=None,
@@ -61,7 +68,7 @@
# we don't want to modify the dict passed
if shared:
parent = dict(parent)
- for key, value in locals.iteritems():
+ for key, value in six.iteritems(locals):
if key[:2] == 'l_' and value is not missing:
parent[key[2:]] = value
return Context(environment, parent, template_name, blocks)
@@ -117,7 +124,7 @@
# create the initial mapping of blocks. Whenever template inheritance
# takes place the runtime will update this mapping with the new blocks
# from the template.
- self.blocks = dict((k, [v]) for k, v in blocks.iteritems())
+ self.blocks = dict((k, [v]) for k, v in six.iteritems(blocks))
def super(self, name, current):
"""Render a parent block."""
@@ -169,6 +176,17 @@
"""
if __debug__:
__traceback_hide__ = True
+
+ # Allow callable classes to take a context
+ if hasattr(__obj, '__call__'):
+ fn = __obj.__call__
+ for fn_type in ('contextfunction',
+ 'evalcontextfunction',
+ 'environmentfunction'):
+ if hasattr(fn, fn_type):
+ __obj = fn
+ break
+
if isinstance(__obj, _context_function_types):
if getattr(__obj, 'contextfunction', 0):
args = (__self,) + args
@@ -189,7 +207,7 @@
self.parent, True, None, locals)
context.vars.update(self.vars)
context.eval_ctx = self.eval_ctx
- context.blocks.update((k, list(v)) for k, v in self.blocks.iteritems())
+ context.blocks.update((k, list(v)) for k, v in six.iteritems(self.blocks))
return context
def _all(meth):
@@ -270,6 +288,7 @@
def __init__(self, iterable, recurse=None):
self._iterator = iter(iterable)
self._recurse = recurse
+ self._after = self._safe_next()
self.index0 = -1
# try to get the length of the iterable early. This must be done
@@ -288,7 +307,7 @@
return args[self.index0 % len(args)]
first = property(lambda x: x.index0 == 0)
- last = property(lambda x: x.index0 + 1 == x.length)
+ last = property(lambda x: x._after is _last_iteration)
index = property(lambda x: x.index0 + 1)
revindex = property(lambda x: x.length - x.index0)
revindex0 = property(lambda x: x.length - x.index)
@@ -299,6 +318,12 @@
def __iter__(self):
return LoopContextIterator(self)
+ def _safe_next(self):
+ try:
+ return six.advance_iterator(self._iterator)
+ except StopIteration:
+ return _last_iteration
+
@internalcode
def loop(self, iterable):
if self._recurse is None:
@@ -331,7 +356,7 @@
)
-class LoopContextIterator(object):
+class LoopContextIterator(six.Iterator):
"""The iterator for a loop context."""
__slots__ = ('context',)
@@ -341,10 +366,14 @@
def __iter__(self):
return self
- def next(self):
+ def __next__(self):
ctx = self.context
ctx.index0 += 1
- return next(ctx._iterator), ctx
+ if ctx._after is _last_iteration:
+ raise StopIteration()
+ next_elem = ctx._after
+ ctx._after = ctx._safe_next()
+ return next_elem, ctx
class Macro(object):
@@ -442,7 +471,7 @@
if self._undefined_hint is None:
if self._undefined_obj is missing:
hint = '%r is undefined' % self._undefined_name
- elif not isinstance(self._undefined_name, basestring):
+ elif not isinstance(self._undefined_name, six.string_types):
hint = '%s has no element %r' % (
object_type_repr(self._undefined_obj),
self._undefined_name
@@ -470,12 +499,9 @@
_fail_with_undefined_error
def __str__(self):
- return unicode(self).encode('utf-8')
+ s = self.__unicode__()
+ return s if six.PY3 else s.encode('utf-8')
- # unicode goes after __str__ because we configured 2to3 to rename
- # __unicode__ to __str__. because the 2to3 tree is not designed to
- # remove nodes from it, we leave the above __str__ around and let
- # it override at runtime.
def __unicode__(self):
return u''
@@ -508,6 +534,10 @@
"""
__slots__ = ()
+ def __str__(self):
+ s = self.__unicode__()
+ return s if six.PY3 else s.encode('utf-8')
+
def __unicode__(self):
if self._undefined_hint is None:
if self._undefined_obj is missing:
diff --git a/jinja2/sandbox.py b/jinja2/sandbox.py
index a1cbb29..ed145d5 100644
--- a/jinja2/sandbox.py
+++ b/jinja2/sandbox.py
@@ -13,6 +13,7 @@
:license: BSD.
"""
import operator
+import six
from jinja2.environment import Environment
from jinja2.exceptions import SecurityError
from jinja2.utils import FunctionType, MethodType, TracebackType, CodeType, \
@@ -90,7 +91,7 @@
"""A range that can't generate ranges with a length of more than
MAX_RANGE items.
"""
- rng = xrange(*args)
+ rng = range(*args)
if len(rng) > MAX_RANGE:
raise OverflowError('range too big, maximum size for range is %d' %
MAX_RANGE)
@@ -114,7 +115,7 @@
"""Test if the attribute given is an internal python attribute. For
example this function returns `True` for the `func_code` attribute of
python objects. This is useful if the environment method
- :meth:`~SandboxedEnvironment.is_safe_attribute` is overriden.
+ :meth:`~SandboxedEnvironment.is_safe_attribute` is overridden.
>>> from jinja2.sandbox import is_internal_attribute
>>> is_internal_attribute(lambda: None, "func_code")
@@ -299,7 +300,7 @@
try:
return obj[argument]
except (TypeError, LookupError):
- if isinstance(argument, basestring):
+ if isinstance(argument, six.string_types):
try:
attr = str(argument)
except Exception:
diff --git a/jinja2/tests.py b/jinja2/tests.py
index 50510b0..140b5bf 100644
--- a/jinja2/tests.py
+++ b/jinja2/tests.py
@@ -10,6 +10,7 @@
"""
import re
from jinja2.runtime import Undefined
+import six
try:
from collections import Mapping as MappingType
@@ -76,17 +77,17 @@
def test_lower(value):
"""Return true if the variable is lowercased."""
- return unicode(value).islower()
+ return six.text_type(value).islower()
def test_upper(value):
"""Return true if the variable is uppercased."""
- return unicode(value).isupper()
+ return six.text_type(value).isupper()
def test_string(value):
"""Return true if the object is a string."""
- return isinstance(value, basestring)
+ return isinstance(value, six.string_types)
def test_mapping(value):
@@ -99,7 +100,7 @@
def test_number(value):
"""Return true if the variable is a number."""
- return isinstance(value, (int, long, float, complex))
+ return isinstance(value, (int, float, complex))
def test_sequence(value):
diff --git a/jinja2/testsuite/__init__.py b/jinja2/testsuite/__init__.py
index 1f10ef6..335292d 100644
--- a/jinja2/testsuite/__init__.py
+++ b/jinja2/testsuite/__init__.py
@@ -59,7 +59,7 @@
def assert_traceback_matches(self, callback, expected_tb):
try:
callback()
- except Exception, e:
+ except Exception as e:
tb = format_exception(*sys.exc_info())
if re.search(expected_tb.strip(), ''.join(tb)) is None:
raise self.fail('Traceback did not match:\n\n%s\nexpected:\n%s'
@@ -89,7 +89,7 @@
# doctests will not run on python 3 currently. Too many issues
# with that, do not test that on that platform.
- if sys.version_info < (3, 0):
+ if sys.version_info[0] < 3:
suite.addTest(doctests.suite())
return suite
diff --git a/jinja2/testsuite/api.py b/jinja2/testsuite/api.py
index c8f9634..4f6a1d0 100644
--- a/jinja2/testsuite/api.py
+++ b/jinja2/testsuite/api.py
@@ -9,6 +9,9 @@
:license: BSD, see LICENSE for more details.
"""
import unittest
+import os
+import tempfile
+import shutil
from jinja2.testsuite import JinjaTestCase
@@ -16,6 +19,7 @@
StrictUndefined, UndefinedError, meta, \
is_undefined, Template, DictLoader
from jinja2.utils import Cycler
+import six
env = Environment()
@@ -50,8 +54,8 @@
c = Cycler(*items)
for item in items + items:
assert c.current == item
- assert c.next() == item
- c.next()
+ assert six.advance_iterator(c) == item
+ six.advance_iterator(c)
assert c.current == 2
c.reset()
assert c.current == 1
@@ -107,8 +111,8 @@
def test_find_refererenced_templates(self):
ast = env.parse('{% extends "layout.html" %}{% include helper %}')
i = meta.find_referenced_templates(ast)
- assert i.next() == 'layout.html'
- assert i.next() is None
+ assert six.advance_iterator(i) == 'layout.html'
+ assert six.advance_iterator(i) is None
assert list(i) == []
ast = env.parse('{% extends "layout.html" %}'
@@ -141,21 +145,21 @@
def test_basic_streaming(self):
tmpl = env.from_string("<ul>{% for item in seq %}<li>{{ loop.index "
"}} - {{ item }}</li>{%- endfor %}</ul>")
- stream = tmpl.stream(seq=range(4))
- self.assert_equal(stream.next(), '<ul>')
- self.assert_equal(stream.next(), '<li>1 - 0</li>')
- self.assert_equal(stream.next(), '<li>2 - 1</li>')
- self.assert_equal(stream.next(), '<li>3 - 2</li>')
- self.assert_equal(stream.next(), '<li>4 - 3</li>')
- self.assert_equal(stream.next(), '</ul>')
+ stream = tmpl.stream(seq=list(range(4)))
+ self.assert_equal(six.advance_iterator(stream), '<ul>')
+ self.assert_equal(six.advance_iterator(stream), '<li>1 - 0</li>')
+ self.assert_equal(six.advance_iterator(stream), '<li>2 - 1</li>')
+ self.assert_equal(six.advance_iterator(stream), '<li>3 - 2</li>')
+ self.assert_equal(six.advance_iterator(stream), '<li>4 - 3</li>')
+ self.assert_equal(six.advance_iterator(stream), '</ul>')
def test_buffered_streaming(self):
tmpl = env.from_string("<ul>{% for item in seq %}<li>{{ loop.index "
"}} - {{ item }}</li>{%- endfor %}</ul>")
- stream = tmpl.stream(seq=range(4))
+ stream = tmpl.stream(seq=list(range(4)))
stream.enable_buffering(size=3)
- self.assert_equal(stream.next(), u'<ul><li>1 - 0</li><li>2 - 1</li>')
- self.assert_equal(stream.next(), u'<li>3 - 2</li><li>4 - 3</li></ul>')
+ self.assert_equal(six.advance_iterator(stream), u'<ul><li>1 - 0</li><li>2 - 1</li>')
+ self.assert_equal(six.advance_iterator(stream), u'<li>3 - 2</li><li>4 - 3</li></ul>')
def test_streaming_behavior(self):
tmpl = env.from_string("")
@@ -166,6 +170,17 @@
stream.disable_buffering()
assert not stream.buffered
+ def test_dump_stream(self):
+ tmp = tempfile.mkdtemp()
+ try:
+ tmpl = env.from_string(u"\u2713")
+ stream = tmpl.stream()
+ stream.dump(os.path.join(tmp, 'dump.txt'), 'utf-8')
+ with open(os.path.join(tmp, 'dump.txt'), 'rb') as f:
+ self.assertEqual(f.read(), b'\xe2\x9c\x93')
+ finally:
+ shutil.rmtree(tmp)
+
class UndefinedTestCase(JinjaTestCase):
@@ -222,7 +237,7 @@
def test_none_gives_proper_error(self):
try:
Environment().getattr(None, 'split')()
- except UndefinedError, e:
+ except UndefinedError as e:
assert e.message == "'None' has no attribute 'split'"
else:
assert False, 'expected exception'
@@ -230,7 +245,7 @@
def test_object_repr(self):
try:
Undefined(obj=42, name='upper')()
- except UndefinedError, e:
+ except UndefinedError as e:
assert e.message == "'int object' has no attribute 'upper'"
else:
assert False, 'expected exception'
diff --git a/jinja2/testsuite/core_tags.py b/jinja2/testsuite/core_tags.py
index 2b5f580..409a579 100644
--- a/jinja2/testsuite/core_tags.py
+++ b/jinja2/testsuite/core_tags.py
@@ -22,7 +22,7 @@
def test_simple(self):
tmpl = env.from_string('{% for item in seq %}{{ item }}{% endfor %}')
- assert tmpl.render(seq=range(10)) == '0123456789'
+ assert tmpl.render(seq=list(range(10))) == '0123456789'
def test_else(self):
tmpl = env.from_string('{% for item in seq %}XXX{% else %}...{% endfor %}')
@@ -55,12 +55,12 @@
tmpl = env.from_string('''{% for item in seq %}{{
loop.cycle('<1>', '<2>') }}{% endfor %}{%
for item in seq %}{{ loop.cycle(*through) }}{% endfor %}''')
- output = tmpl.render(seq=range(4), through=('<1>', '<2>'))
+ output = tmpl.render(seq=list(range(4)), through=('<1>', '<2>'))
assert output == '<1><2>' * 4
def test_scope(self):
tmpl = env.from_string('{% for item in seq %}{% endfor %}{{ item }}')
- output = tmpl.render(seq=range(10))
+ output = tmpl.render(seq=list(range(10)))
assert not output
def test_varlen(self):
diff --git a/jinja2/testsuite/debug.py b/jinja2/testsuite/debug.py
index 7552dec..2588a83 100644
--- a/jinja2/testsuite/debug.py
+++ b/jinja2/testsuite/debug.py
@@ -8,7 +8,6 @@
:copyright: (c) 2010 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
-import sys
import unittest
from jinja2.testsuite import JinjaTestCase, filesystem_loader
@@ -20,12 +19,11 @@
class DebugTestCase(JinjaTestCase):
- if sys.version_info[:2] != (2, 4):
- def test_runtime_error(self):
- def test():
- tmpl.render(fail=lambda: 1 / 0)
- tmpl = env.get_template('broken.html')
- self.assert_traceback_matches(test, r'''
+ def test_runtime_error(self):
+ def test():
+ tmpl.render(fail=lambda: 1 / 0)
+ tmpl = env.get_template('broken.html')
+ self.assert_traceback_matches(test, r'''
File ".*?broken.html", line 2, in (top-level template code|<module>)
\{\{ fail\(\) \}\}
File ".*?debug.pyc?", line \d+, in <lambda>
diff --git a/jinja2/testsuite/ext.py b/jinja2/testsuite/ext.py
index 6ca6c22..29f78b1 100644
--- a/jinja2/testsuite/ext.py
+++ b/jinja2/testsuite/ext.py
@@ -17,14 +17,8 @@
from jinja2.exceptions import TemplateAssertionError
from jinja2.ext import Extension
from jinja2.lexer import Token, count_newlines
-from jinja2.utils import next
-
-# 2.x / 3.x
-try:
- from io import BytesIO
-except ImportError:
- from StringIO import StringIO as BytesIO
-
+import six
+from six import BytesIO
importable_object = 23
@@ -115,7 +109,7 @@
self.attr('ext_attr'),
nodes.ImportedName(__name__ + '.importable_object'),
nodes.ContextReference()
- ])]).set_lineno(next(parser.stream).lineno)
+ ])]).set_lineno(six.advance_iterator(parser.stream).lineno)
def _dump(self, sandboxed, ext_attr, imported_object, context):
return '%s|%s|%s|%s' % (
@@ -222,7 +216,7 @@
original = Environment(extensions=[TestExtension])
overlay = original.overlay()
for env in original, overlay:
- for ext in env.extensions.itervalues():
+ for ext in six.itervalues(env.extensions):
assert ext.environment is env
def test_preprocessor_extension(self):
@@ -432,7 +426,7 @@
'''
tmpl = env.from_string(tmplsource)
assert tmpl.render(val=True).split()[0] == 'Markup'
- assert tmpl.render(val=False).split()[0] == unicode.__name__
+ assert tmpl.render(val=False).split()[0] == six.text_type.__name__
# looking at the source we should see <testing> there in raw
# (and then escaped as well)
diff --git a/jinja2/testsuite/filters.py b/jinja2/testsuite/filters.py
index aefe768..7f6b314 100644
--- a/jinja2/testsuite/filters.py
+++ b/jinja2/testsuite/filters.py
@@ -12,6 +12,8 @@
from jinja2.testsuite import JinjaTestCase
from jinja2 import Markup, Environment
+import six
+from six.moves import map
env = Environment()
@@ -47,14 +49,14 @@
def test_batch(self):
tmpl = env.from_string("{{ foo|batch(3)|list }}|"
"{{ foo|batch(3, 'X')|list }}")
- out = tmpl.render(foo=range(10))
+ out = tmpl.render(foo=list(range(10)))
assert out == ("[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]|"
"[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 'X', 'X']]")
def test_slice(self):
tmpl = env.from_string('{{ foo|slice(3)|list }}|'
'{{ foo|slice(3, "X")|list }}')
- out = tmpl.render(foo=range(10))
+ out = tmpl.render(foo=list(range(10)))
assert out == ("[[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]|"
"[[0, 1, 2, 3], [4, 5, 6, 'X'], [7, 8, 9, 'X']]")
@@ -84,14 +86,32 @@
'{{ 1000000000000|filesizeformat(true) }}'
)
out = tmpl.render()
- assert out == (
- '100 Bytes|0.0 kB|0.0 MB|0.0 GB|0.0 TB|100 Bytes|'
- '1000 Bytes|1.0 KiB|0.9 MiB|0.9 GiB'
+ self.assert_equal(out, (
+ '100 Bytes|1.0 kB|1.0 MB|1.0 GB|1.0 TB|100 Bytes|'
+ '1000 Bytes|976.6 KiB|953.7 MiB|931.3 GiB'
+ ))
+
+ def test_filesizeformat_issue59(self):
+ tmpl = env.from_string(
+ '{{ 300|filesizeformat }}|'
+ '{{ 3000|filesizeformat }}|'
+ '{{ 3000000|filesizeformat }}|'
+ '{{ 3000000000|filesizeformat }}|'
+ '{{ 3000000000000|filesizeformat }}|'
+ '{{ 300|filesizeformat(true) }}|'
+ '{{ 3000|filesizeformat(true) }}|'
+ '{{ 3000000|filesizeformat(true) }}'
)
+ out = tmpl.render()
+ self.assert_equal(out, (
+ '300 Bytes|3.0 kB|3.0 MB|3.0 GB|3.0 TB|300 Bytes|'
+ '2.9 KiB|2.9 MiB'
+ ))
+
def test_first(self):
tmpl = env.from_string('{{ foo|first }}')
- out = tmpl.render(foo=range(10))
+ out = tmpl.render(foo=list(range(10)))
assert out == '0'
def test_float(self):
@@ -137,7 +157,7 @@
def test_last(self):
tmpl = env.from_string('''{{ foo|last }}''')
- out = tmpl.render(foo=range(10))
+ out = tmpl.render(foo=list(range(10)))
assert out == '9'
def test_length(self):
@@ -153,12 +173,12 @@
def test_pprint(self):
from pprint import pformat
tmpl = env.from_string('''{{ data|pprint }}''')
- data = range(1000)
+ data = list(range(1000))
assert tmpl.render(data=data) == pformat(data)
def test_random(self):
tmpl = env.from_string('''{{ seq|random }}''')
- seq = range(100)
+ seq = list(range(100))
for _ in range(10):
assert int(tmpl.render(seq=seq)) in seq
@@ -170,11 +190,21 @@
def test_string(self):
x = [1, 2, 3, 4, 5]
tmpl = env.from_string('''{{ obj|string }}''')
- assert tmpl.render(obj=x) == unicode(x)
+ assert tmpl.render(obj=x) == six.text_type(x)
def test_title(self):
tmpl = env.from_string('''{{ "foo bar"|title }}''')
assert tmpl.render() == "Foo Bar"
+ tmpl = env.from_string('''{{ "foo's bar"|title }}''')
+ assert tmpl.render() == "Foo's Bar"
+ tmpl = env.from_string('''{{ "foo bar"|title }}''')
+ assert tmpl.render() == "Foo Bar"
+ tmpl = env.from_string('''{{ "f bar f"|title }}''')
+ assert tmpl.render() == "F Bar F"
+ tmpl = env.from_string('''{{ "foo-bar"|title }}''')
+ assert tmpl.render() == "Foo-Bar"
+ tmpl = env.from_string('''{{ "foo\tbar"|title }}''')
+ assert tmpl.render() == "Foo\tBar"
def test_truncate(self):
tmpl = env.from_string(
@@ -269,7 +299,8 @@
def __init__(self, value):
self.value = value
def __unicode__(self):
- return unicode(self.value)
+ return six.text_type(self.value)
+ __str__ = __unicode__
tmpl = env.from_string('''{{ items|sort(attribute='value')|join }}''')
assert tmpl.render(items=map(Magic, [3, 2, 4, 1])) == '1234'
@@ -349,6 +380,18 @@
tmpl = env.from_string('{{ "<div>foo</div>" }}')
assert tmpl.render() == '<div>foo</div>'
+ def test_urlencode(self):
+ env = Environment(autoescape=True)
+ tmpl = env.from_string('{{ "Hello, world!"|urlencode }}')
+ assert tmpl.render() == 'Hello%2C%20world%21'
+ tmpl = env.from_string('{{ o|urlencode }}')
+ assert tmpl.render(o=u"Hello, world\u203d") == "Hello%2C%20world%E2%80%BD"
+ assert tmpl.render(o=(("f", 1),)) == "f=1"
+ assert tmpl.render(o=(('f', 1), ("z", 2))) == "f=1&z=2"
+ assert tmpl.render(o=((u"\u203d", 1),)) == "%E2%80%BD=1"
+ assert tmpl.render(o={u"\u203d": 1}) == "%E2%80%BD=1"
+ assert tmpl.render(o={0: 1}) == "0=1"
+
def suite():
suite = unittest.TestSuite()
diff --git a/jinja2/testsuite/imports.py b/jinja2/testsuite/imports.py
index 1cb12cb..3db9008 100644
--- a/jinja2/testsuite/imports.py
+++ b/jinja2/testsuite/imports.py
@@ -83,7 +83,7 @@
self.assert_raises(TemplateNotFound, t.render)
try:
t.render()
- except TemplatesNotFound, e:
+ except TemplatesNotFound as e:
assert e.templates == ['missing', 'missing2']
assert e.name == 'missing2'
else:
diff --git a/jinja2/testsuite/inheritance.py b/jinja2/testsuite/inheritance.py
index 355aa0c..7909b03 100644
--- a/jinja2/testsuite/inheritance.py
+++ b/jinja2/testsuite/inheritance.py
@@ -148,7 +148,7 @@
}))
t = env.from_string('{% extends "master.html" %}{% block item %}'
'{{ item }}{% endblock %}')
- assert t.render(seq=range(5)) == '[0][1][2][3][4]'
+ assert t.render(seq=list(range(5))) == '[0][1][2][3][4]'
def test_super_in_scoped_block(self):
env = Environment(loader=DictLoader({
@@ -157,7 +157,7 @@
}))
t = env.from_string('{% extends "master.html" %}{% block item %}'
'{{ super() }}|{{ item * 2 }}{% endblock %}')
- assert t.render(seq=range(5)) == '[0|0][1|2][2|4][3|6][4|8]'
+ assert t.render(seq=list(range(5))) == '[0|0][1|2][2|4][3|6][4|8]'
def test_scoped_block_after_inheritance(self):
env = Environment(loader=DictLoader({
diff --git a/jinja2/testsuite/lexnparse.py b/jinja2/testsuite/lexnparse.py
index 562df62..f05f601 100644
--- a/jinja2/testsuite/lexnparse.py
+++ b/jinja2/testsuite/lexnparse.py
@@ -15,18 +15,44 @@
from jinja2 import Environment, Template, TemplateSyntaxError, \
UndefinedError, nodes
+from jinja2.lexer import Token, TokenStream, TOKEN_EOF, TOKEN_BLOCK_BEGIN, TOKEN_BLOCK_END
+import six
env = Environment()
# how does a string look like in jinja syntax?
-if sys.version_info < (3, 0):
+if sys.version_info[0] < 3:
def jinja_string_repr(string):
return repr(string)[1:]
else:
jinja_string_repr = repr
+class TokenStreamTestCase(JinjaTestCase):
+ test_tokens = [Token(1, TOKEN_BLOCK_BEGIN, ''),
+ Token(2, TOKEN_BLOCK_END, ''),
+ ]
+
+ def test_simple(self):
+ ts = TokenStream(self.test_tokens, "foo", "bar")
+ assert ts.current.type is TOKEN_BLOCK_BEGIN
+ assert bool(ts)
+ assert not bool(ts.eos)
+ six.advance_iterator(ts)
+ assert ts.current.type is TOKEN_BLOCK_END
+ assert bool(ts)
+ assert not bool(ts.eos)
+ six.advance_iterator(ts)
+ assert ts.current.type is TOKEN_EOF
+ assert not bool(ts)
+ assert bool(ts.eos)
+
+ def test_iter(self):
+ token_types = [t.type for t in TokenStream(self.test_tokens, "foo", "bar")]
+ assert token_types == ['block_begin', 'block_end', ]
+
+
class LexerTestCase(JinjaTestCase):
def test_raw1(self):
@@ -42,7 +68,7 @@
env = Environment('{%', '%}', '${', '}')
tmpl = env.from_string('''{% for item in seq
%}${{'foo': item}|upper}{% endfor %}''')
- assert tmpl.render(seq=range(3)) == "{'FOO': 0}{'FOO': 1}{'FOO': 2}"
+ assert tmpl.render(seq=list(range(3))) == "{'FOO': 0}{'FOO': 1}{'FOO': 2}"
def test_comments(self):
env = Environment('<!--', '-->', '{', '}')
@@ -52,7 +78,7 @@
<li>{item}</li>
<!--- endfor -->
</ul>''')
- assert tmpl.render(seq=range(3)) == ("<ul>\n <li>0</li>\n "
+ assert tmpl.render(seq=list(range(3))) == ("<ul>\n <li>0</li>\n "
"<li>1</li>\n <li>2</li>\n</ul>")
def test_string_escapes(self):
@@ -68,11 +94,11 @@
def test_operators(self):
from jinja2.lexer import operators
- for test, expect in operators.iteritems():
+ for test, expect in six.iteritems(operators):
if test in '([{}])':
continue
stream = env.lexer.tokenize('{{ %s }}' % test)
- stream.next()
+ six.advance_iterator(stream)
assert stream.current.type == expect
def test_normalizing(self):
@@ -92,7 +118,7 @@
<? for item in seq -?>
<?= item ?>
<?- endfor ?>''')
- assert tmpl.render(seq=range(5)) == '01234'
+ assert tmpl.render(seq=list(range(5))) == '01234'
def test_erb_syntax(self):
env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>')
@@ -101,7 +127,7 @@
<% for item in seq -%>
<%= item %>
<%- endfor %>''')
- assert tmpl.render(seq=range(5)) == '01234'
+ assert tmpl.render(seq=list(range(5))) == '01234'
def test_comment_syntax(self):
env = Environment('<!--', '-->', '${', '}', '<!--#', '-->')
@@ -110,7 +136,7 @@
<!-- for item in seq --->
${item}
<!--- endfor -->''')
- assert tmpl.render(seq=range(5)) == '01234'
+ assert tmpl.render(seq=list(range(5))) == '01234'
def test_balancing(self):
tmpl = env.from_string('''{{{'foo':'bar'}.foo}}''')
@@ -130,8 +156,8 @@
% for item in seq:
${item}
% endfor''')
- assert [int(x.strip()) for x in tmpl.render(seq=range(5)).split()] == \
- range(5)
+ assert [int(x.strip()) for x in tmpl.render(seq=list(range(5))).split()] == \
+ list(range(5))
env = Environment('<%', '%>', '${', '}', '<%#', '%>', '%', '##')
tmpl = env.from_string('''\
@@ -139,8 +165,8 @@
% for item in seq:
${item} ## the rest of the stuff
% endfor''')
- assert [int(x.strip()) for x in tmpl.render(seq=range(5)).split()] == \
- range(5)
+ assert [int(x.strip()) for x in tmpl.render(seq=list(range(5))).split()] == \
+ list(range(5))
def test_line_syntax_priority(self):
# XXX: why is the whitespace there in front of the newline?
@@ -166,10 +192,10 @@
def assert_error(code, expected):
try:
Template(code)
- except TemplateSyntaxError, e:
+ except TemplateSyntaxError as e:
assert str(e) == expected, 'unexpected error message'
else:
- assert False, 'that was suposed to be an error'
+ assert False, 'that was supposed to be an error'
assert_error('{% for item in seq %}...{% endif %}',
"Encountered unknown tag 'endif'. Jinja was looking "
@@ -189,7 +215,7 @@
"that needs to be closed is 'for'.")
assert_error('{% block foo-bar-baz %}',
"Block names in Jinja have to be valid Python identifiers "
- "and may not contain hypens, use an underscore instead.")
+ "and may not contain hyphens, use an underscore instead.")
assert_error('{% unknown_tag %}',
"Encountered unknown tag 'unknown_tag'.")
@@ -317,7 +343,7 @@
self.assert_raises(TemplateSyntaxError, env.from_string,
'{% block x %}{% endblock y %}')
- def test_contant_casing(self):
+ def test_constant_casing(self):
for const in True, False, None:
tmpl = env.from_string('{{ %s }}|{{ %s }}|{{ %s }}' % (
str(const), str(const).lower(), str(const).upper()
@@ -327,7 +353,7 @@
def test_test_chaining(self):
self.assert_raises(TemplateSyntaxError, env.from_string,
'{{ foo is string is sequence }}')
- env.from_string('{{ 42 is string or 42 is number }}'
+ assert env.from_string('{{ 42 is string or 42 is number }}'
).render() == 'True'
def test_string_concatenation(self):
@@ -335,9 +361,9 @@
assert tmpl.render() == 'foobarbaz'
def test_notin(self):
- bar = xrange(100)
+ bar = range(100)
tmpl = env.from_string('''{{ not 42 in bar }}''')
- assert tmpl.render(bar=bar) == unicode(not 42 in bar)
+ assert tmpl.render(bar=bar) == six.text_type(not 42 in bar)
def test_implicit_subscribed_tuple(self):
class Foo(object):
@@ -381,6 +407,7 @@
def suite():
suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(TokenStreamTestCase))
suite.addTest(unittest.makeSuite(LexerTestCase))
suite.addTest(unittest.makeSuite(ParserTestCase))
suite.addTest(unittest.makeSuite(SyntaxTestCase))
diff --git a/jinja2/testsuite/loader.py b/jinja2/testsuite/loader.py
index fb1e53d..f62ec92 100644
--- a/jinja2/testsuite/loader.py
+++ b/jinja2/testsuite/loader.py
@@ -182,6 +182,34 @@
tmpl_3c4ddf650c1a73df961a6d3d2ce2752f1b8fd490
assert mod.__file__.endswith('.pyc')
+ def test_choice_loader(self):
+ log = self.compile_down(py_compile=True)
+ assert 'Byte-compiled "a/test.html"' in log
+
+ self.mod_env.loader = loaders.ChoiceLoader([
+ self.mod_env.loader,
+ loaders.DictLoader({'DICT_SOURCE': 'DICT_TEMPLATE'})
+ ])
+
+ tmpl1 = self.mod_env.get_template('a/test.html')
+ self.assert_equal(tmpl1.render(), 'BAR')
+ tmpl2 = self.mod_env.get_template('DICT_SOURCE')
+ self.assert_equal(tmpl2.render(), 'DICT_TEMPLATE')
+
+ def test_prefix_loader(self):
+ log = self.compile_down(py_compile=True)
+ assert 'Byte-compiled "a/test.html"' in log
+
+ self.mod_env.loader = loaders.PrefixLoader({
+ 'MOD': self.mod_env.loader,
+ 'DICT': loaders.DictLoader({'test.html': 'DICT_TEMPLATE'})
+ })
+
+ tmpl1 = self.mod_env.get_template('MOD/a/test.html')
+ self.assert_equal(tmpl1.render(), 'BAR')
+ tmpl2 = self.mod_env.get_template('DICT/test.html')
+ self.assert_equal(tmpl2.render(), 'DICT_TEMPLATE')
+
def suite():
suite = unittest.TestSuite()
diff --git a/jinja2/testsuite/regression.py b/jinja2/testsuite/regression.py
index 4db9076..4198259 100644
--- a/jinja2/testsuite/regression.py
+++ b/jinja2/testsuite/regression.py
@@ -14,6 +14,7 @@
from jinja2 import Template, Environment, DictLoader, TemplateSyntaxError, \
TemplateNotFound, PrefixLoader
+import six
env = Environment()
@@ -118,7 +119,7 @@
''')
- assert tmpl.render().split() == map(unicode, range(1, 11)) * 5
+ assert tmpl.render().split() == [six.text_type(x) for x in range(1, 11)] * 5
def test_weird_inline_comment(self):
env = Environment(line_statement_prefix='%')
@@ -242,11 +243,24 @@
}))
try:
env.get_template('foo/bar.html')
- except TemplateNotFound, e:
+ except TemplateNotFound as e:
assert e.name == 'foo/bar.html'
else:
assert False, 'expected error here'
+ def test_contextfunction_callable_classes(self):
+ from jinja2.utils import contextfunction
+ class CallableClass(object):
+ @contextfunction
+ def __call__(self, ctx):
+ return ctx.resolve('hello')
+
+ tpl = Template("""{{ callableclass() }}""")
+ output = tpl.render(callableclass = CallableClass(), hello = 'TEST')
+ expected = 'TEST'
+
+ self.assert_equal(output, expected)
+
def suite():
suite = unittest.TestSuite()
diff --git a/jinja2/testsuite/security.py b/jinja2/testsuite/security.py
index 4518eac..c892fed 100644
--- a/jinja2/testsuite/security.py
+++ b/jinja2/testsuite/security.py
@@ -18,6 +18,7 @@
from jinja2 import Markup, escape
from jinja2.exceptions import SecurityError, TemplateSyntaxError, \
TemplateRuntimeError
+import six
class PrivateStuff(object):
@@ -76,7 +77,7 @@
# adding two strings should escape the unsafe one
unsafe = '<script type="application/x-some-script">alert("foo");</script>'
safe = Markup('<em>username</em>')
- assert unsafe + safe == unicode(escape(unsafe)) + unicode(safe)
+ assert unsafe + safe == six.text_type(escape(unsafe)) + six.text_type(safe)
# string interpolations are safe to use too
assert Markup('<em>%s</em>') % '<bad user>' == \
@@ -114,7 +115,7 @@
'{{ say_hello("<blink>foo</blink>") }}')
escaped_out = '<p>Hello <blink>foo</blink>!</p>'
assert t.render() == escaped_out
- assert unicode(t.module) == escaped_out
+ assert six.text_type(t.module) == escaped_out
assert escape(t.module) == escaped_out
assert t.module.say_hello('<blink>foo</blink>') == escaped_out
assert escape(t.module.say_hello('<blink>foo</blink>')) == escaped_out
@@ -136,7 +137,7 @@
t = env.from_string('{{ %s }}' % expr)
try:
t.render(ctx)
- except TemplateRuntimeError, e:
+ except TemplateRuntimeError as e:
pass
else:
self.fail('expected runtime error')
@@ -153,7 +154,7 @@
t = env.from_string('{{ %s }}' % expr)
try:
t.render(ctx)
- except TemplateRuntimeError, e:
+ except TemplateRuntimeError as e:
pass
else:
self.fail('expected runtime error')
diff --git a/jinja2/testsuite/utils.py b/jinja2/testsuite/utils.py
index be2e902..cab9b09 100644
--- a/jinja2/testsuite/utils.py
+++ b/jinja2/testsuite/utils.py
@@ -60,8 +60,8 @@
def test_markup_leaks(self):
counts = set()
- for count in xrange(20):
- for item in xrange(1000):
+ for count in range(20):
+ for item in range(1000):
escape("foo")
escape("<foo>")
escape(u"foo")
diff --git a/jinja2/utils.py b/jinja2/utils.py
index 49e9e9a..3440979 100644
--- a/jinja2/utils.py
+++ b/jinja2/utils.py
@@ -11,19 +11,26 @@
import re
import sys
import errno
+import six
+from six.moves import map
+try:
+ from urllib.parse import quote_from_bytes as url_quote
+except ImportError:
+ from urllib import quote as url_quote
try:
from thread import allocate_lock
except ImportError:
- from dummy_thread import allocate_lock
+ try:
+ from _thread import allocate_lock # py 3
+ except ImportError:
+ from dummy_thread import allocate_lock
from collections import deque
-from itertools import imap
-
_word_split_re = re.compile(r'(\s+)')
_punctuation_re = re.compile(
'^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % (
- '|'.join(imap(re.escape, ('(', '<', '<'))),
- '|'.join(imap(re.escape, ('.', ',', ')', '>', '\n', '>')))
+ '|'.join(map(re.escape, ('(', '<', '<'))),
+ '|'.join(map(re.escape, ('.', ',', ')', '>', '\n', '>')))
)
)
_simple_email_re = re.compile(r'^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$')
@@ -38,38 +45,7 @@
# internal code
internal_code = set()
-
-# concatenate a list of strings and convert them to unicode.
-# unfortunately there is a bug in python 2.4 and lower that causes
-# unicode.join trash the traceback.
-_concat = u''.join
-try:
- def _test_gen_bug():
- raise TypeError(_test_gen_bug)
- yield None
- _concat(_test_gen_bug())
-except TypeError, _error:
- if not _error.args or _error.args[0] is not _test_gen_bug:
- def concat(gen):
- try:
- return _concat(list(gen))
- except Exception:
- # this hack is needed so that the current frame
- # does not show up in the traceback.
- exc_type, exc_value, tb = sys.exc_info()
- raise exc_type, exc_value, tb.tb_next
- else:
- concat = _concat
- del _test_gen_bug, _error
-
-
-# for python 2.x we create outselves a next() function that does the
-# basics without exception catching.
-try:
- next = next
-except NameError:
- def next(x):
- return x.next()
+concat = u''.join
# if this python version is unable to deal with unicode filenames
@@ -77,7 +53,7 @@
# This is used in a couple of places. As far as Jinja is concerned
# filenames are unicode *or* bytestrings in 2.x and unicode only in
# 3.x because compile cannot handle bytes
-if sys.version_info < (3, 0):
+if sys.version_info[0] < 3:
def _encode_filename(filename):
if isinstance(filename, unicode):
return filename.encode('utf-8')
@@ -100,8 +76,8 @@
yield None
FunctionType = type(_func)
GeneratorType = type(_func())
-MethodType = type(_C.method)
-CodeType = type(_C.method.func_code)
+MethodType = type(_C().method)
+CodeType = type(_C.method.__code__)
try:
raise TypeError()
except TypeError:
@@ -128,7 +104,7 @@
def evalcontextfunction(f):
- """This decoraotr can be used to mark a function or method as an eval
+ """This decorator can be used to mark a function or method as an eval
context callable. This is similar to the :func:`contextfunction`
but instead of passing the context, an evaluation context object is
passed. For more information about the eval context, see
@@ -152,7 +128,7 @@
def internalcode(f):
"""Marks the function as internally used"""
- internal_code.add(f.func_code)
+ internal_code.add(f.__code__)
return f
@@ -191,7 +167,7 @@
def import_string(import_name, silent=False):
- """Imports an object based on a string. This use useful if you want to
+ """Imports an object based on a string. This is useful if you want to
use import paths as endpoints or something similar. An import path can
be specified either in dotted notation (``xml.sax.saxutils.escape``)
or with a colon as object delimiter (``xml.sax.saxutils:escape``).
@@ -222,7 +198,7 @@
"""
try:
return open(filename, mode)
- except IOError, e:
+ except IOError as e:
if e.errno not in (errno.ENOENT, errno.EISDIR):
raise
@@ -271,7 +247,7 @@
trim_url = lambda x, limit=trim_url_limit: limit is not None \
and (x[:limit] + (len(x) >=limit and '...'
or '')) or x
- words = _word_split_re.split(unicode(escape(text)))
+ words = _word_split_re.split(six.text_type(escape(text)))
nofollow_attr = nofollow and ' rel="nofollow"' or ''
for i, word in enumerate(words):
match = _punctuation_re.match(word)
@@ -280,6 +256,7 @@
if middle.startswith('www.') or (
'@' not in middle and
not middle.startswith('http://') and
+ not middle.startswith('https://') and
len(middle) > 0 and
middle[0] in _letters + _digits and (
middle.endswith('.org') or
@@ -307,7 +284,7 @@
words = LOREM_IPSUM_WORDS.split()
result = []
- for _ in xrange(n):
+ for _ in range(n):
next_capitalized = True
last_comma = last_fullstop = 0
word = None
@@ -315,7 +292,7 @@
p = []
# each paragraph contains out of 20 to 100 words.
- for idx, _ in enumerate(xrange(randrange(min, max))):
+ for idx, _ in enumerate(range(randrange(min, max))):
while True:
word = choice(words)
if word != last:
@@ -349,6 +326,21 @@
return Markup(u'\n'.join(u'<p>%s</p>' % escape(x) for x in result))
+def unicode_urlencode(obj, charset='utf-8'):
+ """URL escapes a single bytestring or unicode string with the
+ given charset if applicable to URL safe quoting under all rules
+ that need to be considered under all supported Python versions.
+
+ If non strings are provided they are converted to their unicode
+ representation first.
+ """
+ if not isinstance(obj, six.string_types):
+ obj = six.text_type(obj)
+ if isinstance(obj, six.text_type):
+ obj = obj.encode(charset)
+ return six.text_type(url_quote(obj))
+
+
class LRUCache(object):
"""A simple LRU Cache implementation."""
@@ -366,18 +358,10 @@
# alias all queue methods for faster lookup
self._popleft = self._queue.popleft
self._pop = self._queue.pop
- if hasattr(self._queue, 'remove'):
- self._remove = self._queue.remove
+ self._remove = self._queue.remove
self._wlock = allocate_lock()
self._append = self._queue.append
- def _remove(self, obj):
- """Python 2.4 compatibility."""
- for idx, item in enumerate(self._queue):
- if item == obj:
- del self._queue[idx]
- break
-
def __getstate__(self):
return {
'capacity': self.capacity,
@@ -393,7 +377,7 @@
return (self.capacity,)
def copy(self):
- """Return an shallow copy of the instance."""
+ """Return a shallow copy of the instance."""
rv = self.__class__(self.capacity)
rv._mapping.update(self._mapping)
rv._queue = deque(self._queue)
@@ -410,11 +394,15 @@
"""Set `default` if the key is not in the cache otherwise
leave unchanged. Return the value of this key.
"""
+ self._wlock.acquire()
try:
- return self[key]
- except KeyError:
- self[key] = default
- return default
+ try:
+ return self[key]
+ except KeyError:
+ self[key] = default
+ return default
+ finally:
+ self._wlock.release()
def clear(self):
"""Clear the cache."""
@@ -443,19 +431,23 @@
"""Get an item from the cache. Moves the item up so that it has the
highest priority then.
- Raise an `KeyError` if it does not exist.
+ Raise a `KeyError` if it does not exist.
"""
- rv = self._mapping[key]
- if self._queue[-1] != key:
- try:
- self._remove(key)
- except ValueError:
- # if something removed the key from the container
- # when we read, ignore the ValueError that we would
- # get otherwise.
- pass
- self._append(key)
- return rv
+ self._wlock.acquire()
+ try:
+ rv = self._mapping[key]
+ if self._queue[-1] != key:
+ try:
+ self._remove(key)
+ except ValueError:
+ # if something removed the key from the container
+ # when we read, ignore the ValueError that we would
+ # get otherwise.
+ pass
+ self._append(key)
+ return rv
+ finally:
+ self._wlock.release()
def __setitem__(self, key, value):
"""Sets the value for an item. Moves the item up so that it
@@ -464,11 +456,7 @@
self._wlock.acquire()
try:
if key in self._mapping:
- try:
- self._remove(key)
- except ValueError:
- # __getitem__ is not locked, it might happen
- pass
+ self._remove(key)
elif len(self._mapping) == self.capacity:
del self._mapping[self._popleft()]
self._append(key)
@@ -478,7 +466,7 @@
def __delitem__(self, key):
"""Remove an item from the cache dict.
- Raise an `KeyError` if it does not exist.
+ Raise a `KeyError` if it does not exist.
"""
self._wlock.acquire()
try:
@@ -538,7 +526,7 @@
pass
-class Cycler(object):
+class Cycler(six.Iterator):
"""A cycle helper for templates."""
def __init__(self, *items):
@@ -556,7 +544,7 @@
"""Returns the current item."""
return self.items[self.pos]
- def next(self):
+ def __next__(self):
"""Goes one item ahead and returns it."""
rv = self.current
self.pos = (self.pos + 1) % len(self.items)
@@ -579,7 +567,7 @@
# try markupsafe first, if that fails go with Jinja2's bundled version
# of markupsafe. Markupsafe was previously Jinja2's implementation of
-# the Markup object but was moved into a separate package in a patchleve
+# the Markup object but was moved into a separate package in a patchlevel
# release
try:
from markupsafe import Markup, escape, soft_unicode
@@ -587,15 +575,4 @@
from jinja2._markupsafe import Markup, escape, soft_unicode
-# partials
-try:
- from functools import partial
-except ImportError:
- class partial(object):
- def __init__(self, _func, *args, **kwargs):
- self._func = _func
- self._args = args
- self._kwargs = kwargs
- def __call__(self, *args, **kwargs):
- kwargs.update(self._kwargs)
- return self._func(*(self._args + args), **kwargs)
+from functools import partial
diff --git a/setup.py b/setup.py
index 4147956..72c8c5b 100644
--- a/setup.py
+++ b/setup.py
@@ -37,24 +37,8 @@
"""
import sys
-from setuptools import setup, Extension, Feature
+from setuptools import setup
-debugsupport = Feature(
- 'optional C debug support',
- standard=False,
- ext_modules = [
- Extension('jinja2._debugsupport', ['jinja2/_debugsupport.c']),
- ],
-)
-
-
-# tell distribute to use 2to3 with our own fixers.
-extra = {}
-if sys.version_info >= (3, 0):
- extra.update(
- use_2to3=True,
- use_2to3_fixers=['custom_fixers']
- )
# ignore the old '--with-speedups' flag
try:
@@ -62,11 +46,10 @@
except ValueError:
pass
else:
- sys.argv[speedups_pos] = '--with-debugsupport'
+ sys.argv[speedups_pos] = ''
sys.stderr.write('*' * 74 + '\n')
sys.stderr.write('WARNING:\n')
- sys.stderr.write(' the --with-speedups flag is deprecated, assuming '
- '--with-debugsupport\n')
+ sys.stderr.write(' the --with-speedups flag is deprecated\n')
sys.stderr.write(' For the actual speedups install the MarkupSafe '
'package.\n')
sys.stderr.write('*' * 74 + '\n')
@@ -98,13 +81,12 @@
],
packages=['jinja2', 'jinja2.testsuite', 'jinja2.testsuite.res',
'jinja2._markupsafe'],
+ install_requires=['six>=1.3.0'],
extras_require={'i18n': ['Babel>=0.8']},
test_suite='jinja2.testsuite.suite',
include_package_data=True,
entry_points="""
[babel.extractors]
jinja2 = jinja2.ext:babel_extract[i18n]
- """,
- features={'debugsupport': debugsupport},
- **extra
+ """
)
diff --git a/tox.ini b/tox.ini
new file mode 100644
index 0000000..ffd4b87
--- /dev/null
+++ b/tox.ini
@@ -0,0 +1,5 @@
+[tox]
+envlist = py26, py27, pypy, py33
+
+[testenv]
+commands = python setup.py test