dict_item_iter = 'items'
+# does if 0: dummy(x) get us x into the scope?
+def unoptimize_before_dead_code():
+ x = 42
+ def f():
+ if 0: dummy(x)
+ return f
+unoptimize_before_dead_code = bool(unoptimize_before_dead_code().func_closure)
+
+
def generate(node, environment, name, filename, stream=None):
"""Generate the python source for a node tree."""
if not isinstance(node, nodes.Template):
# is removed. If that breaks we have to add a dummy function
# that just accepts the arguments and does nothing.
if frame.identifiers.declared:
- self.writeline('if 0: dummy(%s)' % ', '.join(
- 'l_' + name for name in frame.identifiers.declared))
+ self.writeline('%sdummy(%s)' % (
+ unoptimize_before_dead_code and 'if 0: ' or '',
+ ', '.join('l_' + name for name in frame.identifiers.declared)
+ ))
def push_scope(self, frame, extra_vars=()):
"""This function returns all the shadowed variables in a dict
from jinja2.runtime import __all__ as exported
self.writeline('from __future__ import division')
self.writeline('from jinja2.runtime import ' + ', '.join(exported))
+ if not unoptimize_before_dead_code:
+ self.writeline('dummy = lambda *x: None')
# do we have an extends tag at all? If not, we can save some
# overhead by just not processing any inheritance code.
"""Rewrites a syntax error to please traceback systems."""
error.source = source
error.translated = True
- exc_info = (type(error), error, None)
+ exc_info = (error.__class__, error, None)
filename = error.filename
if filename is None:
filename = '<unknown>'
: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
class DebugTestCase(JinjaTestCase):
- 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'''
+ 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'''
File ".*?broken.html", line 2, in (top-level template code|<module>)
\{\{ fail\(\) \}\}
File ".*?debug.pyc?", line \d+, in <lambda>