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
7 changes: 4 additions & 3 deletions 7 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 3 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 7 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 19 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 1 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 4 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 6 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 1 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 13 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.