From 059f3a0e457224c0c1587fe8249547c85176f4f0 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Tue, 9 May 2023 18:50:47 +0100 Subject: [PATCH] [mypyc] Fix gen_is_coroutine on Python 3.12 We've copied some code CPython, which was changed in 3.12. --- mypyc/lib-rt/pythonsupport.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/mypyc/lib-rt/pythonsupport.h b/mypyc/lib-rt/pythonsupport.h index 8a1159a98853..6751f6f4af02 100644 --- a/mypyc/lib-rt/pythonsupport.h +++ b/mypyc/lib-rt/pythonsupport.h @@ -13,6 +13,10 @@ #include #include "mypyc_util.h" +#if CPY_3_12_FEATURES +#include "internal/pycore_frame.h" +#endif + #ifdef __cplusplus extern "C" { #endif @@ -389,6 +393,31 @@ _CPyObject_HasAttrId(PyObject *v, _Py_Identifier *name) { _PyObject_CallMethodIdObjArgs((self), (name), (arg), NULL) #endif +#if CPY_3_12_FEATURES + +// These are copied from genobject.c in Python 3.12 + +/* Returns a borrowed reference */ +static inline PyCodeObject * +_PyGen_GetCode(PyGenObject *gen) { + _PyInterpreterFrame *frame = (_PyInterpreterFrame *)(gen->gi_iframe); + return frame->f_code; +} + +static int +gen_is_coroutine(PyObject *o) +{ + if (PyGen_CheckExact(o)) { + PyCodeObject *code = _PyGen_GetCode((PyGenObject*)o); + if (code->co_flags & CO_ITERABLE_COROUTINE) { + return 1; + } + } + return 0; +} + +#else + // Copied from genobject.c in Python 3.10 static int gen_is_coroutine(PyObject *o) @@ -402,6 +431,8 @@ gen_is_coroutine(PyObject *o) return 0; } +#endif + /* * This helper function returns an awaitable for `o`: * - `o` if `o` is a coroutine-object;