annotate markup/eval.py @ 131:203f459e7e26

* Support for line numbers in exceptions in expression evaluation (#22). * Fix bug in expression evaluation when item access was used inside a lambda or list comprehension. Thanks to Kevin Dangoor for reporting the problem.
author cmlenz
date Fri, 04 Aug 2006 13:07:52 +0000
parents eb6cead67474
children 0288dedce3d2
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
131
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
19 import new
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
20
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
21 from markup.core import Stream
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
22
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
23 __all__ = ['Expression']
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
24
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
25
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
26 class Expression(object):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
27 """Evaluates Python expressions used in templates.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
28
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
29 >>> data = dict(test='Foo', items=[1, 2, 3], dict={'some': 'thing'})
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
30 >>> Expression('test').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
31 'Foo'
81
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
32
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
33 >>> Expression('items[0]').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
34 1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
35 >>> Expression('items[-1]').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
36 3
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
37 >>> Expression('dict["some"]').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
38 'thing'
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
39
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
40 Similar to e.g. Javascript, expressions in templates can use the dot
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
41 notation for attribute access to access items in mappings:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
42
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
43 >>> Expression('dict.some').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
44 'thing'
86
5d98c4259d68 Accidentially left some doctests disabled.
cmlenz
parents: 82
diff changeset
45
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
46 This also works the other way around: item access can be used to access
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
47 any object attribute (meaning there's no use for `getattr()` in templates):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
48
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
49 >>> class MyClass(object):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
50 ... myattr = 'Bar'
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
51 >>> data = dict(mine=MyClass(), key='myattr')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
52 >>> Expression('mine.myattr').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
53 'Bar'
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
54 >>> Expression('mine["myattr"]').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
55 'Bar'
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
56 >>> Expression('mine[key]').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
57 'Bar'
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
58
31
9a958398bed9 * More test cases for expression evaluation.
cmlenz
parents: 30
diff changeset
59 All of the standard Python operators are available to template expressions.
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
60 Built-in functions such as `len()` are also available in template
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
61 expressions:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
62
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
63 >>> data = dict(items=[1, 2, 3])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
64 >>> Expression('len(items)').evaluate(data)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
65 3
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
66 """
81
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
67 __slots__ = ['source', 'code']
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
68
81
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
69 def __init__(self, source, filename=None, lineno=-1):
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
70 """Create the expression.
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
71
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
72 @param source: the expression as string
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 16
diff changeset
73 """
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
74 self.source = source
116
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
75 self.code = _compile(source, filename, lineno)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
76
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
77 def __repr__(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
78 return '<Expression "%s">' % self.source
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
79
120
eb6cead67474 * Allow `py:with` directives to define `lambda`s
cmlenz
parents: 118
diff changeset
80 def evaluate(self, data, nocall=False):
81
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
81 """Evaluate the expression against the given data dictionary.
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
82
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
83 @param data: a mapping containing the data to evaluate against
120
eb6cead67474 * Allow `py:with` directives to define `lambda`s
cmlenz
parents: 118
diff changeset
84 @param nocall: if true, the result of the evaluation is not called if
eb6cead67474 * Allow `py:with` directives to define `lambda`s
cmlenz
parents: 118
diff changeset
85 if it is a callable
81
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
86 @return: the result of the evaluation
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
87 """
118
226613431921 Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents: 116
diff changeset
88 retval = eval(self.code, {'data': data,
226613431921 Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents: 116
diff changeset
89 '_lookup_name': _lookup_name,
226613431921 Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents: 116
diff changeset
90 '_lookup_attr': _lookup_attr,
226613431921 Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents: 116
diff changeset
91 '_lookup_item': _lookup_item})
120
eb6cead67474 * Allow `py:with` directives to define `lambda`s
cmlenz
parents: 118
diff changeset
92 if not nocall and callable(retval):
90
242610137d1f When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents: 88
diff changeset
93 retval = retval()
242610137d1f When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents: 88
diff changeset
94 return retval
30
2ee9f28e16e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
95
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
96
116
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
97 def _compile(source, filename=None, lineno=-1):
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
98 tree = parse(source, 'eval')
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
99 xform = ExpressionASTTransformer()
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
100 tree = xform.visit(tree)
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
101
116
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
102 if isinstance(filename, unicode):
131
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
103 # unicode file names not allowed for code objects
116
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
104 filename = filename.encode('utf-8', 'replace')
131
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
105 elif not filename:
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
106 filename = '<string>'
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
107 tree.filename = '<string>'
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
108 if lineno <= 0:
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
109 lineno = 1
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
110
116
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
111 gen = ExpressionCodeGenerator(tree)
131
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
112 gen.optimized = True
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
113 code = gen.getCode()
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
114
131
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
115 # We'd like to just set co_firstlineno, but it's readonly. So we need to
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
116 # clone the code object while adjusting the line number
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
117 return new.code(0, code.co_nlocals, code.co_stacksize,
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
118 code.co_flags | 0x0040, code.co_code, code.co_consts,
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
119 code.co_names, code.co_varnames, filename, code.co_name,
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
120 lineno, code.co_lnotab, (), ())
116
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
121
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
122 def _lookup_name(data, name, locals=None):
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
123 val = data.get(name)
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
124 if val is None and locals:
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
125 val = locals.get(name)
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
126 if val is None:
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
127 val = getattr(__builtin__, name, None)
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
128 return val
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
129
118
226613431921 Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents: 116
diff changeset
130 def _lookup_attr(data, obj, key):
116
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
131 if hasattr(obj, key):
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
132 return getattr(obj, key)
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
133 try:
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
134 return obj[key]
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
135 except (KeyError, TypeError):
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
136 return None
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
137
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
138 def _lookup_item(data, obj, key):
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
139 if len(key) == 1:
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
140 key = key[0]
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
141 try:
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
142 return obj[key]
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
143 except (KeyError, IndexError, TypeError), e:
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
144 pass
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
145 if isinstance(key, basestring):
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
146 try:
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
147 return getattr(obj, key)
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
148 except (AttributeError, TypeError), e:
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
149 pass
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
150
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 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
152 """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
153
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 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
155 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
156 """
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 _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
158
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
159 def visit(self, node, *args, **kwargs):
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
160 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
161 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
162 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
163 self._visitors[node.__class__] = v
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
164 return v(node, *args, **kwargs)
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
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
166 def visitExpression(self, node, *args, **kwargs):
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
167 node.node = self.visit(node.node, *args, **kwargs)
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
168 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
169
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 # 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
171
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
172 def visitCallFunc(self, node, *args, **kwargs):
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
173 node.node = self.visit(node.node, *args, **kwargs)
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
174 node.args = map(lambda x: self.visit(x, *args, **kwargs), node.args)
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
175 if node.star_args:
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
176 node.star_args = map(lambda x: self.visit(x, *args, **kwargs),
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
177 node.star_args)
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
178 if node.dstar_args:
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
179 node.dstart_args = map(lambda x: self.visit(x, *args, **kwargs),
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
180 node.dstar_args)
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
181 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
182
118
226613431921 Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents: 116
diff changeset
183 def visitLambda(self, node, *args, **kwargs):
226613431921 Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents: 116
diff changeset
184 node.code = self.visit(node.code, *args, **kwargs)
226613431921 Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents: 116
diff changeset
185 node.filename = '<string>' # workaround for bug in pycodegen
226613431921 Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents: 116
diff changeset
186 return node
226613431921 Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents: 116
diff changeset
187
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
188 def visitGetattr(self, node, *args, **kwargs):
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
189 node.expr = self.visit(node.expr, *args, **kwargs)
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
190 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
191
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
192 def visitSubscript(self, node, *args, **kwargs):
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
193 node.expr = self.visit(node.expr, *args, **kwargs)
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
194 node.subs = map(lambda x: self.visit(x, *args, **kwargs), node.subs)
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
195 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
196
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
197 # 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
198
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
199 def _visitBoolOp(self, node, *args, **kwargs):
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
200 node.nodes = map(lambda x: self.visit(x, *args, **kwargs), node.nodes)
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
201 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
202 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
203
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
204 def _visitBinOp(self, node, *args, **kwargs):
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
205 node.left = self.visit(node.left, *args, **kwargs)
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
206 node.right = self.visit(node.right, *args, **kwargs)
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
207 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
208 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
209 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
210 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
211
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
212 def visitCompare(self, node, *args, **kwargs):
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
213 node.expr = self.visit(node.expr, *args, **kwargs)
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
214 node.ops = map(lambda (op, n): (op, self.visit(n, *args, **kwargs)),
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
215 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
216 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
217
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
218 def _visitUnaryOp(self, node, *args, **kwargs):
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
219 node.expr = self.visit(node.expr, *args, **kwargs)
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
220 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
221 visitUnaryAdd = visitUnarySub = visitNot = visitInvert = _visitUnaryOp
98
bc73d3ab823f Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 90
diff changeset
222 visitBackquote = _visitUnaryOp
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
223
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
224 # Identifiers, Literals and Comprehensions
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
225
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
226 def _visitDefault(self, node, *args, **kwargs):
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
227 return node
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
228 visitAssName = visitAssTuple = _visitDefault
102
12efdfe8af47 Ported [118] to trunk
jonas
parents: 101
diff changeset
229 visitConst = visitName = _visitDefault
12efdfe8af47 Ported [118] to trunk
jonas
parents: 101
diff changeset
230
12efdfe8af47 Ported [118] to trunk
jonas
parents: 101
diff changeset
231 def visitKeyword(self, node, *args, **kwargs):
12efdfe8af47 Ported [118] to trunk
jonas
parents: 101
diff changeset
232 node.expr = self.visit(node.expr, *args, **kwargs)
12efdfe8af47 Ported [118] to trunk
jonas
parents: 101
diff changeset
233 return 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
234
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
235 def visitDict(self, node, *args, **kwargs):
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
236 node.items = map(lambda (k, v): (self.visit(k, *args, **kwargs),
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
237 self.visit(v, *args, **kwargs)),
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
238 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
239 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
240
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
241 def visitTuple(self, node, *args, **kwargs):
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
242 node.nodes = map(lambda n: self.visit(n, *args, **kwargs), node.nodes)
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
243 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
244
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
245 def visitList(self, node, *args, **kwargs):
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
246 node.nodes = map(lambda n: self.visit(n, *args, **kwargs), node.nodes)
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
247 return node
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
248
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
249 def visitListComp(self, node, *args, **kwargs):
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
250 node.expr = self.visit(node.expr, *args, **kwargs)
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
251 node.quals = map(lambda x: self.visit(x, *args, **kwargs), node.quals)
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
252 return node
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
253
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
254 def visitListCompFor(self, node, *args, **kwargs):
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
255 node.assign = self.visit(node.assign, *args, **kwargs)
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
256 node.list = self.visit(node.list, *args, **kwargs)
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
257 node.ifs = map(lambda x: self.visit(x, *args, **kwargs), node.ifs)
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
258 return node
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
259
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
260 def visitListCompIf(self, node, *args, **kwargs):
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
261 node.test = self.visit(node.test, *args, **kwargs)
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
262 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
263
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
264
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
265 class ExpressionASTTransformer(ASTTransformer):
112
a834a6669681 Docstring typo fix.
cmlenz
parents: 102
diff changeset
266 """Concrete AST transformer that implements the AST transformations needed
a834a6669681 Docstring typo fix.
cmlenz
parents: 102
diff changeset
267 for template expressions.
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
268 """
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
269
131
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
270 def visitGetattr(self, node, locals_=False):
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
271 return ast.CallFunc(ast.Name('_lookup_attr'), [
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
272 ast.Name('data'), self.visit(node.expr, locals_=locals_),
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
273 ast.Const(node.attrname)
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
274 ])
30
2ee9f28e16e5 Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents: 27
diff changeset
275
131
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
276 def visitLambda(self, node, locals_=False):
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
277 node.code = self.visit(node.code, locals_=True)
118
226613431921 Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents: 116
diff changeset
278 node.filename = '<string>' # workaround for bug in pycodegen
226613431921 Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents: 116
diff changeset
279 return node
226613431921 Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents: 116
diff changeset
280
131
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
281 def visitListComp(self, node, locals_=False):
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
282 node.expr = self.visit(node.expr, locals_=True)
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
283 node.quals = map(lambda x: self.visit(x, locals_=True), node.quals)
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
284 return node
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
285
131
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
286 def visitName(self, node, locals_=False):
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
287 func_args = [ast.Name('data'), ast.Const(node.name)]
131
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
288 if locals_:
88
9ecae580dd93 Add support for list comprehension in expressions (see #12).
cmlenz
parents: 87
diff changeset
289 func_args.append(ast.CallFunc(ast.Name('locals'), []))
116
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 112
diff changeset
290 return ast.CallFunc(ast.Name('_lookup_name'), func_args)
81
cc034182061e Template expressions are now compiled to Python bytecode.
cmlenz
parents: 69
diff changeset
291
131
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
292 def visitSubscript(self, node, locals_=False):
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
293 return ast.CallFunc(ast.Name('_lookup_item'), [
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
294 ast.Name('data'), self.visit(node.expr, locals_=locals_),
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
295 ast.Tuple(map(lambda x: self.visit(x, locals_=locals_), node.subs))
203f459e7e26 * Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents: 120
diff changeset
296 ])
Copyright (C) 2012-2017 Edgewall Software