From efe84d492bd77f20c777b523f2e681d1f83f7ac4 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Fri, 23 Jun 2023 18:21:06 +0100 Subject: [PATCH] [mypyc] Fix generators on Python 3.12 (#15472) I think we relied on undocumented behavior on older Python versions. Now it works consistently across all supported Python versions. Work on mypyc/mypyc#995. --- mypyc/lib-rt/exc_ops.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mypyc/lib-rt/exc_ops.c b/mypyc/lib-rt/exc_ops.c index a7536994d059..d8307ecf21f8 100644 --- a/mypyc/lib-rt/exc_ops.c +++ b/mypyc/lib-rt/exc_ops.c @@ -24,6 +24,12 @@ void CPy_Reraise(void) { } void CPyErr_SetObjectAndTraceback(PyObject *type, PyObject *value, PyObject *traceback) { + if (!PyType_Check(type) && value == Py_None) { + // The first argument must be an exception instance + value = type; + type = (PyObject *)Py_TYPE(value); + } + // Set the value and traceback of an error. Because calling // PyErr_Restore takes away a reference to each object passed in // as an argument, we manually increase the reference count of