Skip to content

Commit 4f92794

Browse files
Do not expose not-ready interpreters in the high-level module.
1 parent cd7d5ee commit 4f92794

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

Lib/test/support/interpreters/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def create():
8080
def list_all():
8181
"""Return all existing interpreters."""
8282
return [Interpreter(id, _whence=whence)
83-
for id, whence in _interpreters.list_all()]
83+
for id, whence in _interpreters.list_all(require_ready=True)]
8484

8585

8686
def get_current():

Modules/_xxsubinterpretersmodule.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -719,29 +719,39 @@ So does an unrecognized ID.");
719719

720720

721721
static PyObject *
722-
interp_list_all(PyObject *self, PyObject *Py_UNUSED(ignored))
722+
interp_list_all(PyObject *self, PyObject *args, PyObject *kwargs)
723723
{
724+
static char *kwlist[] = {"require_ready", NULL};
725+
int reqready = 0;
726+
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
727+
"|$p:" MODULE_NAME_STR ".list_all",
728+
kwlist, &reqready))
729+
{
730+
return NULL;
731+
}
732+
724733
PyObject *ids = PyList_New(0);
725734
if (ids == NULL) {
726735
return NULL;
727736
}
728737

729738
PyInterpreterState *interp = PyInterpreterState_Head();
730739
while (interp != NULL) {
731-
PyObject *item = get_summary(interp);
732-
if (item == NULL) {
733-
Py_DECREF(ids);
734-
return NULL;
735-
}
740+
if (!reqready || _PyInterpreterState_IsReady(interp)) {
741+
PyObject *item = get_summary(interp);
742+
if (item == NULL) {
743+
Py_DECREF(ids);
744+
return NULL;
745+
}
736746

737-
// insert at front of list
738-
int res = PyList_Insert(ids, 0, item);
739-
Py_DECREF(item);
740-
if (res < 0) {
741-
Py_DECREF(ids);
742-
return NULL;
747+
// insert at front of list
748+
int res = PyList_Insert(ids, 0, item);
749+
Py_DECREF(item);
750+
if (res < 0) {
751+
Py_DECREF(ids);
752+
return NULL;
753+
}
743754
}
744-
745755
interp = PyInterpreterState_Next(interp);
746756
}
747757

@@ -1417,8 +1427,8 @@ static PyMethodDef module_functions[] = {
14171427
METH_VARARGS | METH_KEYWORDS, create_doc},
14181428
{"destroy", _PyCFunction_CAST(interp_destroy),
14191429
METH_VARARGS | METH_KEYWORDS, destroy_doc},
1420-
{"list_all", interp_list_all,
1421-
METH_NOARGS, list_all_doc},
1430+
{"list_all", _PyCFunction_CAST(interp_list_all),
1431+
METH_VARARGS | METH_KEYWORDS, list_all_doc},
14221432
{"get_current", interp_get_current,
14231433
METH_NOARGS, get_current_doc},
14241434
{"get_main", interp_get_main,

0 commit comments

Comments
 (0)