From 67fdddf4f26a0c0420c3002f097a36f038971feb Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Fri, 16 May 2008 09:27:51 +0200 Subject: [PATCH] removed attribute local aliasing again because that implementation was broken --HG-- branch : trunk --- docs/extensions.rst | 6 +++- jinja2/compiler.py | 67 ------------------------------------------- jinja2/environment.py | 3 ++ jinja2/parser.py | 2 -- 4 files changed, 8 insertions(+), 70 deletions(-) diff --git a/docs/extensions.rst b/docs/extensions.rst index fd71922..9d9bea0 100644 --- a/docs/extensions.rst +++ b/docs/extensions.rst @@ -172,10 +172,14 @@ extensions: .. attribute:: filename The filename of the template the parser processes. This is **not** - the load name of the template which is unavailable at parsing time. + the load name of the template. For the load name see :attr:`name`. For templates that were not loaded form the file system this is `None`. + .. attribute:: name + + The load name of the template. + .. attribute:: stream The current :class:`~jinja2.lexer.TokenStream` diff --git a/jinja2/compiler.py b/jinja2/compiler.py index 52a85cd..114ca2d 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -107,9 +107,6 @@ class Identifiers(object): # names that are declared by parameters self.declared_parameter = set() - # static subscribes - self.static_subscribes = {} - def add_special(self, name): """Register a special name like `loop`.""" self.undeclared.discard(name) @@ -152,9 +149,6 @@ class Frame(object): # the name of the block we're in, otherwise None. self.block = parent and parent.block or None - # node overlays. see `CodeGenerator.overlay` for more details - self.overlays = {} - # the parent of this frame self.parent = parent @@ -262,27 +256,6 @@ class FrameIdentifierVisitor(NodeVisitor): self.identifiers.is_declared(node.name, self.hard_scope): self.identifiers.undeclared.add(node.name) - def visit_Subscript(self, node): - """Under some circumstances subscripts are aliased with local names: - - - the subscript node is either already aliased or a name that was not - reassigned. - - and the subscription argument is a constant string. - """ - self.generic_visit(node) - if isinstance(node.arg, nodes.Const) and \ - isinstance(node.arg.value, basestring) and \ - ((isinstance(node.node, nodes.Name) and - # this code ignores parameter declared names as the may only - # occour at the very beginning of a scope and we pull the - # attributes afterwards. - node.node.name not in (self.identifiers.declared_locally)) or - node.node in self.identifiers.static_subscribes): - if node in self.identifiers.static_subscribes: - self.identifiers.static_subscribes[node] += 1 - else: - self.identifiers.static_subscribes[node] = 1 - def visit_Macro(self, node): self.identifiers.declared_locally.add(node.name) @@ -497,47 +470,11 @@ class CodeGenerator(NodeVisitor): self.write('**') self.visit(node.dyn_kwargs, frame) - def overlay(self, node, frame): - """Visit an overlay and return `True` or `False` if the overlay - does not exist or is exhausted. An overlay is used to replace a - node with another one (or an identifier) N times. This is for - example used to replace static subscribes before reassignments - of a name that occour more than one time. If a node has overlays - it's important that this method is called, otherwise the count - will be out of sync and the generated code is broken. - """ - if node not in frame.overlays: - return False - overlay, count = frame.overlays[node] - if count is not None and count <= 0: - return False - if isinstance(overlay, basestring): - self.write(overlay) - else: - self.visit(overlay, frame) - frame.overlays[node] = (overlay, count - 1) - return True - def pull_locals(self, frame): """Pull all the references identifiers into the local scope.""" for name in frame.identifiers.undeclared: self.writeline('l_%s = context.resolve(%r)' % (name, name)) - # find all the static subscribes with a count > 2 and - # order them properly so that subscribes depending on other - # subscribes are handled later. - static_subscribes = [(node, count) for node, count in - frame.identifiers.static_subscribes.items() if - count >= 2] - if static_subscribes: - static_subscribes.sort(key=lambda x: type(x[0].node) - is nodes.Subscript) - for node, count in static_subscribes: - ident = self.temporary_identifier() - self.writeline(ident + ' = ', node) - self.visit(node, frame) - frame.overlays[node] = (ident, count) - def pull_dependencies(self, nodes): """Pull all the dependencies.""" visitor = DependencyFinderVisitor() @@ -1350,10 +1287,6 @@ class CodeGenerator(NodeVisitor): self.visit(node.expr, frame) def visit_Subscript(self, node, frame): - # subscripts can have overlays - if self.overlay(node, frame): - return - # slices or integer subscriptions bypass the subscribe # method if we can determine that at compile time. if isinstance(node.arg, nodes.Slice) or \ diff --git a/jinja2/environment.py b/jinja2/environment.py index 3f6855a..41b0ce7 100644 --- a/jinja2/environment.py +++ b/jinja2/environment.py @@ -297,6 +297,8 @@ class Environment(object): If you are :ref:`developing Jinja2 extensions ` this gives you a good overview of the node tree generated. """ + if isinstance(filename, unicode): + filename = filename.encode('utf-8') try: return Parser(self, source, name, filename).parse() except TemplateSyntaxError, e: @@ -330,6 +332,7 @@ class Environment(object): if self.optimized: node = optimize(source, self) source = generate(node, self, name, filename) + #print source if raw: return source if filename is None: diff --git a/jinja2/parser.py b/jinja2/parser.py index 70c1cdc..a4ce491 100644 --- a/jinja2/parser.py +++ b/jinja2/parser.py @@ -25,8 +25,6 @@ class Parser(object): def __init__(self, environment, source, name=None, filename=None): self.environment = environment - if isinstance(filename, unicode): - filename = filename.encode('utf-8') self.source = unicode(source) self.name = name self.filename = filename -- 2.26.2