Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2409,6 +2409,26 @@ always available. Unless explicitly noted otherwise, all variables are read-only
.. versionadded:: 3.2


.. data:: EXIT_SUCCESS

An int you can use to indicate your program ended gracefully.

.. code-block:: python

sys.exit(sys.EXIT_SUCCESS)

.. versionadded:: 3.16

.. data:: EXIT_FAILURE

An int you can use to indicate your program encountered an error.

.. code-block:: python

sys.exit(sys.EXIT_FAILURE)

.. versionadded:: 3.16

.. rubric:: Citations

.. [C99] ISO/IEC 9899:1999. "Programming languages -- C." A public draft of this standard is available at https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf\ .
11 changes: 11 additions & 0 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ def test_exit(self):
self.assertEqual(out, b'')
self.assertEqual(err, b'')

# test EXIT_SUCCESS and EXIT_FAILURE constants
self.assertNotEqual(sys.EXIT_SUCCESS, sys.EXIT_FAILURE)
rc, out, err = assert_python_ok('-c', 'import sys; sys.exit(sys.EXIT_SUCCESS)')
self.assertEqual(rc, sys.EXIT_SUCCESS)
self.assertEqual(out, b'')
self.assertEqual(err, b'')
rc, out, err = assert_python_failure('-c', 'import sys; sys.exit(sys.EXIT_FAILURE)')
self.assertEqual(rc, sys.EXIT_FAILURE)
self.assertEqual(out, b'')
self.assertEqual(err, b'')

def check_exit_message(code, expected, **env_vars):
rc, out, err = assert_python_failure('-c', code, **env_vars)
self.assertEqual(rc, 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add EXIT_FAILURE and EXIT_SUCCESS in the sys module.
3 changes: 3 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4329,6 +4329,9 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p)
PyUnstable_Module_SetGIL(sysmod, Py_MOD_GIL_NOT_USED);
#endif

PyModule_AddIntMacro(sysmod, EXIT_SUCCESS);
PyModule_AddIntMacro(sysmod, EXIT_FAILURE);

PyObject *sysdict = PyModule_GetDict(sysmod);
if (sysdict == NULL) {
goto error;
Expand Down
Loading