Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Doc/c-api/reflection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ Reflection
.. versionadded:: 3.9


.. c:function:: int PyFrame_GetLastInstr(PyFrameObject *frame)

Get the index of last attempted instruction in bytecode of *frame*.

Return ``-1`` for new frame (not run yet).

*frame* must not be ``NULL``.

.. versionadded:: 3.9


.. c:function:: int PyFrame_GetLineNumber(PyFrameObject *frame)

Return the line number that *frame* is currently executing.
Expand Down
2 changes: 2 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ Build and C API Changes

* New :c:func:`PyFrame_GetCode` function: return a borrowed reference to the
frame code.
New :c:func:`PyFrame_GetLastInstr` function: get the index of last attempted
instruction in bytecode of a frame.
(Contributed by Victor Stinner in :issue:`40421`.)

* Add :c:func:`PyFrame_GetLineNumber` to the limited C API.
Expand Down
2 changes: 2 additions & 0 deletions Include/cpython/frameobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ PyAPI_FUNC(int) PyFrame_ClearFreeList(void);

PyAPI_FUNC(void) _PyFrame_DebugMallocStats(FILE *out);

PyAPI_FUNC(int) PyFrame_GetLastInstr(PyFrameObject *frame);

#ifdef __cplusplus
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
New :c:func:`PyFrame_GetLastInstr` function: get the index of last attempted
instruction in bytecode of a frame.
8 changes: 8 additions & 0 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1229,3 +1229,11 @@ PyFrame_GetCode(PyFrameObject *frame)
assert(frame != NULL);
return frame->f_code;
}


int
PyFrame_GetLastInstr(PyFrameObject *frame)
{
assert(frame != NULL);
return frame->f_lasti;
}
6 changes: 3 additions & 3 deletions Python/traceback.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ _PyTraceBack_FromFrame(PyObject *tb_next, PyFrameObject *frame)
assert(tb_next == NULL || PyTraceBack_Check(tb_next));
assert(frame != NULL);

return tb_create_raw((PyTracebackObject *)tb_next, frame, frame->f_lasti,
return tb_create_raw((PyTracebackObject *)tb_next, frame,
PyFrame_GetLastInstr(frame),
PyFrame_GetLineNumber(frame));
}

Expand Down Expand Up @@ -767,8 +768,7 @@ dump_frame(int fd, PyFrameObject *frame)
PUTS(fd, "???");
}

/* PyFrame_GetLineNumber() was introduced in Python 2.7.0 and 3.2.0 */
lineno = PyCode_Addr2Line(code, frame->f_lasti);
lineno = PyCode_Addr2Line(code, PyFrame_GetLastInstr(frame));
PUTS(fd, ", line ");
if (lineno >= 0) {
_Py_DumpDecimal(fd, (unsigned long)lineno);
Expand Down