Skip to content

Commit 25b17d4

Browse files
bpo-24053: Add EXIT_SUCCESS and EXIT_FAILURE values in the sys module (GH-14773)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
1 parent 3758955 commit 25b17d4

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
@@ -2432,6 +2432,26 @@ always available. Unless explicitly noted otherwise, all variables are read-only
24322432
.. versionadded:: 3.2
24332433

24342434

2435+
.. data:: EXIT_SUCCESS
2436+
2437+
An int you can use to indicate your program ended gracefully.
2438+
2439+
.. code-block:: python
2440+
2441+
sys.exit(sys.EXIT_SUCCESS)
2442+
2443+
.. versionadded:: 3.16
2444+
2445+
.. data:: EXIT_FAILURE
2446+
2447+
An int you can use to indicate your program encountered an error.
2448+
2449+
.. code-block:: python
2450+
2451+
sys.exit(sys.EXIT_FAILURE)
2452+
2453+
.. versionadded:: 3.16
2454+
24352455
.. rubric:: Citations
24362456

24372457
.. [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\ .

‎Lib/test/test_sys.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,17 @@ def test_exit(self):
265265
self.assertEqual(out, b'')
266266
self.assertEqual(err, b'')
267267

268+
# test EXIT_SUCCESS and EXIT_FAILURE constants
269+
self.assertNotEqual(sys.EXIT_SUCCESS, sys.EXIT_FAILURE)
270+
rc, out, err = assert_python_ok('-c', 'import sys; sys.exit(sys.EXIT_SUCCESS)')
271+
self.assertEqual(rc, sys.EXIT_SUCCESS)
272+
self.assertEqual(out, b'')
273+
self.assertEqual(err, b'')
274+
rc, out, err = assert_python_failure('-c', 'import sys; sys.exit(sys.EXIT_FAILURE)')
275+
self.assertEqual(rc, sys.EXIT_FAILURE)
276+
self.assertEqual(out, b'')
277+
self.assertEqual(err, b'')
278+
268279
def check_exit_message(code, expected, **env_vars):
269280
rc, out, err = assert_python_failure('-c', code, **env_vars)
270281
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
@@ -4380,6 +4380,9 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p)
43804380
PyUnstable_Module_SetGIL(sysmod, Py_MOD_GIL_NOT_USED);
43814381
#endif
43824382

4383+
PyModule_AddIntMacro(sysmod, EXIT_SUCCESS);
4384+
PyModule_AddIntMacro(sysmod, EXIT_FAILURE);
4385+
43834386
PyObject *sysdict = PyModule_GetDict(sysmod);
43844387
if (sysdict == NULL) {
43854388
goto error;

0 commit comments

Comments
 (0)