diff options
author | Tom de Vries <tdevries@suse.de> | 2024-09-25 19:29:57 +0200 |
---|---|---|
committer | Tom de Vries <tdevries@suse.de> | 2024-09-25 19:29:57 +0200 |
commit | dfba4847f6a22eef700917e2718ea847d9f5cc9d (patch) | |
tree | 4f8d775a0fcf607b4299391b078b942767288e84 /gdb/python | |
parent | f82fd76c96ce848d77f86b8f1beefcff7656159c (diff) | |
download | binutils-dfba4847f6a22eef700917e2718ea847d9f5cc9d.zip binutils-dfba4847f6a22eef700917e2718ea847d9f5cc9d.tar.gz binutils-dfba4847f6a22eef700917e2718ea847d9f5cc9d.tar.bz2 |
[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 a207f6b3a38 ("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
Diffstat (limited to 'gdb/python')
-rw-r--r-- | gdb/python/py-utils.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c index 8064cec..0bc18a6 100644 --- a/gdb/python/py-utils.c +++ b/gdb/python/py-utils.c @@ -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') { |