Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] [gdb/python] Handle error in gdbpy_initialize_gdb_readline
@ 2026-08-07 16:48 Tom de Vries
  2026-08-07 19:26 ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Tom de Vries @ 2026-08-07 16:48 UTC (permalink / raw)
  To: gdb-patches

On Fedora Rawhide aarch64-linux, with test-case gdb.python/py-failed-init.exp
I ran into:
...
builtin_spawn $build/gdb/gdb -nw -nx -q -iex set height 0 -iex set width 0 \
  -data-directory $build/gdb/data-directory -iex set interactive-mode on
WARN: Could not find the standard library directory! The Python 'home' \
  directory was set to 'foo', is this correct?
Error occurred computing Python error message.
$build/gdb/gdb: warning:
Could not load the Python gdb module from `$build/gdb/data-directory/python'.
Limited Python support is available from the _gdb module.
Suggest passing --data-directory=/path/to/gdb/data-directory.
(gdb) set height 0
(gdb) set width 0
(gdb) dir
Reinitialize source path to empty? (y or n) y
Source directories searched: $cdir:$cwd
(gdb) dir $src/gdb/testsuite/gdb.python
Source directories searched: $src/gdb/testsuite/gdb.python:$cdir:$cwd
(gdb) python print (1)
1
(gdb) FAIL: $exp: gdb-command<python print (1)>
quit
Exception ignored on threading shutdown:
Traceback (most recent call last):
  File "<string>", line 2, in <module>
ModuleNotFoundError: No module named 'importlib'
PASS: $exp: quit
...

The test-case tries to break python:
...
save_vars { env(PYTHONHOME) } {
    setenv PYTHONHOME foo
    clean_restart
}
...
enough to get it to this point:
...
gdb_test "python print (1)" \
    "Python not initialized"
...
but apparently, that doesn't work anymore in this python version:
...
$ python --version
Python 3.15.0b4
...

The test-case needs updating, and I've submitted a testsuite patch [1] for
that.

The next question is why we're seeing a ModuleNotFoundError on quit.

I investigated this, and found that it originates from
gdbpy_initialize_gdb_readline, where we do:
...
   if (eval_python_command (code, Py_file_input) == 0)
    PyOS_ReadlineFunctionPointer = gdbpy_readline_wrapper;
...
but don't report and reset the python error state, so instead the error is
reported by Py_Finalize.

Fix this by:
- making sure that the error is reported immediately, though in the form of a
  warning rather than an error, and
- disabling the python-interactive command if gdbpy_initialize_gdb_readline
  fails, to avoid broken readline behavior in a python-interactive session.

Also make the test-case a bit stricter by checking that there's no output when
quitting.

Tested on aarch64-linux.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34485

[1] https://sourceware.org/pipermail/gdb-patches/2026-August/229193.html
---
 gdb/python/py-gdb-readline.c                | 19 +++++++++++++++++++
 gdb/python/python-internal.h                |  5 +++++
 gdb/python/python.c                         |  4 ++++
 gdb/testsuite/gdb.python/py-failed-init.exp |  6 ++++++
 4 files changed, 34 insertions(+)

diff --git a/gdb/python/py-gdb-readline.c b/gdb/python/py-gdb-readline.c
index e8e2c23547c..df628efb1f4 100644
--- a/gdb/python/py-gdb-readline.c
+++ b/gdb/python/py-gdb-readline.c
@@ -116,8 +116,27 @@ sys.meta_path.insert(2, GdbRemoveReadlineFinder())\n\
 ";
   if (eval_python_command (code, Py_file_input) == 0)
     PyOS_ReadlineFunctionPointer = gdbpy_readline_wrapper;
+  else
+    {
+      if (PyErr_Occurred ())
+	{
+	  PyErr_Print ();
+	  PyErr_Clear ();
+	}
+
+      warning (_("Disabling import readline failed, python-interactive"
+		 " command disabled"));
+    }
 
   return 0;
 }
 
+/* See python-internal.h.  */
+
+bool
+gdbpy_import_readline_disabled ()
+{
+  return PyOS_ReadlineFunctionPointer == gdbpy_readline_wrapper;
+}
+
 GDBPY_INITIALIZE_FILE (gdbpy_initialize_gdb_readline);
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 4ca28bb6957..e9e02785a43 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -1387,4 +1387,9 @@ py_notimplemented ()
 #undef Py_RETURN_FALSE
 #undef Py_RETURN_NOTIMPLEMENTED
 
+/* Return true if import readline was successfully disabled during
+   initialization.  */
+
+extern bool gdbpy_import_readline_disabled ();
+
 #endif /* GDB_PYTHON_PYTHON_INTERNAL_H */
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 214abd03905..2b482a6f35c 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -383,6 +383,10 @@ eval_python_command (const char *command, int start_symbol,
 static void
 python_interactive_command (const char *arg, int from_tty)
 {
+  if (!gdbpy_import_readline_disabled ())
+    error (_("Disabling import readline failed, python-interactive"
+	     " command disabled"));
+
   struct ui *ui = current_ui;
   int err;
 
diff --git a/gdb/testsuite/gdb.python/py-failed-init.exp b/gdb/testsuite/gdb.python/py-failed-init.exp
index 622743d8d9b..c2b9c990e29 100644
--- a/gdb/testsuite/gdb.python/py-failed-init.exp
+++ b/gdb/testsuite/gdb.python/py-failed-init.exp
@@ -24,8 +24,14 @@ save_vars { env(PYTHONHOME) } {
 gdb_test "python print (1)" \
     "Python not initialized"
 
+set output_seen 0
 gdb_test_multiple "quit" "" {
+    -re ^quit\r\n {
+	exp_continue
+    }
     eof {
+	set output_seen [expr [string length $expect_out(buffer)] != 0]
 	pass $gdb_test_name
     }
 }
+gdb_assert {!$output_seen} "no output after quit"

base-commit: 7800332405e135eaf3e1482b8195669b3662c142
-- 
2.51.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-08-07 22:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-07 16:48 [PATCH] [gdb/python] Handle error in gdbpy_initialize_gdb_readline Tom de Vries
2026-08-07 19:26 ` Tom Tromey
2026-08-07 22:36   ` Tom de Vries

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox