Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

# 

# LSST Data Management System 

# Copyright 2017 AURA/LSST. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <https://www.lsstcorp.org/LegalNotices/>. 

# 

 

__all__ = ['getCallerFrame', 'getStackFrame', 'StackFrame', 'getCallStack'] 

 

import inspect 

import linecache 

 

 

def getCallerFrame(relative=0): 

"""Retrieve the frame for the caller 

 

By "caller", we mean our user's caller. 

 

Parameters 

---------- 

relative : `int`, non-negative 

Number of frames above the caller to retrieve. 

 

Returns 

------- 

frame : `__builtin__.Frame` 

Frame for the caller. 

""" 

frame = inspect.currentframe().f_back.f_back # Our caller's caller 

for ii in range(relative): 

frame = frame.f_back 

return frame 

 

 

def getStackFrame(relative=0): 

"""Retrieve the stack frame for the caller 

 

By "caller", we mean our user's caller. 

 

Parameters 

---------- 

relative : `int`, non-negative 

Number of frames above the caller to retrieve. 

 

Returns 

------- 

frame : `StackFrame` 

Stack frame for the caller. 

""" 

frame = getCallerFrame(relative + 1) 

return StackFrame.fromFrame(frame) 

 

 

class StackFrame: 

"""A single element of the stack trace 

 

This differs slightly from the standard system mechanisms for 

getting a stack trace by the fact that it does not look up the 

source code until it is absolutely necessary, reducing the I/O. 

 

Parameters 

---------- 

filename : `str` 

Name of file containing the code being executed. 

lineno : `int` 

Line number of file being executed. 

function : `str` 

Function name being executed. 

content : `str` or `None` 

The actual content being executed. If not provided, it will be 

loaded from the file. 

""" 

_STRIP = "/python/lsst/" # String to strip from the filename 

 

def __init__(self, filename, lineno, function, content=None): 

loc = filename.rfind(self._STRIP) 

if loc > 0: 

filename = filename[loc + len(self._STRIP):] 

self.filename = filename 

self.lineno = lineno 

self.function = function 

self._content = content 

 

@property 

def content(self): 

"""Getter for content being executed 

 

Load from file on demand. 

""" 

105 ↛ 107line 105 didn't jump to line 107, because the condition on line 105 was never false if self._content is None: 

self._content = linecache.getline(self.filename, self.lineno).strip() 

return self._content 

 

@classmethod 

def fromFrame(cls, frame): 

"""Construct from a Frame object 

 

inspect.currentframe() provides a Frame object. This is 

a convenience constructor to interpret that Frame object. 

 

Parameters 

---------- 

frame : `Frame` 

Frame object to interpret. 

 

Returns 

------- 

output : `StackFrame` 

Constructed object. 

""" 

filename = frame.f_code.co_filename 

lineno = frame.f_lineno 

function = frame.f_code.co_name 

return cls(filename, lineno, function) 

 

def __repr__(self): 

return "%s(%s, %s, %s)" % (self.__class__.__name__, self.filename, self.lineno, self.function) 

 

def format(self, full=False): 

"""Format for printing 

 

Parameters 

---------- 

full : `bool` 

Print full details, including content being executed? 

 

Returns 

------- 

result : `str` 

Formatted string. 

""" 

result = " File %s:%s (%s)" % (self.filename, self.lineno, self.function) 

148 ↛ 149line 148 didn't jump to line 149, because the condition on line 148 was never true if full: 

result += "\n %s" % (self.content,) 

return result 

 

 

def getCallStack(skip=0): 

"""Retrieve the call stack for the caller 

 

By "caller", we mean our user's caller - we don't include ourselves 

or our caller. 

 

The result is ordered with the most recent frame last. 

 

Parameters 

---------- 

skip : `int`, non-negative 

Number of stack frames above caller to skip. 

 

Returns 

------- 

output : `list` of `StackFrame` 

The call stack. 

""" 

frame = getCallerFrame(skip + 1) 

stack = [] 

while frame: 

stack.append(StackFrame.fromFrame(frame)) 

frame = frame.f_back 

return list(reversed(stack))