aboutsummaryrefslogtreecommitdiff
path: root/gdb/python
diff options
context:
space:
mode:
authorMatthieu Longo <matthieu.longo@arm.com>2025-07-25 11:58:56 +0100
committerMatthieu Longo <matthieu.longo@arm.com>2026-01-14 11:01:40 +0000
commit7cb9d3f07827ccdcf2fb17d9271ab3c4574b2635 (patch)
tree35af58853600580aff1f7e43402a1eef453a361a /gdb/python
parentee585288a459c64fab2389c893763bb1053e44ff (diff)
downloadbinutils-7cb9d3f07827ccdcf2fb17d9271ab3c4574b2635.tar.gz
binutils-7cb9d3f07827ccdcf2fb17d9271ab3c4574b2635.tar.bz2
binutils-7cb9d3f07827ccdcf2fb17d9271ab3c4574b2635.zip
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 <tom@tromey.com> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32473
Diffstat (limited to 'gdb/python')
-rw-r--r--gdb/python/py-gdb-readline.c27
1 files changed, 18 insertions, 9 deletions
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;