23 from __future__
import print_function, division, absolute_import
25 __all__ = [
'getCallerFrame',
'getStackFrame',
'StackFrame',
'getCallStack']
27 from builtins
import object
34 """Retrieve the frame for the caller
36 By "caller", we mean our user's caller.
40 relative : `int`, non-negative
41 Number of frames above the caller to retrieve.
45 frame : `__builtin__.Frame`
48 frame = inspect.currentframe().f_back.f_back
49 for ii
in range(relative):
55 """Retrieve the stack frame for the caller
57 By "caller", we mean our user's caller.
61 relative : `int`, non-negative
62 Number of frames above the caller to retrieve.
67 Stack frame for the caller.
70 return StackFrame.fromFrame(frame)
74 """A single element of the stack trace
76 This differs slightly from the standard system mechanisms for
77 getting a stack trace by the fact that it does not look up the
78 source code until it is absolutely necessary, reducing the I/O.
83 Name of file containing the code being executed.
85 Line number of file being executed.
87 Function name being executed.
88 content : `str` or `None`
89 The actual content being executed. If not provided, it will be
92 _STRIP =
"/python/lsst/"
94 def __init__(self, filename, lineno, function, content=None):
95 loc = filename.rfind(self.
_STRIP)
97 filename = filename[loc + len(self.
_STRIP):]
105 """Getter for content being executed
107 Load from file on demand.
115 """Construct from a Frame object
117 inspect.currentframe() provides a Frame object. This is
118 a convenience constructor to interpret that Frame object.
123 Frame object to interpret.
127 output : `StackFrame`
130 filename = frame.f_code.co_filename
131 lineno = frame.f_lineno
132 function = frame.f_code.co_name
133 return cls(filename, lineno, function)
139 """Format for printing
144 Print full details, including content being executed?
153 result +=
"\n %s" % (self.
content,)
158 """Retrieve the call stack for the caller
160 By "caller", we mean our user's caller - we don't include ourselves
163 The result is ordered with the most recent frame last.
167 skip : `int`, non-negative
168 Number of stack frames above caller to skip.
172 output : `list` of `StackFrame`
178 stack.append(StackFrame.fromFrame(frame))
180 return list(reversed(stack))