From 821a4232be53dd39714df74bbb956972f30436f9 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 17 Feb 2010 07:59:38 +0100 Subject: [PATCH] Fixed some tests for python 2.4. Disabled a test for 2.4 that does not work because of a python limitation. --HG-- branch : trunk --- jinja2/compiler.py | 17 +++++++++++++++-- jinja2/debug.py | 2 +- jinja2/testsuite/debug.py | 12 +++++++----- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/jinja2/compiler.py b/jinja2/compiler.py index efe534b..a52b1c7 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -44,6 +44,15 @@ else: 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): @@ -576,8 +585,10 @@ class CodeGenerator(NodeVisitor): # 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 @@ -739,6 +750,8 @@ class CodeGenerator(NodeVisitor): 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. diff --git a/jinja2/debug.py b/jinja2/debug.py index 08e828c..c2bd07b 100644 --- a/jinja2/debug.py +++ b/jinja2/debug.py @@ -113,7 +113,7 @@ def translate_syntax_error(error, source=None): """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 = '' diff --git a/jinja2/testsuite/debug.py b/jinja2/testsuite/debug.py index ee79498..a0d3ca9 100644 --- a/jinja2/testsuite/debug.py +++ b/jinja2/testsuite/debug.py @@ -8,6 +8,7 @@ :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 @@ -19,11 +20,12 @@ env = Environment(loader=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|) \{\{ fail\(\) \}\} File ".*?debug.pyc?", line \d+, in -- 2.26.2