4545
4646class Scanner3 (scan .Scanner ):
4747
48- ## FIXME opnames should be passed in here
4948 def __init__ (self , version ):
50- self . version = version
51- self . opnames = {} # will eventually get passed in
52- scan . Scanner . __init__ ( self , version )
53-
49+ if PYTHON3 :
50+ super (). __init__ ( version )
51+ else :
52+ super ( Scanner3 , self ). __init__ ( version )
5453
55- ## FIXME opnames should be moved to init
56- def disassemble3 (self , co , opnames , classname = None , code_objects = {}):
54+ def disassemble3 (self , co , classname = None , code_objects = {}):
5755 """
58- Disassemble a Python 3 ode object, returning a list of 'Token'.
56+ Disassemble a Python 3 code object, returning a list of 'Token'.
5957 Various tranformations are made to assist the deparsing grammar.
6058 For example:
6159 - various types of LOAD_CONST's are categorized in terms of what they load
@@ -65,8 +63,6 @@ def disassemble3(self, co, opnames, classname=None, code_objects={}):
6563 dis.disassemble().
6664 """
6765
68- self .opnames = opnames # will eventually disasppear
69-
7066 # import dis; dis.disassemble(co) # DEBUG
7167
7268 # Container for tokens
@@ -76,7 +72,7 @@ def disassemble3(self, co, opnames, classname=None, code_objects={}):
7672 self .build_lines_data (co )
7773 self .build_prev_op ()
7874
79- bytecode = dis3 .Bytecode (co , opnames )
75+ bytecode = dis3 .Bytecode (co , self . opname )
8076
8177 # Scan for assertions. Later we will
8278 # turn 'LOAD_GLOBAL' to 'LOAD_ASSERT' for those
@@ -164,7 +160,7 @@ def disassemble3(self, co, opnames, classname=None, code_objects={}):
164160 pattr = inst .argval
165161 target = self .get_target (inst .offset )
166162 if target < inst .offset :
167- next_opname = opnames [self .code [inst .offset + 3 ]]
163+ next_opname = self . opname [self .code [inst .offset + 3 ]]
168164 if (inst .offset in self .stmts and
169165 next_opname not in ('END_FINALLY' , 'POP_BLOCK' )
170166 and inst .offset not in self .not_continue ):
@@ -187,7 +183,7 @@ def disassemble3(self, co, opnames, classname=None, code_objects={}):
187183 pass
188184 return tokens , {}
189185
190- def disassemble3_native (self , co , opnames , classname = None , code_objects = {}):
186+ def disassemble3_native (self , co , classname = None , code_objects = {}):
191187 """
192188 Like disassemble3 but doesn't try to adjust any opcodes.
193189 """
@@ -196,7 +192,7 @@ def disassemble3_native(self, co, opnames, classname=None, code_objects={}):
196192
197193 self .code = array ('B' , co .co_code )
198194
199- bytecode = dis3 .Bytecode (co , opnames )
195+ bytecode = dis3 .Bytecode (co , self . opname )
200196
201197 for inst in bytecode :
202198 pattr = inst .argrepr
@@ -292,7 +288,7 @@ def unmangle(name):
292288 pass
293289
294290 op = code [offset ]
295- op_name = op3 .opname [op ]
291+ op_name = self .opname [op ]
296292
297293 oparg = None ; pattr = None
298294
@@ -907,11 +903,15 @@ def remove_mid_line_ifs(self, ifs):
907903 return filtered
908904
909905if __name__ == "__main__" :
910- import inspect
911- co = inspect .currentframe ().f_code
912906 from uncompyle6 import PYTHON_VERSION
913- from opcode import opname
914- tokens , customize = Scanner3 (PYTHON_VERSION ).disassemble3 (co , opname )
915- for t in tokens :
916- print (t )
907+ if PYTHON_VERSION >= 3.2 :
908+ import inspect
909+ co = inspect .currentframe ().f_code
910+ from uncompyle6 import PYTHON_VERSION
911+ tokens , customize = Scanner3 (PYTHON_VERSION ).disassemble3 (co )
912+ for t in tokens :
913+ print (t .format ())
914+ else :
915+ print ("Need to be Python 3.2 or greater to demo; I am %s." %
916+ PYTHON_VERSION )
917917 pass
0 commit comments