From: Matthieu Longo <matthieu.longo@arm.com>
To: <gdb-patches@sourceware.org>
Cc: Tom Tromey <tom@tromey.com>, Andrew Burgess <aburgess@redhat.com>,
Matthieu Longo <matthieu.longo@arm.com>
Subject: [PATCH v1] py-gdb-readline: replace deprecated interfaces in GdbRemoveReadlineFinder
Date: Wed, 7 Jan 2026 16:18:54 +0000 [thread overview]
Message-ID: <20260107161854.1712307-1-matthieu.longo@arm.com> (raw)
A previous patch [1] enabled readline in Python in a GDB-specific way
and blocked the standard Python readline module to prevent conflicts
with GDB by adding a custom importer raising an exception for the readline
module.
This custom importer was written back in 2012 for old Python versions,
and does not seem to work anymore with Python 3.x. find_module() and
load_module() have been deprecated since Python 3.4, and the first one
has been removed since 3.12, and the second will be removed in 3.15.
The GDB testsuite does not cover this use case, and the removal of
find_module() was not detected by the testsuite. This issue is tracked
in bug 32473.
importlib.abc.MetaPathFinder:
find_module(fullname, path)
Deprecated since version 3.4: Use find_spec() instead.
Changed in version 3.10: Use of find_module() by the import
system now raises ImportWarning.
Changed in version 3.12: find_module() has been removed. Use
find_spec() instead.
find_spec(fullname, path, target=None)
New in version 3.4, as a replacement for find_module.
importlib.abc.Loader:
load_module(fullname):
Deprecated since version 3.4, will be removed in version 3.15
The recommended API for loading a module is exec_module()
(and create_module()).
This patch uses Patryk Sondej's approach detailed in bug 32473, but with
a slight variation regarding the finder insertion in sys.meta_path.
It also adds a new test to prevent future regression.
[1]: 037bbc8eeaf8e6f3a5e185e78268aa71a1159ae7
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32473
---
gdb/python/py-gdb-readline.c | 27 ++++++++++++-------
.../gdb.python/remove-readline-finder.exp | 27 +++++++++++++++++++
2 files changed, 45 insertions(+), 9 deletions(-)
create mode 100644 gdb/testsuite/gdb.python/remove-readline-finder.exp
diff --git a/gdb/python/py-gdb-readline.c b/gdb/python/py-gdb-readline.c
index 21503168725..9cb70bc3fcb 100644
--- a/gdb/python/py-gdb-readline.c
+++ b/gdb/python/py-gdb-readline.c
@@ -90,20 +90,29 @@ gdbpy_initialize_gdb_readline ()
GDB's readline should be implemented to replace Python's readline
and prevent conflicts. For now, this file implements a
sys.meta_path finder that simply fails to import the readline
- module. */
+ module.
+
+ Notes: Python includes three default importers. As mentioned in the
+ documentation [1]:
+ - The first one knows how to locate built-in modules.
+ - The second one knows how to locate frozen modules.
+ - A third default finder searches an import path for modules.
+ [1]: https://docs.python.org/3/reference/import.html#finders-and-loaders
+
+ The third default finder is the one that will load readline, so the custom
+ finder to disable the import of readline in GDB has to be placed before
+ this third default finder. */
if (PyRun_SimpleString ("\
import sys\n\
+from importlib.abc import MetaPathFinder\n\
\n\
-class GdbRemoveReadlineFinder:\n\
- def find_module(self, fullname, path=None):\n\
- if fullname == 'readline' and path is None:\n\
- return self\n\
- return None\n\
+class GdbRemoveReadlineFinder(MetaPathFinder):\n\
\n\
- def load_module(self, fullname):\n\
- raise ImportError('readline module disabled under GDB')\n\
+ def find_spec(self, fullname, path=None, target=None):\n\
+ if fullname == \"readline\":\n\
+ raise ImportError(\"readline module disabled under GDB\")\n\
\n\
-sys.meta_path.append(GdbRemoveReadlineFinder())\n\
+sys.meta_path.insert(2, GdbRemoveReadlineFinder())\n\
") == 0)
PyOS_ReadlineFunctionPointer = gdbpy_readline_wrapper;
diff --git a/gdb/testsuite/gdb.python/remove-readline-finder.exp b/gdb/testsuite/gdb.python/remove-readline-finder.exp
new file mode 100644
index 00000000000..362e90e6b7c
--- /dev/null
+++ b/gdb/testsuite/gdb.python/remove-readline-finder.exp
@@ -0,0 +1,27 @@
+# Copyright 2026 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Test that the readline module cannot be imported from GDB (PR 32473).
+
+load_lib gdb-python.exp
+
+require allow_python_tests
+
+standard_testfile
+
+clean_restart
+
+gdb_test "py import readline" \
+ "Python Exception <class 'ImportError'>: readline module disabled under GDB\r\n.*"
--
2.52.0
next reply other threads:[~2026-01-07 16:21 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-07 16:18 Matthieu Longo [this message]
2026-01-13 11:34 ` Matthieu Longo
2026-01-13 16:25 ` Tom Tromey
2026-08-05 13:17 ` Tom de Vries
2026-08-05 15:00 ` Tom de Vries
2026-01-13 16:27 ` Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260107161854.1712307-1-matthieu.longo@arm.com \
--to=matthieu.longo@arm.com \
--cc=aburgess@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=tom@tromey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox