Changeset 2817 for trunk

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

Add comment to --inline to indicate what Python code or binOp generated it

Location:
trunk/fipy
Files:
3 modified

Legend:

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

    r2611 r2817  
    1 import sys 
     1import inspect 
     2 
    23from fipy.tools import numerix 
    34from fipy.tools.inline import inlineFlagOn 
     
    910        return pythonFn(*args) 
    1011                          
    11 def _runInline(code_in, converters=None, verbose=0, **args): 
     12def _frameComment(code, level=2): 
     13    frame = inspect.getouterframes(inspect.currentframe())[level] 
     14 
     15    # note:  
     16    # don't use #line because it actually makes it harder  
     17    # to find the offending code in both the C++ source and in the Python 
     18    #line %d "%s" 
     19     
     20    return ''' 
     21/*  
     22    %s:%d 
     23 
     24    %s  
     25*/ 
     26''' % (frame[1], frame[2] - len(code.splitlines()), frame[3]) 
     27 
     28def _runInline(code_in, converters=None, verbose=0, comment=None, **args): 
    1229    argsKeys = args.keys() 
    1330    dimList = ['i', 'j', 'k'] 
     
    3552        code = 'int ' + ','.join(declarations) + ';\n' + loops + "\t" * dimensions + code_in + enders 
    3653 
     54    if comment is None: 
     55        comment = _frameComment(code_in) 
     56         
     57    code = "\n" + comment + "\n" + code 
     58     
    3759    from scipy import weave 
    3860 
     
    5072                 extra_compile_args =['-O3']) 
    5173                  
    52 def _runIterateElementInline(code_in, converters=None, verbose=0, **args): 
     74def _runIterateElementInline(code_in, converters=None, verbose=0, comment=None, **args): 
    5375    loops = """ 
    5476int i; 
     
    7092""" 
    7193         
     94    indent = "\t" * rank 
     95     
    7296    code = """ 
    7397    #define ITEM(arr,i,vec) (arr[arrayIndex(arr##_array, i, vec)]) 
    7498                     
    7599    int vec[%(rank)d]; 
    76     %(loops)s%(indent)s%(code)s%(enders)s 
     100    %(loops)s%(indent)s%(code_in)s%(enders)s 
    77101                     
    78102    #undef ITEM 
    79     """ % { 
    80         'rank': rank,  
    81         'loops': loops,  
    82         'indent': "\t" * rank,  
    83         'code': code_in,  
    84         'enders': enders 
    85     } 
     103    """ % locals() 
     104 
     105    if comment is None: 
     106        comment = _frameComment(code_in) 
     107 
     108    code = "\n" + comment + "\n" + code 
    86109 
    87110    from scipy import weave 
  • trunk/fipy/variables/operatorVariable.py

    r2781 r2817  
    3535def _OperatorVariableClass(baseClass=None): 
    3636    class _OperatorVariable(baseClass): 
    37         def __init__(self, op, var, opShape=(), canInline=True, unit=None, *args, **kwargs): 
     37        def __init__(self, op, var, opShape=(), canInline=True, unit=None, inlineComment=None, *args, **kwargs): 
    3838            self.op = op 
    3939            self.var = var 
     
    5353            self.dontCacheMe() 
    5454 
     55            self.comment = inlineComment 
     56 
    5557        def _calcValue(self): 
    5658            if not self.canInline: 
     
    6163 
    6264        def _calcValueIn(self): 
    63             return self._execInline() 
     65            return self._execInline(comment=self.comment) 
    6466 
    6567        def _calcValuePy(self): 
  • trunk/fipy/variables/variable.py

    r2781 r2817  
    3737import sys 
    3838import os 
     39import inspect 
    3940 
    4041from fipy.tools.dimensions import physicalField 
     
    667668        return Variable 
    668669         
    669     def _execInline(self): 
     670    def _execInline(self, comment=None): 
    670671        """ 
    671672        Gets the stack from _getCstring() which calls _getRepresentation() 
     
    750751                argDict['result'] = numerix.reshape(argDict['result'], (1,)) 
    751752 
    752             inline._runInline(string, converters=None, **argDict) 
     753            inline._runInline(string, converters=None, comment=comment, **argDict) 
    753754 
    754755            if resultShape == (): 
     
    803804        return operatorVariable._OperatorVariableClass(baseClass=baseClass) 
    804805             
     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 
    805828    def _UnaryOperatorVariable(self, op, operatorClass=None, opShape=None, canInline=True, unit=None): 
    806829        """ 
     
    826849            canInline = False 
    827850 
    828         return unOp(op=op, var=[self], opShape=opShape, canInline=canInline, unit=unit) 
     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 
    829855 
    830856    def _shapeClassAndOther(self, opShape, operatorClass, other): 
     
    872898        binOp = binaryOperatorVariable._BinaryOperatorVariable(operatorClass) 
    873899         
    874         return binOp(op=op, var=[self, other], opShape=opShape, canInline=canInline, unit=unit) 
     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 
    875904     
    876905    def __add__(self, other):