From: Armin Ronacher Date: Wed, 10 Feb 2010 00:35:13 +0000 (+0100) Subject: Down to 7 failures for Python 3. We're onto something. X-Git-Tag: 2.3~7 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=0d242be185f34fcc3b4466545393e9ccbc86d199;p=jinja2.git Down to 7 failures for Python 3. We're onto something. --HG-- branch : trunk --- diff --git a/custom_fixers/fix_broken_reraising.py b/custom_fixers/fix_broken_reraising.py new file mode 100644 index 0000000..fd0ea68 --- /dev/null +++ b/custom_fixers/fix_broken_reraising.py @@ -0,0 +1,21 @@ +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/jinja2/compiler.py b/jinja2/compiler.py index 672d696..efe534b 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -36,6 +36,14 @@ 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'): + dict_item_iter = 'iteritems' +else: + dict_item_iter = 'items' + + def generate(node, environment, name, filename, stream=None): """Generate the python source for a node tree.""" if not isinstance(node, nodes.Template): @@ -867,7 +875,7 @@ class CodeGenerator(NodeVisitor): self.visit(node.template, frame) self.write(', %r)' % self.name) self.writeline('for name, parent_block in parent_template.' - 'blocks.iteritems():') + 'blocks.%s():' % dict_item_iter) self.indent() self.writeline('context.blocks.setdefault(name, []).' 'append(parent_block)') diff --git a/jinja2/environment.py b/jinja2/environment.py index fe7ed02..e3c64e3 100644 --- a/jinja2/environment.py +++ b/jinja2/environment.py @@ -19,7 +19,7 @@ from jinja2.runtime import Undefined, new_context from jinja2.exceptions import TemplateSyntaxError, TemplateNotFound, \ TemplatesNotFound from jinja2.utils import import_string, LRUCache, Markup, missing, \ - concat, consume, internalcode + concat, consume, internalcode, _encode_filename # for direct template usage we have up to ten living environments @@ -375,9 +375,7 @@ class Environment(object): def _parse(self, source, name, filename): """Internal parsing function used by `parse` and `compile`.""" - if isinstance(filename, unicode): - filename = filename.encode('utf-8') - return Parser(self, source, name, filename).parse() + return Parser(self, source, name, _encode_filename(filename)).parse() def lex(self, source, name=None, filename=None): """Lex the given sourcecode and return a generator that yields @@ -442,8 +440,8 @@ class Environment(object): return source if filename is None: filename = '