Skip to content

Commit b26f7b4

Browse files
Add get_main().
1 parent 7fedbc0 commit b26f7b4

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

Doc/library/_interpreters.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ It defines the following functions:
4141
Return the ID of the currently running interpreter.
4242

4343

44+
.. function:: get_main()
45+
46+
Return the ID of the main interpreter.
47+
48+
4449
.. function:: is_running(id)
4550

4651
Return whether or not the identified interpreter is currently

Lib/test/test__interpreters.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ def test_sub(self):
111111
self.assertEqual(id2, id1)
112112

113113

114+
class GetMainTests(TestBase):
115+
116+
def test_main(self):
117+
expected, = interpreters.enumerate()
118+
main = interpreters.get_main()
119+
120+
self.assertEqual(main, 0)
121+
self.assertEqual(main, expected)
122+
123+
114124
class IsRunningTests(TestBase):
115125

116126
def test_main_running(self):

Modules/_interpretersmodule.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,19 @@ PyDoc_STRVAR(get_current_doc,
320320
Return the ID of current interpreter.");
321321

322322

323+
static PyObject *
324+
interp_get_main(PyObject *self)
325+
{
326+
// Currently, 0 is always the main interpreter.
327+
return PyLong_FromLongLong(0);
328+
}
329+
330+
PyDoc_STRVAR(get_main_doc,
331+
"get_main() -> ID\n\
332+
\n\
333+
Return the ID of main interpreter.");
334+
335+
323336
static PyObject *
324337
interp_run_string(PyObject *self, PyObject *args)
325338
{
@@ -455,6 +468,8 @@ static PyMethodDef module_functions[] = {
455468
METH_NOARGS, enumerate_doc},
456469
{"get_current", (PyCFunction)interp_get_current,
457470
METH_NOARGS, get_current_doc},
471+
{"get_main", (PyCFunction)interp_get_main,
472+
METH_NOARGS, get_main_doc},
458473
{"is_running", (PyCFunction)interp_is_running,
459474
METH_VARARGS, is_running_doc},
460475

0 commit comments

Comments
 (0)