Edgewall Software

Ticket #190: no__data__.diff

File no__data__.diff, 3.5 kB (added by cmlenz, 6 months ago)

Another workaround, cleaner but as of yet incomplete

  • genshi/template/eval.py

     
    139139        :return: the result of the evaluation 
    140140        """ 
    141141        __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) 
    145144 
    146145 
    147146class Suite(Code): 
     
    161160        :param data: a mapping containing the data to execute in 
    162161        """ 
    163162        __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 
    167164 
    168165 
    169166UNDEFINED = object() 
     
    248245class LookupBase(object): 
    249246    """Abstract base class for variable lookup implementations.""" 
    250247 
    251     def globals(cls): 
     248    def globals(cls, data): 
    252249        """Construct the globals dictionary to use as the execution context for 
    253250        the expression or suite. 
    254251        """ 
    255252        return { 
    256             '_lookup_name': cls.lookup_name, 
     253            '_lookup_name': cls.lookup_name(data), 
    257254            '_lookup_attr': cls.lookup_attr, 
    258255            '_lookup_item': cls.lookup_item, 
    259256            'UndefinedError': UndefinedError 
    260257        } 
    261258    globals = classmethod(globals) 
    262259 
    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) 
    268264            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 
    271270    lookup_name = classmethod(lookup_name) 
    272271 
    273     def lookup_attr(cls, data, obj, key): 
     272    def lookup_attr(cls, obj, key): 
    274273        __traceback_hide__ = True 
    275274        val = getattr(obj, key, UNDEFINED) 
    276275        if val is UNDEFINED: 
     
    281280        return val 
    282281    lookup_attr = classmethod(lookup_attr) 
    283282 
    284     def lookup_item(cls, data, obj, key): 
     283    def lookup_item(cls, obj, key): 
    285284        __traceback_hide__ = True 
    286285        if len(key) == 1: 
    287286            key = key[0] 
     
    742741        # generator expression, leave it alone 
    743742        if node.name not in flatten(self.locals): 
    744743            # 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)] 
    746745            node = ast.CallFunc(ast.Name('_lookup_name'), func_args) 
    747746        return node 
    748747 
     
    754753 
    755754    def visitGetattr(self, node): 
    756755        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) 
    759757        ]) 
    760758 
    761759    def visitSubscript(self, node): 
    762760        return ast.CallFunc(ast.Name('_lookup_item'), [ 
    763             ast.Name('__data__'), self.visit(node.expr), 
     761            self.visit(node.expr), 
    764762            ast.Tuple([self.visit(sub) for sub in node.subs]) 
    765763        ])