]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Python limited API: migrate Py_CompileStringExFlags and PyRun_SimpleString
authorMatthieu Longo <matthieu.longo@arm.com>
Tue, 6 Jan 2026 13:12:38 +0000 (13:12 +0000)
committerMatthieu Longo <matthieu.longo@arm.com>
Wed, 28 Jan 2026 09:41:25 +0000 (09:41 +0000)
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>
gdb/python/py-gdb-readline.c
gdb/python/python-internal.h
gdb/python/python.c

index f0fe967f0736209556477c0b80b9035549e3299f..e8e2c23547cee673361405435e3a5cdd4c62344d 100644 (file)
@@ -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;
index 85c76779a4987a5abbe7f7fa1edc43c3b5fabcda..65d2eee38ed26011412e8312d107cbf365bbb514 100644 (file)
@@ -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 */
index 87fce272c42d8ad66f6375b2d44d76c96d619ef5..989add70d1a7531e92c966efd64034afce5014ea 100644 (file)
@@ -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)