annotate markup/eval.py @ 87:c6f07b7cd3ea

Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
author cmlenz
date Mon, 17 Jul 2006 15:08:53 +0000
parents 5d98c4259d68
children 9ecae580dd93
rev   line source
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
2 #
66
822089ae65ce Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 42
diff changeset
3 # Copyright (C) 2006 Edgewall Software
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
4 # All rights reserved.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
5 #
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
66
822089ae65ce Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 42
diff changeset
8 # are also available at http://markup.edgewall.org/wiki/License.
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
9 #
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
66
822089ae65ce Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 42
diff changeset
12 # history and logs, available at http://markup.edgewall.org/log/.
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
13
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
14 """Support for "safe" evaluation of Python expressions."""
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
15
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
16 import __builtin__
87
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
17 from compiler import ast, parse
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
18 from compiler.pycodegen import ExpressionCodeGenerator
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
19
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
20 from markup.core import Stream
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
21
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
22 __all__ = ['Expression']
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
23
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
24
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
25 class Expression(object):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
26 """Evaluates Python expressions used in templates.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
27
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
28 >>> data = dict(test='Foo', items=[1, 2, 3], dict={'some': 'thing'})
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
29 >>> Expression('test').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
30 'Foo'
81
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
31
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
32 >>> Expression('items[0]').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
33 1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
34 >>> Expression('items[-1]').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
35 3
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
36 >>> Expression('dict["some"]').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
37 'thing'
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
38
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
39 Similar to e.g. Javascript, expressions in templates can use the dot
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
40 notation for attribute access to access items in mappings:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
41
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
42 >>> Expression('dict.some').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
43 'thing'
86
5d98c4259d68 Accidentially left some doctests disabled.
cmlenz
parents: 82
diff changeset
44
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
45 This also works the other way around: item access can be used to access
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
46 any object attribute (meaning there's no use for `getattr()` in templates):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
47
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
48 >>> class MyClass(object):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
49 ... myattr = 'Bar'
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
50 >>> data = dict(mine=MyClass(), key='myattr')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
51 >>> Expression('mine.myattr').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
52 'Bar'
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
53 >>> Expression('mine["myattr"]').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
54 'Bar'
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
55 >>> Expression('mine[key]').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
56 'Bar'
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
57
31
9a958398bed9 * More test cases for expression evaluation.
cmlenz
parents: 30
diff changeset
58 All of the standard Python operators are available to template expressions.
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
59 Built-in functions such as `len()` are also available in template
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
60 expressions:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
61
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
62 >>> data = dict(items=[1, 2, 3])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
63 >>> Expression('len(items)').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
64 3
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
65 """
81
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
66 __slots__ = ['source', 'code']
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
67
81
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
68 def __init__(self, source, filename=None, lineno=-1):
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
69 """Create the expression.
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
70
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
71 @param source: the expression as string
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
72 """
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
73 self.source = source
87
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
74 self.code = self._compile(source, filename, lineno)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
75
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
76 def __repr__(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
77 return '<Expression "%s">' % self.source
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
78
81
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
79 def evaluate(self, data):
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
80 """Evaluate the expression against the given data dictionary.
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
81
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
82 @param data: a mapping containing the data to evaluate against
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
83 @return: the result of the evaluation
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
84 """
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
85 return eval(self.code)
30
2ee9f28e16e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
86
87
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
87 def _compile(self, source, filename, lineno):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
88 tree = parse(self.source, 'eval')
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
89 xform = ExpressionASTTransformer()
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
90 tree = xform.visit(tree)
30
2ee9f28e16e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
91
87
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
92 if isinstance(filename, unicode):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
93 # pycodegen doesn't like unicode in the filename
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
94 filename = filename.encode('utf-8', 'replace')
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
95 tree.filename = filename or '<string>'
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
96
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
97 gen = ExpressionCodeGenerator(tree)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
98 if lineno >= 0:
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
99 gen.emit('SET_LINENO', lineno)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
100
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
101 return gen.getCode()
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
102
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
103 def _lookup_name(self, data, name):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
104 val = data.get(name)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
105 if val is None:
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
106 val = getattr(__builtin__, name, None)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
107 return val
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
108
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
109 def _lookup_attribute(self, data, obj, key):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
110 if hasattr(obj, key):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
111 return getattr(obj, key)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
112 try:
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
113 return obj[key]
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
114 except (KeyError, TypeError):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
115 return None
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
116
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
117 def _lookup_item(self, data, obj, key):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
118 if len(key) == 1:
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
119 key = key[0]
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
120 try:
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
121 return obj[key]
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
122 except (KeyError, IndexError, TypeError), e:
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
123 pass
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
124 if isinstance(key, basestring):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
125 try:
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
126 return getattr(obj, key)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
127 except (AttributeError, TypeError), e:
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
128 pass
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
129
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
130
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
131 class ASTTransformer(object):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
132 """General purpose base class for AST transformations.
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
133
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
134 Every visitor method can be overridden to return an AST node that has been
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
135 altered or replaced in some way.
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
136 """
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
137 _visitors = {}
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
138
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
139 def visit(self, node):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
140 v = self._visitors.get(node.__class__)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
141 if not v:
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
142 v = getattr(self, 'visit%s' % node.__class__.__name__)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
143 self._visitors[node.__class__] = v
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
144 return v(node)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
145
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
146 def visitExpression(self, node):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
147 node.node = self.visit(node.node)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
148 return node
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
149
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
150 # Functions & Accessors
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
151
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
152 def visitCallFunc(self, node):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
153 node.node = self.visit(node.node)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
154 node.args = map(self.visit, node.args)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
155 if node.star_args:
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
156 node.star_args = map(self.visit, node.star_args)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
157 if node.dstar_args:
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
158 node.dstart_args = map(self.visit, node.dstar_args)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
159 return node
30
2ee9f28e16e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
160
81
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
161 def visitGetattr(self, node):
87
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
162 node.expr = self.visit(node.expr)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
163 return node
30
2ee9f28e16e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
164
87
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
165 def visitSubscript(self, node):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
166 node.expr = self.visit(node.expr)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
167 node.subs = map(self.visit, node.subs)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
168 return node
30
2ee9f28e16e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
169
87
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
170 # Operators
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
171
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
172 def _visitBoolOp(self, node):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
173 node.nodes = map(self.visit, node.nodes)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
174 return node
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
175 visitAnd = visitOr = visitBitand = visitBitor = _visitBoolOp
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
176
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
177 def _visitBinOp(self, node):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
178 node.left = self.visit(node.left)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
179 node.right = self.visit(node.right)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
180 return node
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
181 visitAdd = visitSub = _visitBinOp
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
182 visitDiv = visitFloorDiv = visitMod = visitMul = visitPower = _visitBinOp
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
183 visitLeftShift = visitRightShift = _visitBinOp
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
184
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
185 def visitCompare(self, node):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
186 node.expr = self.visit(node.expr)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
187 node.ops = map(lambda (op, expr): (op, self.visit(expr)),
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
188 node.ops)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
189 return node
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
190
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
191 def _visitUnaryOp(self, node):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
192 node.expr = self.visit(node.expr)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
193 return node
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
194 visitUnaryAdd = visitUnarySub = visitNot = visitInvert = _visitUnaryOp
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
195
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
196 # Identifiers & Literals
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
197
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
198 def _visitDefault(self, node):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
199 return node
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
200 visitConst = visitKeyword = visitName = _visitDefault
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
201
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
202 def visitDict(self, node):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
203 node.items = map(lambda (k, v): (self.visit(k), self.visit(v)),
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
204 node.items)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
205 return node
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
206
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
207 def visitTuple(self, node):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
208 node.nodes = map(lambda n: self.visit(n), node.nodes)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
209 return node
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
210
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
211 def visitList(self, node):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
212 node.nodes = map(lambda n: self.visit(n), node.nodes)
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
213 return node
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
214
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
215
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
216 class ExpressionASTTransformer(ASTTransformer):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
217 """Concrete AST transformer that implementations the AST transformations
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
218 needed for template expressions.
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
219 """
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
220
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
221 def visitGetattr(self, node):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
222 return ast.CallFunc(
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
223 ast.Getattr(ast.Name('self'), '_lookup_attribute'),
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
224 [ast.Name('data'), self.visit(node.expr), ast.Const(node.attrname)]
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
225 )
30
2ee9f28e16e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
226
81
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
227 def visitName(self, node):
87
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
228 return ast.CallFunc(
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
229 ast.Getattr(ast.Name('self'), '_lookup_name'),
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
230 [ast.Name('data'), ast.Const(node.name)]
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
231 )
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
232 return node
81
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
233
87
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
234 def visitSubscript(self, node):
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
235 return ast.CallFunc(
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
236 ast.Getattr(ast.Name('self'), '_lookup_item'),
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
237 [ast.Name('data'), self.visit(node.expr),
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
238 ast.Tuple(map(self.visit, node.subs))]
c6f07b7cd3ea Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents: 86
diff changeset
239 )
Copyright (C) 2012-2017 Edgewall Software