Return failing source code.
(
self, astcache: dict[str | Path, ast.AST] | None = None
)
| 284 | return self.frame.code.firstlineno |
| 285 | |
| 286 | def getsource( |
| 287 | self, astcache: dict[str | Path, ast.AST] | None = None |
| 288 | ) -> Source | None: |
| 289 | """Return failing source code.""" |
| 290 | # we use the passed in astcache to not reparse asttrees |
| 291 | # within exception info printing |
| 292 | source = self.frame.code.fullsource |
| 293 | if source is None: |
| 294 | return None |
| 295 | key = astnode = None |
| 296 | if astcache is not None: |
| 297 | key = self.frame.code.path |
| 298 | if key is not None: |
| 299 | astnode = astcache.get(key, None) |
| 300 | start = self.getfirstlinesource() |
| 301 | try: |
| 302 | astnode, _, end = getstatementrange_ast( |
| 303 | self.lineno, source, astnode=astnode |
| 304 | ) |
| 305 | except SyntaxError: |
| 306 | end = self.lineno + 1 |
| 307 | else: |
| 308 | if key is not None and astcache is not None: |
| 309 | astcache[key] = astnode |
| 310 | return source[start:end] |
| 311 | |
| 312 | source = property(getsource) |
| 313 |