2021-07-26 07:22:16 -03:00
|
|
|
|
2022-05-18 10:38:43 -03:00
|
|
|
#define _PY_INTERPRETER
|
|
|
|
|
2021-07-26 07:22:16 -03:00
|
|
|
#include "Python.h"
|
|
|
|
#include "frameobject.h"
|
2022-06-19 07:02:33 -03:00
|
|
|
#include "pycore_code.h" // stats
|
2021-07-26 07:22:16 -03:00
|
|
|
#include "pycore_frame.h"
|
|
|
|
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
|
2022-01-20 07:46:39 -04:00
|
|
|
#include "opcode.h"
|
2021-07-26 07:22:16 -03:00
|
|
|
|
|
|
|
int
|
2022-02-25 11:22:00 -04:00
|
|
|
_PyFrame_Traverse(_PyInterpreterFrame *frame, visitproc visit, void *arg)
|
2021-07-26 07:22:16 -03:00
|
|
|
{
|
|
|
|
Py_VISIT(frame->frame_obj);
|
|
|
|
Py_VISIT(frame->f_locals);
|
2021-11-23 05:53:24 -04:00
|
|
|
Py_VISIT(frame->f_func);
|
2021-07-26 07:22:16 -03:00
|
|
|
Py_VISIT(frame->f_code);
|
|
|
|
/* locals */
|
|
|
|
PyObject **locals = _PyFrame_GetLocalsArray(frame);
|
2021-08-25 09:44:20 -03:00
|
|
|
int i = 0;
|
|
|
|
/* locals and stack */
|
|
|
|
for (; i <frame->stacktop; i++) {
|
2021-07-26 07:22:16 -03:00
|
|
|
Py_VISIT(locals[i]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyFrameObject *
|
2022-02-25 11:22:00 -04:00
|
|
|
_PyFrame_MakeAndSetFrameObject(_PyInterpreterFrame *frame)
|
2021-07-26 07:22:16 -03:00
|
|
|
{
|
|
|
|
assert(frame->frame_obj == NULL);
|
|
|
|
PyObject *error_type, *error_value, *error_traceback;
|
|
|
|
PyErr_Fetch(&error_type, &error_value, &error_traceback);
|
2021-11-29 08:34:59 -04:00
|
|
|
|
|
|
|
PyFrameObject *f = _PyFrame_New_NoTrack(frame->f_code);
|
2021-07-26 07:22:16 -03:00
|
|
|
if (f == NULL) {
|
|
|
|
Py_XDECREF(error_type);
|
|
|
|
Py_XDECREF(error_value);
|
|
|
|
Py_XDECREF(error_traceback);
|
|
|
|
}
|
|
|
|
else {
|
2022-03-22 09:57:19 -03:00
|
|
|
assert(frame->owner != FRAME_OWNED_BY_FRAME_OBJECT);
|
|
|
|
assert(frame->owner != FRAME_CLEARED);
|
2021-11-29 08:34:59 -04:00
|
|
|
f->f_frame = frame;
|
|
|
|
frame->frame_obj = f;
|
2021-07-26 07:22:16 -03:00
|
|
|
PyErr_Restore(error_type, error_value, error_traceback);
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2021-12-06 06:13:49 -04:00
|
|
|
void
|
2022-02-25 11:22:00 -04:00
|
|
|
_PyFrame_Copy(_PyInterpreterFrame *src, _PyInterpreterFrame *dest)
|
2021-07-26 07:22:16 -03:00
|
|
|
{
|
2021-12-06 06:13:49 -04:00
|
|
|
assert(src->stacktop >= src->f_code->co_nlocalsplus);
|
|
|
|
Py_ssize_t size = ((char*)&src->localsplus[src->stacktop]) - (char *)src;
|
|
|
|
memcpy(dest, src, size);
|
2021-07-26 07:22:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2022-02-25 11:22:00 -04:00
|
|
|
take_ownership(PyFrameObject *f, _PyInterpreterFrame *frame)
|
2021-07-26 07:22:16 -03:00
|
|
|
{
|
2022-03-22 09:57:19 -03:00
|
|
|
assert(frame->owner != FRAME_OWNED_BY_FRAME_OBJECT);
|
|
|
|
assert(frame->owner != FRAME_CLEARED);
|
2021-11-29 08:34:59 -04:00
|
|
|
Py_ssize_t size = ((char*)&frame->localsplus[frame->stacktop]) - (char *)frame;
|
2022-02-25 11:22:00 -04:00
|
|
|
memcpy((_PyInterpreterFrame *)f->_f_frame_data, frame, size);
|
|
|
|
frame = (_PyInterpreterFrame *)f->_f_frame_data;
|
2021-07-26 07:22:16 -03:00
|
|
|
f->f_frame = frame;
|
2022-03-22 09:57:19 -03:00
|
|
|
frame->owner = FRAME_OWNED_BY_FRAME_OBJECT;
|
2021-07-26 07:22:16 -03:00
|
|
|
assert(f->f_back == NULL);
|
2022-07-01 07:08:20 -03:00
|
|
|
_PyInterpreterFrame *prev = frame->previous;
|
|
|
|
while (prev && _PyFrame_IsIncomplete(prev)) {
|
|
|
|
prev = prev->previous;
|
|
|
|
}
|
|
|
|
if (prev) {
|
2022-02-25 11:22:00 -04:00
|
|
|
/* Link PyFrameObjects.f_back and remove link through _PyInterpreterFrame.previous */
|
2022-07-01 07:08:20 -03:00
|
|
|
PyFrameObject *back = _PyFrame_GetFrameObject(prev);
|
2021-07-26 07:22:16 -03:00
|
|
|
if (back == NULL) {
|
|
|
|
/* Memory error here. */
|
|
|
|
assert(PyErr_ExceptionMatches(PyExc_MemoryError));
|
|
|
|
/* Nothing we can do about it */
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
f->f_back = (PyFrameObject *)Py_NewRef(back);
|
|
|
|
}
|
|
|
|
frame->previous = NULL;
|
|
|
|
}
|
|
|
|
if (!_PyObject_GC_IS_TRACKED((PyObject *)f)) {
|
|
|
|
_PyObject_GC_TRACK((PyObject *)f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-29 08:34:59 -04:00
|
|
|
void
|
2022-02-25 11:22:00 -04:00
|
|
|
_PyFrame_Clear(_PyInterpreterFrame *frame)
|
2021-07-26 07:22:16 -03:00
|
|
|
{
|
2021-11-22 10:01:23 -04:00
|
|
|
/* It is the responsibility of the owning generator/coroutine
|
2022-01-20 07:46:39 -04:00
|
|
|
* to have cleared the enclosing generator, if any. */
|
2022-03-22 09:57:19 -03:00
|
|
|
assert(frame->owner != FRAME_OWNED_BY_GENERATOR ||
|
|
|
|
_PyFrame_GetGenerator(frame)->gi_frame_state == FRAME_CLEARED);
|
2021-07-26 07:22:16 -03:00
|
|
|
if (frame->frame_obj) {
|
|
|
|
PyFrameObject *f = frame->frame_obj;
|
|
|
|
frame->frame_obj = NULL;
|
|
|
|
if (Py_REFCNT(f) > 1) {
|
|
|
|
take_ownership(f, frame);
|
|
|
|
Py_DECREF(f);
|
2021-11-29 08:34:59 -04:00
|
|
|
return;
|
2021-07-26 07:22:16 -03:00
|
|
|
}
|
|
|
|
Py_DECREF(f);
|
|
|
|
}
|
2021-12-06 06:13:49 -04:00
|
|
|
assert(frame->stacktop >= 0);
|
2021-08-25 09:44:20 -03:00
|
|
|
for (int i = 0; i < frame->stacktop; i++) {
|
|
|
|
Py_XDECREF(frame->localsplus[i]);
|
2021-07-26 07:22:16 -03:00
|
|
|
}
|
2022-01-20 07:46:39 -04:00
|
|
|
Py_XDECREF(frame->frame_obj);
|
|
|
|
Py_XDECREF(frame->f_locals);
|
|
|
|
Py_DECREF(frame->f_func);
|
|
|
|
Py_DECREF(frame->f_code);
|
2021-07-26 07:22:16 -03:00
|
|
|
}
|
2022-01-28 08:42:30 -04:00
|
|
|
|
2022-04-07 16:31:01 -03:00
|
|
|
int
|
|
|
|
_PyInterpreterFrame_GetLine(_PyInterpreterFrame *frame)
|
|
|
|
{
|
|
|
|
int addr = _PyInterpreterFrame_LASTI(frame) * sizeof(_Py_CODEUNIT);
|
|
|
|
return PyCode_Addr2Line(frame->f_code, addr);
|
|
|
|
}
|