A single entry in a Traceback.
| 189 | |
| 190 | |
| 191 | class TracebackEntry: |
| 192 | """A single entry in a Traceback.""" |
| 193 | |
| 194 | __slots__ = ("_rawentry", "_excinfo", "_repr_style") |
| 195 | |
| 196 | def __init__( |
| 197 | self, |
| 198 | rawentry: TracebackType, |
| 199 | excinfo: Optional["ReferenceType[ExceptionInfo[BaseException]]"] = None, |
| 200 | ) -> None: |
| 201 | self._rawentry = rawentry |
| 202 | self._excinfo = excinfo |
| 203 | self._repr_style: Optional['Literal["short", "long"]'] = None |
| 204 | |
| 205 | @property |
| 206 | def lineno(self) -> int: |
| 207 | return self._rawentry.tb_lineno - 1 |
| 208 | |
| 209 | def set_repr_style(self, mode: "Literal['short', 'long']") -> None: |
| 210 | assert mode in ("short", "long") |
| 211 | self._repr_style = mode |
| 212 | |
| 213 | @property |
| 214 | def frame(self) -> Frame: |
| 215 | return Frame(self._rawentry.tb_frame) |
| 216 | |
| 217 | @property |
| 218 | def relline(self) -> int: |
| 219 | return self.lineno - self.frame.code.firstlineno |
| 220 | |
| 221 | def __repr__(self) -> str: |
| 222 | return "<TracebackEntry %s:%d>" % (self.frame.code.path, self.lineno + 1) |
| 223 | |
| 224 | @property |
| 225 | def statement(self) -> "Source": |
| 226 | """_pytest._code.Source object for the current statement.""" |
| 227 | source = self.frame.code.fullsource |
| 228 | assert source is not None |
| 229 | return source.getstatement(self.lineno) |
| 230 | |
| 231 | @property |
| 232 | def path(self) -> Union[Path, str]: |
| 233 | """Path to the source code.""" |
| 234 | return self.frame.code.path |
| 235 | |
| 236 | @property |
| 237 | def locals(self) -> Dict[str, Any]: |
| 238 | """Locals of underlying frame.""" |
| 239 | return self.frame.f_locals |
| 240 | |
| 241 | def getfirstlinesource(self) -> int: |
| 242 | return self.frame.code.firstlineno |
| 243 | |
| 244 | def getsource( |
| 245 | self, astcache: Optional[Dict[Union[str, Path], ast.AST]] = None |
| 246 | ) -> Optional["Source"]: |
| 247 | """Return failing source code.""" |
| 248 | # we use the passed in astcache to not reparse asttrees |