[binutils-gdb] gdb: make Python conftest compatible with Python limited C API

Matthieu Longo mlongo@sourceware.org
Wed Oct 15 15:24:43 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=5643188363374dfa776497b2b42b5c9920b2349c

commit 5643188363374dfa776497b2b42b5c9920b2349c
Author: Matthieu Longo <matthieu.longo@arm.com>
Date:   Wed Jul 16 17:17:36 2025 +0100

    gdb: make Python conftest compatible with Python limited C API
    
    The current test to check the support of '--dynamic-list' linker flag
    uses PyRun_SimpleString (), which is part of the unstable API. As it is
    now, the test will systematically fail due to the undefined symbol
    rather than testing the import of ctypes.
    This patch replaces PyRun_SimpleString () by an equivalent code relying
    on the limited C API, and compatible with Python 3.4.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=23830
    Approved-By: Tom Tromey <tom@tromey.com>

Diff:
---
 gdb/configure    | 13 ++++++++++---
 gdb/configure.ac | 13 ++++++++++---
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/gdb/configure b/gdb/configure
index db63481b1c6..b7a2079d206 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -31034,11 +31034,18 @@ else
 int
 main ()
 {
-int err;
+
+          const char *code = "import ctypes\n";
           Py_Initialize ();
-          err = PyRun_SimpleString ("import ctypes\n");
+          PyObject *main_module = PyImport_AddModule ("__main__");
+          PyObject *global_dict = PyModule_GetDict (main_module);
+          PyObject *local_dict = PyDict_New ();
+          PyObject *py_code = Py_CompileString (code, "test", Py_single_input);
+          if (py_code == NULL)
+            return 1;
+          PyObject *res = PyEval_EvalCode (py_code, global_dict, local_dict);
           Py_Finalize ();
-          return err == 0 ? 0 : 1;
+          return res ? 0 : 1;
   ;
   return 0;
 }
diff --git a/gdb/configure.ac b/gdb/configure.ac
index 52924106bca..a88b6ebffe5 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -1761,11 +1761,18 @@ if test "${gdb_native}" = yes; then
      AC_RUN_IFELSE(
        [AC_LANG_PROGRAM(
          [#include "Python.h"],
-         [int err;
+         [
+          const char *code = "import ctypes\n";
           Py_Initialize ();
-          err = PyRun_SimpleString ("import ctypes\n");
+          PyObject *main_module = PyImport_AddModule ("__main__");
+          PyObject *global_dict = PyModule_GetDict (main_module);
+          PyObject *local_dict = PyDict_New ();
+          PyObject *py_code = Py_CompileString (code, "test", Py_single_input);
+          if (py_code == NULL)
+            return 1;
+          PyObject *res = PyEval_EvalCode (py_code, global_dict, local_dict);
           Py_Finalize ();
-          return err == 0 ? 0 : 1;])],
+          return res ? 0 : 1;])],
        [dynamic_list=true], [], [true])
      LIBS="$old_LIBS"
      CFLAGS="$old_CFLAGS"


More information about the Gdb-cvs mailing list