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)
| 1566 | return next(itertools.islice(positions_gen, instruction_index // 2, None)) |
| 1567 | |
| 1568 | def getframeinfo(frame, context=1): |
| 1569 | """Get information about a frame or traceback object. |
| 1570 | |
| 1571 | A tuple of five things is returned: the filename, the line number of |
| 1572 | the current line, the function name, a list of lines of context from |
| 1573 | the source code, and the index of the current line within that list. |
| 1574 | The optional second argument specifies the number of lines of context |
| 1575 | to return, which are centered around the current line.""" |
| 1576 | if istraceback(frame): |
| 1577 | positions = _get_code_position_from_tb(frame) |
| 1578 | lineno = frame.tb_lineno |
| 1579 | frame = frame.tb_frame |
| 1580 | else: |
| 1581 | lineno = frame.f_lineno |
| 1582 | positions = _get_code_position(frame.f_code, frame.f_lasti) |
| 1583 | |
| 1584 | if positions[0] is None: |
| 1585 | frame, *positions = (frame, lineno, *positions[1:]) |
| 1586 | else: |
| 1587 | frame, *positions = (frame, *positions) |
| 1588 | |
| 1589 | lineno = positions[0] |
| 1590 | |
| 1591 | if not isframe(frame): |
| 1592 | raise TypeError('{!r} is not a frame or traceback object'.format(frame)) |
| 1593 | |
| 1594 | filename = getsourcefile(frame) or getfile(frame) |
| 1595 | if context > 0: |
| 1596 | start = lineno - 1 - context//2 |
| 1597 | try: |
| 1598 | lines, lnum = findsource(frame) |
| 1599 | except OSError: |
| 1600 | lines = index = None |
| 1601 | else: |
| 1602 | start = max(0, min(start, len(lines) - context)) |
| 1603 | lines = lines[start:start+context] |
| 1604 | index = lineno - 1 - start |
| 1605 | else: |
| 1606 | lines = index = None |
| 1607 | |
| 1608 | return Traceback(filename, lineno, frame.f_code.co_name, lines, |
| 1609 | index, positions=dis.Positions(*positions)) |
| 1610 | |
| 1611 | def getlineno(frame): |
| 1612 | """Get the line number from a frame object, allowing for optimization.""" |
no test coverage detected