Fix the availability statement for the spawn*() functions to reflect the
[python/dscho.git] / Lib / inspect.py
blobe55edcab2892ac5a259c68dcd304612ac87a91d5
1 """Get useful information from live Python objects.
3 This module encapsulates the interface provided by the internal special
4 attributes (func_*, co_*, im_*, tb_*, etc.) in a friendlier fashion.
5 It also provides some help for examining source code and class layout.
7 Here are some of the useful functions provided by this module:
9 ismodule(), isclass(), ismethod(), isfunction(), istraceback(),
10 isframe(), iscode(), isbuiltin(), isroutine() - check object types
11 getmembers() - get members of an object that satisfy a given condition
13 getfile(), getsourcefile(), getsource() - find an object's source code
14 getdoc(), getcomments() - get documentation on an object
15 getmodule() - determine the module that an object came from
16 getclasstree() - arrange classes so as to represent their hierarchy
18 getargspec(), getargvalues() - get info about function arguments
19 formatargspec(), formatargvalues() - format an argument spec
20 getouterframes(), getinnerframes() - get info about frames
21 currentframe() - get the current stack frame
22 stack(), trace() - get info about frames on the stack or in a traceback
23 """
25 # This module is in the public domain. No warranties.
27 __author__ = 'Ka-Ping Yee <ping@lfw.org>'
28 __date__ = '1 Jan 2001'
30 import sys, os, types, string, re, dis, imp, tokenize
32 # ----------------------------------------------------------- type-checking
33 def ismodule(object):
34 """Return true if the object is a module.
36 Module objects provide these attributes:
37 __doc__ documentation string
38 __file__ filename (missing for built-in modules)"""
39 return isinstance(object, types.ModuleType)
41 def isclass(object):
42 """Return true if the object is a class.
44 Class objects provide these attributes:
45 __doc__ documentation string
46 __module__ name of module in which this class was defined"""
47 return isinstance(object, types.ClassType) or hasattr(object, '__bases__')
49 def ismethod(object):
50 """Return true if the object is an instance method.
52 Instance method objects provide these attributes:
53 __doc__ documentation string
54 __name__ name with which this method was defined
55 im_class class object in which this method belongs
56 im_func function object containing implementation of method
57 im_self instance to which this method is bound, or None"""
58 return isinstance(object, types.MethodType)
60 def ismethoddescriptor(object):
61 """Return true if the object is a method descriptor.
63 But not if ismethod() or isclass() or isfunction() are true.
65 This is new in Python 2.2, and, for example, is true of int.__add__.
66 An object passing this test has a __get__ attribute but not a __set__
67 attribute, but beyond that the set of attributes varies. __name__ is
68 usually sensible, and __doc__ often is.
70 Methods implemented via descriptors that also pass one of the other
71 tests return false from the ismethoddescriptor() test, simply because
72 the other tests promise more -- you can, e.g., count on having the
73 im_func attribute (etc) when an object passes ismethod()."""
74 return (hasattr(object, "__get__")
75 and not hasattr(object, "__set__") # else it's a data descriptor
76 and not ismethod(object) # mutual exclusion
77 and not isfunction(object)
78 and not isclass(object))
80 def isfunction(object):
81 """Return true if the object is a user-defined function.
83 Function objects provide these attributes:
84 __doc__ documentation string
85 __name__ name with which this function was defined
86 func_code code object containing compiled function bytecode
87 func_defaults tuple of any default values for arguments
88 func_doc (same as __doc__)
89 func_globals global namespace in which this function was defined
90 func_name (same as __name__)"""
91 return isinstance(object, types.FunctionType)
93 def istraceback(object):
94 """Return true if the object is a traceback.
96 Traceback objects provide these attributes:
97 tb_frame frame object at this level
98 tb_lasti index of last attempted instruction in bytecode
99 tb_lineno current line number in Python source code
100 tb_next next inner traceback object (called by this level)"""
101 return isinstance(object, types.TracebackType)
103 def isframe(object):
104 """Return true if the object is a frame object.
106 Frame objects provide these attributes:
107 f_back next outer frame object (this frame's caller)
108 f_builtins built-in namespace seen by this frame
109 f_code code object being executed in this frame
110 f_exc_traceback traceback if raised in this frame, or None
111 f_exc_type exception type if raised in this frame, or None
112 f_exc_value exception value if raised in this frame, or None
113 f_globals global namespace seen by this frame
114 f_lasti index of last attempted instruction in bytecode
115 f_lineno current line number in Python source code
116 f_locals local namespace seen by this frame
117 f_restricted 0 or 1 if frame is in restricted execution mode
118 f_trace tracing function for this frame, or None"""
119 return isinstance(object, types.FrameType)
121 def iscode(object):
122 """Return true if the object is a code object.
124 Code objects provide these attributes:
125 co_argcount number of arguments (not including * or ** args)
126 co_code string of raw compiled bytecode
127 co_consts tuple of constants used in the bytecode
128 co_filename name of file in which this code object was created
129 co_firstlineno number of first line in Python source code
130 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
131 co_lnotab encoded mapping of line numbers to bytecode indices
132 co_name name with which this code object was defined
133 co_names tuple of names of local variables
134 co_nlocals number of local variables
135 co_stacksize virtual machine stack space required
136 co_varnames tuple of names of arguments and local variables"""
137 return isinstance(object, types.CodeType)
139 def isbuiltin(object):
140 """Return true if the object is a built-in function or method.
142 Built-in functions and methods provide these attributes:
143 __doc__ documentation string
144 __name__ original name of this function or method
145 __self__ instance to which a method is bound, or None"""
146 return isinstance(object, types.BuiltinFunctionType)
148 def isroutine(object):
149 """Return true if the object is any kind of function or method."""
150 return (isbuiltin(object)
151 or isfunction(object)
152 or ismethod(object)
153 or ismethoddescriptor(object))
155 def getmembers(object, predicate=None):
156 """Return all members of an object as (name, value) pairs sorted by name.
157 Optionally, only return members that satisfy a given predicate."""
158 results = []
159 for key in dir(object):
160 value = getattr(object, key)
161 if not predicate or predicate(value):
162 results.append((key, value))
163 results.sort()
164 return results
166 def classify_class_attrs(cls):
167 """Return list of attribute-descriptor tuples.
169 For each name in dir(cls), the return list contains a 4-tuple
170 with these elements:
172 0. The name (a string).
174 1. The kind of attribute this is, one of these strings:
175 'class method' created via classmethod()
176 'static method' created via staticmethod()
177 'property' created via property()
178 'method' any other flavor of method
179 'data' not a method
181 2. The class which defined this attribute (a class).
183 3. The object as obtained directly from the defining class's
184 __dict__, not via getattr. This is especially important for
185 data attributes: C.data is just a data object, but
186 C.__dict__['data'] may be a data descriptor with additional
187 info, like a __doc__ string.
190 mro = getmro(cls)
191 names = dir(cls)
192 result = []
193 for name in names:
194 # Get the object associated with the name.
195 # Getting an obj from the __dict__ sometimes reveals more than
196 # using getattr. Static and class methods are dramatic examples.
197 if name in cls.__dict__:
198 obj = cls.__dict__[name]
199 else:
200 obj = getattr(cls, name)
202 # Figure out where it was defined.
203 homecls = getattr(obj, "__objclass__", None)
204 if homecls is None:
205 # search the dicts.
206 for base in mro:
207 if name in base.__dict__:
208 homecls = base
209 break
211 # Get the object again, in order to get it from the defining
212 # __dict__ instead of via getattr (if possible).
213 if homecls is not None and name in homecls.__dict__:
214 obj = homecls.__dict__[name]
216 # Also get the object via getattr.
217 obj_via_getattr = getattr(cls, name)
219 # Classify the object.
220 if isinstance(obj, staticmethod):
221 kind = "static method"
222 elif isinstance(obj, classmethod):
223 kind = "class method"
224 elif isinstance(obj, property):
225 kind = "property"
226 elif (ismethod(obj_via_getattr) or
227 ismethoddescriptor(obj_via_getattr)):
228 kind = "method"
229 else:
230 kind = "data"
232 result.append((name, kind, homecls, obj))
234 return result
236 # ----------------------------------------------------------- class helpers
237 def _searchbases(cls, accum):
238 # Simulate the "classic class" search order.
239 if cls in accum:
240 return
241 accum.append(cls)
242 for base in cls.__bases__:
243 _searchbases(base, accum)
245 def getmro(cls):
246 "Return tuple of base classes (including cls) in method resolution order."
247 if hasattr(cls, "__mro__"):
248 return cls.__mro__
249 else:
250 result = []
251 _searchbases(cls, result)
252 return tuple(result)
254 # -------------------------------------------------- source code extraction
255 def indentsize(line):
256 """Return the indent size, in spaces, at the start of a line of text."""
257 expline = string.expandtabs(line)
258 return len(expline) - len(string.lstrip(expline))
260 def getdoc(object):
261 """Get the documentation string for an object.
263 All tabs are expanded to spaces. To clean up docstrings that are
264 indented to line up with blocks of code, any whitespace than can be
265 uniformly removed from the second line onwards is removed."""
266 if hasattr(object, '__doc__') and object.__doc__:
267 lines = string.split(string.expandtabs(object.__doc__), '\n')
268 margin = None
269 for line in lines[1:]:
270 content = len(string.lstrip(line))
271 if not content: continue
272 indent = len(line) - content
273 if margin is None: margin = indent
274 else: margin = min(margin, indent)
275 if margin is not None:
276 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
277 return string.join(lines, '\n')
279 def getfile(object):
280 """Work out which source or compiled file an object was defined in."""
281 if ismodule(object):
282 if hasattr(object, '__file__'):
283 return object.__file__
284 raise TypeError, 'arg is a built-in module'
285 if isclass(object):
286 object = sys.modules.get(object.__module__)
287 if hasattr(object, '__file__'):
288 return object.__file__
289 raise TypeError, 'arg is a built-in class'
290 if ismethod(object):
291 object = object.im_func
292 if isfunction(object):
293 object = object.func_code
294 if istraceback(object):
295 object = object.tb_frame
296 if isframe(object):
297 object = object.f_code
298 if iscode(object):
299 return object.co_filename
300 raise TypeError, 'arg is not a module, class, method, ' \
301 'function, traceback, frame, or code object'
303 def getmoduleinfo(path):
304 """Get the module name, suffix, mode, and module type for a given file."""
305 filename = os.path.basename(path)
306 suffixes = map(lambda (suffix, mode, mtype):
307 (-len(suffix), suffix, mode, mtype), imp.get_suffixes())
308 suffixes.sort() # try longest suffixes first, in case they overlap
309 for neglen, suffix, mode, mtype in suffixes:
310 if filename[neglen:] == suffix:
311 return filename[:neglen], suffix, mode, mtype
313 def getmodulename(path):
314 """Return the module name for a given file, or None."""
315 info = getmoduleinfo(path)
316 if info: return info[0]
318 def getsourcefile(object):
319 """Return the Python source file an object was defined in, if it exists."""
320 filename = getfile(object)
321 if string.lower(filename[-4:]) in ['.pyc', '.pyo']:
322 filename = filename[:-4] + '.py'
323 for suffix, mode, kind in imp.get_suffixes():
324 if 'b' in mode and string.lower(filename[-len(suffix):]) == suffix:
325 # Looks like a binary file. We want to only return a text file.
326 return None
327 if os.path.exists(filename):
328 return filename
330 def getabsfile(object):
331 """Return an absolute path to the source or compiled file for an object.
333 The idea is for each object to have a unique origin, so this routine
334 normalizes the result as much as possible."""
335 return os.path.normcase(
336 os.path.abspath(getsourcefile(object) or getfile(object)))
338 modulesbyfile = {}
340 def getmodule(object):
341 """Return the module an object was defined in, or None if not found."""
342 if ismodule(object):
343 return object
344 if isclass(object):
345 return sys.modules.get(object.__module__)
346 try:
347 file = getabsfile(object)
348 except TypeError:
349 return None
350 if modulesbyfile.has_key(file):
351 return sys.modules[modulesbyfile[file]]
352 for module in sys.modules.values():
353 if hasattr(module, '__file__'):
354 modulesbyfile[getabsfile(module)] = module.__name__
355 if modulesbyfile.has_key(file):
356 return sys.modules[modulesbyfile[file]]
357 main = sys.modules['__main__']
358 if hasattr(main, object.__name__):
359 mainobject = getattr(main, object.__name__)
360 if mainobject is object:
361 return main
362 builtin = sys.modules['__builtin__']
363 if hasattr(builtin, object.__name__):
364 builtinobject = getattr(builtin, object.__name__)
365 if builtinobject is object:
366 return builtin
368 def findsource(object):
369 """Return the entire source file and starting line number for an object.
371 The argument may be a module, class, method, function, traceback, frame,
372 or code object. The source code is returned as a list of all the lines
373 in the file and the line number indexes a line in that list. An IOError
374 is raised if the source code cannot be retrieved."""
375 try:
376 file = open(getsourcefile(object))
377 except (TypeError, IOError):
378 raise IOError, 'could not get source code'
379 lines = file.readlines()
380 file.close()
382 if ismodule(object):
383 return lines, 0
385 if isclass(object):
386 name = object.__name__
387 pat = re.compile(r'^\s*class\s*' + name + r'\b')
388 for i in range(len(lines)):
389 if pat.match(lines[i]): return lines, i
390 else: raise IOError, 'could not find class definition'
392 if ismethod(object):
393 object = object.im_func
394 if isfunction(object):
395 object = object.func_code
396 if istraceback(object):
397 object = object.tb_frame
398 if isframe(object):
399 object = object.f_code
400 if iscode(object):
401 if not hasattr(object, 'co_firstlineno'):
402 raise IOError, 'could not find function definition'
403 lnum = object.co_firstlineno - 1
404 pat = re.compile(r'^\s*def\s')
405 while lnum > 0:
406 if pat.match(lines[lnum]): break
407 lnum = lnum - 1
408 return lines, lnum
410 def getcomments(object):
411 """Get lines of comments immediately preceding an object's source code."""
412 try: lines, lnum = findsource(object)
413 except IOError: return None
415 if ismodule(object):
416 # Look for a comment block at the top of the file.
417 start = 0
418 if lines and lines[0][:2] == '#!': start = 1
419 while start < len(lines) and string.strip(lines[start]) in ['', '#']:
420 start = start + 1
421 if start < len(lines) and lines[start][:1] == '#':
422 comments = []
423 end = start
424 while end < len(lines) and lines[end][:1] == '#':
425 comments.append(string.expandtabs(lines[end]))
426 end = end + 1
427 return string.join(comments, '')
429 # Look for a preceding block of comments at the same indentation.
430 elif lnum > 0:
431 indent = indentsize(lines[lnum])
432 end = lnum - 1
433 if end >= 0 and string.lstrip(lines[end])[:1] == '#' and \
434 indentsize(lines[end]) == indent:
435 comments = [string.lstrip(string.expandtabs(lines[end]))]
436 if end > 0:
437 end = end - 1
438 comment = string.lstrip(string.expandtabs(lines[end]))
439 while comment[:1] == '#' and indentsize(lines[end]) == indent:
440 comments[:0] = [comment]
441 end = end - 1
442 if end < 0: break
443 comment = string.lstrip(string.expandtabs(lines[end]))
444 while comments and string.strip(comments[0]) == '#':
445 comments[:1] = []
446 while comments and string.strip(comments[-1]) == '#':
447 comments[-1:] = []
448 return string.join(comments, '')
450 class ListReader:
451 """Provide a readline() method to return lines from a list of strings."""
452 def __init__(self, lines):
453 self.lines = lines
454 self.index = 0
456 def readline(self):
457 i = self.index
458 if i < len(self.lines):
459 self.index = i + 1
460 return self.lines[i]
461 else: return ''
463 class EndOfBlock(Exception): pass
465 class BlockFinder:
466 """Provide a tokeneater() method to detect the end of a code block."""
467 def __init__(self):
468 self.indent = 0
469 self.started = 0
470 self.last = 0
472 def tokeneater(self, type, token, (srow, scol), (erow, ecol), line):
473 if not self.started:
474 if type == tokenize.NAME: self.started = 1
475 elif type == tokenize.NEWLINE:
476 self.last = srow
477 elif type == tokenize.INDENT:
478 self.indent = self.indent + 1
479 elif type == tokenize.DEDENT:
480 self.indent = self.indent - 1
481 if self.indent == 0: raise EndOfBlock, self.last
483 def getblock(lines):
484 """Extract the block of code at the top of the given list of lines."""
485 try:
486 tokenize.tokenize(ListReader(lines).readline, BlockFinder().tokeneater)
487 except EndOfBlock, eob:
488 return lines[:eob.args[0]]
490 def getsourcelines(object):
491 """Return a list of source lines and starting line number for an object.
493 The argument may be a module, class, method, function, traceback, frame,
494 or code object. The source code is returned as a list of the lines
495 corresponding to the object and the line number indicates where in the
496 original source file the first line of code was found. An IOError is
497 raised if the source code cannot be retrieved."""
498 lines, lnum = findsource(object)
500 if ismodule(object): return lines, 0
501 else: return getblock(lines[lnum:]), lnum + 1
503 def getsource(object):
504 """Return the text of the source code for an object.
506 The argument may be a module, class, method, function, traceback, frame,
507 or code object. The source code is returned as a single string. An
508 IOError is raised if the source code cannot be retrieved."""
509 lines, lnum = getsourcelines(object)
510 return string.join(lines, '')
512 # --------------------------------------------------- class tree extraction
513 def walktree(classes, children, parent):
514 """Recursive helper function for getclasstree()."""
515 results = []
516 classes.sort(lambda a, b: cmp(a.__name__, b.__name__))
517 for c in classes:
518 results.append((c, c.__bases__))
519 if children.has_key(c):
520 results.append(walktree(children[c], children, c))
521 return results
523 def getclasstree(classes, unique=0):
524 """Arrange the given list of classes into a hierarchy of nested lists.
526 Where a nested list appears, it contains classes derived from the class
527 whose entry immediately precedes the list. Each entry is a 2-tuple
528 containing a class and a tuple of its base classes. If the 'unique'
529 argument is true, exactly one entry appears in the returned structure
530 for each class in the given list. Otherwise, classes using multiple
531 inheritance and their descendants will appear multiple times."""
532 children = {}
533 roots = []
534 for c in classes:
535 if c.__bases__:
536 for parent in c.__bases__:
537 if not children.has_key(parent):
538 children[parent] = []
539 children[parent].append(c)
540 if unique and parent in classes: break
541 elif c not in roots:
542 roots.append(c)
543 for parent in children.keys():
544 if parent not in classes:
545 roots.append(parent)
546 return walktree(roots, children, None)
548 # ------------------------------------------------ argument list extraction
549 # These constants are from Python's compile.h.
550 CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS = 1, 2, 4, 8
552 def getargs(co):
553 """Get information about the arguments accepted by a code object.
555 Three things are returned: (args, varargs, varkw), where 'args' is
556 a list of argument names (possibly containing nested lists), and
557 'varargs' and 'varkw' are the names of the * and ** arguments or None."""
558 if not iscode(co): raise TypeError, 'arg is not a code object'
560 code = co.co_code
561 nargs = co.co_argcount
562 names = co.co_varnames
563 args = list(names[:nargs])
564 step = 0
566 # The following acrobatics are for anonymous (tuple) arguments.
567 for i in range(nargs):
568 if args[i][:1] in ['', '.']:
569 stack, remain, count = [], [], []
570 while step < len(code):
571 op = ord(code[step])
572 step = step + 1
573 if op >= dis.HAVE_ARGUMENT:
574 opname = dis.opname[op]
575 value = ord(code[step]) + ord(code[step+1])*256
576 step = step + 2
577 if opname in ['UNPACK_TUPLE', 'UNPACK_SEQUENCE']:
578 remain.append(value)
579 count.append(value)
580 elif opname == 'STORE_FAST':
581 stack.append(names[value])
582 remain[-1] = remain[-1] - 1
583 while remain[-1] == 0:
584 remain.pop()
585 size = count.pop()
586 stack[-size:] = [stack[-size:]]
587 if not remain: break
588 remain[-1] = remain[-1] - 1
589 if not remain: break
590 args[i] = stack[0]
592 varargs = None
593 if co.co_flags & CO_VARARGS:
594 varargs = co.co_varnames[nargs]
595 nargs = nargs + 1
596 varkw = None
597 if co.co_flags & CO_VARKEYWORDS:
598 varkw = co.co_varnames[nargs]
599 return args, varargs, varkw
601 def getargspec(func):
602 """Get the names and default values of a function's arguments.
604 A tuple of four things is returned: (args, varargs, varkw, defaults).
605 'args' is a list of the argument names (it may contain nested lists).
606 'varargs' and 'varkw' are the names of the * and ** arguments or None.
607 'defaults' is an n-tuple of the default values of the last n arguments."""
608 if not isfunction(func): raise TypeError, 'arg is not a Python function'
609 args, varargs, varkw = getargs(func.func_code)
610 return args, varargs, varkw, func.func_defaults
612 def getargvalues(frame):
613 """Get information about arguments passed into a particular frame.
615 A tuple of four things is returned: (args, varargs, varkw, locals).
616 'args' is a list of the argument names (it may contain nested lists).
617 'varargs' and 'varkw' are the names of the * and ** arguments or None.
618 'locals' is the locals dictionary of the given frame."""
619 args, varargs, varkw = getargs(frame.f_code)
620 return args, varargs, varkw, frame.f_locals
622 def joinseq(seq):
623 if len(seq) == 1:
624 return '(' + seq[0] + ',)'
625 else:
626 return '(' + string.join(seq, ', ') + ')'
628 def strseq(object, convert, join=joinseq):
629 """Recursively walk a sequence, stringifying each element."""
630 if type(object) in [types.ListType, types.TupleType]:
631 return join(map(lambda o, c=convert, j=join: strseq(o, c, j), object))
632 else:
633 return convert(object)
635 def formatargspec(args, varargs=None, varkw=None, defaults=None,
636 formatarg=str,
637 formatvarargs=lambda name: '*' + name,
638 formatvarkw=lambda name: '**' + name,
639 formatvalue=lambda value: '=' + repr(value),
640 join=joinseq):
641 """Format an argument spec from the 4 values returned by getargspec.
643 The first four arguments are (args, varargs, varkw, defaults). The
644 other four arguments are the corresponding optional formatting functions
645 that are called to turn names and values into strings. The ninth
646 argument is an optional function to format the sequence of arguments."""
647 specs = []
648 if defaults:
649 firstdefault = len(args) - len(defaults)
650 for i in range(len(args)):
651 spec = strseq(args[i], formatarg, join)
652 if defaults and i >= firstdefault:
653 spec = spec + formatvalue(defaults[i - firstdefault])
654 specs.append(spec)
655 if varargs:
656 specs.append(formatvarargs(varargs))
657 if varkw:
658 specs.append(formatvarkw(varkw))
659 return '(' + string.join(specs, ', ') + ')'
661 def formatargvalues(args, varargs, varkw, locals,
662 formatarg=str,
663 formatvarargs=lambda name: '*' + name,
664 formatvarkw=lambda name: '**' + name,
665 formatvalue=lambda value: '=' + repr(value),
666 join=joinseq):
667 """Format an argument spec from the 4 values returned by getargvalues.
669 The first four arguments are (args, varargs, varkw, locals). The
670 next four arguments are the corresponding optional formatting functions
671 that are called to turn names and values into strings. The ninth
672 argument is an optional function to format the sequence of arguments."""
673 def convert(name, locals=locals,
674 formatarg=formatarg, formatvalue=formatvalue):
675 return formatarg(name) + formatvalue(locals[name])
676 specs = []
677 for i in range(len(args)):
678 specs.append(strseq(args[i], convert, join))
679 if varargs:
680 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
681 if varkw:
682 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
683 return '(' + string.join(specs, ', ') + ')'
685 # -------------------------------------------------- stack frame extraction
686 def getframeinfo(frame, context=1):
687 """Get information about a frame or traceback object.
689 A tuple of five things is returned: the filename, the line number of
690 the current line, the function name, a list of lines of context from
691 the source code, and the index of the current line within that list.
692 The optional second argument specifies the number of lines of context
693 to return, which are centered around the current line."""
694 if istraceback(frame):
695 frame = frame.tb_frame
696 if not isframe(frame):
697 raise TypeError, 'arg is not a frame or traceback object'
699 filename = getsourcefile(frame)
700 lineno = getlineno(frame)
701 if context > 0:
702 start = lineno - 1 - context//2
703 try:
704 lines, lnum = findsource(frame)
705 except IOError:
706 lines = index = None
707 else:
708 start = max(start, 1)
709 start = min(start, len(lines) - context)
710 lines = lines[start:start+context]
711 index = lineno - 1 - start
712 else:
713 lines = index = None
715 return (filename, lineno, frame.f_code.co_name, lines, index)
717 def getlineno(frame):
718 """Get the line number from a frame object, allowing for optimization."""
719 # Written by Marc-André Lemburg; revised by Jim Hugunin and Fredrik Lundh.
720 lineno = frame.f_lineno
721 code = frame.f_code
722 if hasattr(code, 'co_lnotab'):
723 table = code.co_lnotab
724 lineno = code.co_firstlineno
725 addr = 0
726 for i in range(0, len(table), 2):
727 addr = addr + ord(table[i])
728 if addr > frame.f_lasti: break
729 lineno = lineno + ord(table[i+1])
730 return lineno
732 def getouterframes(frame, context=1):
733 """Get a list of records for a frame and all higher (calling) frames.
735 Each record contains a frame object, filename, line number, function
736 name, a list of lines of context, and index within the context."""
737 framelist = []
738 while frame:
739 framelist.append((frame,) + getframeinfo(frame, context))
740 frame = frame.f_back
741 return framelist
743 def getinnerframes(tb, context=1):
744 """Get a list of records for a traceback's frame and all lower frames.
746 Each record contains a frame object, filename, line number, function
747 name, a list of lines of context, and index within the context."""
748 framelist = []
749 while tb:
750 framelist.append((tb.tb_frame,) + getframeinfo(tb, context))
751 tb = tb.tb_next
752 return framelist
754 def currentframe():
755 """Return the frame object for the caller's stack frame."""
756 try:
757 raise 'catch me'
758 except:
759 return sys.exc_traceback.tb_frame.f_back
761 if hasattr(sys, '_getframe'): currentframe = sys._getframe
763 def stack(context=1):
764 """Return a list of records for the stack above the caller's frame."""
765 return getouterframes(currentframe().f_back, context)
767 def trace(context=1):
768 """Return a list of records for the stack below the current exception."""
769 return getinnerframes(sys.exc_traceback, context)