MCPcopy Index your code
hub / github.com/python/cpython / getframeinfo

Function getframeinfo

Lib/inspect.py:1592–1633  ·  view source on GitHub ↗

Get information about a frame or traceback object. A tuple of five things is returned: the filename, the line number of the current line, the function name, a list of lines of context from the source code, and the index of the current line within that list. The optional second argum

(frame, context=1)

Source from the content-addressed store, hash-verified

1590 return next(itertools.islice(positions_gen, instruction_index // 2, None))
1591
1592def getframeinfo(frame, context=1):
1593 """Get information about a frame or traceback object.
1594
1595 A tuple of five things is returned: the filename, the line number of
1596 the current line, the function name, a list of lines of context from
1597 the source code, and the index of the current line within that list.
1598 The optional second argument specifies the number of lines of context
1599 to return, which are centered around the current line."""
1600 if istraceback(frame):
1601 positions = _get_code_position_from_tb(frame)
1602 lineno = frame.tb_lineno
1603 frame = frame.tb_frame
1604 else:
1605 lineno = frame.f_lineno
1606 positions = _get_code_position(frame.f_code, frame.f_lasti)
1607
1608 if positions[0] is None:
1609 frame, *positions = (frame, lineno, *positions[1:])
1610 else:
1611 frame, *positions = (frame, *positions)
1612
1613 lineno = positions[0]
1614
1615 if not isframe(frame):
1616 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
1617
1618 filename = getsourcefile(frame) or getfile(frame)
1619 if context > 0:
1620 start = lineno - 1 - context//2
1621 try:
1622 lines, lnum = findsource(frame)
1623 except OSError:
1624 lines = index = None
1625 else:
1626 start = max(0, min(start, len(lines) - context))
1627 lines = lines[start:start+context]
1628 index = lineno - 1 - start
1629 else:
1630 lines = index = None
1631
1632 return Traceback(filename, lineno, frame.f_code.co_name, lines,
1633 index, positions=dis.Positions(*positions))
1634
1635def getlineno(frame):
1636 """Get the line number from a frame object, allowing for optimization."""

Callers 2

getouterframesFunction · 0.85
getinnerframesFunction · 0.85

Calls 9

istracebackFunction · 0.85
isframeFunction · 0.85
getsourcefileFunction · 0.85
getfileFunction · 0.85
findsourceFunction · 0.85
_get_code_positionFunction · 0.70
TracebackClass · 0.70
formatMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…