[binutils-gdb] gdb/python: work around missing symbols not yet part of Python limited API

Matthieu Longo via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=58f83d84710e37a36f2b8b6a6404f9904dc4ae88

commit 58f83d84710e37a36f2b8b6a6404f9904dc4ae88
Author: Matthieu Longo <[email protected]>
Date:   Mon Mar 30 17:08:03 2026 +0100

    gdb/python: work around missing symbols not yet part of Python limited API
    
    Most Python API usages in GDB can be migrated to the limited API, except
    the following:
    
    - PEP-741's configuration structures and functions, which use opaque
      types. They were originally intended to be part of the Python limited
      API, but some Python core maintainers opposed their inclusion at the
      time.
    - PyOS_ReadlineFunctionPointer, a global variable storing a function
      used to override PyOS_StdioReadline(). The signature has remained
      unchanged for a long time.
    - PyRun_InteractiveLoop, used to read and execute Python statements when
      embedding an interactive interpreter. Its signature has also remained
      stable for a long time.
    
    Since no limited API alternatives exist for these, and given their long
    history of ABI stability, one approach is to expose them in a GDB header
    and rely on their continued stability. While this is not without risk,
    it seems acceptable given the arguments above. This would remove the
    remaining obstacles preventing GDB from being agnostic to the Python
    version available at runtime.
    That said, issues should be opened on CPython issue tracker to request
    that these functions be included in the limited API in future versions.
    Last but not least, GDB does not need to officially support the Python
    limited API. The '--enable-py-limited-api' option can remain experimental,
    with appropriate forewarnings about its limitations and guarantees.
    
    This patch adds a new header, python-limited-api-missing.h, which
    exposes symbols not yet part of the Python limited API.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=23830
    Approved-By: Tom Tromey <[email protected]>

Diff:
---
 gdb/Makefile.in                         |  1 +
 gdb/python/python-internal.h            |  3 ++
 gdb/python/python-limited-api-missing.h | 62 +++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+)

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index a0c11f5eb57..b2b7ef3d606 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1616,6 +1616,7 @@ HFILES_NO_SRCDIR = \
 	python/py-stopevent.h \
 	python/python.h \
 	python/python-internal.h \
+	python/python-limited-api-missing.h \
 	python/py-uiout.h \
 	quick-symbol.h \
 	ravenscar-thread.h \
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 37bc37691fe..3147156d5be 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -57,6 +57,9 @@
    double quotes.  On case-insensitive filesystems, this prevents us
    from including our python/python.h header file.  */
 #include <Python.h>
+#ifdef Py_LIMITED_API
+#include "python-limited-api-missing.h"
+#endif
 #include <frameobject.h>
 #include "py-ref.h"
 #include "py-obj-type.h"
diff --git a/gdb/python/python-limited-api-missing.h b/gdb/python/python-limited-api-missing.h
new file mode 100644
index 00000000000..07882ca1cf2
--- /dev/null
+++ b/gdb/python/python-limited-api-missing.h
@@ -0,0 +1,62 @@
+/* Gdb/Python header exposing missing symbols in the Python limited API.
+   Note: this is a workaround solution until those existing symbols below,
+   or new symbols are exposed in the limited API.
+
+   Copyright (C) 2026 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef GDB_PYTHON_LIMITED_API_MISSING_H
+#define GDB_PYTHON_LIMITED_API_MISSING_H
+
+#ifdef Py_LIMITED_API
+extern "C"
+{
+
+/* Symbols belonging to the configuration API introduced in PEP-741, and
+   required in gdb_PyInitializer.  */
+
+typedef struct PyInitConfig PyInitConfig;
+
+PyAPI_FUNC(PyInitConfig*) PyInitConfig_Create (void);
+PyAPI_FUNC(void) PyInitConfig_Free (PyInitConfig *config);
+
+PyAPI_FUNC(int) PyInitConfig_SetInt (PyInitConfig *config,
+				     const char *name,
+				     int64_t value);
+PyAPI_FUNC(int) PyInitConfig_SetStr (PyInitConfig *config,
+				     const char *name,
+				     const char *value);
+
+PyAPI_FUNC(int) PyInitConfig_GetError (PyInitConfig* config,
+				       const char **err_msg);
+PyAPI_FUNC(int) PyInitConfig_GetExitCode (PyInitConfig* config,
+					  int *exitcode);
+
+PyAPI_FUNC(int) Py_InitializeFromInitConfig (PyInitConfig *config);
+
+/* Handler for GDB's readline support.  */
+
+PyAPI_DATA(char) *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, const char *);
+
+/* Utils from Python's high level layer API.  */
+
+PyAPI_FUNC(int) PyRun_InteractiveLoop (FILE *f, const char *p);
+
+}
+#endif /* Py_LIMITED_API */
+
+#endif /* GDB_PYTHON_LIMITED_API_MISSING_H */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.