Format a single stack frame
(self, frame_info: FrameInfo)
| 135 | self.skip_hidden = True |
| 136 | |
| 137 | def format_record(self, frame_info: FrameInfo) -> str: |
| 138 | """Format a single stack frame""" |
| 139 | assert isinstance(frame_info, FrameInfo) |
| 140 | |
| 141 | if isinstance(frame_info._sd, stack_data.RepeatedFrames): |
| 142 | return theme_table[self._theme_name].format( |
| 143 | [ |
| 144 | (Token, " "), |
| 145 | ( |
| 146 | Token.ExcName, |
| 147 | "[... skipping similar frames: %s]" % frame_info.description, |
| 148 | ), |
| 149 | (Token, "\n"), |
| 150 | ] |
| 151 | ) |
| 152 | |
| 153 | indent: str = " " * INDENT_SIZE |
| 154 | |
| 155 | assert isinstance(frame_info.lineno, int) |
| 156 | args, varargs, varkw, locals_ = inspect.getargvalues(frame_info.frame) |
| 157 | if frame_info.executing is not None: |
| 158 | func = frame_info.executing.code_qualname() |
| 159 | else: |
| 160 | func = "?" |
| 161 | if func == "<module>": |
| 162 | call = "" |
| 163 | else: |
| 164 | # Decide whether to include variable details or not |
| 165 | var_repr = eqrepr if self.include_vars else nullrepr |
| 166 | try: |
| 167 | scope = inspect.formatargvalues( |
| 168 | args, varargs, varkw, locals_, formatvalue=var_repr |
| 169 | ) |
| 170 | assert isinstance(scope, str) |
| 171 | call = theme_table[self._theme_name].format( |
| 172 | [(Token, "in "), (Token.VName, func), (Token.ValEm, scope)] |
| 173 | ) |
| 174 | except KeyError: |
| 175 | # This happens in situations like errors inside generator |
| 176 | # expressions, where local variables are listed in the |
| 177 | # line, but can't be extracted from the frame. I'm not |
| 178 | # 100% sure this isn't actually a bug in inspect itself, |
| 179 | # but since there's no info for us to compute with, the |
| 180 | # best we can do is report the failure and move on. Here |
| 181 | # we must *not* call any traceback construction again, |
| 182 | # because that would mess up use of %debug later on. So we |
| 183 | # simply report the failure and move on. The only |
| 184 | # limitation will be that this frame won't have locals |
| 185 | # listed in the call signature. Quite subtle problem... |
| 186 | # I can't think of a good way to validate this in a unit |
| 187 | # test, but running a script consisting of: |
| 188 | # dict( (k,v.strip()) for (k,v) in range(10) ) |
| 189 | # will illustrate the error, if this exception catch is |
| 190 | # disabled. |
| 191 | call = theme_table[self._theme_name].format( |
| 192 | [ |
| 193 | (Token, "in "), |
| 194 | (Token.VName, func), |
no test coverage detected