diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2012-01-17 23:22:06 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2012-01-17 23:22:06 (GMT) |
commit | 7422b22e5e2b5462908b4f5f45574ef2de7961c6 (patch) | |
tree | 7e97fd259a3898ba549f15fc360850d8e878de4a /Modules/_testcapimodule.c | |
parent | 5e8260b09c2aa3faa7ec4722b44e452072301b20 (diff) | |
parent | 2f828f2c8859b331bf9519bdc660eb8a2288779a (diff) | |
download | cpython-7422b22e5e2b5462908b4f5f45574ef2de7961c6.zip cpython-7422b22e5e2b5462908b4f5f45574ef2de7961c6.tar.gz cpython-7422b22e5e2b5462908b4f5f45574ef2de7961c6.tar.bz2 |
Test running of code in a sub-interpreter
(prelude to issue #6531).
Diffstat (limited to 'Modules/_testcapimodule.c')
-rw-r--r-- | Modules/_testcapimodule.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index a9bb5be..b00ac20 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -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 */ }; |