Skip to content

Commit f5589e0

Browse files
Add _interpreters.run_func().
1 parent 7e8320c commit f5589e0

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

Lib/test/test__xxsubinterpreters.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515

1616
interpreters = import_helper.import_module('_xxsubinterpreters')
17-
interpreters.run_func = interpreters.exec
1817

1918

2019
##################################

Modules/_xxsubinterpretersmodule.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,40 @@ Execute the provided string in the identified interpreter.\n\
961961
\n\
962962
(See " MODULE_NAME ".exec().");
963963

964+
static PyObject *
965+
interp_run_func(PyObject *self, PyObject *args, PyObject *kwds)
966+
{
967+
static char *kwlist[] = {"id", "func", "shared", NULL};
968+
PyObject *id, *func;
969+
PyObject *shared = NULL;
970+
if (!PyArg_ParseTupleAndKeywords(args, kwds,
971+
"OO|O:" MODULE_NAME ".run_func", kwlist,
972+
&id, &func, &shared)) {
973+
return NULL;
974+
}
975+
976+
PyCodeObject *code = convert_code_arg(func, MODULE_NAME ".exec",
977+
"argument 2",
978+
"a function or a code object");
979+
if (code == NULL) {
980+
return NULL;
981+
}
982+
983+
if (_interp_exec(self, id, (PyObject *)code, shared) < 0) {
984+
return NULL;
985+
}
986+
Py_RETURN_NONE;
987+
}
988+
989+
PyDoc_STRVAR(run_func_doc,
990+
"run_func(id, func, shared=None)\n\
991+
\n\
992+
Execute the body of the provided function in the identified interpreter.\n\
993+
Code objects are also supported. In both cases, closures and args\n\
994+
are not supported. Methods and other callables are not supported either.\n\
995+
\n\
996+
(See " MODULE_NAME ".exec().");
997+
964998

965999
static PyObject *
9661000
object_is_shareable(PyObject *self, PyObject *args, PyObject *kwds)
@@ -1030,6 +1064,8 @@ static PyMethodDef module_functions[] = {
10301064
METH_VARARGS | METH_KEYWORDS, exec_doc},
10311065
{"run_string", _PyCFunction_CAST(interp_run_string),
10321066
METH_VARARGS | METH_KEYWORDS, run_string_doc},
1067+
{"run_func", _PyCFunction_CAST(interp_run_func),
1068+
METH_VARARGS | METH_KEYWORDS, run_func_doc},
10331069

10341070
{"is_shareable", _PyCFunction_CAST(object_is_shareable),
10351071
METH_VARARGS | METH_KEYWORDS, is_shareable_doc},

0 commit comments

Comments
 (0)