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 v2 1/2] [gdb/python] Introduce py_none/py_true/py_false functions
Date: Wed, 13 May 2026 18:30:04 +0200	[thread overview]
Message-ID: <20260513163005.3997485-2-tdevries@suse.de> (raw)
In-Reply-To: <20260513163005.3997485-1-tdevries@suse.de>

I came across this code:
...
   if (c)
     return v;
   else
     Py_RETURN_NONE;
...
and realized I couldn't easily rewrite it into "return c ? v : ...".

Probably something like this would work:
...
  return c ? v : ([] { Py_RETURN_NONE; } ());
...
but it made me wonder if we can rid of Py_RETURN_NONE.

The only point of it seems to be to increase the reference count on the
Py_None object for older python versions.

Add a refcount-safe wrapper py_none (returning a gdbpy_ref), with the aim of
replacing all uses of Py_RETURN_NONE with "return py_none ().release ()".

Likewise for Py_RETURN_TRUE/Py_RETURN_FALSE.

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

diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 37bc37691fe..c3c1f0e6a6f 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -1340,4 +1340,27 @@ class gdbpy_memoizing_registry_storage
 extern int eval_python_command (const char *command, int start_symbol,
 				const char *filename = nullptr);
 
+/* The following three functions are refcount-safe wrappers around
+   Py_RETURN_{NONE,TRUE,FALSE}.  Starting with python 3.12, None, True and
+   False are immortal, and increasing and decreasing refcount for such object
+   is a no-op, but while supporting older versions we ignore that aspect
+   here.  */
+
+static inline gdbpy_ref<>
+py_none ()
+{
+  return gdbpy_ref<> ([] { Py_RETURN_NONE; } ());
+}
+
+static inline gdbpy_ref<>
+py_true ()
+{
+  return gdbpy_ref<> ([] { Py_RETURN_TRUE; } ());
+}
+
+static inline gdbpy_ref<>
+py_false ()
+{
+  return gdbpy_ref<> ([] { Py_RETURN_FALSE; } ());
+}
 #endif /* GDB_PYTHON_PYTHON_INTERNAL_H */
-- 
2.51.0


  reply	other threads:[~2026-05-13 16:35 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-13 16:30 [PATCH v2 0/2] [gdb/python] Remove Py_RETURN_{NONE,TRUE,FALSE} Tom de Vries
2026-05-13 16:30 ` Tom de Vries [this message]
2026-05-14 17:46   ` [PATCH v2 1/2] [gdb/python] Introduce py_none/py_true/py_false functions Tom Tromey
2026-05-15 10:53     ` Tom de Vries
2026-05-13 16:30 ` [PATCH v2 2/2] [gdb/python] Remove Py_RETURN_{NONE,TRUE,FALSE} Tom de Vries
2026-05-14 17:47   ` Tom Tromey

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=20260513163005.3997485-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