Skip to content

Commit 271d20d

Browse files
Add get_main().
1 parent c50c51c commit 271d20d

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
@@ -467,6 +467,19 @@ PyDoc_STRVAR(get_current_doc,
467467
Return the ID of current interpreter.");
468468

469469

470+
static PyObject *
471+
interp_get_main(PyObject *self)
472+
{
473+
// Currently, 0 is always the main interpreter.
474+
return PyLong_FromLongLong(0);
475+
}
476+
477+
PyDoc_STRVAR(get_main_doc,
478+
"get_main() -> ID\n\
479+
\n\
480+
Return the ID of main interpreter.");
481+
482+
470483
static PyObject *
471484
interp_run_string(PyObject *self, PyObject *args)
472485
{
@@ -571,6 +584,8 @@ static PyMethodDef module_functions[] = {
571584
METH_NOARGS, enumerate_doc},
572585
{"get_current", (PyCFunction)interp_get_current,
573586
METH_NOARGS, get_current_doc},
587+
{"get_main", (PyCFunction)interp_get_main,
588+
METH_NOARGS, get_main_doc},
574589
{"is_running", (PyCFunction)interp_is_running,
575590
METH_VARARGS, is_running_doc},
576591

0 commit comments

Comments
 (0)