1111# define PYTHON3 1
1212#endif
1313
14- static PyObject *
15- ASSERT_FAILED (const char * err_msg )
16- {
17- PyErr_SetString (PyExc_AssertionError , err_msg );
18- return NULL ;
19- }
14+ // Ignore reference count checks on PyPy
15+ #if !defined(PYPY_VERSION )
16+ # define CHECK_REFCNT
17+ #endif
18+
19+ #ifdef CHECK_REFCNT
20+ # define ASSERT_REFCNT (expr ) assert(expr)
21+ #else
22+ # define ASSERT_REFCNT (expr )
23+ #endif
2024
2125static PyObject *
2226test_object (PyObject * Py_UNUSED (module ), PyObject * Py_UNUSED (ignored ))
@@ -111,6 +115,7 @@ test_steal_ref(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
111115}
112116
113117
118+ #if !defined(PYPY_VERSION )
114119static PyObject *
115120test_frame (PyObject * Py_UNUSED (module ), PyObject * Py_UNUSED (ignored ))
116121{
@@ -119,7 +124,8 @@ test_frame(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
119124 // test PyThreadState_GetFrame()
120125 PyFrameObject * frame = PyThreadState_GetFrame (tstate );
121126 if (frame == NULL ) {
122- return ASSERT_FAILED ("PyThreadState_GetFrame failed" );
127+ PyErr_SetString (PyExc_AssertionError , "PyThreadState_GetFrame failed" );
128+ return NULL ;
123129 }
124130
125131 // test _PyThreadState_GetFrameBorrow()
@@ -162,6 +168,7 @@ test_frame(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
162168 Py_DECREF (frame );
163169 Py_RETURN_NONE ;
164170}
171+ #endif
165172
166173
167174static PyObject *
@@ -173,12 +180,14 @@ test_thread_state(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
173180 PyInterpreterState * interp = PyThreadState_GetInterpreter (tstate );
174181 assert (interp != NULL );
175182
183+ #if !defined(PYPY_VERSION )
176184 // test PyThreadState_GetFrame()
177185 PyFrameObject * frame = PyThreadState_GetFrame (tstate );
178186 if (frame != NULL ) {
179187 assert (PyFrame_Check (frame ));
180188 }
181189 Py_XDECREF (frame );
190+ #endif
182191
183192#if 0x030700A1 <= PY_VERSION_HEX
184193 uint64_t id = PyThreadState_GetID (tstate );
@@ -240,11 +249,13 @@ test_gc(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
240249 Py_INCREF (Py_None );
241250 PyTuple_SET_ITEM (tuple , 0 , Py_None );
242251
252+ #if !defined(PYPY_VERSION )
243253 // test PyObject_GC_IsTracked()
244254 int tracked = PyObject_GC_IsTracked (tuple );
245255 assert (tracked );
256+ #endif
246257
247- #if PY_VERSION_HEX >= 0x030400F0
258+ #if PY_VERSION_HEX >= 0x030400F0 && !defined( PYPY_VERSION )
248259 // test PyObject_GC_IsFinalized()
249260 int finalized = PyObject_GC_IsFinalized (tuple );
250261 assert (!finalized );
@@ -265,12 +276,14 @@ test_module_add_type(PyObject *module)
265276#else
266277 const char * type_name = "unicode" ;
267278#endif
279+ #ifdef CHECK_REFCNT
268280 Py_ssize_t refcnt = Py_REFCNT (type );
281+ #endif
269282
270283 if (PyModule_AddType (module , type ) < 0 ) {
271284 return -1 ;
272285 }
273- assert (Py_REFCNT (type ) == refcnt + 1 );
286+ ASSERT_REFCNT (Py_REFCNT (type ) == refcnt + 1 );
274287
275288 PyObject * attr = PyObject_GetAttrString (module , type_name );
276289 if (attr == NULL ) {
@@ -282,7 +295,7 @@ test_module_add_type(PyObject *module)
282295 if (PyObject_DelAttrString (module , type_name ) < 0 ) {
283296 return -1 ;
284297 }
285- assert (Py_REFCNT (type ) == refcnt );
298+ ASSERT_REFCNT (Py_REFCNT (type ) == refcnt );
286299 return 0 ;
287300}
288301
@@ -292,19 +305,21 @@ static int
292305test_module_addobjectref (PyObject * module )
293306{
294307 PyObject * obj = Py_True ;
295- Py_ssize_t refcnt = Py_REFCNT (obj );
296308 const char * name = "test_module_addobjectref" ;
309+ #ifdef CHECK_REFCNT
310+ Py_ssize_t refcnt = Py_REFCNT (obj );
311+ #endif
297312
298313 if (PyModule_AddObjectRef (module , name , obj ) < 0 ) {
299- assert (Py_REFCNT (obj ) == refcnt );
314+ ASSERT_REFCNT (Py_REFCNT (obj ) == refcnt );
300315 return -1 ;
301316 }
302- assert (Py_REFCNT (obj ) == refcnt + 1 );
317+ ASSERT_REFCNT (Py_REFCNT (obj ) == refcnt + 1 );
303318
304319 if (PyObject_DelAttrString (module , name ) < 0 ) {
305320 return -1 ;
306321 }
307- assert (Py_REFCNT (obj ) == refcnt );
322+ ASSERT_REFCNT (Py_REFCNT (obj ) == refcnt );
308323
309324 // PyModule_AddObjectRef() with value=NULL must not crash
310325 int res = PyModule_AddObjectRef (module , name , NULL );
@@ -344,7 +359,9 @@ test_module(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
344359static struct PyMethodDef methods [] = {
345360 {"test_object" , test_object , METH_NOARGS , NULL },
346361 {"test_steal_ref" , test_steal_ref , METH_NOARGS , NULL },
362+ #if !defined (PYPY_VERSION )
347363 {"test_frame" , test_frame , METH_NOARGS , NULL },
364+ #endif
348365 {"test_thread_state" , test_thread_state , METH_NOARGS , NULL },
349366 {"test_interpreter" , test_interpreter , METH_NOARGS , NULL },
350367 {"test_calls" , test_calls , METH_NOARGS , NULL },
0 commit comments