@@ -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))
620623static PyObject *
621624test_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