summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2023-12-12 18:06:06 (GMT)
committerGitHub <noreply@github.com>2023-12-12 18:06:06 (GMT)
commit9898e6104171dcdd88b32776e69ca2cddf515e63 (patch)
treeffc33f1653dbbb7737a18bdf3db474b1b5203a89 /Modules
parenta49b427b0265c415d9089da0be39f4b5ccd1f15f (diff)
downloadcpython-9898e6104171dcdd88b32776e69ca2cddf515e63.zip
cpython-9898e6104171dcdd88b32776e69ca2cddf515e63.tar.gz
cpython-9898e6104171dcdd88b32776e69ca2cddf515e63.tar.bz2
gh-76785: Add Interpreter.prepare_main() (gh-113021)
This is one of the last pieces to get test.support.interpreters in sync with PEP 734.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_xxsubinterpretersmodule.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/Modules/_xxsubinterpretersmodule.c b/Modules/_xxsubinterpretersmodule.c
index 37959e9..4bb54c9 100644
--- a/Modules/_xxsubinterpretersmodule.c
+++ b/Modules/_xxsubinterpretersmodule.c
@@ -685,6 +685,60 @@ PyDoc_STRVAR(get_main_doc,
\n\
Return the ID of main interpreter.");
+static PyObject *
+interp_set___main___attrs(PyObject *self, PyObject *args)
+{
+ PyObject *id, *updates;
+ if (!PyArg_ParseTuple(args, "OO:" MODULE_NAME ".set___main___attrs",
+ &id, &updates))
+ {
+ return NULL;
+ }
+
+ // Look up the interpreter.
+ PyInterpreterState *interp = PyInterpreterID_LookUp(id);
+ if (interp == NULL) {
+ return NULL;
+ }
+
+ // Check the updates.
+ if (updates != Py_None) {
+ Py_ssize_t size = PyObject_Size(updates);
+ if (size < 0) {
+ return NULL;
+ }
+ if (size == 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "arg 2 must be a non-empty mapping");
+ return NULL;
+ }
+ }
+
+ _PyXI_session session = {0};
+
+ // Prep and switch interpreters, including apply the updates.
+ if (_PyXI_Enter(&session, interp, updates) < 0) {
+ if (!PyErr_Occurred()) {
+ _PyXI_ApplyCapturedException(&session);
+ assert(PyErr_Occurred());
+ }
+ else {
+ assert(!_PyXI_HasCapturedException(&session));
+ }
+ return NULL;
+ }
+
+ // Clean up and switch back.
+ _PyXI_Exit(&session);
+
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(set___main___attrs_doc,
+"set___main___attrs(id, ns)\n\
+\n\
+Bind the given attributes in the interpreter's __main__ module.");
+
static PyUnicodeObject *
convert_script_arg(PyObject *arg, const char *fname, const char *displayname,
const char *expected)
@@ -1033,6 +1087,8 @@ static PyMethodDef module_functions[] = {
{"run_func", _PyCFunction_CAST(interp_run_func),
METH_VARARGS | METH_KEYWORDS, run_func_doc},
+ {"set___main___attrs", _PyCFunction_CAST(interp_set___main___attrs),
+ METH_VARARGS, set___main___attrs_doc},
{"is_shareable", _PyCFunction_CAST(object_is_shareable),
METH_VARARGS | METH_KEYWORDS, is_shareable_doc},