Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mypyc] Fix gen_is_coroutine on Python 3.12 #15469

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions mypyc/lib-rt/pythonsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include <assert.h>
#include "mypyc_util.h"

#if CPY_3_12_FEATURES
#include "internal/pycore_frame.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down