Skip to content

Commit 6907343

Browse files
authored
[mypyc] Fix gen_is_coroutine on Python 3.12 (#15469)
1 parent c0e6d28 commit 6907343

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

mypyc/lib-rt/pythonsupport.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include <assert.h>
1414
#include "mypyc_util.h"
1515

16+
#if CPY_3_12_FEATURES
17+
#include "internal/pycore_frame.h"
18+
#endif
19+
1620
#ifdef __cplusplus
1721
extern "C" {
1822
#endif
@@ -389,6 +393,31 @@ _CPyObject_HasAttrId(PyObject *v, _Py_Identifier *name) {
389393
_PyObject_CallMethodIdObjArgs((self), (name), (arg), NULL)
390394
#endif
391395

396+
#if CPY_3_12_FEATURES
397+
398+
// These are copied from genobject.c in Python 3.12
399+
400+
/* Returns a borrowed reference */
401+
static inline PyCodeObject *
402+
_PyGen_GetCode(PyGenObject *gen) {
403+
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)(gen->gi_iframe);
404+
return frame->f_code;
405+
}
406+
407+
static int
408+
gen_is_coroutine(PyObject *o)
409+
{
410+
if (PyGen_CheckExact(o)) {
411+
PyCodeObject *code = _PyGen_GetCode((PyGenObject*)o);
412+
if (code->co_flags & CO_ITERABLE_COROUTINE) {
413+
return 1;
414+
}
415+
}
416+
return 0;
417+
}
418+
419+
#else
420+
392421
// Copied from genobject.c in Python 3.10
393422
static int
394423
gen_is_coroutine(PyObject *o)
@@ -402,6 +431,8 @@ gen_is_coroutine(PyObject *o)
402431
return 0;
403432
}
404433

434+
#endif
435+
405436
/*
406437
* This helper function returns an awaitable for `o`:
407438
* - `o` if `o` is a coroutine-object;

0 commit comments

Comments
 (0)