Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

I think I've seen this ORM somewhere before...

Changeset 426

Show
Ignore:
Timestamp:
03/15/07 18:21:29
Author:
fumanchu
Message:

Moved "copy co_code attribute" block from Rewriter, LambdaDecompiler? into the parent Visitor class.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/codewalk.py

    r425 r426  
    124124 
    125125class Visitor(object): 
    126     """Visitor(obj=function, code object, string, or list of opcodes).""" 
     126    """A visitor class for bytecode sequences. 
     127     
     128    obj: a function, code object, string, or list of opcodes. 
     129    """ 
    127130     
    128131    def __init__(self, obj): 
     
    133136            obj = obj.im_func 
    134137        if isinstance(obj, FunctionType): 
     138            self._func = obj 
    135139            obj = obj.func_code 
     140         
     141        # Copy code object attributes (if present). 
     142        selfdict = self.__dict__ 
     143        try: 
     144            for name, _type in _co_code_attrs.iteritems(): 
     145                value = getattr(obj, name) 
     146                if _type is tuple: 
     147                    value = list(value) 
     148                selfdict[name] = value 
     149        except AttributeError: 
     150            pass 
     151         
    136152        try: 
    137153            obj = obj.co_code 
     
    141157        # Map the code block string to a list of opcode numbers. 
    142158        if isinstance(obj, basestring): 
    143             obj = map(ord, obj) 
     159            bytecode = map(ord, obj) 
    144160        elif isinstance(obj, list): 
    145             obj = obj[:] 
    146          
    147         self._bytecode = obj 
     161            bytecode = obj[:] 
     162        else: 
     163            raise TypeError("obj arg of incorrect type '%s'" % type(obj)) 
     164         
     165        self._bytecode = bytecode 
    148166     
    149167    def debug(self, *messages): 
     
    270288    """ 
    271289     
    272     def __init__(self, obj): 
    273         self.verbose = False 
    274          
    275         if isinstance(obj, MethodType): 
    276             obj = obj.im_func 
    277         if isinstance(obj, FunctionType): 
    278             self._func = obj 
    279             obj = obj.func_code 
    280          
    281         # Copy code object attributes. 
    282         selfdict = self.__dict__ 
    283         for name, _type in _co_code_attrs.iteritems(): 
    284             value = getattr(obj, name) 
    285             if _type is tuple: 
    286                 value = list(value) 
    287             selfdict[name] = value 
    288          
    289         try: 
    290             obj = obj.co_code 
    291         except AttributeError: 
    292             pass 
    293          
    294         # Map the code block to a list of opcode numbers. 
    295         if isinstance(obj, basestring): 
    296             bytecode = map(ord, obj) 
    297         elif isinstance(obj, list): 
    298             bytecode = obj[:] 
    299         else: 
    300             raise TypeError("obj arg of incorrect type '%s'" % type(obj)) 
    301          
    302         self._bytecode = bytecode 
    303      
    304290    def bytecode(self): 
    305291        """Walk self and return new bytecode.""" 
     
    722708    Produce decompiled Python code (as a string) from a supplied lambda.""" 
    723709     
    724     def __init__(self, obj): 
    725         self.verbose = False 
    726          
    727         # Distill supplied 'obj' arg to a code block string. 
    728         if isinstance(obj, MethodType): 
    729             obj = obj.im_func 
    730         if isinstance(obj, FunctionType): 
    731             obj = obj.func_code 
    732          
    733         # Copy code object attributes. 
    734         selfdict = self.__dict__ 
    735         for name, _type in _co_code_attrs.iteritems(): 
    736             value = getattr(obj, name) 
    737             if _type is tuple: 
    738                 value = list(value) 
    739             selfdict[name] = value 
    740          
    741         try: 
    742             obj = obj.co_code 
    743         except AttributeError: 
    744             pass 
    745          
    746         # Map the code block to a list of opcode numbers. 
    747         if isinstance(obj, basestring): 
    748             obj = map(ord, obj) 
    749         elif isinstance(obj, list): 
    750             obj = obj[:] 
    751          
    752         self._bytecode = obj 
    753      
    754710    def code(self, include_func_header=True): 
    755711        self.walk()