MCPcopy
hub / github.com/pytest-dev/pytest / getsource

Method getsource

src/_pytest/_code/code.py:286–310  ·  view source on GitHub ↗

Return failing source code.

(
        self, astcache: dict[str | Path, ast.AST] | None = None
    )

Source from the content-addressed store, hash-verified

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

Callers 7

_getentrysourceMethod · 0.80
__init__Method · 0.80
test_getsourceMethod · 0.80

Calls 3

getfirstlinesourceMethod · 0.95
getstatementrange_astFunction · 0.90
getMethod · 0.45