blob: 4b2058e4379c91534e54fe8eac7380004a07ef7e [file] [log] [blame]
Martin v. Löwis09776b72002-08-04 17:22:59 +00001# -*- coding: iso-8859-1 -*-
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00002"""Get useful information from live Python objects.
3
4This module encapsulates the interface provided by the internal special
5attributes (func_*, co_*, im_*, tb_*, etc.) in a friendlier fashion.
6It also provides some help for examining source code and class layout.
7
8Here are some of the useful functions provided by this module:
9
10 ismodule(), isclass(), ismethod(), isfunction(), istraceback(),
11 isframe(), iscode(), isbuiltin(), isroutine() - check object types
12 getmembers() - get members of an object that satisfy a given condition
13
14 getfile(), getsourcefile(), getsource() - find an object's source code
15 getdoc(), getcomments() - get documentation on an object
16 getmodule() - determine the module that an object came from
17 getclasstree() - arrange classes so as to represent their hierarchy
18
19 getargspec(), getargvalues() - get info about function arguments
20 formatargspec(), formatargvalues() - format an argument spec
21 getouterframes(), getinnerframes() - get info about frames
22 currentframe() - get the current stack frame
23 stack(), trace() - get info about frames on the stack or in a traceback
24"""
25
26# This module is in the public domain. No warranties.
27
Ka-Ping Yee8b58b842001-03-01 13:56:16 +000028__author__ = 'Ka-Ping Yee <ping@lfw.org>'
29__date__ = '1 Jan 2001'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000030
Neil Schemenauerf06f8532002-03-23 23:51:04 +000031import sys, os, types, string, re, dis, imp, tokenize, linecache
Raymond Hettingera1a992c2005-03-11 06:46:45 +000032from operator import attrgetter
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000033
34# ----------------------------------------------------------- type-checking
35def ismodule(object):
36 """Return true if the object is a module.
37
38 Module objects provide these attributes:
39 __doc__ documentation string
40 __file__ filename (missing for built-in modules)"""
Tim Peters28bc59f2001-09-16 08:40:16 +000041 return isinstance(object, types.ModuleType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000042
43def isclass(object):
44 """Return true if the object is a class.
45
46 Class objects provide these attributes:
47 __doc__ documentation string
48 __module__ name of module in which this class was defined"""
Tim Peters28bc59f2001-09-16 08:40:16 +000049 return isinstance(object, types.ClassType) or hasattr(object, '__bases__')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000050
51def ismethod(object):
52 """Return true if the object is an instance method.
53
54 Instance method objects provide these attributes:
55 __doc__ documentation string
56 __name__ name with which this method was defined
57 im_class class object in which this method belongs
58 im_func function object containing implementation of method
59 im_self instance to which this method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +000060 return isinstance(object, types.MethodType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000061
Tim Peters536d2262001-09-20 05:13:38 +000062def ismethoddescriptor(object):
Tim Petersf1d90b92001-09-20 05:47:55 +000063 """Return true if the object is a method descriptor.
64
65 But not if ismethod() or isclass() or isfunction() are true.
Tim Peters536d2262001-09-20 05:13:38 +000066
67 This is new in Python 2.2, and, for example, is true of int.__add__.
68 An object passing this test has a __get__ attribute but not a __set__
69 attribute, but beyond that the set of attributes varies. __name__ is
70 usually sensible, and __doc__ often is.
71
Tim Petersf1d90b92001-09-20 05:47:55 +000072 Methods implemented via descriptors that also pass one of the other
73 tests return false from the ismethoddescriptor() test, simply because
74 the other tests promise more -- you can, e.g., count on having the
75 im_func attribute (etc) when an object passes ismethod()."""
Tim Peters536d2262001-09-20 05:13:38 +000076 return (hasattr(object, "__get__")
77 and not hasattr(object, "__set__") # else it's a data descriptor
78 and not ismethod(object) # mutual exclusion
Tim Petersf1d90b92001-09-20 05:47:55 +000079 and not isfunction(object)
Tim Peters536d2262001-09-20 05:13:38 +000080 and not isclass(object))
81
Martin v. Löwise59e2ba2003-05-03 09:09:02 +000082def isdatadescriptor(object):
83 """Return true if the object is a data descriptor.
84
85 Data descriptors have both a __get__ and a __set__ attribute. Examples are
86 properties (defined in Python) and getsets and members (defined in C).
87 Typically, data descriptors will also have __name__ and __doc__ attributes
88 (properties, getsets, and members have both of these attributes), but this
89 is not guaranteed."""
90 return (hasattr(object, "__set__") and hasattr(object, "__get__"))
91
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000092def isfunction(object):
93 """Return true if the object is a user-defined function.
94
95 Function objects provide these attributes:
96 __doc__ documentation string
97 __name__ name with which this function was defined
98 func_code code object containing compiled function bytecode
99 func_defaults tuple of any default values for arguments
100 func_doc (same as __doc__)
101 func_globals global namespace in which this function was defined
102 func_name (same as __name__)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000103 return isinstance(object, types.FunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000104
105def istraceback(object):
106 """Return true if the object is a traceback.
107
108 Traceback objects provide these attributes:
109 tb_frame frame object at this level
110 tb_lasti index of last attempted instruction in bytecode
111 tb_lineno current line number in Python source code
112 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000113 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000114
115def isframe(object):
116 """Return true if the object is a frame object.
117
118 Frame objects provide these attributes:
119 f_back next outer frame object (this frame's caller)
120 f_builtins built-in namespace seen by this frame
121 f_code code object being executed in this frame
122 f_exc_traceback traceback if raised in this frame, or None
123 f_exc_type exception type if raised in this frame, or None
124 f_exc_value exception value if raised in this frame, or None
125 f_globals global namespace seen by this frame
126 f_lasti index of last attempted instruction in bytecode
127 f_lineno current line number in Python source code
128 f_locals local namespace seen by this frame
129 f_restricted 0 or 1 if frame is in restricted execution mode
130 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000131 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000132
133def iscode(object):
134 """Return true if the object is a code object.
135
136 Code objects provide these attributes:
137 co_argcount number of arguments (not including * or ** args)
138 co_code string of raw compiled bytecode
139 co_consts tuple of constants used in the bytecode
140 co_filename name of file in which this code object was created
141 co_firstlineno number of first line in Python source code
142 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
143 co_lnotab encoded mapping of line numbers to bytecode indices
144 co_name name with which this code object was defined
145 co_names tuple of names of local variables
146 co_nlocals number of local variables
147 co_stacksize virtual machine stack space required
148 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000149 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000150
151def isbuiltin(object):
152 """Return true if the object is a built-in function or method.
153
154 Built-in functions and methods provide these attributes:
155 __doc__ documentation string
156 __name__ original name of this function or method
157 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000158 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000159
160def isroutine(object):
161 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000162 return (isbuiltin(object)
163 or isfunction(object)
164 or ismethod(object)
165 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000166
167def getmembers(object, predicate=None):
168 """Return all members of an object as (name, value) pairs sorted by name.
169 Optionally, only return members that satisfy a given predicate."""
170 results = []
171 for key in dir(object):
172 value = getattr(object, key)
173 if not predicate or predicate(value):
174 results.append((key, value))
175 results.sort()
176 return results
177
Tim Peters13b49d32001-09-23 02:00:29 +0000178def classify_class_attrs(cls):
179 """Return list of attribute-descriptor tuples.
180
181 For each name in dir(cls), the return list contains a 4-tuple
182 with these elements:
183
184 0. The name (a string).
185
186 1. The kind of attribute this is, one of these strings:
187 'class method' created via classmethod()
188 'static method' created via staticmethod()
189 'property' created via property()
190 'method' any other flavor of method
191 'data' not a method
192
193 2. The class which defined this attribute (a class).
194
195 3. The object as obtained directly from the defining class's
196 __dict__, not via getattr. This is especially important for
197 data attributes: C.data is just a data object, but
198 C.__dict__['data'] may be a data descriptor with additional
199 info, like a __doc__ string.
200 """
201
202 mro = getmro(cls)
203 names = dir(cls)
204 result = []
205 for name in names:
206 # Get the object associated with the name.
207 # Getting an obj from the __dict__ sometimes reveals more than
208 # using getattr. Static and class methods are dramatic examples.
209 if name in cls.__dict__:
210 obj = cls.__dict__[name]
211 else:
212 obj = getattr(cls, name)
213
214 # Figure out where it was defined.
Tim Peters13b49d32001-09-23 02:00:29 +0000215 homecls = getattr(obj, "__objclass__", None)
216 if homecls is None:
Guido van Rossum687ae002001-10-15 22:03:32 +0000217 # search the dicts.
Tim Peters13b49d32001-09-23 02:00:29 +0000218 for base in mro:
219 if name in base.__dict__:
220 homecls = base
221 break
222
223 # Get the object again, in order to get it from the defining
224 # __dict__ instead of via getattr (if possible).
225 if homecls is not None and name in homecls.__dict__:
226 obj = homecls.__dict__[name]
227
228 # Also get the object via getattr.
229 obj_via_getattr = getattr(cls, name)
230
231 # Classify the object.
232 if isinstance(obj, staticmethod):
233 kind = "static method"
234 elif isinstance(obj, classmethod):
235 kind = "class method"
236 elif isinstance(obj, property):
237 kind = "property"
238 elif (ismethod(obj_via_getattr) or
239 ismethoddescriptor(obj_via_getattr)):
240 kind = "method"
241 else:
242 kind = "data"
243
244 result.append((name, kind, homecls, obj))
245
246 return result
247
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000248# ----------------------------------------------------------- class helpers
249def _searchbases(cls, accum):
250 # Simulate the "classic class" search order.
251 if cls in accum:
252 return
253 accum.append(cls)
254 for base in cls.__bases__:
255 _searchbases(base, accum)
256
257def getmro(cls):
258 "Return tuple of base classes (including cls) in method resolution order."
259 if hasattr(cls, "__mro__"):
260 return cls.__mro__
261 else:
262 result = []
263 _searchbases(cls, result)
264 return tuple(result)
265
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000266# -------------------------------------------------- source code extraction
267def indentsize(line):
268 """Return the indent size, in spaces, at the start of a line of text."""
269 expline = string.expandtabs(line)
270 return len(expline) - len(string.lstrip(expline))
271
272def getdoc(object):
273 """Get the documentation string for an object.
274
275 All tabs are expanded to spaces. To clean up docstrings that are
276 indented to line up with blocks of code, any whitespace than can be
277 uniformly removed from the second line onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000278 try:
279 doc = object.__doc__
280 except AttributeError:
281 return None
Michael W. Hudson755f75e2002-05-20 17:29:46 +0000282 if not isinstance(doc, types.StringTypes):
Tim Peters24008312002-03-17 18:56:20 +0000283 return None
284 try:
285 lines = string.split(string.expandtabs(doc), '\n')
286 except UnicodeError:
287 return None
288 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000289 # Find minimum indentation of any non-blank lines after first line.
290 margin = sys.maxint
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000291 for line in lines[1:]:
292 content = len(string.lstrip(line))
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000293 if content:
294 indent = len(line) - content
295 margin = min(margin, indent)
296 # Remove indentation.
297 if lines:
298 lines[0] = lines[0].lstrip()
299 if margin < sys.maxint:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000300 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000301 # Remove any trailing or leading blank lines.
302 while lines and not lines[-1]:
303 lines.pop()
304 while lines and not lines[0]:
305 lines.pop(0)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000306 return string.join(lines, '\n')
307
308def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000309 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000310 if ismodule(object):
311 if hasattr(object, '__file__'):
312 return object.__file__
Jeremy Hyltonab919022003-06-27 18:41:20 +0000313 raise TypeError('arg is a built-in module')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000314 if isclass(object):
Ka-Ping Yeec99e0f12001-04-13 12:10:40 +0000315 object = sys.modules.get(object.__module__)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000316 if hasattr(object, '__file__'):
317 return object.__file__
Jeremy Hyltonab919022003-06-27 18:41:20 +0000318 raise TypeError('arg is a built-in class')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000319 if ismethod(object):
320 object = object.im_func
321 if isfunction(object):
322 object = object.func_code
323 if istraceback(object):
324 object = object.tb_frame
325 if isframe(object):
326 object = object.f_code
327 if iscode(object):
328 return object.co_filename
Tim Peters478c1052003-06-29 05:46:54 +0000329 raise TypeError('arg is not a module, class, method, '
Jeremy Hyltonab919022003-06-27 18:41:20 +0000330 'function, traceback, frame, or code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000331
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000332def getmoduleinfo(path):
333 """Get the module name, suffix, mode, and module type for a given file."""
334 filename = os.path.basename(path)
335 suffixes = map(lambda (suffix, mode, mtype):
336 (-len(suffix), suffix, mode, mtype), imp.get_suffixes())
337 suffixes.sort() # try longest suffixes first, in case they overlap
338 for neglen, suffix, mode, mtype in suffixes:
339 if filename[neglen:] == suffix:
340 return filename[:neglen], suffix, mode, mtype
341
342def getmodulename(path):
343 """Return the module name for a given file, or None."""
344 info = getmoduleinfo(path)
345 if info: return info[0]
346
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000347def getsourcefile(object):
348 """Return the Python source file an object was defined in, if it exists."""
349 filename = getfile(object)
Raymond Hettingerdbecd932005-02-06 06:57:08 +0000350 if string.lower(filename[-4:]) in ('.pyc', '.pyo'):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000351 filename = filename[:-4] + '.py'
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000352 for suffix, mode, kind in imp.get_suffixes():
353 if 'b' in mode and string.lower(filename[-len(suffix):]) == suffix:
354 # Looks like a binary file. We want to only return a text file.
355 return None
Phillip J. Eby72ae6c82006-04-30 15:59:26 +0000356 if os.path.exists(filename):
357 return filename
358 # Ugly but necessary - '<stdin>' and '<string>' mean that getmodule()
359 # would infinitely recurse, because they're not real files nor loadable
360 # Note that this means that writing a PEP 302 loader that uses '<'
361 # at the start of a filename is now not a good idea. :(
362 if filename[:1]!='<' and hasattr(getmodule(object), '__loader__'):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000363 return filename
364
365def getabsfile(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000366 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000367
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000368 The idea is for each object to have a unique origin, so this routine
369 normalizes the result as much as possible."""
370 return os.path.normcase(
371 os.path.abspath(getsourcefile(object) or getfile(object)))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000372
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000373modulesbyfile = {}
374
375def getmodule(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000376 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000377 if ismodule(object):
378 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000379 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000380 return sys.modules.get(object.__module__)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000381 try:
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000382 file = getabsfile(object)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000383 except TypeError:
384 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000385 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000386 return sys.modules.get(modulesbyfile[file])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000387 for module in sys.modules.values():
Phillip J. Eby47032112006-04-11 01:07:43 +0000388 if ismodule(module) and hasattr(module, '__file__'):
Brett Cannonb3de2e12004-08-13 18:46:24 +0000389 modulesbyfile[
390 os.path.realpath(
391 getabsfile(module))] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000392 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000393 return sys.modules.get(modulesbyfile[file])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000394 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000395 if not hasattr(object, '__name__'):
396 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000397 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000398 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000399 if mainobject is object:
400 return main
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000401 builtin = sys.modules['__builtin__']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000402 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000403 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000404 if builtinobject is object:
405 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000406
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000407def findsource(object):
408 """Return the entire source file and starting line number for an object.
409
410 The argument may be a module, class, method, function, traceback, frame,
411 or code object. The source code is returned as a list of all the lines
412 in the file and the line number indexes a line in that list. An IOError
413 is raised if the source code cannot be retrieved."""
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000414 file = getsourcefile(object) or getfile(object)
Phillip J. Eby47032112006-04-11 01:07:43 +0000415 lines = linecache.getlines(file, getmodule(object).__dict__)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000416 if not lines:
Jeremy Hyltonab919022003-06-27 18:41:20 +0000417 raise IOError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000418
419 if ismodule(object):
420 return lines, 0
421
422 if isclass(object):
423 name = object.__name__
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000424 pat = re.compile(r'^\s*class\s*' + name + r'\b')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000425 for i in range(len(lines)):
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000426 if pat.match(lines[i]): return lines, i
Jeremy Hyltonab919022003-06-27 18:41:20 +0000427 else:
428 raise IOError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000429
430 if ismethod(object):
431 object = object.im_func
432 if isfunction(object):
433 object = object.func_code
434 if istraceback(object):
435 object = object.tb_frame
436 if isframe(object):
437 object = object.f_code
438 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000439 if not hasattr(object, 'co_firstlineno'):
Jeremy Hyltonab919022003-06-27 18:41:20 +0000440 raise IOError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000441 lnum = object.co_firstlineno - 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000442 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000443 while lnum > 0:
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000444 if pat.match(lines[lnum]): break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000445 lnum = lnum - 1
446 return lines, lnum
Jeremy Hyltonab919022003-06-27 18:41:20 +0000447 raise IOError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000448
449def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000450 """Get lines of comments immediately preceding an object's source code.
451
452 Returns None when source can't be found.
453 """
454 try:
455 lines, lnum = findsource(object)
456 except (IOError, TypeError):
457 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000458
459 if ismodule(object):
460 # Look for a comment block at the top of the file.
461 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000462 if lines and lines[0][:2] == '#!': start = 1
Raymond Hettingerdbecd932005-02-06 06:57:08 +0000463 while start < len(lines) and string.strip(lines[start]) in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000464 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000465 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000466 comments = []
467 end = start
468 while end < len(lines) and lines[end][:1] == '#':
469 comments.append(string.expandtabs(lines[end]))
470 end = end + 1
471 return string.join(comments, '')
472
473 # Look for a preceding block of comments at the same indentation.
474 elif lnum > 0:
475 indent = indentsize(lines[lnum])
476 end = lnum - 1
477 if end >= 0 and string.lstrip(lines[end])[:1] == '#' and \
478 indentsize(lines[end]) == indent:
479 comments = [string.lstrip(string.expandtabs(lines[end]))]
480 if end > 0:
481 end = end - 1
482 comment = string.lstrip(string.expandtabs(lines[end]))
483 while comment[:1] == '#' and indentsize(lines[end]) == indent:
484 comments[:0] = [comment]
485 end = end - 1
486 if end < 0: break
487 comment = string.lstrip(string.expandtabs(lines[end]))
488 while comments and string.strip(comments[0]) == '#':
489 comments[:1] = []
490 while comments and string.strip(comments[-1]) == '#':
491 comments[-1:] = []
492 return string.join(comments, '')
493
Tim Peters4efb6e92001-06-29 23:51:08 +0000494class EndOfBlock(Exception): pass
495
496class BlockFinder:
497 """Provide a tokeneater() method to detect the end of a code block."""
498 def __init__(self):
499 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000500 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000501 self.started = False
502 self.passline = False
Armin Rigodd5c0232005-09-25 11:45:45 +0000503 self.last = 1
Tim Peters4efb6e92001-06-29 23:51:08 +0000504
505 def tokeneater(self, type, token, (srow, scol), (erow, ecol), line):
506 if not self.started:
Armin Rigodd5c0232005-09-25 11:45:45 +0000507 # look for the first "def", "class" or "lambda"
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000508 if token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000509 if token == "lambda":
510 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000511 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000512 self.passline = True # skip to the end of the line
Tim Peters4efb6e92001-06-29 23:51:08 +0000513 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +0000514 self.passline = False # stop skipping when a NEWLINE is seen
Tim Peters4efb6e92001-06-29 23:51:08 +0000515 self.last = srow
Armin Rigodd5c0232005-09-25 11:45:45 +0000516 if self.islambda: # lambdas always end at the first NEWLINE
517 raise EndOfBlock
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000518 elif self.passline:
519 pass
Tim Peters4efb6e92001-06-29 23:51:08 +0000520 elif type == tokenize.INDENT:
521 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000522 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +0000523 elif type == tokenize.DEDENT:
524 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +0000525 # the end of matching indent/dedent pairs end a block
526 # (note that this only works for "def"/"class" blocks,
527 # not e.g. for "if: else:" or "try: finally:" blocks)
528 if self.indent <= 0:
529 raise EndOfBlock
530 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
531 # any other token on the same indentation level end the previous
532 # block as well, except the pseudo-tokens COMMENT and NL.
533 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +0000534
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000535def getblock(lines):
536 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +0000537 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +0000538 try:
Armin Rigodd5c0232005-09-25 11:45:45 +0000539 tokenize.tokenize(iter(lines).next, blockfinder.tokeneater)
540 except (EndOfBlock, IndentationError):
541 pass
542 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000543
544def getsourcelines(object):
545 """Return a list of source lines and starting line number for an object.
546
547 The argument may be a module, class, method, function, traceback, frame,
548 or code object. The source code is returned as a list of the lines
549 corresponding to the object and the line number indicates where in the
550 original source file the first line of code was found. An IOError is
551 raised if the source code cannot be retrieved."""
552 lines, lnum = findsource(object)
553
554 if ismodule(object): return lines, 0
555 else: return getblock(lines[lnum:]), lnum + 1
556
557def getsource(object):
558 """Return the text of the source code for an object.
559
560 The argument may be a module, class, method, function, traceback, frame,
561 or code object. The source code is returned as a single string. An
562 IOError is raised if the source code cannot be retrieved."""
563 lines, lnum = getsourcelines(object)
564 return string.join(lines, '')
565
566# --------------------------------------------------- class tree extraction
567def walktree(classes, children, parent):
568 """Recursive helper function for getclasstree()."""
569 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +0000570 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000571 for c in classes:
572 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +0000573 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000574 results.append(walktree(children[c], children, c))
575 return results
576
577def getclasstree(classes, unique=0):
578 """Arrange the given list of classes into a hierarchy of nested lists.
579
580 Where a nested list appears, it contains classes derived from the class
581 whose entry immediately precedes the list. Each entry is a 2-tuple
582 containing a class and a tuple of its base classes. If the 'unique'
583 argument is true, exactly one entry appears in the returned structure
584 for each class in the given list. Otherwise, classes using multiple
585 inheritance and their descendants will appear multiple times."""
586 children = {}
587 roots = []
588 for c in classes:
589 if c.__bases__:
590 for parent in c.__bases__:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000591 if not parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000592 children[parent] = []
593 children[parent].append(c)
594 if unique and parent in classes: break
595 elif c not in roots:
596 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +0000597 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000598 if parent not in classes:
599 roots.append(parent)
600 return walktree(roots, children, None)
601
602# ------------------------------------------------ argument list extraction
603# These constants are from Python's compile.h.
604CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS = 1, 2, 4, 8
605
606def getargs(co):
607 """Get information about the arguments accepted by a code object.
608
609 Three things are returned: (args, varargs, varkw), where 'args' is
610 a list of argument names (possibly containing nested lists), and
611 'varargs' and 'varkw' are the names of the * and ** arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +0000612
613 if not iscode(co):
614 raise TypeError('arg is not a code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000615
616 code = co.co_code
617 nargs = co.co_argcount
618 names = co.co_varnames
619 args = list(names[:nargs])
620 step = 0
621
622 # The following acrobatics are for anonymous (tuple) arguments.
623 for i in range(nargs):
Raymond Hettingerdbecd932005-02-06 06:57:08 +0000624 if args[i][:1] in ('', '.'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000625 stack, remain, count = [], [], []
626 while step < len(code):
627 op = ord(code[step])
628 step = step + 1
629 if op >= dis.HAVE_ARGUMENT:
630 opname = dis.opname[op]
631 value = ord(code[step]) + ord(code[step+1])*256
632 step = step + 2
Raymond Hettingerdbecd932005-02-06 06:57:08 +0000633 if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000634 remain.append(value)
635 count.append(value)
636 elif opname == 'STORE_FAST':
637 stack.append(names[value])
Matthias Klose2e829c02004-08-15 17:04:33 +0000638
639 # Special case for sublists of length 1: def foo((bar))
640 # doesn't generate the UNPACK_TUPLE bytecode, so if
641 # `remain` is empty here, we have such a sublist.
642 if not remain:
643 stack[0] = [stack[0]]
644 break
645 else:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000646 remain[-1] = remain[-1] - 1
Matthias Klose2e829c02004-08-15 17:04:33 +0000647 while remain[-1] == 0:
648 remain.pop()
649 size = count.pop()
650 stack[-size:] = [stack[-size:]]
651 if not remain: break
652 remain[-1] = remain[-1] - 1
653 if not remain: break
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000654 args[i] = stack[0]
655
656 varargs = None
657 if co.co_flags & CO_VARARGS:
658 varargs = co.co_varnames[nargs]
659 nargs = nargs + 1
660 varkw = None
661 if co.co_flags & CO_VARKEYWORDS:
662 varkw = co.co_varnames[nargs]
663 return args, varargs, varkw
664
665def getargspec(func):
666 """Get the names and default values of a function's arguments.
667
668 A tuple of four things is returned: (args, varargs, varkw, defaults).
669 'args' is a list of the argument names (it may contain nested lists).
670 'varargs' and 'varkw' are the names of the * and ** arguments or None.
Jeremy Hylton64967882003-06-27 18:14:39 +0000671 'defaults' is an n-tuple of the default values of the last n arguments.
672 """
673
674 if ismethod(func):
675 func = func.im_func
676 if not isfunction(func):
677 raise TypeError('arg is not a Python function')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000678 args, varargs, varkw = getargs(func.func_code)
679 return args, varargs, varkw, func.func_defaults
680
681def getargvalues(frame):
682 """Get information about arguments passed into a particular frame.
683
684 A tuple of four things is returned: (args, varargs, varkw, locals).
685 'args' is a list of the argument names (it may contain nested lists).
686 'varargs' and 'varkw' are the names of the * and ** arguments or None.
687 'locals' is the locals dictionary of the given frame."""
688 args, varargs, varkw = getargs(frame.f_code)
689 return args, varargs, varkw, frame.f_locals
690
691def joinseq(seq):
692 if len(seq) == 1:
693 return '(' + seq[0] + ',)'
694 else:
695 return '(' + string.join(seq, ', ') + ')'
696
697def strseq(object, convert, join=joinseq):
698 """Recursively walk a sequence, stringifying each element."""
Raymond Hettingerdbecd932005-02-06 06:57:08 +0000699 if type(object) in (list, tuple):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000700 return join(map(lambda o, c=convert, j=join: strseq(o, c, j), object))
701 else:
702 return convert(object)
703
704def formatargspec(args, varargs=None, varkw=None, defaults=None,
705 formatarg=str,
706 formatvarargs=lambda name: '*' + name,
707 formatvarkw=lambda name: '**' + name,
708 formatvalue=lambda value: '=' + repr(value),
709 join=joinseq):
710 """Format an argument spec from the 4 values returned by getargspec.
711
712 The first four arguments are (args, varargs, varkw, defaults). The
713 other four arguments are the corresponding optional formatting functions
714 that are called to turn names and values into strings. The ninth
715 argument is an optional function to format the sequence of arguments."""
716 specs = []
717 if defaults:
718 firstdefault = len(args) - len(defaults)
719 for i in range(len(args)):
720 spec = strseq(args[i], formatarg, join)
721 if defaults and i >= firstdefault:
722 spec = spec + formatvalue(defaults[i - firstdefault])
723 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +0000724 if varargs is not None:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000725 specs.append(formatvarargs(varargs))
Raymond Hettinger936654b2002-06-01 03:06:31 +0000726 if varkw is not None:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000727 specs.append(formatvarkw(varkw))
728 return '(' + string.join(specs, ', ') + ')'
729
730def formatargvalues(args, varargs, varkw, locals,
731 formatarg=str,
732 formatvarargs=lambda name: '*' + name,
733 formatvarkw=lambda name: '**' + name,
734 formatvalue=lambda value: '=' + repr(value),
735 join=joinseq):
736 """Format an argument spec from the 4 values returned by getargvalues.
737
738 The first four arguments are (args, varargs, varkw, locals). The
739 next four arguments are the corresponding optional formatting functions
740 that are called to turn names and values into strings. The ninth
741 argument is an optional function to format the sequence of arguments."""
742 def convert(name, locals=locals,
743 formatarg=formatarg, formatvalue=formatvalue):
744 return formatarg(name) + formatvalue(locals[name])
745 specs = []
746 for i in range(len(args)):
747 specs.append(strseq(args[i], convert, join))
748 if varargs:
749 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
750 if varkw:
751 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
752 return '(' + string.join(specs, ', ') + ')'
753
754# -------------------------------------------------- stack frame extraction
755def getframeinfo(frame, context=1):
756 """Get information about a frame or traceback object.
757
758 A tuple of five things is returned: the filename, the line number of
759 the current line, the function name, a list of lines of context from
760 the source code, and the index of the current line within that list.
761 The optional second argument specifies the number of lines of context
762 to return, which are centered around the current line."""
763 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +0000764 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000765 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +0000766 else:
767 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000768 if not isframe(frame):
Jeremy Hyltonab919022003-06-27 18:41:20 +0000769 raise TypeError('arg is not a frame or traceback object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000770
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000771 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000772 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +0000773 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000774 try:
775 lines, lnum = findsource(frame)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000776 except IOError:
777 lines = index = None
778 else:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000779 start = max(start, 1)
Raymond Hettingera0501712004-06-15 11:22:53 +0000780 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000781 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +0000782 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000783 else:
784 lines = index = None
785
Ka-Ping Yee59ade082001-03-01 03:55:35 +0000786 return (filename, lineno, frame.f_code.co_name, lines, index)
787
788def getlineno(frame):
789 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000790 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
791 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000792
793def getouterframes(frame, context=1):
794 """Get a list of records for a frame and all higher (calling) frames.
795
796 Each record contains a frame object, filename, line number, function
797 name, a list of lines of context, and index within the context."""
798 framelist = []
799 while frame:
800 framelist.append((frame,) + getframeinfo(frame, context))
801 frame = frame.f_back
802 return framelist
803
804def getinnerframes(tb, context=1):
805 """Get a list of records for a traceback's frame and all lower frames.
806
807 Each record contains a frame object, filename, line number, function
808 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000809 framelist = []
810 while tb:
811 framelist.append((tb.tb_frame,) + getframeinfo(tb, context))
812 tb = tb.tb_next
813 return framelist
814
Jeremy Hyltonab919022003-06-27 18:41:20 +0000815currentframe = sys._getframe
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000816
817def stack(context=1):
818 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +0000819 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000820
821def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +0000822 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +0000823 return getinnerframes(sys.exc_info()[2], context)