Python 3.9+osfunction
os.waitstatus_to_exitcode
os — process
Convert a raw wait status (from waitpid) to an exit code (3.9+).
Syntax
os.waitstatus_to_exitcode(status)
For a process killed by a signal it returns the negative signal number: -15 for SIGTERM. subprocess encodes returncode the same way, so the two compare easily. For a stopped rather than exited process it raises ValueError. It decodes the status from os.system, os.wait or popen().close() without manual W* macros.
Examples
import os
print(os.waitstatus_to_exitcode(0)) # → 0
print(os.waitstatus_to_exitcode(1 << 8)) # → 1
status = 3 << 8 # так кодируется потомок, завершившийся с кодом 3
print(os.waitstatus_to_exitcode(status)) # → 3
print(os.waitstatus_to_exitcode(9)) # → -9 — потомок убит сигналом 9 (SIGKILL), результат отрицательный
print(os.waitstatus_to_exitcode(127)) # → ValueError — процесс остановлен, а не завершён
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy