Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

GH-96803: Add three C-API functions to make _PyInterpreterFrame less opaque for users of PEP 523. #96849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 5, 2023
Prev Previous commit
Next Next commit
Make new function part of unstable API.
  • Loading branch information
markshannon committed May 4, 2023
commit a6a6662dd73c8547f6ed611c844ff76d146a9693
8 changes: 5 additions & 3 deletions 8 Include/cpython/frameobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# error "this header file must not be included directly"
#endif

struct _PyInterpreterFrame;

/* Standard object interface */

PyAPI_FUNC(PyFrameObject *) PyFrame_New(PyThreadState *, PyCodeObject *,
Expand Down Expand Up @@ -33,12 +35,12 @@ PyAPI_FUNC(void) PyFrame_FastToLocals(PyFrameObject *);

/* Returns the code object of the frame (strong reference).
* Does not raise an exception. */
Copy link
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);
PyAPI_FUNC(PyCodeObject *) PyUnstable_InterpreterFrame_GetCode(struct _PyInterpreterFrame *frame);

/* Returns a byte ofsset into the last executed instruction.
markshannon marked this conversation as resolved.
Show resolved Hide resolved
* Does not raise an exception. */
PyAPI_FUNC(int) _PyInterpreterFrame_GetLasti(struct _PyInterpreterFrame *frame);
PyAPI_FUNC(int) PyUnstable_InterpreterFrame_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);
PyAPI_FUNC(int) PyUnstable_InterpreterFrame_GetLine(struct _PyInterpreterFrame *frame);
2 changes: 1 addition & 1 deletion 2 Modules/_tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ static void
tracemalloc_get_frame(_PyInterpreterFrame *pyframe, frame_t *frame)
{
frame->filename = &_Py_STR(anon_unknown);
int lineno = _PyInterpreterFrame_GetLine(pyframe);
int lineno = PyUnstable_InterpreterFrame_GetLine(pyframe);
if (lineno < 0) {
lineno = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion 2 Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ PyFrame_GetLineNumber(PyFrameObject *f)
return f->f_lineno;
}
else {
return _PyInterpreterFrame_GetLine(f->f_frame);
return PyUnstable_InterpreterFrame_GetLine(f->f_frame);
}
}

Expand Down
2 changes: 1 addition & 1 deletion 2 Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1328,7 +1328,7 @@ compute_cr_origin(int origin_depth, _PyInterpreterFrame *current_frame)
frame = current_frame;
for (int i = 0; i < frame_count; ++i) {
PyCodeObject *code = frame->f_code;
int line = _PyInterpreterFrame_GetLine(frame);
int line = PyUnstable_InterpreterFrame_GetLine(frame);
PyObject *frameinfo = Py_BuildValue("OiO", code->co_filename, line,
code->co_name);
if (!frameinfo) {
Expand Down
6 changes: 3 additions & 3 deletions 6 Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -5047,7 +5047,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
next_instr points the current instruction without TARGET(). */
opcode = _Py_OPCODE(*next_instr);
fprintf(stderr, "XXX lineno: %d, opcode: %d\n",
_PyInterpreterFrame_GetLine(frame), opcode);
PyUnstable_InterpreterFrame_GetLine(frame), opcode);
_PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
goto error;

Expand Down Expand Up @@ -7277,7 +7277,7 @@ dtrace_function_entry(_PyInterpreterFrame *frame)
PyCodeObject *code = frame->f_code;
filename = PyUnicode_AsUTF8(code->co_filename);
funcname = PyUnicode_AsUTF8(code->co_name);
lineno = _PyInterpreterFrame_GetLine(frame);
lineno = PyUnstable_InterpreterFrame_GetLine(frame);

PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
}
Expand All @@ -7292,7 +7292,7 @@ dtrace_function_return(_PyInterpreterFrame *frame)
PyCodeObject *code = frame->f_code;
filename = PyUnicode_AsUTF8(code->co_filename);
funcname = PyUnicode_AsUTF8(code->co_name);
lineno = _PyInterpreterFrame_GetLine(frame);
lineno = PyUnstable_InterpreterFrame_GetLine(frame);

PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
}
Expand Down
10 changes: 6 additions & 4 deletions 10 Python/frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,24 @@ _PyFrame_Clear(_PyInterpreterFrame *frame)
Py_DECREF(frame->f_code);
}

/* Limited API functions */
/* Unstable API functions */

PyCodeObject *_PyInterpreterFrame_GetCode(struct _PyInterpreterFrame *frame)
PyCodeObject *
PyUnstable_InterpreterFrame_GetCode(struct _PyInterpreterFrame *frame)
{
PyCodeObject *code = frame->f_code;
Py_INCREF(code);
return code;
}

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

int
_PyInterpreterFrame_GetLine(_PyInterpreterFrame *frame)
PyUnstable_InterpreterFrame_GetLine(_PyInterpreterFrame *frame)
{
int addr = _PyInterpreterFrame_LASTI(frame) * sizeof(_Py_CODEUNIT);
return PyCode_Addr2Line(frame->f_code, addr);
Expand Down
2 changes: 1 addition & 1 deletion 2 Python/traceback.c
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ dump_frame(int fd, _PyInterpreterFrame *frame)
PUTS(fd, "???");
}

int lineno = _PyInterpreterFrame_GetLine(frame);
int lineno = PyUnstable_InterpreterFrame_GetLine(frame);
PUTS(fd, ", line ");
if (lineno >= 0) {
_Py_DumpDecimal(fd, (size_t)lineno);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.