@@ -2300,6 +2300,32 @@ crash_no_current_thread(PyObject *self)
23002300 return NULL ;
23012301}
23022302
2303+ /* To run some code in a sub-interpreter. */
2304+ static PyObject *
2305+ run_in_subinterp (PyObject * self , PyObject * args )
2306+ {
2307+ const char * code ;
2308+ int r ;
2309+ PyThreadState * substate , * mainstate ;
2310+
2311+ if (!PyArg_ParseTuple (args , "s:run_in_subinterp" ,
2312+ & code ))
2313+ return NULL ;
2314+
2315+ mainstate = PyThreadState_Get ();
2316+
2317+ PyThreadState_Swap (NULL );
2318+
2319+ substate = Py_NewInterpreter ();
2320+ r = PyRun_SimpleString (code );
2321+ Py_EndInterpreter (substate );
2322+
2323+ PyThreadState_Swap (mainstate );
2324+
2325+ return PyLong_FromLong (r );
2326+ }
2327+
2328+
23032329static PyMethodDef TestMethods [] = {
23042330 {"raise_exception" , raise_exception , METH_VARARGS },
23052331 {"raise_memoryerror" , (PyCFunction )raise_memoryerror , METH_NOARGS },
@@ -2385,6 +2411,7 @@ static PyMethodDef TestMethods[] = {
23852411 {"make_memoryview_from_NULL_pointer" , (PyCFunction )make_memoryview_from_NULL_pointer ,
23862412 METH_NOARGS },
23872413 {"crash_no_current_thread" , (PyCFunction )crash_no_current_thread , METH_NOARGS },
2414+ {"run_in_subinterp" , run_in_subinterp , METH_VARARGS },
23882415 {NULL , NULL } /* sentinel */
23892416};
23902417
0 commit comments