Skip to content

Commit

Permalink
bpo-40429: PyFrame_GetCode() now returns a strong reference (pythonGH…
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Apr 28, 2020
1 parent 5e8c691 commit 8852ad4
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 26 deletions.
7 changes: 4 additions & 3 deletions Doc/c-api/reflection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ Reflection
.. c:function:: int PyFrame_GetCode(PyFrameObject *frame)
Return a borrowed reference to the *frame* code.
The frame code cannot be ``NULL``.
Get the *frame* code.
*frame* must not be ``NULL``.
Return a strong reference.
*frame* must not be ``NULL``. The result (frame code) cannot be ``NULL``.
.. versionadded:: 3.9
Expand Down
3 changes: 1 addition & 2 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,7 @@ Optimizations
Build and C API Changes
=======================

* New :c:func:`PyFrame_GetCode` function: return a borrowed reference to the
frame code.
* New :c:func:`PyFrame_GetCode` function: get a frame code.
(Contributed by Victor Stinner in :issue:`40421`.)

* Add :c:func:`PyFrame_GetLineNumber` to the limited C API.
Expand Down
7 changes: 6 additions & 1 deletion Modules/_lsprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,14 +390,19 @@ profiler_callback(PyObject *self, PyFrameObject *frame, int what,
{
PyCodeObject *code = PyFrame_GetCode(frame);
ptrace_enter_call(self, (void *)code, (PyObject *)code);
Py_DECREF(code);
break;
}

/* the 'frame' of a called function is about to finish
(either normally or with an exception) */
case PyTrace_RETURN:
ptrace_leave_call(self, (void *)PyFrame_GetCode(frame));
{
PyCodeObject *code = PyFrame_GetCode(frame);
ptrace_leave_call(self, (void *)code);
Py_DECREF(code);
break;
}

/* case PyTrace_EXCEPTION:
If the exception results in the function exiting, a
Expand Down
19 changes: 9 additions & 10 deletions Modules/_tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,26 +335,24 @@ hashtable_compare_traceback(_Py_hashtable_t *ht, const void *pkey,
static void
tracemalloc_get_frame(PyFrameObject *pyframe, frame_t *frame)
{
PyCodeObject *code;
PyObject *filename;
_Py_hashtable_entry_t *entry;
int lineno;

frame->filename = unknown_filename;
lineno = PyFrame_GetLineNumber(pyframe);
if (lineno < 0)
int lineno = PyFrame_GetLineNumber(pyframe);
if (lineno < 0) {
lineno = 0;
}
frame->lineno = (unsigned int)lineno;

code = PyFrame_GetCode(pyframe);
if (code->co_filename == NULL) {
PyCodeObject *code = PyFrame_GetCode(pyframe);
PyObject *filename = code->co_filename;
Py_DECREF(code);

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

filename = code->co_filename;
assert(filename != NULL);
if (filename == NULL)
return;
Expand All @@ -375,6 +373,7 @@ tracemalloc_get_frame(PyFrameObject *pyframe, frame_t *frame)
}

/* intern the filename */
_Py_hashtable_entry_t *entry;
entry = _Py_HASHTABLE_GET_ENTRY(tracemalloc_filenames, filename);
if (entry != NULL) {
_Py_HASHTABLE_ENTRY_READ_KEY(tracemalloc_filenames, entry, filename);
Expand Down
1 change: 1 addition & 0 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1237,5 +1237,6 @@ PyFrame_GetCode(PyFrameObject *frame)
assert(frame != NULL);
PyCodeObject *code = frame->f_code;
assert(code != NULL);
Py_INCREF(code);
return code;
}
4 changes: 2 additions & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -8031,15 +8031,15 @@ super_init(PyObject *self, PyObject *args, PyObject *kwds)
/* Call super(), without args -- fill in from __class__
and first local variable on the stack. */
PyFrameObject *f;
PyCodeObject *co;
Py_ssize_t i, n;
f = PyThreadState_GetFrame(_PyThreadState_GET());
if (f == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"super(): no current frame");
return -1;
}
co = PyFrame_GetCode(f);
PyCodeObject *co = PyFrame_GetCode(f);
Py_DECREF(co); // use a borrowed reference
if (co->co_argcount == 0) {
PyErr_SetString(PyExc_RuntimeError,
"super(): no arguments");
Expand Down
6 changes: 5 additions & 1 deletion Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,8 @@ is_internal_frame(PyFrameObject *frame)

PyCodeObject *code = PyFrame_GetCode(frame);
PyObject *filename = code->co_filename;
Py_DECREF(code);

if (filename == NULL) {
return 0;
}
Expand Down Expand Up @@ -850,7 +852,9 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
}
else {
globals = f->f_globals;
*filename = PyFrame_GetCode(f)->co_filename;
PyCodeObject *code = PyFrame_GetCode(f);
*filename = code->co_filename;
Py_DECREF(code);
Py_INCREF(*filename);
*lineno = PyFrame_GetLineNumber(f);
}
Expand Down
1 change: 1 addition & 0 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,7 @@ remove_importlib_frames(PyThreadState *tstate)
else {
prev_link = (PyObject **) &traceback->tb_next;
}
Py_DECREF(code);
tb = next;
}
done:
Expand Down
13 changes: 6 additions & 7 deletions Python/traceback.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
err = PyErr_CheckSignals();
}
}
Py_DECREF(code);
tb = tb->tb_next;
}
if (err == 0 && cnt > TB_RECURSIVE_CUTOFF) {
Expand Down Expand Up @@ -752,12 +753,9 @@ _Py_DumpASCII(int fd, PyObject *text)
static void
dump_frame(int fd, PyFrameObject *frame)
{
PyCodeObject *code;
int lineno;

code = PyFrame_GetCode(frame);
PyCodeObject *code = PyFrame_GetCode(frame);
PUTS(fd, " File ");
if (code != NULL && code->co_filename != NULL
if (code->co_filename != NULL
&& PyUnicode_Check(code->co_filename))
{
PUTS(fd, "\"");
Expand All @@ -768,7 +766,7 @@ dump_frame(int fd, PyFrameObject *frame)
}

/* PyFrame_GetLineNumber() was introduced in Python 2.7.0 and 3.2.0 */
lineno = PyCode_Addr2Line(code, frame->f_lasti);
int lineno = PyCode_Addr2Line(code, frame->f_lasti);
PUTS(fd, ", line ");
if (lineno >= 0) {
_Py_DumpDecimal(fd, (unsigned long)lineno);
Expand All @@ -778,7 +776,7 @@ dump_frame(int fd, PyFrameObject *frame)
}
PUTS(fd, " in ");

if (code != NULL && code->co_name != NULL
if (code->co_name != NULL
&& PyUnicode_Check(code->co_name)) {
_Py_DumpASCII(fd, code->co_name);
}
Expand All @@ -787,6 +785,7 @@ dump_frame(int fd, PyFrameObject *frame)
}

PUTS(fd, "\n");
Py_DECREF(code);
}

static void
Expand Down

0 comments on commit 8852ad4

Please sign in to comment.