From: Matthieu Longo Date: Tue, 6 Jan 2026 13:12:38 +0000 (+0000) Subject: Python limited API: migrate Py_CompileStringExFlags and PyRun_SimpleString X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=264a8a2236e8aa64b333a69e42a55ff8c0844f6e;p=thirdparty%2Fbinutils-gdb.git 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 --- 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 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 - ? "" - : 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 + ? "" + : filename, + start_symbol)); int result = -1; if (code != nullptr)