Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH 1/2] [gdb/python] Add init_py_immortals
Date: Tue, 12 May 2026 18:34:37 +0200	[thread overview]
Message-ID: <20260512163438.2446764-2-tdevries@suse.de> (raw)
In-Reply-To: <20260512163438.2446764-1-tdevries@suse.de>

Add and call init_py_immortals, allowing to use "return Py_None" instead of
Py_RETURN_NONE, and likewise for Py_RETURN_{TRUE,FALSE}.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34145
---
 gdb/python/python.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/gdb/python/python.c b/gdb/python/python.c
index 502fe682b2c..d4c3454bbee 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -2587,6 +2587,25 @@ py_initialize ()
 #endif
 }
 
+/* Increase the refcount for Py_None/Py_True/Py_False, if necessary.  This
+   allows us to use a simple "return Py_None" instead of Py_RETURN_NONE
+   everywhere else.  */
+
+static void
+init_py_immortals ()
+{
+  [[maybe_unused]] static PyObject *py_none = nullptr;
+  [[maybe_unused]] static PyObject *py_true = nullptr;
+  [[maybe_unused]] static PyObject *py_false = nullptr;
+
+  gdb_assert (py_none == nullptr);
+
+  /* Increase refcount for Py_None/Py_True/Py_False, if necessary.  */
+  py_none = [] { Py_RETURN_NONE; } ();
+  py_true = [] { Py_RETURN_TRUE; } ();
+  py_false = [] { Py_RETURN_FALSE; } ();
+}
+
 static bool
 do_start_initialization ()
 {
@@ -2606,6 +2625,8 @@ do_start_initialization ()
   PyEval_InitThreads ();
 #endif
 
+  init_py_immortals ();
+
   gdb_module = PyImport_ImportModule ("_gdb");
   if (gdb_module == NULL)
     return false;
-- 
2.51.0


  reply	other threads:[~2026-05-12 16:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-12 16:34 [PATCH 0/2] [gdb/python] Remove Py_RETURN_{NONE,TRUE,FALSE} Tom de Vries
2026-05-12 16:34 ` Tom de Vries [this message]
2026-05-12 16:34 ` [PATCH 2/2] " Tom de Vries
2026-05-13 14:44 ` [PATCH 0/2] " Tom Tromey
2026-05-13 16:42   ` Tom de Vries

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=20260512163438.2446764-2-tdevries@suse.de \
    --to=tdevries@suse.de \
    --cc=gdb-patches@sourceware.org \
    /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