From: Matthieu Longo Date: Fri, 25 Jul 2025 10:58:56 +0000 (+0100) Subject: py-gdb-readline: replace deprecated interfaces in GdbRemoveReadlineFinder X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7cb9d3f07827ccdcf2fb17d9271ab3c4574b2635;p=thirdparty%2Fbinutils-gdb.git py-gdb-readline: replace deprecated interfaces in GdbRemoveReadlineFinder 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 Approved-By: Tom Tromey Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32473 --- diff --git a/gdb/python/py-gdb-readline.c b/gdb/python/py-gdb-readline.c index 07ecffaeb3e..f0fe967f073 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 . + +# 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 : readline module disabled under GDB\r\n.*"