From df10932e313c434d122608baf89e1b3d62f53009 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Sun, 28 May 2023 16:58:06 +0100 Subject: [PATCH] [mypyc] Fix generators on Python 3.12 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 219914bf3470..5456ca082863 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