From 8427cb861812e0f2da88f651b87793c16f9d1915 Mon Sep 17 00:00:00 2001 From: Malcolm Smith Date: Thu, 1 May 2025 16:57:33 +0100 Subject: [PATCH 1/2] Correct documentation of how Py_RunMain handles SystemExit --- Doc/c-api/init.rst | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst index 52f64a61006b74..629a9b07505e42 100644 --- a/Doc/c-api/init.rst +++ b/Doc/c-api/init.rst @@ -545,23 +545,18 @@ Initializing and finalizing the interpreter If :c:member:`PyConfig.inspect` is not set (the default), the return value will be ``0`` if the interpreter exits normally (that is, without raising - an exception), or ``1`` if the interpreter exits due to an exception. If an - otherwise unhandled :exc:`SystemExit` is raised, the function will immediately - exit the process instead of returning ``1``. + an exception), the corresponding exit status if there was an unhandled + :exc:`SystemExit`, or ``1`` for any other unhandled exception. If :c:member:`PyConfig.inspect` is set (such as when the :option:`-i` option is used), rather than returning when the interpreter exits, execution will instead resume in an interactive Python prompt (REPL) using the ``__main__`` module's global namespace. If the interpreter exited with an exception, it is immediately raised in the REPL session. The function return value is - then determined by the way the *REPL session* terminates: returning ``0`` - if the session terminates without raising an unhandled exception, exiting - immediately for an unhandled :exc:`SystemExit`, and returning ``1`` for - any other unhandled exception. - - This function always finalizes the Python interpreter regardless of whether - it returns a value or immediately exits the process due to an unhandled - :exc:`SystemExit` exception. + then determined by the way the *REPL session* terminates: ``0``, ``1``, or + the status of a :exc:`SystemExit`, as specified above. + + This function always finalizes the Python interpreter before it returns. See :ref:`Python Configuration ` for an example of a customized Python that always runs in isolated mode using From 4d8a45bf18eeece178273bb90985540c0ce98187 Mon Sep 17 00:00:00 2001 From: Malcolm Smith Date: Tue, 10 Jun 2025 10:25:22 +0100 Subject: [PATCH 2/2] Correct documentation of Py_Main as well --- Doc/c-api/init.rst | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst index ba3550805fa98e..3106bf9808f254 100644 --- a/Doc/c-api/init.rst +++ b/Doc/c-api/init.rst @@ -492,17 +492,8 @@ Initializing and finalizing the interpreter strings other than those passed in (however, the contents of the strings pointed to by the argument list are not modified). - The return value will be ``0`` if the interpreter exits normally (i.e., - without an exception), ``1`` if the interpreter exits due to an exception, - or ``2`` if the argument list does not represent a valid Python command - line. - - Note that if an otherwise unhandled :exc:`SystemExit` is raised, this - function will not return ``1``, but exit the process, as long as - ``Py_InspectFlag`` is not set. If ``Py_InspectFlag`` is set, execution will - drop into the interactive Python prompt, at which point a second otherwise - unhandled :exc:`SystemExit` will still exit the process, while any other - means of exiting will set the return value as described above. + The return value is ``2`` if the argument list does not represent a valid + Python command line, and otherwise the same as :c:func:`Py_RunMain`. In terms of the CPython runtime configuration APIs documented in the :ref:`runtime configuration ` section (and without accounting @@ -539,8 +530,8 @@ Initializing and finalizing the interpreter If :c:member:`PyConfig.inspect` is not set (the default), the return value will be ``0`` if the interpreter exits normally (that is, without raising - an exception), the corresponding exit status if there was an unhandled - :exc:`SystemExit`, or ``1`` for any other unhandled exception. + an exception), the exit status of an unhandled :exc:`SystemExit`, or ``1`` + for any other unhandled exception. If :c:member:`PyConfig.inspect` is set (such as when the :option:`-i` option is used), rather than returning when the interpreter exits, execution will