@@ -1690,11 +1690,12 @@ create_interpreter(PyObject *self, PyObject *args, PyObject *kwargs)
16901690static PyObject *
16911691destroy_interpreter (PyObject * self , PyObject * args , PyObject * kwargs )
16921692{
1693- static char * kwlist [] = {"id" , NULL };
1693+ static char * kwlist [] = {"id" , "basic" , NULL };
16941694 PyObject * idobj = NULL ;
1695+ int basic = 0 ;
16951696 if (!PyArg_ParseTupleAndKeywords (args , kwargs ,
1696- "O:destroy_interpreter" , kwlist ,
1697- & idobj ))
1697+ "O|p :destroy_interpreter" , kwlist ,
1698+ & idobj , & basic ))
16981699 {
16991700 return NULL ;
17001701 }
@@ -1704,7 +1705,27 @@ destroy_interpreter(PyObject *self, PyObject *args, PyObject *kwargs)
17041705 return NULL ;
17051706 }
17061707
1707- _PyXI_EndInterpreter (interp , NULL , NULL );
1708+ if (basic )
1709+ {
1710+ // Test the basic Py_EndInterpreter with weird out of order thread states
1711+ PyThreadState * t1 , * t2 ;
1712+ PyThreadState * prev ;
1713+ t1 = interp -> threads .head ;
1714+ if (t1 == NULL ) {
1715+ t1 = PyThreadState_New (interp );
1716+ }
1717+ t2 = PyThreadState_New (interp );
1718+ prev = PyThreadState_Swap (t2 );
1719+ PyThreadState_Clear (t1 );
1720+ PyThreadState_Delete (t1 );
1721+ Py_EndInterpreter (t2 );
1722+ PyThreadState_Swap (prev );
1723+ }
1724+ else
1725+ {
1726+ // use the cross interpreter _PyXI_EndInterpreter normally
1727+ _PyXI_EndInterpreter (interp , NULL , NULL );
1728+ }
17081729 Py_RETURN_NONE ;
17091730}
17101731
0 commit comments