MCPcopy Create free account
hub / github.com/pytest-dev/pytest / getsource

Method getsource

src/_pytest/_code/code.py:244–268  ·  view source on GitHub ↗

Return failing source code.

(
        self, astcache: Optional[Dict[Union[str, Path], ast.AST]] = None
    )

Source from the content-addressed store, hash-verified

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
249 # within exception info printing
250 source = self.frame.code.fullsource
251 if source is None:
252 return None
253 key = astnode = None
254 if astcache is not None:
255 key = self.frame.code.path
256 if key is not None:
257 astnode = astcache.get(key, None)
258 start = self.getfirstlinesource()
259 try:
260 astnode, _, end = getstatementrange_ast(
261 self.lineno, source, astnode=astnode
262 )
263 except SyntaxError:
264 end = self.lineno + 1
265 else:
266 if key is not None and astcache is not None:
267 astcache[key] = astnode
268 return source[start:end]
269
270 source = property(getsource)
271

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