23 from __future__
import print_function, division, absolute_import
25 __all__ = [
'getCallerFrame',
'getStackFrame',
'StackFrame',
'getCallStack']
27 from builtins
import object
36 """Retrieve the frame for the caller
38 By "caller", we mean our user's caller.
42 relative : `int`, non-negative
43 Number of frames above the caller to retrieve.
47 frame : `__builtin__.Frame`
50 frame = inspect.currentframe().f_back.f_back
51 for ii
in range(relative):
57 """Retrieve the stack frame for the caller
59 By "caller", we mean our user's caller.
63 relative : `int`, non-negative
64 Number of frames 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
79 getting a stack trace by the fact that it does not look up the
80 source code until it 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` or `None`
91 The actual content being executed. If not provided, it will be
94 _STRIP =
"/python/lsst/"
96 def __init__(self, filename, lineno, function, content=None):
97 loc = filename.rfind(self.
_STRIP)
99 filename = filename[loc + len(self.
_STRIP):]
107 """Getter for content being executed
109 Load from file on demand.
117 """Construct from a Frame object
119 inspect.currentframe() provides a Frame object. This is
120 a convenience constructor to interpret that Frame object.
125 Frame object to interpret.
129 output : `StackFrame`
132 filename = frame.f_code.co_filename
133 lineno = frame.f_lineno
134 function = frame.f_code.co_name
135 return cls(filename, lineno, function)
141 """Format for printing
146 Print full details, including content being executed?
155 result +=
"\n %s" % (self.
content,)
160 """Retrieve the call stack for the caller
162 By "caller", we mean our user's caller - we don't include ourselves
165 The result is ordered with the most recent frame last.
169 skip : `int`, non-negative
170 Number of stack frames above caller to skip.
174 output : `list` of `StackFrame`
180 stack.append(StackFrame.fromFrame(frame))
182 return list(reversed(stack))