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
Merged
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
1 change: 1 addition & 0 deletions 1 Doc/c-api/reflection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Reflection
.. c:function:: int PyFrame_GetCode(PyFrameObject *frame)

Return a borrowed reference to the *frame* code.
The frame code cannot be ``NULL``.

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

Expand Down
7 changes: 0 additions & 7 deletions 7 Modules/_tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,6 @@ tracemalloc_get_frame(PyFrameObject *pyframe, frame_t *frame)
frame->lineno = (unsigned int)lineno;

code = PyFrame_GetCode(pyframe);
if (code == NULL) {
#ifdef TRACE_DEBUG
tracemalloc_error("failed to get the code object of the frame");
#endif
return;
}

if (code->co_filename == NULL) {
#ifdef TRACE_DEBUG
tracemalloc_error("failed to get the filename of the code object");
Expand Down
52 changes: 31 additions & 21 deletions 52 Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -665,12 +665,18 @@ frame_dealloc(PyFrameObject *f)
Py_TRASHCAN_SAFE_END(f)
}

static inline Py_ssize_t
frame_nslots(PyFrameObject *frame)
{
PyCodeObject *code = frame->f_code;
return (code->co_nlocals
+ PyTuple_GET_SIZE(code->co_cellvars)
+ PyTuple_GET_SIZE(code->co_freevars));
}

static int
frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
{
PyObject **fastlocals, **p;
Py_ssize_t i, slots;

Py_VISIT(f->f_back);
Py_VISIT(f->f_code);
Py_VISIT(f->f_builtins);
Expand All @@ -679,46 +685,45 @@ frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
Py_VISIT(f->f_trace);

/* locals */
slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
fastlocals = f->f_localsplus;
for (i = slots; --i >= 0; ++fastlocals)
PyObject **fastlocals = f->f_localsplus;
for (Py_ssize_t i = frame_nslots(f); --i >= 0; ++fastlocals) {
Py_VISIT(*fastlocals);
}

/* stack */
if (f->f_stacktop != NULL) {
for (p = f->f_valuestack; p < f->f_stacktop; p++)
for (PyObject **p = f->f_valuestack; p < f->f_stacktop; p++) {
Py_VISIT(*p);
}
}
return 0;
}

