@@ -2550,37 +2550,46 @@ static PyObject *
25502550test_interp_refcount (PyObject * self , PyObject * unused )
25512551{
25522552 PyInterpreterState * interp = PyInterpreterState_Get ();
2553+ PyInterpreterRef ref1 ;
2554+ PyInterpreterRef ref2 ;
25532555
25542556 // Reference counts are technically 0 by default
25552557 assert (_PyInterpreterState_Refcount (interp ) == 0 );
2556- PyInterpreterState * held = PyInterpreterState_Hold ();
2558+ ref1 = PyInterpreterRef_Get ();
25572559 assert (_PyInterpreterState_Refcount (interp ) == 1 );
2558- held = PyInterpreterState_Hold ();
2560+ ref2 = PyInterpreterRef_Get ();
25592561 assert (_PyInterpreterState_Refcount (interp ) == 2 );
2560- PyInterpreterState_Release ( held );
2562+ PyInterpreterRef_Close ( ref1 );
25612563 assert (_PyInterpreterState_Refcount (interp ) == 1 );
2562- PyInterpreterState_Release (held );
2564+ PyInterpreterRef_Close (ref2 );
2565+ assert (_PyInterpreterState_Refcount (interp ) == 0 );
2566+
2567+ ref1 = PyInterpreterRef_Get ();
2568+ ref2 = PyInterpreterRef_Dup (ref1 );
2569+ assert (_PyInterpreterState_Refcount (interp ) == 2 );
2570+ assert (PyInterpreterRef_AsInterpreter (ref1 ) == interp );
2571+ assert (PyInterpreterRef_AsInterpreter (ref2 ) == interp );
2572+ PyInterpreterRef_Close (ref1 );
2573+ PyInterpreterRef_Close (ref2 );
25632574 assert (_PyInterpreterState_Refcount (interp ) == 0 );
25642575
25652576 Py_RETURN_NONE ;
25662577}
25672578
25682579static PyObject *
2569- test_interp_lookup (PyObject * self , PyObject * unused )
2580+ test_interp_weak_ref (PyObject * self , PyObject * unused )
25702581{
25712582 PyInterpreterState * interp = PyInterpreterState_Get ();
2583+ PyInterpreterWeakRef wref = PyInterpreterWeakRef_Get ();
25722584 assert (_PyInterpreterState_Refcount (interp ) == 0 );
2573- int64_t interp_id = PyInterpreterState_GetID (interp );
2574- PyInterpreterState * ref = PyInterpreterState_Lookup (interp_id );
2575- assert (ref == interp );
2585+
2586+ PyInterpreterRef ref ;
2587+ int res = PyInterpreterWeakRef_AsStrong (wref , & ref );
2588+ assert (res == 0 );
2589+ assert (PyInterpreterRef_AsInterpreter (ref ) == interp );
25762590 assert (_PyInterpreterState_Refcount (interp ) == 1 );
2577- PyInterpreterState_Release (ref );
2578- assert (PyInterpreterState_Lookup (10000 ) == NULL );
2579- Py_BEGIN_ALLOW_THREADS ;
2580- ref = PyInterpreterState_Lookup (interp_id );
2581- assert (ref == interp );
2582- PyInterpreterState_Release (ref );
2583- Py_END_ALLOW_THREADS ;
2591+ PyInterpreterWeakRef_Close (wref );
2592+ PyInterpreterRef_Close (ref );
25842593
25852594 Py_RETURN_NONE ;
25862595}
@@ -2589,20 +2598,20 @@ static PyObject *
25892598test_interp_ensure (PyObject * self , PyObject * unused )
25902599{
25912600 PyInterpreterState * interp = PyInterpreterState_Get ();
2601+ PyInterpreterRef ref = PyInterpreterRef_Get ();
25922602 PyThreadState * save_tstate = PyThreadState_Swap (NULL );
25932603 PyThreadState * tstate = Py_NewInterpreter ();
2604+ PyInterpreterRef sub_ref = PyInterpreterRef_Get ();
25942605 PyInterpreterState * subinterp = PyThreadState_GetInterpreter (tstate );
25952606
25962607 for (int i = 0 ; i < 10 ; ++ i ) {
2597- _PyInterpreterState_Incref (interp );
2598- int res = PyThreadState_Ensure (interp );
2608+ int res = PyThreadState_Ensure (sub_ref );
25992609 assert (res == 0 );
26002610 assert (PyInterpreterState_Get () == interp );
26012611 }
26022612
26032613 for (int i = 0 ; i < 10 ; ++ i ) {
2604- _PyInterpreterState_Incref (subinterp );
2605- int res = PyThreadState_Ensure (subinterp );
2614+ int res = PyThreadState_Ensure (sub_ref );
26062615 assert (res == 0 );
26072616 assert (PyInterpreterState_Get () == subinterp );
26082617 }
@@ -2611,6 +2620,9 @@ test_interp_ensure(PyObject *self, PyObject *unused)
26112620 PyThreadState_Release ();
26122621 }
26132622
2623+ PyInterpreterRef_Close (ref );
2624+ PyInterpreterRef_Close (sub_ref );
2625+
26142626 PyThreadState_Swap (save_tstate );
26152627 Py_RETURN_NONE ;
26162628}
@@ -2710,7 +2722,7 @@ static PyMethodDef TestMethods[] = {
27102722 {"code_offset_to_line" , _PyCFunction_CAST (code_offset_to_line ), METH_FASTCALL },
27112723 {"toggle_reftrace_printer" , toggle_reftrace_printer , METH_O },
27122724 {"test_interp_refcount" , test_interp_refcount , METH_NOARGS },
2713- {"test_interp_lookup " , test_interp_lookup , METH_NOARGS },
2725+ {"test_interp_weak_ref " , test_interp_weak_ref , METH_NOARGS },
27142726 {"test_interp_ensure" , test_interp_ensure , METH_NOARGS },
27152727 {NULL , NULL } /* sentinel */
27162728};
0 commit comments