mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
commit
7422b22e5e
2 changed files with 44 additions and 0 deletions
|
@ -2379,6 +2379,32 @@ crash_no_current_thread(PyObject *self)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
/* To run some code in a sub-interpreter. */
|
||||
static PyObject *
|
||||
run_in_subinterp(PyObject *self, PyObject *args)
|
||||
{
|
||||
const char *code;
|
||||
int r;
|
||||
PyThreadState *substate, *mainstate;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s:run_in_subinterp",
|
||||
&code))
|
||||
return NULL;
|
||||
|
||||
mainstate = PyThreadState_Get();
|
||||
|
||||
PyThreadState_Swap(NULL);
|
||||
|
||||
substate = Py_NewInterpreter();
|
||||
r = PyRun_SimpleString(code);
|
||||
Py_EndInterpreter(substate);
|
||||
|
||||
PyThreadState_Swap(mainstate);
|
||||
|
||||
return PyLong_FromLong(r);
|
||||
}
|
||||
|
||||
|
||||
static PyMethodDef TestMethods[] = {
|
||||
{"raise_exception", raise_exception, METH_VARARGS},
|
||||
{"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS},
|
||||
|
@ -2467,6 +2493,7 @@ static PyMethodDef TestMethods[] = {
|
|||
{"make_memoryview_from_NULL_pointer", (PyCFunction)make_memoryview_from_NULL_pointer,
|
||||
METH_NOARGS},
|
||||
{"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS},
|
||||
{"run_in_subinterp", run_in_subinterp, METH_VARARGS},
|
||||
{NULL, NULL} /* sentinel */
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue