Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Matthieu Longo <matthieu.longo@arm.com>
To: <gdb-patches@sourceware.org>
Cc: Tom Tromey <tom@tromey.com>, Matthieu Longo <matthieu.longo@arm.com>
Subject: [PATCH v2 1/6] Python limited API: migrate Py_CompileStringExFlags and PyRun_SimpleString
Date: Tue, 27 Jan 2026 17:02:10 +0000	[thread overview]
Message-ID: <20260127170215.1803582-2-matthieu.longo@arm.com> (raw)
In-Reply-To: <20260127170215.1803582-1-matthieu.longo@arm.com>

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
---
 gdb/python/py-gdb-readline.c |  5 +++--
 gdb/python/python-internal.h |  3 +++
 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 @@ class gdbpy_memoizing_registry_storage
   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)
-- 
2.52.0


  reply	other threads:[~2026-01-27 17:04 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-27 17:02 [PATCH v2 0/6] gdb: minor fixes for Python limited C API support Matthieu Longo
2026-01-27 17:02 ` Matthieu Longo [this message]
2026-01-27 17:54   ` [PATCH v2 1/6] Python limited API: migrate Py_CompileStringExFlags and PyRun_SimpleString Tom Tromey
2026-01-27 17:02 ` [PATCH v2 2/6] Python limited API: migrate PyImport_ExtendInittab Matthieu Longo
2026-01-27 17:54   ` Tom Tromey
2026-01-27 17:02 ` [PATCH v2 3/6] gdbpy_registry: cast C extension type object to PyObject * before Py_XINCREF Matthieu Longo
2026-01-27 18:01   ` Tom Tromey
2026-01-27 18:29   ` Tom Tromey
2026-01-28 11:58     ` Matthieu Longo
2026-01-27 17:02 ` [PATCH v2 4/6] gdb: new setters and getters for __dict__, and attributes Matthieu Longo
2026-01-27 19:06   ` Tom Tromey
2026-01-28 11:57     ` Matthieu Longo
2026-01-28 17:43     ` Matthieu Longo
2026-01-28 17:51       ` Tom Tromey
2026-01-27 17:02 ` [PATCH v2 5/6] gdb: cast all Python extension objects passed to gdbpy_ref_policy to PyObject* Matthieu Longo
2026-01-27 18:28   ` Tom Tromey
2026-01-28 11:58     ` Matthieu Longo
2026-01-27 17:02 ` [PATCH v2 6/6] gdb: make remaining Python extension objects inherit from PyObject Matthieu Longo
2026-01-27 18:29   ` Tom Tromey
2026-01-28 11:58     ` Matthieu Longo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260127170215.1803582-2-matthieu.longo@arm.com \
    --to=matthieu.longo@arm.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox