[Python-checkins] cpython (merge 3.2 -> default): Test running of code in a sub-interpreter
antoine.pitrou
python-checkins at python.org
Wed Jan 18 00:24:09 CET 2012
http://hg.python.org/cpython/rev/a108818aaa0d
changeset: 74472:a108818aaa0d
parent: 74470:463acb73fd79
parent: 74471:eb47af6e9e22
user: Antoine Pitrou <solipsis at pitrou.net>
date: Wed Jan 18 00:22:06 2012 +0100
summary:
Test running of code in a sub-interpreter
(prelude to issue #6531).
files:
Lib/test/test_capi.py | 17 +++++++++++++++++
Modules/_testcapimodule.c | 27 +++++++++++++++++++++++++++
2 files changed, 44 insertions(+), 0 deletions(-)
diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -3,6 +3,7 @@
from __future__ import with_statement
import os
+import pickle
import random
import subprocess
import sys
@@ -137,6 +138,22 @@
self.pendingcalls_submit(l, n)
self.pendingcalls_wait(l, n)
+ def test_subinterps(self):
+ # XXX this test leaks in refleak runs
+ import builtins
+ r, w = os.pipe()
+ code = """if 1:
+ import sys, builtins, pickle
+ with open({:d}, "wb") as f:
+ pickle.dump(id(sys.modules), f)
+ pickle.dump(id(builtins), f)
+ """.format(w)
+ with open(r, "rb") as f:
+ ret = _testcapi.run_in_subinterp(code)
+ self.assertEqual(ret, 0)
+ self.assertNotEqual(pickle.load(f), id(sys.modules))
+ self.assertNotEqual(pickle.load(f), id(builtins))
+
# Bug #6012
class Test6012(unittest.TestCase):
def test(self):
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -2379,6 +2379,32 @@
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 @@
{"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 */
};
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list