summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2023-11-07 18:54:36 (GMT)
committerGitHub <noreply@github.com>2023-11-07 18:54:36 (GMT)
commit2f9cb7e095370e38bde58c79c8a8ea7705eefdc2 (patch)
treefe3b2edfda6852374e8467a6682517f645f38452 /Objects
parent178861b19324c94d98478e4c2bba075964c3baa4 (diff)
downloadcpython-2f9cb7e095370e38bde58c79c8a8ea7705eefdc2.zip
cpython-2f9cb7e095370e38bde58c79c8a8ea7705eefdc2.tar.gz
cpython-2f9cb7e095370e38bde58c79c8a8ea7705eefdc2.tar.bz2
gh-81137: deprecate assignment of code object to a function of a mismatched type (#111823)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/funcobject.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index 8ce1bff..b3701eb 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -557,6 +557,20 @@ func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
nclosure, nfree);
return -1;
}
+
+ PyObject *func_code = PyFunction_GET_CODE(op);
+ int old_flags = ((PyCodeObject *)func_code)->co_flags;
+ int new_flags = ((PyCodeObject *)value)->co_flags;
+ int mask = CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR;
+ if ((old_flags & mask) != (new_flags & mask)) {
+ if (PyErr_Warn(PyExc_DeprecationWarning,
+ "Assigning a code object of non-matching type is deprecated "
+ "(e.g., from a generator to a plain function)") < 0)
+ {
+ return -1;
+ }
+ }
+
handle_func_event(PyFunction_EVENT_MODIFY_CODE, op, value);
_PyFunction_SetVersion(op, 0);
Py_XSETREF(op->func_code, Py_NewRef(value));