From a8cc9106bc5f27e6b43bdd3d9e78d0d2fb4e1b83 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 14 Mar 2024 13:47:23 +0100 Subject: [PATCH] PEP 741: Add PyInitConfig_GetExitcode() function (#3718) --- peps/pep-0741.rst | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/peps/pep-0741.rst b/peps/pep-0741.rst index 84f3f3699..a0b9ac15e 100644 --- a/peps/pep-0741.rst +++ b/peps/pep-0741.rst @@ -114,7 +114,7 @@ configure Python, there is no public API to get Users of the limited C API are asking for a public API to get the current runtime configuration. -Cython needs to access the ``optimization_level`` configuration option: +Cython needs to get the ``optimization_level`` configuration option: `issue `_. When global configuration variables were deprecated in 2022, `Marc-André @@ -331,6 +331,18 @@ a C API to **set** the value of some configuration options at runtime: * ``inspect`` * ``write_bytecode`` +Previously, it was possible to set directly global configuration +variables: + +* ``Py_OptimizeFlag`` +* ``Py_VerboseFlag`` +* ``Py_DebugFlag`` +* ``Py_InspectFlag`` +* ``Py_DontWriteBytecodeFlag`` + +But these configuration flags were deprecated in Python 3.12 and are +scheduled for removal in Python 3.14. + Specification ============= @@ -375,6 +387,7 @@ initialization: * Error handling: * ``PyInitConfig_GetError(config, &err_msg)``. + * ``PyInitConfig_GetExitcode(config, &exitcode)``. Add C API functions to get and set the current runtime configuration: @@ -890,6 +903,10 @@ Initialize Python * Return ``0`` on success. * Set an error in *config* and return ``-1`` on error. + * Set an exit code in *config* and return ``-1`` if Python wants to + exit. + + See ``PyInitConfig_GetExitcode()`` for the exitcode case. Error Handling @@ -903,10 +920,28 @@ Error Handling An error message is an UTF-8 encoded string. + If *config* has an exit code, format the exit code as an error + message. + The error message remains valid until another ``PyInitConfig`` function is called with *config*. The caller doesn't have to free the error message. +``int PyInitConfig_GetExitcode(PyInitConfig* config, int *exitcode)``: + Get the *config* exit code. + + * Set *\*exitcode* and return ``1`` if Python wants to exit. + * Return ``0`` if *config* has no exit code set. + + Only the ``Py_InitializeFromInitConfig()`` function can set an exit + code if the ``parse_argv`` option is non-zero. For example, an + isolated configuration cannot set an exit code by default, since + ``parse_argv`` is zero by default. + + An exit code can be set when parsing the command line failed (exit + code 2) or when a command line option asks to display the command + line help (exit code 0). + Get and Set the Runtime Configuration -------------------------------------