@@ -3665,6 +3665,11 @@ written in Python, such as a mail server's external command delivery program.
3665
3665
subprocess was killed.) On Windows systems, the return value
3666
3666
contains the signed integer return code from the child process.
3667
3667
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
+
3668
3673
This is implemented using :class: `subprocess.Popen `; see that class's
3669
3674
documentation for more powerful ways to manage and communicate with
3670
3675
subprocesses.
@@ -3968,6 +3973,10 @@ written in Python, such as a mail server's external command delivery program.
3968
3973
to using this function. See the :ref: `subprocess-replacements ` section in
3969
3974
the :mod: `subprocess ` documentation for some helpful recipes.
3970
3975
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
+
3971
3980
.. audit-event :: os.system command os.system
3972
3981
3973
3982
.. availability :: Unix, Windows.
@@ -4008,8 +4017,16 @@ written in Python, such as a mail server's external command delivery program.
4008
4017
number is zero); the high bit of the low byte is set if a core file was
4009
4018
produced.
4010
4019
4020
+ :func: `waitstatus_to_exitcode ` can be used to convert the exit status into an
4021
+ exit code.
4022
+
4011
4023
.. availability :: Unix.
4012
4024
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
+
4013
4030
.. function :: waitid(idtype, id, options)
4014
4031
4015
4032
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.
4105
4122
id is known, not necessarily a child process. The :func: `spawn\* <spawnl> `
4106
4123
functions called with :const: `P_NOWAIT ` return suitable process handles.
4107
4124
4125
+ :func: `waitstatus_to_exitcode ` can be used to convert the exit status into an
4126
+ exit code.
4127
+
4108
4128
.. versionchanged :: 3.5
4109
4129
If the system call is interrupted and the signal handler does not raise an
4110
4130
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.
4120
4140
information. The option argument is the same as that provided to
4121
4141
:func: `waitpid ` and :func: `wait4 `.
4122
4142
4143
+ :func: `waitstatus_to_exitcode ` can be used to convert the exit status into an
4144
+ exitcode.
4145
+
4123
4146
.. availability :: Unix.
4124
4147
4125
4148
@@ -4131,9 +4154,42 @@ written in Python, such as a mail server's external command delivery program.
4131
4154
resource usage information. The arguments to :func: `wait4 ` are the same
4132
4155
as those provided to :func: `waitpid `.
4133
4156
4157
+ :func: `waitstatus_to_exitcode ` can be used to convert the exit status into an
4158
+ exitcode.
4159
+
4134
4160
.. availability :: Unix.
4135
4161
4136
4162
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
+
4137
4193
.. data :: WNOHANG
4138
4194
4139
4195
The option for :func: `waitpid ` to return immediately if no child process status
0 commit comments