static int
frame_tp_clear(PyFrameObject *f)
{
PyObject **fastlocals, **p, **oldtop;
Py_ssize_t i, slots;

/* Before anything else, make sure that this frame is clearly marked
* as being defunct! Else, e.g., a generator reachable from this
* frame may also point to this frame, believe itself to still be
* active, and try cleaning up this frame again.
*/
oldtop = f->f_stacktop;
PyObject **oldtop = f->f_stacktop;
f->f_stacktop = NULL;
f->f_executing = 0;

Py_CLEAR(f->f_trace);

/* locals */
slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
fastlocals = f->f_localsplus;
for (i = slots; --i >= 0; ++fastlocals)
PyObject **fastlocals = f->f_localsplus;
for (Py_ssize_t i = frame_nslots(f); --i >= 0; ++fastlocals) {
Py_CLEAR(*fastlocals);
}

/* stack */
if (oldtop != NULL) {
for (p = f->f_valuestack; p < oldtop; p++)
for (PyObject **p = f->f_valuestack; p < oldtop; p++) {
Py_CLEAR(*p);
}
}
return 0;
}
Expand Down Expand Up @@ -747,10 +752,10 @@ frame_sizeof(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
{
Py_ssize_t res, extras, ncells, nfrees;

ncells = PyTuple_GET_SIZE(f->f_code->co_cellvars);
nfrees = PyTuple_GET_SIZE(f->f_code->co_freevars);
extras = f->f_code->co_stacksize + f->f_code->co_nlocals +
ncells + nfrees;
PyCodeObject *code = f->f_code;
ncells = PyTuple_GET_SIZE(code->co_cellvars);
nfrees = PyTuple_GET_SIZE(code->co_freevars);
extras = code->co_stacksize + code->co_nlocals + ncells + nfrees;
/* subtract one as it is already included in PyFrameObject */
res = sizeof(PyFrameObject) + (extras-1) * sizeof(PyObject *);

Expand All @@ -764,9 +769,10 @@ static PyObject *
frame_repr(PyFrameObject *f)
{
int lineno = PyFrame_GetLineNumber(f);
PyCodeObject *code = f->f_code;
return PyUnicode_FromFormat(
"<frame at %p, file %R, line %d, code %S>",
f, f->f_code->co_filename, lineno, f->f_code->co_name);
f, code->co_filename, lineno, code->co_name);
}

static PyMethodDef frame_methods[] = {
Expand Down Expand Up @@ -940,6 +946,8 @@ _PyFrame_New_NoTrack(PyThreadState *tstate, PyCodeObject *code,
f->f_trace_opcodes = 0;
f->f_trace_lines = 1;

assert(f->f_code != NULL);

return f;
}

Expand Down Expand Up @@ -1227,5 +1235,7 @@ PyCodeObject *
PyFrame_GetCode(PyFrameObject *frame)
{
assert(frame != NULL);
return frame->f_code;
PyCodeObject *code = frame->f_code;
assert(code != NULL);
return code;
}
10 changes: 5 additions & 5 deletions 10 Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1117,11 +1117,11 @@ compute_cr_origin(int origin_depth)
}
frame = PyEval_GetFrame();
for (int i = 0; i < frame_count; ++i) {
PyObject *frameinfo = Py_BuildValue(
"OiO",
frame->f_code->co_filename,
PyFrame_GetLineNumber(frame),
frame->f_code->co_name);
PyCodeObject *code = frame->f_code;
PyObject *frameinfo = Py_BuildValue("OiO",
code->co_filename,
PyFrame_GetLineNumber(frame),
code->co_name);
if (!frameinfo) {
Py_DECREF(cr_origin);
return NULL;
Expand Down
5 changes: 0 additions & 5 deletions 5 Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -8040,11 +8040,6 @@ super_init(PyObject *self, PyObject *args, PyObject *kwds)
return -1;
}
co = PyFrame_GetCode(f);
if (co == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"super(): no code object");
return -1;
}
if (co->co_argcount == 0) {
PyErr_SetString(PyExc_RuntimeError,
"super(): no arguments");
Expand Down
4 changes: 0 additions & 4 deletions 4 Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -784,10 +784,6 @@ is_internal_frame(PyFrameObject *frame)
}

PyCodeObject *code = PyFrame_GetCode(frame);
if (code == NULL) {
return 0;
}

PyObject *filename = code->co_filename;
if (filename == NULL) {
return 0;
Expand Down
14 changes: 8 additions & 6 deletions 14 Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -5580,9 +5580,10 @@ dtrace_function_entry(PyFrameObject *f)
const char *funcname;
int lineno;

filename = PyUnicode_AsUTF8(f->f_code->co_filename);
funcname = PyUnicode_AsUTF8(f->f_code->co_name);
lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
PyCodeObject *code = f->f_code;
filename = PyUnicode_AsUTF8(code->co_filename);
funcname = PyUnicode_AsUTF8(code->co_name);
lineno = PyCode_Addr2Line(code, f->f_lasti);

PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
}
Expand All @@ -5594,9 +5595,10 @@ dtrace_function_return(PyFrameObject *f)
const char *funcname;
int lineno;

filename = PyUnicode_AsUTF8(f->f_code->co_filename);
funcname = PyUnicode_AsUTF8(f->f_code->co_name);
lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
PyCodeObject *code = f->f_code;
filename = PyUnicode_AsUTF8(code->co_filename);
funcname = PyUnicode_AsUTF8(code->co_name);
lineno = PyCode_Addr2Line(code, f->f_lasti);

PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.