@@ -3665,6 +3665,11 @@ written in Python, such as a mail server's external command delivery program.
36653665 subprocess was killed.) On Windows systems, the return value
36663666 contains the signed integer return code from the child process.
36673667
3668+ On Unix, :func: `waitstatus_to_exitcode ` can be used to convert the ``close ``
3669+ method result (exit status) into an exit code if it is not ``None ``. On
3670+ Windows, the ``close `` method result is directly the exit code
3671+ (or ``None ``).
3672+
36683673 This is implemented using :class: `subprocess.Popen `; see that class's
36693674 documentation for more powerful ways to manage and communicate with
36703675 subprocesses.
@@ -3968,6 +3973,10 @@ written in Python, such as a mail server's external command delivery program.
39683973 to using this function. See the :ref: `subprocess-replacements ` section in
39693974 the :mod: `subprocess ` documentation for some helpful recipes.
39703975
3976+ On Unix, :func: `waitstatus_to_exitcode ` can be used to convert the result
3977+ (exit status) into an exit code. On Windows, the result is directly the exit
3978+ code.
3979+
39713980 .. audit-event :: os.system command os.system
39723981
39733982 .. availability :: Unix, Windows.
@@ -4008,8 +4017,16 @@ written in Python, such as a mail server's external command delivery program.
40084017 number is zero); the high bit of the low byte is set if a core file was
40094018 produced.
40104019
4020+ :func: `waitstatus_to_exitcode ` can be used to convert the exit status into an
4021+ exit code.
4022+
40114023 .. availability :: Unix.
40124024
4025+ .. seealso ::
4026+
4027+ :func: `waitpid ` can be used to wait for the completion of a specific
4028+ child process and has more options.
4029+
40134030.. function :: waitid(idtype, id, options)
40144031
40154032 Wait for the completion of one or more child processes.
@@ -4105,6 +4122,9 @@ written in Python, such as a mail server's external command delivery program.
41054122 id is known, not necessarily a child process. The :func: `spawn\* <spawnl> `
41064123 functions called with :const: `P_NOWAIT ` return suitable process handles.
41074124
4125+ :func: `waitstatus_to_exitcode ` can be used to convert the exit status into an
4126+ exit code.
4127+
41084128 .. versionchanged :: 3.5
41094129 If the system call is interrupted and the signal handler does not raise an
41104130 exception, the function now retries the system call instead of raising an
@@ -4120,6 +4140,9 @@ written in Python, such as a mail server's external command delivery program.
41204140 information. The option argument is the same as that provided to
41214141 :func: `waitpid ` and :func: `wait4 `.
41224142
4143+ :func: `waitstatus_to_exitcode ` can be used to convert the exit status into an
4144+ exitcode.
4145+
41234146 .. availability :: Unix.
41244147
41254148
@@ -4131,9 +4154,42 @@ written in Python, such as a mail server's external command delivery program.
41314154 resource usage information. The arguments to :func: `wait4 ` are the same
41324155 as those provided to :func: `waitpid `.
41334156
4157+ :func: `waitstatus_to_exitcode ` can be used to convert the exit status into an
4158+ exitcode.
4159+
41344160 .. availability :: Unix.
41354161
41364162
4163+ .. function :: waitstatus_to_exitcode(status)
4164+
4165+ Convert a wait status to an exit code.
4166+
4167+ On Unix:
4168+
4169+ * If the process exited normally (if ``WIFEXITED(status) `` is true),
4170+ return the process exit status (return ``WEXITSTATUS(status) ``):
4171+ result greater than or equal to 0.
4172+ * If the process was terminated by a signal (if ``WIFSIGNALED(status) `` is
4173+ true), return ``-signum `` where *signum * is the number of the signal that
4174+ caused the process to terminate (return ``-WTERMSIG(status) ``):
4175+ result less than 0.
4176+ * Otherwise, raise a :exc: `ValueError `.
4177+
4178+ On Windows, return *status * shifted right by 8 bits.
4179+
4180+ On Unix, if the process is being traced or if :func: `waitpid ` was called
4181+ with :data: `WUNTRACED ` option, the caller must first check if
4182+ ``WIFSTOPPED(status) `` is true. This function must not be called if
4183+ ``WIFSTOPPED(status) `` is true.
4184+
4185+ .. seealso ::
4186+
4187+ :func: `WIFEXITED `, :func: `WEXITSTATUS `, :func: `WIFSIGNALED `,
4188+ :func: `WTERMSIG `, :func: `WIFSTOPPED `, :func: `WSTOPSIG ` functions.
4189+
4190+ .. versionadded :: 3.9
4191+
4192+
41374193.. data :: WNOHANG
41384194
41394195 The option for :func: `waitpid ` to return immediately if no child process status
0 commit comments