Skip to content

Commit ec77866

Browse files
committed
bpo-24053: Add EXIT_SUCCESS and EXIT_FAILURE values in the sys module
1 parent 036fe85 commit ec77866

4 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎Doc/library/sys.rst‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,26 @@ always available.
16131613
.. versionadded:: 3.2
16141614

16151615

1616+
.. data:: EXIT_SUCCESS
1617+
1618+
An int you can use to indicate your program ended gracefully.
1619+
1620+
.. code-block:: python
1621+
1622+
sys.exit(sys.EXIT_SUCCESS)
1623+
1624+
.. versionadded:: 3.10
1625+
1626+
.. data:: EXIT_FAILURE
1627+
1628+
An int you can use to indicate your program encountered an error.
1629+
1630+
.. code-block:: python
1631+
1632+
sys.exit(sys.EXIT_FAILURE)
1633+
1634+
.. versionadded:: 3.10
1635+
16161636
.. rubric:: Citations
16171637

16181638
.. [C99] ISO/IEC 9899:1999. "Programming languages -- C." A public draft of this standard is available at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf\ .

‎Lib/test/test_sys.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,17 @@ def test_exit(self):
154154
self.assertEqual(out, b'')
155155
self.assertEqual(err, b'')
156156

157+
# test EXIT_SUCCESS and EXIT_FAILURE constants
158+
self.assertNotEqual(sys.EXIT_SUCCESS, sys.EXIT_FAILURE)
159+
rc, out, err = assert_python_ok('-c', 'import sys; sys.exit(sys.EXIT_SUCCESS)')
160+
self.assertEqual(rc, sys.EXIT_SUCCESS)
161+
self.assertEqual(out, b'')
162+
self.assertEqual(err, b'')
163+
rc, out, err = assert_python_failure('-c', 'import sys; sys.exit(sys.EXIT_FAILURE)')
164+
self.assertEqual(rc, sys.EXIT_FAILURE)
165+
self.assertEqual(out, b'')
166+
self.assertEqual(err, b'')
167+
157168
def check_exit_message(code, expected, **env_vars):
158169
rc, out, err = assert_python_failure('-c', code, **env_vars)
159170
self.assertEqual(rc, 1)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add EXIT_FAILURE and EXIT_SUCCESS in the sys module.

‎Python/sysmodule.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,6 +2959,9 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p)
29592959
return _PyStatus_ERR("failed to create a module object");
29602960
}
29612961

2962+
PyModule_AddIntMacro(sysmod, EXIT_SUCCESS);
2963+
PyModule_AddIntMacro(sysmod, EXIT_FAILURE);
2964+
29622965
PyObject *sysdict = PyModule_GetDict(sysmod);
29632966
if (sysdict == NULL) {
29642967
return _PyStatus_ERR("can't initialize sys dict");

0 commit comments

Comments
 (0)