Ticket #190: no__data__.diff
| File no__data__.diff, 3.5 kB (added by cmlenz, 6 months ago) |
|---|
-
genshi/template/eval.py
139 139 :return: the result of the evaluation 140 140 """ 141 141 __traceback_hide__ = 'before_and_this' 142 _globals = self._globals() 143 _globals['__data__'] = data 144 return eval(self.code, _globals, {'__data__': data}) 142 _globals = self._globals(data) 143 return eval(self.code, _globals, data) 145 144 146 145 147 146 class Suite(Code): … … 161 160 :param data: a mapping containing the data to execute in 162 161 """ 163 162 __traceback_hide__ = 'before_and_this' 164 _globals = self._globals() 165 _globals['__data__'] = data 166 exec self.code in _globals, data 163 exec self.code in self._globals(data), data 167 164 168 165 169 166 UNDEFINED = object() … … 248 245 class LookupBase(object): 249 246 """Abstract base class for variable lookup implementations.""" 250 247 251 def globals(cls ):248 def globals(cls, data): 252 249 """Construct the globals dictionary to use as the execution context for 253 250 the expression or suite. 254 251 """ 255 252 return { 256 '_lookup_name': cls.lookup_name ,253 '_lookup_name': cls.lookup_name(data), 257 254 '_lookup_attr': cls.lookup_attr, 258 255 '_lookup_item': cls.lookup_item, 259 256 'UndefinedError': UndefinedError 260 257 } 261 258 globals = classmethod(globals) 262 259 263 def lookup_name(cls, data, name): 264 __traceback_hide__ = True 265 val = data.get(name, UNDEFINED) 266 if val is UNDEFINED: 267 val = BUILTINS.get(name, val) 260 def lookup_name(cls, data): 261 def _lookup(name): 262 __traceback_hide__ = True 263 val = data.get(name, UNDEFINED) 268 264 if val is UNDEFINED: 269 val = cls.undefined(name) 270 return val 265 val = BUILTINS.get(name, val) 266 if val is UNDEFINED: 267 val = cls.undefined(name) 268 return val 269 return _lookup 271 270 lookup_name = classmethod(lookup_name) 272 271 273 def lookup_attr(cls, data,obj, key):272 def lookup_attr(cls, obj, key): 274 273 __traceback_hide__ = True 275 274 val = getattr(obj, key, UNDEFINED) 276 275 if val is UNDEFINED: … … 281 280 return val 282 281 lookup_attr = classmethod(lookup_attr) 283 282 284 def lookup_item(cls, data,obj, key):283 def lookup_item(cls, obj, key): 285 284 __traceback_hide__ = True 286 285 if len(key) == 1: 287 286 key = key[0] … … 742 741 # generator expression, leave it alone 743 742 if node.name not in flatten(self.locals): 744 743 # Otherwise, translate the name ref into a context lookup 745 func_args = [ast. Name('__data__'), ast.Const(node.name)]744 func_args = [ast.Const(node.name)] 746 745 node = ast.CallFunc(ast.Name('_lookup_name'), func_args) 747 746 return node 748 747 … … 754 753 755 754 def visitGetattr(self, node): 756 755 return ast.CallFunc(ast.Name('_lookup_attr'), [ 757 ast.Name('__data__'), self.visit(node.expr), 758 ast.Const(node.attrname) 756 self.visit(node.expr), ast.Const(node.attrname) 759 757 ]) 760 758 761 759 def visitSubscript(self, node): 762 760 return ast.CallFunc(ast.Name('_lookup_item'), [ 763 ast.Name('__data__'),self.visit(node.expr),761 self.visit(node.expr), 764 762 ast.Tuple([self.visit(sub) for sub in node.subs]) 765 763 ])
