Skip to content
15 changes: 15 additions & 0 deletions Include/cpython/frameobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,18 @@ PyAPI_FUNC(int) _PyFrame_IsEntryFrame(PyFrameObject *frame);

PyAPI_FUNC(int) PyFrame_FastToLocalsWithError(PyFrameObject *f);
PyAPI_FUNC(void) PyFrame_FastToLocals(PyFrameObject *);

/* The following functions are for use by debuggers and other tools
* implementing custom frame evaluators with PEP 523. */

/* Returns the code object of the frame.
Comment thread
markshannon marked this conversation as resolved.
Outdated
* Does not raise an exception. */
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somewhere I saw the expression: "This function cannot fail" but I can no longer find it in the doc.

PyAPI_FUNC(PyCodeObject *) _PyInterpreterFrame_GetCode(struct _PyInterpreterFrame *frame);

/* Returns a byte ofsset into the last executed instruction.
Comment thread
markshannon marked this conversation as resolved.
* Does not raise an exception. */
PyAPI_FUNC(int) _PyInterpreterFrame_GetLasti(struct _PyInterpreterFrame *frame);

/* Returns the currently executing line number, or -1 if there is no line number.
* Does not raise an exception. */
PyAPI_FUNC(int) _PyInterpreterFrame_GetLine(struct _PyInterpreterFrame *frame);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using struct here is confusing the Windows compiler

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add struct _PyInterpreterFrame; somewhere to declare that "this structure exists" without having to declare the structure members (so it's declared as an opaque structure), to avoid such compiler warning. For example, at the top of this file.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Expose C-API functions to get the code object, lasti and line number from
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this needs a news entry since the functions are exposed technically on a private header.

the internal ``_PyInterpreterFrame`` in the limited API. The functions are:
Comment thread
markshannon marked this conversation as resolved.

``PyCodeObject * _PyInterpreterFrame_GetCode(struct _PyInterpreterFrame
*frame);``

``int _PyInterpreterFrame_GetLasti(struct _PyInterpreterFrame *frame);``

``int _PyInterpreterFrame_GetLine(struct _PyInterpreterFrame *frame);``
Comment thread
markshannon marked this conversation as resolved.
Outdated
14 changes: 14 additions & 0 deletions Python/frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,20 @@ _PyFrame_Clear(_PyInterpreterFrame *frame)
Py_DECREF(frame->f_code);
}

/* Limited API functions */
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions are excluded from the limited API, they are declared in cpython/.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the part of the API that isn't portable called? "unlimited", "non-limited"?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Limited API is supposed to be the most portable. The "not portable" API is called the "Public API".

For me, the reference documentation about that is now: https://devguide.python.org/developer-workflow/c-api/index.html#changing-python-s-c-api

See also: Include/README.rst.


PyCodeObject *_PyInterpreterFrame_GetCode(struct _PyInterpreterFrame *frame)
{
PyCodeObject *code = frame->f_code;
Py_INCREF(code);
return code;
Comment thread
markshannon marked this conversation as resolved.
}

int _PyInterpreterFrame_GetLasti(struct _PyInterpreterFrame *frame)
{
return _PyInterpreterFrame_LASTI(frame) * sizeof(_Py_CODEUNIT);
}

int
_PyInterpreterFrame_GetLine(_PyInterpreterFrame *frame)
{
Expand Down