summaryrefslogtreecommitdiffstats
path: root/Objects/genobject.c
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2023-02-23 10:19:01 (GMT)
committerGitHub <noreply@github.com>2023-02-23 10:19:01 (GMT)
commit22b8d77b98a5944e688be0927b8139c49d4a7257 (patch)
treec3693ee3bc939140e9ad885677d58cab59673993 /Objects/genobject.c
parent572223f9ce99e8816abdcc1536db6c4ceed2d848 (diff)
downloadcpython-22b8d77b98a5944e688be0927b8139c49d4a7257.zip
cpython-22b8d77b98a5944e688be0927b8139c49d4a7257.tar.gz
cpython-22b8d77b98a5944e688be0927b8139c49d4a7257.tar.bz2
GH-100719: Remove redundant `gi_code` field from generator object. (GH-100749)
Diffstat (limited to 'Objects/genobject.c')
-rw-r--r--Objects/genobject.c72
1 files changed, 55 insertions, 17 deletions
diff --git a/Objects/genobject.c b/Objects/genobject.c
index aec64ca..4ab6581 100644
--- a/Objects/genobject.c
+++ b/Objects/genobject.c
@@ -24,6 +24,21 @@ static const char *NON_INIT_CORO_MSG = "can't send non-None value to a "
static const char *ASYNC_GEN_IGNORED_EXIT_MSG =
"async generator ignored GeneratorExit";
+/* Returns a borrowed reference */
+static inline PyCodeObject *
+_PyGen_GetCode(PyGenObject *gen) {
+ _PyInterpreterFrame *frame = (_PyInterpreterFrame *)(gen->gi_iframe);
+ return frame->f_code;
+}
+
+PyCodeObject *
+PyGen_GetCode(PyGenObject *gen) {
+ assert(PyGen_Check(gen));
+ PyCodeObject *res = _PyGen_GetCode(gen);
+ Py_INCREF(res);
+ return res;
+}
+
static inline int
exc_state_traverse(_PyErr_StackItem *exc_state, visitproc visit, void *arg)
{
@@ -34,7 +49,6 @@ exc_state_traverse(_PyErr_StackItem *exc_state, visitproc visit, void *arg)
static int
gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
{
- Py_VISIT(gen->gi_code);
Py_VISIT(gen->gi_name);
Py_VISIT(gen->gi_qualname);
if (gen->gi_frame_state < FRAME_CLEARED) {
@@ -88,8 +102,8 @@ _PyGen_Finalize(PyObject *self)
/* If `gen` is a coroutine, and if it was never awaited on,
issue a RuntimeWarning. */
- if (gen->gi_code != NULL &&
- ((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE &&
+ assert(_PyGen_GetCode(gen) != NULL);
+ if (_PyGen_GetCode(gen)->co_flags & CO_COROUTINE &&
gen->gi_frame_state == FRAME_CREATED)
{
_PyErr_WarnUnawaitedCoroutine((PyObject *)gen);
@@ -137,12 +151,12 @@ gen_dealloc(PyGenObject *gen)
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe;
gen->gi_frame_state = FRAME_CLEARED;
frame->previous = NULL;
- _PyFrame_Clear(frame);
+ _PyFrame_ClearExceptCode(frame);
}
- if (((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE) {
+ if (_PyGen_GetCode(gen)->co_flags & CO_COROUTINE) {
Py_CLEAR(((PyCoroObject *)gen)->cr_origin_or_finalizer);
}
- Py_CLEAR(gen->gi_code);
+ Py_DECREF(_PyGen_GetCode(gen));
Py_CLEAR(gen->gi_name);
Py_CLEAR(gen->gi_qualname);
_PyErr_ClearExcState(&gen->gi_exc_state);
@@ -332,7 +346,7 @@ _PyGen_yf(PyGenObject *gen)
/* Return immediately if the frame didn't start yet. SEND
always come after LOAD_CONST: a code object should not start
with SEND */
- assert(_PyCode_CODE(gen->gi_code)[0].op.code != SEND);
+ assert(_PyCode_CODE(_PyGen_GetCode(gen))[0].op.code != SEND);
return NULL;
}
_Py_CODEUNIT next = frame->prev_instr[1];
@@ -767,6 +781,21 @@ gen_getframe(PyGenObject *gen, void *Py_UNUSED(ignored))
return _gen_getframe(gen, "gi_frame");
}
+static PyObject *
+_gen_getcode(PyGenObject *gen, const char *const name)
+{
+ if (PySys_Audit("object.__getattr__", "Os", gen, name) < 0) {
+ return NULL;
+ }
+ return Py_NewRef(_PyGen_GetCode(gen));
+}
+
+static PyObject *
+gen_getcode(PyGenObject *gen, void *Py_UNUSED(ignored))
+{
+ return _gen_getcode(gen, "gi_code");
+}
+
static PyGetSetDef gen_getsetlist[] = {
{"__name__", (getter)gen_get_name, (setter)gen_set_name,
PyDoc_STR("name of the generator")},
@@ -777,11 +806,11 @@ static PyGetSetDef gen_getsetlist[] = {
{"gi_running", (getter)gen_getrunning, NULL, NULL},
{"gi_frame", (getter)gen_getframe, NULL, NULL},
{"gi_suspended", (getter)gen_getsuspended, NULL, NULL},
+ {"gi_code", (getter)gen_getcode, NULL, NULL},
{NULL} /* Sentinel */
};
static PyMemberDef gen_memberlist[] = {
- {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY|PY_AUDIT_READ},
{NULL} /* Sentinel */
};
@@ -790,7 +819,7 @@ gen_sizeof(PyGenObject *gen, PyObject *Py_UNUSED(ignored))
{
Py_ssize_t res;
res = offsetof(PyGenObject, gi_iframe) + offsetof(_PyInterpreterFrame, localsplus);
- PyCodeObject *code = gen->gi_code;
+ PyCodeObject *code = _PyGen_GetCode(gen);
res += _PyFrame_NumSlotsForCodeObject(code) * sizeof(PyObject *);
return PyLong_FromSsize_t(res);
}
@@ -878,7 +907,6 @@ make_gen(PyTypeObject *type, PyFunctionObject *func)
return NULL;
}
gen->gi_frame_state = FRAME_CLEARED;
- gen->gi_code = (PyCodeObject *)Py_NewRef(func->func_code);
gen->gi_weakreflist = NULL;
gen->gi_exc_state.exc_value = NULL;
gen->gi_exc_state.previous_item = NULL;
@@ -960,8 +988,6 @@ gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f,
f->f_frame = frame;
frame->owner = FRAME_OWNED_BY_GENERATOR;
assert(PyObject_GC_IsTracked((PyObject *)f));
- gen->gi_code = PyFrame_GetCode(f);
- Py_INCREF(gen->gi_code);
Py_DECREF(f);
gen->gi_weakreflist = NULL;
gen->gi_exc_state.exc_value = NULL;
@@ -969,11 +995,11 @@ gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f,
if (name != NULL)
gen->gi_name = Py_NewRef(name);
else
- gen->gi_name = Py_NewRef(gen->gi_code->co_name);
+ gen->gi_name = Py_NewRef(_PyGen_GetCode(gen)->co_name);
if (qualname != NULL)
gen->gi_qualname = Py_NewRef(qualname);
else
- gen->gi_qualname = Py_NewRef(gen->gi_code->co_qualname);
+ gen->gi_qualname = Py_NewRef(_PyGen_GetCode(gen)->co_qualname);
_PyObject_GC_TRACK(gen);
return (PyObject *)gen;
}
@@ -1001,7 +1027,7 @@ static int
gen_is_coroutine(PyObject *o)
{
if (PyGen_CheckExact(o)) {
- PyCodeObject *code = (PyCodeObject *)((PyGenObject*)o)->gi_code;
+ PyCodeObject *code = _PyGen_GetCode((PyGenObject*)o);
if (code->co_flags & CO_ITERABLE_COROUTINE) {
return 1;
}
@@ -1110,6 +1136,12 @@ cr_getframe(PyCoroObject *coro, void *Py_UNUSED(ignored))
return _gen_getframe((PyGenObject *)coro, "cr_frame");
}
+static PyObject *
+cr_getcode(PyCoroObject *coro, void *Py_UNUSED(ignored))
+{
+ return _gen_getcode((PyGenObject *)coro, "cr_code");
+}
+
static PyGetSetDef coro_getsetlist[] = {
{"__name__", (getter)gen_get_name, (setter)gen_set_name,
@@ -1120,12 +1152,12 @@ static PyGetSetDef coro_getsetlist[] = {
PyDoc_STR("object being awaited on, or None")},
{"cr_running", (getter)cr_getrunning, NULL, NULL},
{"cr_frame", (getter)cr_getframe, NULL, NULL},
+ {"cr_code", (getter)cr_getcode, NULL, NULL},
{"cr_suspended", (getter)cr_getsuspended, NULL, NULL},
{NULL} /* Sentinel */
};
static PyMemberDef coro_memberlist[] = {
- {"cr_code", T_OBJECT, offsetof(PyCoroObject, cr_code), READONLY|PY_AUDIT_READ},
{"cr_origin", T_OBJECT, offsetof(PyCoroObject, cr_origin_or_finalizer), READONLY},
{NULL} /* Sentinel */
};
@@ -1514,6 +1546,12 @@ ag_getframe(PyAsyncGenObject *ag, void *Py_UNUSED(ignored))
return _gen_getframe((PyGenObject *)ag, "ag_frame");
}
+static PyObject *
+ag_getcode(PyGenObject *gen, void *Py_UNUSED(ignored))
+{
+ return _gen_getcode(gen, "ag__code");
+}
+
static PyGetSetDef async_gen_getsetlist[] = {
{"__name__", (getter)gen_get_name, (setter)gen_set_name,
PyDoc_STR("name of the async generator")},
@@ -1522,13 +1560,13 @@ static PyGetSetDef async_gen_getsetlist[] = {
{"ag_await", (getter)coro_get_cr_await, NULL,
PyDoc_STR("object being awaited on, or None")},
{"ag_frame", (getter)ag_getframe, NULL, NULL},
+ {"ag_code", (getter)ag_getcode, NULL, NULL},
{NULL} /* Sentinel */
};
static PyMemberDef async_gen_memberlist[] = {
{"ag_running", T_BOOL, offsetof(PyAsyncGenObject, ag_running_async),
READONLY},
- {"ag_code", T_OBJECT, offsetof(PyAsyncGenObject, ag_code), READONLY|PY_AUDIT_READ},
{NULL} /* Sentinel */
};