@@ -2352,32 +2352,23 @@ get_strong_ref(void)
23522352 return ref ;
23532353}
23542354
2355+ #define NUM_REFS 100
2356+
23552357static PyObject *
23562358test_interp_refcount (PyObject * self , PyObject * unused )
23572359{
23582360 PyInterpreterState * interp = PyInterpreterState_Get ();
2359- PyInterpreterRef ref1 ;
2360- PyInterpreterRef ref2 ;
2361-
2362- // Reference counts are technically 0 by default
2363- assert (_PyInterpreterState_Refcount (interp ) == 0 );
2364- ref1 = get_strong_ref ();
2365- assert (_PyInterpreterState_Refcount (interp ) == 1 );
2366- ref2 = get_strong_ref ();
2367- assert (_PyInterpreterState_Refcount (interp ) == 2 );
2368- PyInterpreterRef_Close (ref1 );
2369- assert (_PyInterpreterState_Refcount (interp ) == 1 );
2370- PyInterpreterRef_Close (ref2 );
23712361 assert (_PyInterpreterState_Refcount (interp ) == 0 );
2362+ PyInterpreterRef refs [NUM_REFS ];
2363+ for (int i = 0 ; i < NUM_REFS ; ++ i ) {
2364+ int res = PyInterpreterRef_Get (& refs [i ]);
2365+ assert (_PyInterpreterState_Refcount (interp ) == i + 1 );
2366+ }
23722367
2373- ref1 = get_strong_ref ();
2374- ref2 = PyInterpreterRef_Dup (ref1 );
2375- assert (_PyInterpreterState_Refcount (interp ) == 2 );
2376- assert (PyInterpreterRef_AsInterpreter (ref1 ) == interp );
2377- assert (PyInterpreterRef_AsInterpreter (ref2 ) == interp );
2378- PyInterpreterRef_Close (ref1 );
2379- PyInterpreterRef_Close (ref2 );
2380- assert (_PyInterpreterState_Refcount (interp ) == 0 );
2368+ for (int i = 0 ; i < NUM_REFS ; ++ i ) {
2369+ PyInterpreterRef_Close (refs [i ]);
2370+ assert (_PyInterpreterState_Refcount (interp ) == (NUM_REFS - i ));
2371+ }
23812372
23822373 Py_RETURN_NONE ;
23832374}
@@ -2392,17 +2383,26 @@ test_interp_weakref_incref(PyObject *self, PyObject *unused)
23922383 }
23932384 assert (_PyInterpreterState_Refcount (interp ) == 0 );
23942385
2395- PyInterpreterRef ref ;
2396- int res = PyInterpreterWeakRef_AsStrong (wref , & ref );
2397- assert (res == 0 );
2398- assert (PyInterpreterRef_AsInterpreter (ref ) == interp );
2399- assert (_PyInterpreterState_Refcount (interp ) == 1 );
2400- PyInterpreterWeakRef_Close (wref );
2401- PyInterpreterRef_Close (ref );
2386+ PyInterpreterRef refs [NUM_REFS ];
2387+
2388+ for (int i = 0 ; i < NUM_REFS ; ++ i ) {
2389+ int res = PyInterpreterWeakRef_AsStrong (wref , & refs [i ]);
2390+ assert (res == 0 );
2391+ assert (PyInterpreterRef_AsInterpreter (refs [i ]) == interp );
2392+ assert (_PyInterpreterState_Refcount (interp ) == i + 1 );
2393+ }
24022394
2395+ for (int i = 0 ; i < NUM_REFS ; ++ i ) {
2396+ PyInterpreterRef_Close (refs [i ]);
2397+ assert (_PyInterpreterState_Refcount (interp ) == (NUM_REFS - i ));
2398+ }
2399+
2400+ PyInterpreterWeakRef_Close (wref );
24032401 Py_RETURN_NONE ;
24042402}
24052403
2404+ #undef NUM_REFS
2405+
24062406static PyMethodDef module_functions [] = {
24072407 {"get_configs" , get_configs , METH_NOARGS },
24082408 {"get_recursion_depth" , get_recursion_depth , METH_NOARGS },
0 commit comments