diff options
| author | Matthieu Longo <matthieu.longo@arm.com> | 2026-01-06 13:12:38 +0000 |
|---|---|---|
| committer | Matthieu Longo <matthieu.longo@arm.com> | 2026-01-28 09:41:25 +0000 |
| commit | 264a8a2236e8aa64b333a69e42a55ff8c0844f6e (patch) | |
| tree | cf460000f0eb1644f7539760603870081fb29a5e /gdb/python | |
| parent | ea96771a0188a235645558ae10d6885c91c1ac00 (diff) | |
| download | binutils-264a8a2236e8aa64b333a69e42a55ff8c0844f6e.tar.gz binutils-264a8a2236e8aa64b333a69e42a55ff8c0844f6e.tar.bz2 binutils-264a8a2236e8aa64b333a69e42a55ff8c0844f6e.zip | |
Python limited API: migrate Py_CompileStringExFlags and PyRun_SimpleString
This patch replaces Py_CompileStringExFlags () with its limited C API
equivalent, Py_CompileString (). The eval_python_command () helper is
now exposed through the private GDB Python API as a utility function.
PyRun_SimpleString () is replaced with eval_python_command () to avoid
code duplication.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=23830
Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdb/python')
| -rw-r--r-- | gdb/python/py-gdb-readline.c | 5 | ||||
| -rw-r--r-- | gdb/python/python-internal.h | 3 | ||||
| -rw-r--r-- | gdb/python/python.c | 17 |
3 files changed, 14 insertions, 11 deletions
diff --git a/gdb/python/py-gdb-readline.c b/gdb/python/py-gdb-readline.c index f0fe967f073..e8e2c23547c 100644 --- a/gdb/python/py-gdb-readline.c +++ b/gdb/python/py-gdb-readline.c @@ -102,7 +102,7 @@ gdbpy_initialize_gdb_readline () 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 ("\ + const char *code = "\ import sys\n\ from importlib.abc import MetaPathFinder\n\ \n\ @@ -113,7 +113,8 @@ class GdbRemoveReadlineFinder(MetaPathFinder):\n\ raise ImportError(\"readline module disabled under GDB\")\n\ \n\ sys.meta_path.insert(2, GdbRemoveReadlineFinder())\n\ -") == 0) +"; + if (eval_python_command (code, Py_file_input) == 0) PyOS_ReadlineFunctionPointer = gdbpy_readline_wrapper; return 0; diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 85c76779a49..65d2eee38ed 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -1319,4 +1319,7 @@ protected: gdb::unordered_map<val_type *, obj_type *> m_objects; }; +extern int eval_python_command (const char *command, int start_symbol, + const char *filename = nullptr); + #endif /* GDB_PYTHON_PYTHON_INTERNAL_H */ diff --git a/gdb/python/python.c b/gdb/python/python.c index 87fce272c42..989add70d1a 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -299,9 +299,9 @@ gdbpy_check_quit_flag (const struct extension_language_defn *extlang) NULL means that this is evaluating a string, not the contents of a file. */ -static int +int eval_python_command (const char *command, int start_symbol, - const char *filename = nullptr) + const char *filename) { PyObject *m, *d; @@ -340,13 +340,12 @@ eval_python_command (const char *command, int start_symbol, } } - /* Use this API because it is in Python 3.2. */ - gdbpy_ref<> code (Py_CompileStringExFlags (command, - filename == nullptr - ? "<string>" - : filename, - start_symbol, - nullptr, -1)); + /* Use this API because it is available with the Python limited API. */ + gdbpy_ref<> code (Py_CompileString (command, + filename == nullptr + ? "<string>" + : filename, + start_symbol)); int result = -1; if (code != nullptr) |
