@@ -19,9 +19,6 @@ def _try_compile(source, name):
1919 Utility function to accept strings in functions that otherwise
2020 expect code objects
2121 """
22- # ncoghlan: currently only used by dis(), but plan to add an
23- # equivalent for show_code() as well (but one that returns a
24- # string rather than printing directly to the console)
2522 try :
2623 c = compile (source , name , 'eval' )
2724 except SyntaxError :
@@ -37,11 +34,11 @@ def dis(x=None):
3734 if x is None :
3835 distb ()
3936 return
40- if hasattr (x , '__func__' ):
37+ if hasattr (x , '__func__' ): # Method
4138 x = x .__func__
42- if hasattr (x , '__code__' ):
39+ if hasattr (x , '__code__' ): # Function
4340 x = x .__code__
44- if hasattr (x , '__dict__' ):
41+ if hasattr (x , '__dict__' ): # Class or module
4542 items = sorted (x .__dict__ .items ())
4643 for name , x1 in items :
4744 if isinstance (x1 , _have_code ):
@@ -51,11 +48,11 @@ def dis(x=None):
5148 except TypeError as msg :
5249 print ("Sorry:" , msg )
5350 print ()
54- elif hasattr (x , 'co_code' ):
51+ elif hasattr (x , 'co_code' ): # Code object
5552 disassemble (x )
56- elif isinstance (x , (bytes , bytearray )):
53+ elif isinstance (x , (bytes , bytearray )): # Raw bytecode
5754 _disassemble_bytes (x )
58- elif isinstance (x , str ):
55+ elif isinstance (x , str ): # Source code
5956 _disassemble_str (x )
6057 else :
6158 raise TypeError ("don't know how to disassemble %s objects" %
@@ -97,35 +94,54 @@ def pretty_flags(flags):
9794 names .append (hex (flags ))
9895 return ", " .join (names )
9996
100- def show_code (co ):
101- """Show details about a code object."""
102- print ("Name: " , co .co_name )
103- print ("Filename: " , co .co_filename )
104- print ("Argument count: " , co .co_argcount )
105- print ("Kw-only arguments:" , co .co_kwonlyargcount )
106- print ("Number of locals: " , co .co_nlocals )
107- print ("Stack size: " , co .co_stacksize )
108- print ("Flags: " , pretty_flags (co .co_flags ))
97+ def code_info (x ):
98+ """Formatted details of methods, functions, or code."""
99+ if hasattr (x , '__func__' ): # Method
100+ x = x .__func__
101+ if hasattr (x , '__code__' ): # Function
102+ x = x .__code__
103+ if isinstance (x , str ): # Source code
104+ x = _try_compile (x , "<code_info>" )
105+ if hasattr (x , 'co_code' ): # Code object
106+ return _format_code_info (x )
107+ else :
108+ raise TypeError ("don't know how to disassemble %s objects" %
109+ type (x ).__name__ )
110+
111+ def _format_code_info (co ):
112+ lines = []
113+ lines .append ("Name: %s" % co .co_name )
114+ lines .append ("Filename: %s" % co .co_filename )
115+ lines .append ("Argument count: %s" % co .co_argcount )
116+ lines .append ("Kw-only arguments: %s" % co .co_kwonlyargcount )
117+ lines .append ("Number of locals: %s" % co .co_nlocals )
118+ lines .append ("Stack size: %s" % co .co_stacksize )
119+ lines .append ("Flags: %s" % pretty_flags (co .co_flags ))
109120 if co .co_consts :
110- print ("Constants:" )
121+ lines . append ("Constants:" )
111122 for i_c in enumerate (co .co_consts ):
112- print ("%4d: %r" % i_c )
123+ lines . append ("%4d: %r" % i_c )
113124 if co .co_names :
114- print ("Names:" )
125+ lines . append ("Names:" )
115126 for i_n in enumerate (co .co_names ):
116- print ("%4d: %s" % i_n )
127+ lines . append ("%4d: %s" % i_n )
117128 if co .co_varnames :
118- print ("Variable names:" )
129+ lines . append ("Variable names:" )
119130 for i_n in enumerate (co .co_varnames ):
120- print ("%4d: %s" % i_n )
131+ lines . append ("%4d: %s" % i_n )
121132 if co .co_freevars :
122- print ("Free variables:" )
133+ lines . append ("Free variables:" )
123134 for i_n in enumerate (co .co_freevars ):
124- print ("%4d: %s" % i_n )
135+ lines . append ("%4d: %s" % i_n )
125136 if co .co_cellvars :
126- print ("Cell variables:" )
137+ lines . append ("Cell variables:" )
127138 for i_n in enumerate (co .co_cellvars ):
128- print ("%4d: %s" % i_n )
139+ lines .append ("%4d: %s" % i_n )
140+ return "\n " .join (lines )
141+
142+ def show_code (co ):
143+ """Show details about a code object."""
144+ print (code_info (co ))
129145
130146def disassemble (co , lasti = - 1 ):
131147 """Disassemble a code object."""
0 commit comments