Changeset 426
- Timestamp:
- 03/15/07 18:21:29
- Files:
-
- trunk/codewalk.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/codewalk.py
r425 r426 124 124 125 125 class 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 """ 127 130 128 131 def __init__(self, obj): … … 133 136 obj = obj.im_func 134 137 if isinstance(obj, FunctionType): 138 self._func = obj 135 139 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 136 152 try: 137 153 obj = obj.co_code … … 141 157 # Map the code block string to a list of opcode numbers. 142 158 if isinstance(obj, basestring): 143 obj= map(ord, obj)159 bytecode = map(ord, obj) 144 160 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 148 166 149 167 def debug(self, *messages): … … 270 288 """ 271 289 272 def __init__(self, obj):273 self.verbose = False274 275 if isinstance(obj, MethodType):276 obj = obj.im_func277 if isinstance(obj, FunctionType):278 self._func = obj279 obj = obj.func_code280 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] = value288 289 try:290 obj = obj.co_code291 except AttributeError:292 pass293 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 = bytecode303 304 290 def bytecode(self): 305 291 """Walk self and return new bytecode.""" … … 722 708 Produce decompiled Python code (as a string) from a supplied lambda.""" 723 709 724 def __init__(self, obj):725 self.verbose = False726 727 # Distill supplied 'obj' arg to a code block string.728 if isinstance(obj, MethodType):729 obj = obj.im_func730 if isinstance(obj, FunctionType):731 obj = obj.func_code732 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] = value740 741 try:742 obj = obj.co_code743 except AttributeError:744 pass745 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 = obj753 754 710 def code(self, include_func_header=True): 755 711 self.walk()
