28 __all__ = [
'getCallerFrame',
'getStackFrame',
'StackFrame',
'getCallStack']
35 """Get the frame for the user's caller.
39 relative : `int`, optional
40 Number of frames (0 or more) above the caller to retrieve.
45 frame : `__builtin__.Frame`
50 This function is excluded from the frame.
52 frame = inspect.currentframe().f_back.f_back
53 for ii
in range(relative):
59 """Get the `StackFrame` for the user's caller.
63 relative : `int`, optional
64 Number of frames (0 or more) above the caller to retrieve.
69 Stack frame for the caller.
72 return StackFrame.fromFrame(frame)
76 """A single element of the stack trace.
78 This differs slightly from the standard system mechanisms for getting a
79 stack trace by the fact that it does not look up the source code until it
80 is absolutely necessary, reducing the I/O.
85 Name of file containing the code being executed.
87 Line number of file being executed.
89 Function name being executed.
90 content : `str`, optional
91 The actual content being executed. If not provided, it will be loaded
96 This differs slightly from the standard system mechanisms for getting a
97 stack trace by the fact that it does not look up the source code until it
98 is absolutely necessary, reducing the I/O.
105 _STRIP =
"/python/lsst/"
106 """String to strip from the ``filename`` in the constructor."""
108 def __init__(self, filename, lineno, function, content=None):
109 loc = filename.rfind(self.
_STRIP)
111 filename = filename[loc + len(self.
_STRIP):]
119 """Content being executed (loaded on demand) (`str`).
127 """Construct from a Frame object.
132 Frame object to interpret, such as from `inspect.currentframe`.
136 stackFrame : `StackFrame`
137 A `StackFrame` instance.
141 `inspect.currentframe` provides a Frame object. This is a convenience
142 constructor to interpret that Frame object:
145 >>> stackFrame = StackFrame.fromFrame(inspect.currentframe())
147 filename = frame.f_code.co_filename
148 lineno = frame.f_lineno
149 function = frame.f_code.co_name
150 return cls(filename, lineno, function)
156 """Format for printing.
160 full : `bool`, optional
161 If `True`, output includes the conentent (`StackFrame.content`)
162 being executed. Default is `False`.
171 result +=
"\n %s" % (self.
content,)
176 """Retrieve the call stack for the caller.
180 skip : `int`, non-negative
181 Number of stack frames above caller to skip.
185 output : `list` of `StackFrame`
186 The call stack. The `list` is ordered with the most recent frame to
191 This function is excluded from the call stack.
196 stack.append(StackFrame.fromFrame(frame))
198 return list(reversed(stack))