Skip to content

GH-137630: Convert _interpreters to use Argument Clinic #137631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5997f18
Convert _interpreters.create()
AA-Turner Aug 11, 2025
4b159b3
Convert _interpreters.destroy()
AA-Turner Aug 11, 2025
eda5fa0
Convert _interpreters.list_all()
AA-Turner Aug 11, 2025
699dc6d
Convert _interpreters.get_current()
AA-Turner Aug 11, 2025
c313b80
Convert _interpreters.get_main()
AA-Turner Aug 11, 2025
abe7aaa
Convert _interpreters.set___main___attrs()
AA-Turner Aug 11, 2025
6903b3f
Convert _interpreters.exec()
AA-Turner Aug 11, 2025
d0b2e8d
Convert _interpreters.run_string()
AA-Turner Aug 11, 2025
91d15b5
Convert _interpreters.run_func()
AA-Turner Aug 11, 2025
98a75b2
Convert _interpreters.call()
AA-Turner Aug 11, 2025
6d863cb
Convert _interpreters.is_shareable()
AA-Turner Aug 11, 2025
84217c8
Convert _interpreters.is_running()
AA-Turner Aug 11, 2025
bfed2bf
Convert _interpreters.get_config()
AA-Turner Aug 11, 2025
599bde4
Convert _interpreters.whence()
AA-Turner Aug 11, 2025
20c67ee
Convert _interpreters.incref()
AA-Turner Aug 11, 2025
b7a49d3
Convert _interpreters.decref()
AA-Turner Aug 11, 2025
d07daae
Convert _interpreters.capture_exception()
AA-Turner Aug 11, 2025
9e5577e
Add note on new_config()
AA-Turner Aug 11, 2025
eb21bb1
Indicate positional-only parameters
AA-Turner Aug 11, 2025
6e26591
Update docstrings
AA-Turner Aug 11, 2025
08dce29
Update tests
AA-Turner Aug 11, 2025
af65824
Blurb
AA-Turner Aug 11, 2025
435389f
make regen-all
AA-Turner Aug 11, 2025
be2fe07
Fix new_config_doc
AA-Turner Aug 11, 2025
073e2ff
Undo docstring changes
AA-Turner Aug 11, 2025
1cdf313
Revert "Indicate positional-only parameters"
AA-Turner Aug 11, 2025
e8f0b6c
Serhiy's review
AA-Turner Aug 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Convert _interpreters.exec()
  • Loading branch information
AA-Turner committed Aug 11, 2025
commit 6903b3f2ceb4198d4528c8d65c4c1bf64bd60ee3
62 changes: 28 additions & 34 deletions Modules/_interpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1102,23 +1102,36 @@ _handle_script_error(struct run_result *runres)
return runres->excinfo;
}

/*[clinic input]
_interpreters.exec
id: object
code: object
shared: object(subclass_of='&PyDict_Type', c_default='NULL') = {}
*
restrict as restricted: bool = False

Execute the provided code in the identified interpreter.

This is equivalent to running the builtin exec() under the target
interpreter, using the __dict__ of its __main__ module as both
globals and locals.

"code" may be a string containing the text of a Python script.

Functions (and code objects) are also supported, with some restrictions.
The code/function must not take any arguments or be a closure
(i.e. have cell vars). Methods and other callables are not supported.

If a function is provided, its code object is used and all its state
is ignored, including its __globals__ dict.
[clinic start generated code]*/

static PyObject *
interp_exec(PyObject *self, PyObject *args, PyObject *kwds)
_interpreters_exec_impl(PyObject *module, PyObject *id, PyObject *code,
PyObject *shared, int restricted)
/*[clinic end generated code: output=492057c4f10dc304 input=5a22c1ed0c5dbcf3]*/
{
#define FUNCNAME MODULE_NAME_STR ".exec"
PyThreadState *tstate = _PyThreadState_GET();
static char *kwlist[] = {"id", "code", "shared", "restrict", NULL};
PyObject *id, *code;
PyObject *shared = NULL;
int restricted = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds,
"OO|O!$p:" FUNCNAME, kwlist,
&id, &code, &PyDict_Type, &shared,
&restricted))
{
return NULL;
}

int reqready = 1;
PyInterpreterState *interp = \
resolve_interp(id, restricted, reqready, "exec code for");
Expand All @@ -1143,26 +1156,8 @@ interp_exec(PyObject *self, PyObject *args, PyObject *kwds)
}
assert(runres.result == NULL);
Py_RETURN_NONE;
#undef FUNCNAME
}

PyDoc_STRVAR(exec_doc,
"exec(id, code, shared=None, *, restrict=False)\n\
\n\
Execute the provided code in the identified interpreter.\n\
This is equivalent to running the builtin exec() under the target\n\
interpreter, using the __dict__ of its __main__ module as both\n\
globals and locals.\n\
\n\
\"code\" may be a string containing the text of a Python script.\n\
\n\
Functions (and code objects) are also supported, with some restrictions.\n\
The code/function must not take any arguments or be a closure\n\
(i.e. have cell vars). Methods and other callables are not supported.\n\
\n\
If a function is provided, its code object is used and all its state\n\
is ignored, including its __globals__ dict.");

static PyObject *
interp_run_string(PyObject *self, PyObject *args, PyObject *kwds)
{
Expand Down Expand Up @@ -1610,8 +1605,7 @@ static PyMethodDef module_functions[] = {
METH_VARARGS | METH_KEYWORDS, get_config_doc},
{"whence", _PyCFunction_CAST(interp_whence),
METH_VARARGS | METH_KEYWORDS, whence_doc},
{"exec", _PyCFunction_CAST(interp_exec),
METH_VARARGS | METH_KEYWORDS, exec_doc},
_INTERPRETERS_EXEC_METHODDEF
{"call", _PyCFunction_CAST(interp_call),
METH_VARARGS | METH_KEYWORDS, call_doc},
{"run_string", _PyCFunction_CAST(interp_run_string),
Expand Down
101 changes: 100 additions & 1 deletion Modules/clinic/_interpretersmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.