Skip to content

Commit 4ed4587

Browse files
committed
Fix test_code() and test_frame() on PyPy 3.12
1 parent ff1e744 commit 4ed4587

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

‎pythoncapi_compat.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern "C" {
2424
#include <stdio.h> // fclose()
2525
#include <string.h> // memcpy()
2626

27-
// Python 3.11.0b4 added PyFrame_Back() to Python.h
27+
// Python 3.11.0b4 added PyFrame_GetBack() to Python.h
2828
#if PY_VERSION_HEX < 0x030b00B4 && !defined(PYPY_VERSION)
2929
# include "frameobject.h" // PyFrameObject, PyFrame_GetBack()
3030
#endif

‎tests/test_pythoncapi_compat_cext.c‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ test_frame(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
214214
assert(frame2 == frame);
215215
assert(Py_REFCNT(_PyObject_CAST(frame)) == frame_refcnt);
216216

217+
// Test issue: the current thread has no code on PyPy
218+
#ifndef PYPY_VERSION
217219
// test PyFrame_GetCode()
218220
PyCodeObject *code = PyFrame_GetCode(frame);
219221
assert(code != _Py_NULL);
@@ -227,6 +229,7 @@ test_frame(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
227229
assert(code2 == code);
228230
assert(Py_REFCNT(_PyObject_CAST(code)) == code_refcnt);
229231
Py_DECREF(code);
232+
#endif
230233

231234
#if !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)
232235
// PyFrame_GetBack()
@@ -620,13 +623,15 @@ test_float_pack(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
620623
static PyObject *
621624
test_code(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
622625
{
623-
PyThreadState *tstate = PyThreadState_Get();
624-
PyFrameObject *frame = PyThreadState_GetFrame(tstate);
625-
if (frame == _Py_NULL) {
626-
PyErr_SetString(PyExc_AssertionError, "PyThreadState_GetFrame failed");
627-
return _Py_NULL;
626+
PyObject *func = PyImport_ImportModuleAttrString("colorsys", "rgb_to_yiq");
627+
if (func == NULL) {
628+
return NULL;
628629
}
629-
PyCodeObject *code = PyFrame_GetCode(frame);
630+
PyObject *code_obj = PyObject_GetAttrString(func, "__code__");
631+
Py_DECREF(func);
632+
633+
assert(PyCode_Check(code_obj));
634+
PyCodeObject *code = (PyCodeObject*)code_obj;
630635

631636
// Test PyCode_GetCode()
632637
{
@@ -664,7 +669,6 @@ test_code(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
664669
}
665670

666671
Py_DECREF(code);
667-
Py_DECREF(frame);
668672
Py_RETURN_NONE;
669673
}
670674
#endif

0 commit comments

Comments
 (0)