[gdb/python] Make sure python sys.exit makes gdb exit

With gdb 15.1, python sys.exit no longer makes gdb exit:
...
$ gdb -q -batch -ex "python sys.exit(2)" -ex "print 123"; echo $?
Python Exception <class 'SystemExit'>: 2
Error occurred in Python: 2
$1 = 123
0
...

This is a change in behaviour since commit a207f6b3a3 ("Rewrite "python"
command exception handling"), first available in gdb 15.1.

This patch reverts to the old behaviour by handling PyExc_SystemExit in
gdbpy_handle_exception, such what we have instead:
...
$ gdb -q -batch -ex "python sys.exit(2)" -ex "print 123"; echo $?
2
...

Tested on x86_64-linux, with python 3.6 and 3.13.

Tested-By: Guinevere Larsen <blarsen@redhat.com>
Approved-By: Tom Tromey <tom@tromey.com>

PR python/31946
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31946
This commit is contained in:
Tom de Vries
2024-09-25 19:29:57 +02:00
parent f82fd76c96
commit dfba4847f6
2 changed files with 98 additions and 0 deletions

View File

@@ -390,6 +390,35 @@ gdbpy_handle_exception ()
if (fetched_error.type_matches (PyExc_KeyboardInterrupt))
throw_quit ("Quit");
else if (fetched_error.type_matches (PyExc_SystemExit))
{
gdbpy_ref<> value = fetched_error.value ();
gdbpy_ref<> code (PyObject_GetAttrString (value.get (), "code"));
int exit_arg;
if (code.get () == Py_None)
{
/* CODE == None: exit status is 0. */
exit_arg = 0;
}
else if (code.get () != nullptr && PyLong_Check (code.get ()))
{
/* CODE == integer: exit status is aforementioned integer. */
exit_arg = PyLong_AsLong (code.get ());
}
else
{
if (code.get () == nullptr)
gdbpy_print_stack ();
/* Otherwise: exit status is 1, print code to stderr. */
if (msg != nullptr)
gdb_printf (gdb_stderr, "%s\n", msg.get ());
exit_arg = 1;
}
quit_force (&exit_arg, 0);
}
else if (! fetched_error.type_matches (gdbpy_gdberror_exc)
|| msg == NULL || *msg == '\0')
{