@@ -55,6 +55,62 @@ def distb(tb=None):
5555 while tb .tb_next : tb = tb .tb_next
5656 disassemble (tb .tb_frame .f_code , tb .tb_lasti )
5757
58+ # XXX This duplicates information from code.h, also duplicated in inspect.py.
59+ # XXX Maybe this ought to be put in a central location, like opcode.py?
60+ flag2name = {
61+ 1 : "OPTIMIZED" ,
62+ 2 : "NEWLOCALS" ,
63+ 4 : "VARARGS" ,
64+ 8 : "VARKEYWORDS" ,
65+ 16 : "NESTED" ,
66+ 32 : "GENERATOR" ,
67+ 64 : "NOFREE" ,
68+ }
69+
70+ def pretty_flags (flags ):
71+ """Return pretty representation of code flags."""
72+ names = []
73+ for i in range (32 ):
74+ flag = 1 << i
75+ if flags & flag :
76+ names .append (flag2name .get (flag , hex (flag )))
77+ flags ^= flag
78+ if not flags :
79+ break
80+ else :
81+ names .append (hex (flags ))
82+ return ", " .join (names )
83+
84+ def show_code (co ):
85+ """Show details about a code object."""
86+ print ("Name: " , co .co_name )
87+ print ("Filename: " , co .co_filename )
88+ print ("Argument count: " , co .co_argcount )
89+ print ("Kw-only arguments:" , co .co_kwonlyargcount )
90+ print ("Number of locals: " , co .co_nlocals )
91+ print ("Stack size: " , co .co_stacksize )
92+ print ("Flags: " , pretty_flags (co .co_flags ))
93+ if co .co_consts :
94+ print ("Constants:" )
95+ for i_c in enumerate (co .co_consts ):
96+ print ("%4d: %r" % i_c )
97+ if co .co_names :
98+ print ("Names:" )
99+ for i_n in enumerate (co .co_names ):
100+ print ("%4d: %s" % i_n )
101+ if co .co_varnames :
102+ print ("Variable names:" )
103+ for i_n in enumerate (co .co_varnames ):
104+ print ("%4d: %s" % i_n )
105+ if co .co_freevars :
106+ print ("Free variables:" )
107+ for i_n in enumerate (co .co_freevars ):
108+ print ("%4d: %s" % i_n )
109+ if co .co_cellvars :
110+ print ("Cell variables:" )
111+ for i_n in enumerate (co .co_cellvars ):
112+ print ("%4d: %s" % i_n )
113+
58114def disassemble (co , lasti = - 1 ):
59115 """Disassemble a code object."""
60116 code = co .co_code
0 commit comments