bpo-40421: Add PyFrame_GetCode() function (GH-19757)

PyFrame_GetCode(frame): return a borrowed reference to the frame
code.

Replace frame->f_code with PyFrame_GetCode(frame) in most code,
except in frameobject.c, genobject.c and ceval.c.

Also add PyFrame_GetLineNumber() to the limited C API.
This commit is contained in:
Victor Stinner 2020-04-28 19:01:31 +02:00 committed by GitHub
parent b8f704d219
commit a42ca74fa3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 58 additions and 25 deletions

View file

@ -762,7 +762,6 @@ is_internal_frame(PyFrameObject *frame)
{
static PyObject *importlib_string = NULL;
static PyObject *bootstrap_string = NULL;
PyObject *filename;
int contains;
if (importlib_string == NULL) {
@ -780,14 +779,23 @@ is_internal_frame(PyFrameObject *frame)
Py_INCREF(bootstrap_string);
}
if (frame == NULL || frame->f_code == NULL ||
frame->f_code->co_filename == NULL) {
if (frame == NULL) {
return 0;
}
PyCodeObject *code = PyFrame_GetCode(frame);
if (code == NULL) {
return 0;
}
PyObject *filename = code->co_filename;
if (filename == NULL) {
return 0;
}
filename = frame->f_code->co_filename;
if (!PyUnicode_Check(filename)) {
return 0;
}
contains = PyUnicode_Contains(filename, importlib_string);
if (contains < 0) {
return 0;
@ -846,7 +854,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
}
else {
globals = f->f_globals;
*filename = f->f_code->co_filename;
*filename = PyFrame_GetCode(f)->co_filename;
Py_INCREF(*filename);
*lineno = PyFrame_GetLineNumber(f);
}

View file

@ -15,7 +15,6 @@
#include "errcode.h"
#include "marshal.h"
#include "code.h"
#include "frameobject.h"
#include "importdl.h"
#include "pydtrace.h"
@ -1536,7 +1535,7 @@ remove_importlib_frames(PyThreadState *tstate)
PyTracebackObject *traceback = (PyTracebackObject *)tb;
PyObject *next = (PyObject *) traceback->tb_next;
PyFrameObject *frame = traceback->tb_frame;
PyCodeObject *code = frame->f_code;
PyCodeObject *code = PyFrame_GetCode(frame);
int now_in_importlib;
assert(PyTraceBack_Check(tb));

View file

@ -560,24 +560,23 @@ tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
tb = tb->tb_next;
}
while (tb != NULL && err == 0) {
PyCodeObject *code = PyFrame_GetCode(tb->tb_frame);
if (last_file == NULL ||
tb->tb_frame->f_code->co_filename != last_file ||
code->co_filename != last_file ||
last_line == -1 || tb->tb_lineno != last_line ||
last_name == NULL || tb->tb_frame->f_code->co_name != last_name) {
last_name == NULL || code->co_name != last_name) {
if (cnt > TB_RECURSIVE_CUTOFF) {
err = tb_print_line_repeated(f, cnt);
}
last_file = tb->tb_frame->f_code->co_filename;
last_file = code->co_filename;
last_line = tb->tb_lineno;
last_name = tb->tb_frame->f_code->co_name;
last_name = code->co_name;
cnt = 0;
}
cnt++;
if (err == 0 && cnt <= TB_RECURSIVE_CUTOFF) {
err = tb_displayline(f,
tb->tb_frame->f_code->co_filename,
tb->tb_lineno,
tb->tb_frame->f_code->co_name);
err = tb_displayline(f, code->co_filename, tb->tb_lineno,
code->co_name);
if (err == 0) {
err = PyErr_CheckSignals();
}
@ -756,7 +755,7 @@ dump_frame(int fd, PyFrameObject *frame)
PyCodeObject *code;
int lineno;
code = frame->f_code;
code = PyFrame_GetCode(frame);
PUTS(fd, " File ");
if (code != NULL && code->co_filename != NULL
&& PyUnicode_Check(code->co_filename))