* [PATCH v1] py-gdb-readline: replace deprecated interfaces in GdbRemoveReadlineFinder
@ 2026-01-07 16:18 Matthieu Longo
2026-01-13 11:34 ` Matthieu Longo
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Matthieu Longo @ 2026-01-07 16:18 UTC (permalink / raw)
To: gdb-patches; +Cc: Tom Tromey, Andrew Burgess, Matthieu Longo
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
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH v1] py-gdb-readline: replace deprecated interfaces in GdbRemoveReadlineFinder
2026-01-07 16:18 [PATCH v1] py-gdb-readline: replace deprecated interfaces in GdbRemoveReadlineFinder Matthieu Longo
@ 2026-01-13 11:34 ` Matthieu Longo
2026-01-13 16:25 ` Tom Tromey
2026-01-13 16:27 ` Tom Tromey
2 siblings, 0 replies; 6+ messages in thread
From: Matthieu Longo @ 2026-01-13 11:34 UTC (permalink / raw)
To: gdb-patches, Tom Tromey, Andrew Burgess
On 07/01/2026 16:18, Matthieu Longo wrote:
> 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
Any thoughts ?
Is it the right approach ?
Matthieu
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v1] py-gdb-readline: replace deprecated interfaces in GdbRemoveReadlineFinder
2026-01-07 16:18 [PATCH v1] py-gdb-readline: replace deprecated interfaces in GdbRemoveReadlineFinder Matthieu Longo
2026-01-13 11:34 ` Matthieu Longo
@ 2026-01-13 16:25 ` Tom Tromey
2026-08-05 13:17 ` Tom de Vries
2026-01-13 16:27 ` Tom Tromey
2 siblings, 1 reply; 6+ messages in thread
From: Tom Tromey @ 2026-01-13 16:25 UTC (permalink / raw)
To: Matthieu Longo; +Cc: gdb-patches, Tom Tromey, Andrew Burgess
>>>>> "Matthieu" == Matthieu Longo <matthieu.longo@arm.com> writes:
Matthieu> A previous patch [1] enabled readline in Python in a GDB-specific way
Matthieu> and blocked the standard Python readline module to prevent conflicts
Matthieu> with GDB by adding a custom importer raising an exception for the readline
Matthieu> module.
I wonder if gdb could do better here. To be clear, not your problem.
Matthieu> This patch uses Patryk Sondej's approach detailed in bug 32473, but with
Matthieu> a slight variation regarding the finder insertion in sys.meta_path.
Matthieu> It also adds a new test to prevent future regression.
Seems reasonable to me, though this isn't an area I know very well.
Approved-By: Tom Tromey <tom@tromey.com>
Tom
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v1] py-gdb-readline: replace deprecated interfaces in GdbRemoveReadlineFinder
2026-01-13 16:25 ` Tom Tromey
@ 2026-08-05 13:17 ` Tom de Vries
2026-08-05 15:00 ` Tom de Vries
0 siblings, 1 reply; 6+ messages in thread
From: Tom de Vries @ 2026-08-05 13:17 UTC (permalink / raw)
To: Tom Tromey, Matthieu Longo; +Cc: gdb-patches, Andrew Burgess
On 1/13/26 5:25 PM, Tom Tromey wrote:
>>>>>> "Matthieu" == Matthieu Longo <matthieu.longo@arm.com> writes:
>
> Matthieu> A previous patch [1] enabled readline in Python in a GDB-specific way
> Matthieu> and blocked the standard Python readline module to prevent conflicts
> Matthieu> with GDB by adding a custom importer raising an exception for the readline
> Matthieu> module.
>
> I wonder if gdb could do better here. To be clear, not your problem.
>
> Matthieu> This patch uses Patryk Sondej's approach detailed in bug 32473, but with
> Matthieu> a slight variation regarding the finder insertion in sys.meta_path.
> Matthieu> It also adds a new test to prevent future regression.
>
> Seems reasonable to me, though this isn't an area I know very well.
>
> Approved-By: Tom Tromey <tom@tromey.com>
I reproduced this regression on the gdb-17-branch with python 3.6 (and
later as well), and can confirm that backporting the patch to the
gdb-17-branch fixes it.
I'm not able to test with python 3.4 though, otherwise I would apply it
to the branch.
Thanks,
- Tom
>
> Tom
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v1] py-gdb-readline: replace deprecated interfaces in GdbRemoveReadlineFinder
2026-08-05 13:17 ` Tom de Vries
@ 2026-08-05 15:00 ` Tom de Vries
0 siblings, 0 replies; 6+ messages in thread
From: Tom de Vries @ 2026-08-05 15:00 UTC (permalink / raw)
To: Tom Tromey, Matthieu Longo; +Cc: gdb-patches, Andrew Burgess
On 8/5/26 3:17 PM, Tom de Vries wrote:
> On 1/13/26 5:25 PM, Tom Tromey wrote:
>>>>>>> "Matthieu" == Matthieu Longo <matthieu.longo@arm.com> writes:
>>
>> Matthieu> A previous patch [1] enabled readline in Python in a GDB-
>> specific way
>> Matthieu> and blocked the standard Python readline module to prevent
>> conflicts
>> Matthieu> with GDB by adding a custom importer raising an exception
>> for the readline
>> Matthieu> module.
>>
>> I wonder if gdb could do better here. To be clear, not your problem.
>>
>> Matthieu> This patch uses Patryk Sondej's approach detailed in bug
>> 32473, but with
>> Matthieu> a slight variation regarding the finder insertion in
>> sys.meta_path.
>> Matthieu> It also adds a new test to prevent future regression.
>>
>> Seems reasonable to me, though this isn't an area I know very well.
>>
>> Approved-By: Tom Tromey <tom@tromey.com>
>
> I reproduced this regression on the gdb-17-branch with python 3.6 (and
> later as well), and can confirm that backporting the patch to the
> gdb-17-branch fixes it.
>
> I'm not able to test with python 3.4 though, otherwise I would apply it
> to the branch.
>
I remembered I had a leap 42.3 container, with system python 3.4. It
has gcc 4.8.5, so I can't build gdb-17-branch, so instead I build
gdb-14-branch. The problem reproduces, and backporting the patch
(applies cleanly) fixes it.
So now that I know that I'm not breaking something for python 3.4, I'll
proceed with the backport.
Thanks,
- Tom
> Thanks,
> - Tom
>
>>
>> Tom
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v1] py-gdb-readline: replace deprecated interfaces in GdbRemoveReadlineFinder
2026-01-07 16:18 [PATCH v1] py-gdb-readline: replace deprecated interfaces in GdbRemoveReadlineFinder Matthieu Longo
2026-01-13 11:34 ` Matthieu Longo
2026-01-13 16:25 ` Tom Tromey
@ 2026-01-13 16:27 ` Tom Tromey
2 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2026-01-13 16:27 UTC (permalink / raw)
To: Matthieu Longo; +Cc: gdb-patches, Tom Tromey, Andrew Burgess
>>>>> "Matthieu" == Matthieu Longo <matthieu.longo@arm.com> writes:
Matthieu> A previous patch [1] enabled readline in Python in a GDB-specific way
Matthieu> and blocked the standard Python readline module to prevent conflicts
Matthieu> with GDB by adding a custom importer raising an exception for the readline
Matthieu> module.
Matthieu> [1]: 037bbc8eeaf8e6f3a5e185e78268aa71a1159ae7
Also -- no tests and no docs in that one :( I didn't look up the thread
since I don't want to find out it's my fault.
Tom
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-08-05 15:02 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-07 16:18 [PATCH v1] py-gdb-readline: replace deprecated interfaces in GdbRemoveReadlineFinder Matthieu Longo
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox