mirror of
https://github.com/python/cpython.git
synced 2025-07-24 03:35:53 +00:00
gh-81137: deprecate assignment of code object to a function of a mismatched type (#111823)
This commit is contained in:
parent
178861b193
commit
2f9cb7e095
4 changed files with 44 additions and 0 deletions
|
@ -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));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue