8000 Fix `gen_is_coroutine` for Python 3.13 (#17501) · python/mypy@d4f7e5c · GitHub
[go: up one dir, main page]

Skip to content

Commit d4f7e5c

Browse files
authored
Fix gen_is_coroutine for Python 3.13 (#17501)
The `_PyInterpreterFrame` struct was changed in python/cpython#105727 to store the code object in `f_executable` instead of `f_code`. Fixes ```cpp /home/runner/work/mypy/mypy/mypyc/lib-rt/pythonsupport.h: In function ‘_PyGen_GetCode’: (diff) /home/runner/work/mypy/mypy/mypyc/lib-rt/pythonsupport.h:403:17: error: ‘_PyInterpreterFrame’ has no member named ‘f_code’ (diff) 403 | return frame->f_code; (diff) | ^~ (diff) ```
1 parent 4c54801 commit d4f7e5c

File tree

2 files changed

+27
-1
lines changed

mypyc/lib-rt/mypyc_util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,7 @@ static inline void CPyLong_SetUnsignedSize(PyLongObject *o, Py_ssize_t n) {
115115

116116
#endif
117117

118+
// Are we targeting Python 3.13 or newer?
119+
#define CPY_3_13_FEATURES (PY_VERSION_HEX >= 0x030d0000)
120+
118121
#endif

mypyc/lib-rt/pythonsupport.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,30 @@ _CPyObject_HasAttrId(PyObject *v, _Py_Identifier *name) {
392392
_PyObject_CallMethodIdObjArgs((self), (name), (arg), NULL)
393393
#endif
394394

395-
#if CPY_3_12_FEATURES
395+
#if CPY_3_13_FEATURES
396+
397+
// These are copied from genobject.c in Python 3.13
398+
399+
/* Returns a borrowed reference */
400+
static inline PyCodeObject *
401+
_PyGen_GetCode(PyGenObject *gen) {
402+
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)(gen->gi_iframe);
403+
return _PyFrame_GetCode(frame);
404+
}
405+
406+
static int
407+
gen_is_coroutine(PyObject *o)
408+
{
409+
if (PyGen_CheckExact(o)) {
410+
PyCodeObject *code = _PyGen_GetCode((PyGenObject*)o);
411+
if (code->co_flags & CO_ITERABLE_COROUTINE) {
412+
return 1;
413+
}
414+
}
415+
return 0;
416+
}
417+
418+
#elif CPY_3_12_FEATURES
396419

397420
// These are copied from genobject.c in Python 3.12
398421

0 commit comments

Comments
 (0)
0