Changeset 2819 for trunk

Show
Ignore:
Timestamp:
11/13/2008 10:22:06 PM (8 weeks ago)
Author:
guyer
Message:

faster implementation of --inline frame comment

Location:
trunk/fipy
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/fipy/tools/inline/inline.py

    r2817 r2819  
    99    else: 
    1010        return pythonFn(*args) 
    11                           
    12 def _frameComment(code, level=2): 
    13     frame = inspect.getouterframes(inspect.currentframe())[level] 
     11               
     12def _getframeinfo(level, context=1): 
     13    """ 
     14    Much faster alternative to `inspect.getouterframes(inspect.currentframe())[level]` 
     15    """ 
     16    frame = inspect.currentframe() 
     17    for l in range(level+1): 
     18        frame = frame.f_back 
     19         
     20    return (frame,) + inspect.getframeinfo(frame, context=context) 
    1421 
     22def _rawCodeComment(code, level=2): 
     23    finfo = _getframeinfo(level=level) 
     24     
    1525    # note:  
    1626    # don't use #line because it actually makes it harder  
     
    2434    %s  
    2535*/ 
    26 ''' % (frame[1], frame[2] - len(code.splitlines()), frame[3]) 
     36''' % (finfo[1], finfo[2] - len(code.splitlines()), finfo[3]) 
     37 
     38def _operatorVariableComment(canInline=True, level=3): 
     39    if canInline and inlineFlagOn: 
     40        finfo = _getframeinfo(level=level) 
     41 
     42        # note:  
     43        # don't use #line because it actually makes it harder  
     44        # to find the offending code in both the C++ source and in the Python 
     45        #line %d "%s" 
     46         
     47        if finfo[4] is not None: 
     48            code = "\n".join(finfo[4]) 
     49        else: 
     50            code = "" 
     51             
     52        return ''' 
     53/*  
     54%s:%d 
     55 
     56%s 
     57 */ 
     58        ''' % (finfo[1], finfo[2], code) 
     59    else: 
     60        return "" 
    2761 
    2862def _runInline(code_in, converters=None, verbose=0, comment=None, **args): 
     
    5387 
    5488    if comment is None: 
    55         comment = _frameComment(code_in) 
     89        comment = _rawCodeComment(code_in) 
    5690         
    5791    code = "\n" + comment + "\n" + code 
     
    104138 
    105139    if comment is None: 
    106         comment = _frameComment(code_in) 
     140        comment = _rawCodeComment(code_in) 
    107141 
    108142    code = "\n" + comment + "\n" + code 
  • trunk/fipy/variables/variable.py

    r2817 r2819  
    4242from fipy.tools import numerix 
    4343from fipy.tools import parser 
     44from fipy.tools.inline import inline 
    4445 
    4546class Variable(object): 
     
    804805        return operatorVariable._OperatorVariableClass(baseClass=baseClass) 
    805806             
    806     def _inlineComment(self, level=3): 
    807         frame = inspect.getouterframes(inspect.currentframe())[level] 
    808  
    809         # note:  
    810         # don't use #line because it actually makes it harder  
    811         # to find the offending code in both the C++ source and in the Python 
    812         #line %d "%s" 
    813          
    814         if frame[4] is not None: 
    815             code = "\n".join(frame[4]) 
    816         else: 
    817             code = "" 
    818              
    819         return ''' 
    820 /*  
    821     %s:%d 
    822      
    823     %s 
    824  */ 
    825         ''' % (frame[1], frame[2], code) 
    826           
    827  
    828807    def _UnaryOperatorVariable(self, op, operatorClass=None, opShape=None, canInline=True, unit=None): 
    829808        """ 
     
    849828            canInline = False 
    850829 
    851         var = unOp(op=op, var=[self], opShape=opShape, canInline=canInline, unit=unit, inlineComment=self._inlineComment()) 
    852 #         var.comment = self._frameComment(level=7) 
    853          
    854         return var 
     830        return unOp(op=op, var=[self], opShape=opShape, canInline=canInline, unit=unit,  
     831                    inlineComment=inline._operatorVariableComment(canInline=canInline)) 
    855832 
    856833    def _shapeClassAndOther(self, opShape, operatorClass, other): 
     
    898875        binOp = binaryOperatorVariable._BinaryOperatorVariable(operatorClass) 
    899876         
    900         var = binOp(op=op, var=[self, other], opShape=opShape, canInline=canInline, unit=unit, inlineComment=self._inlineComment()) 
    901 #         var.comment = self._frameComment(level=7) 
    902  
    903         return var 
     877        return binOp(op=op, var=[self, other], opShape=opShape, canInline=canInline, unit=unit,  
     878                     inlineComment=inline._operatorVariableComment(canInline=canInline)) 
    904879     
    905880    def __add__(self, other):