Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH v3 1/5] [gdb/python] Introduce py_{none, true, false, notimplemented} functions
@ 2026-05-15 13:58 Tom de Vries
  2026-05-15 13:58 ` [PATCH v3 2/5] [gdb/python] Remove Py_RETURN_{NONE, TRUE, FALSE, NOTIMPLEMENTED} Tom de Vries
                   ` (4 more replies)
  0 siblings, 5 replies; 13+ messages in thread
From: Tom de Vries @ 2026-05-15 13:58 UTC (permalink / raw)
  To: gdb-patches

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/Py_RETURN_NOTIMPLEMENTED.

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

diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 3147156d5be..7c58309e4d4 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -1343,4 +1343,48 @@ class gdbpy_memoizing_registry_storage
 extern int eval_python_command (const char *command, int start_symbol,
 				const char *filename = nullptr);
 
+/* The following four functions are refcount-safe wrappers around
+   Py_RETURN_{NONE,TRUE,FALSE,NOTIMPLEMENTED}.
+
+   Starting with python 3.12, None, True, False and Py_NotImplemented are
+   immortal, and increasing and decreasing refcount for such objects is a
+   no-op, and consequently for the limited C API 3.12 and newer,
+   Py_RETURN_NONE is simply "return Py_None".
+
+   So for the limited C API 3.12 and newer, we could just return a
+   "PyObject *" instead.
+
+   For the limited C API 3.12 and newer, while returning gdbpy_ref<> there's
+   an imbalance (we do a Py_DECREF in gdbpy_ref_policy::decref, without
+   corresponding Py_INCREF in Py_RETURN_NONE), but this is not harmful because
+   Py_DECREF is no-op for immortal objects.  */
+
+static inline gdbpy_ref<>
+py_none ()
+{
+  auto f = [] { Py_RETURN_NONE; };
+  return gdbpy_ref<> (f ());
+}
+
+static inline gdbpy_ref<>
+py_true ()
+{
+  auto f = [] { Py_RETURN_TRUE; };
+  return gdbpy_ref<> (f ());
+}
+
+static inline gdbpy_ref<>
+py_false ()
+{
+  auto f = [] { Py_RETURN_FALSE; };
+  return gdbpy_ref<> (f ());
+}
+
+static inline gdbpy_ref<>
+py_notimplemented ()
+{
+  auto f = [] { Py_RETURN_NOTIMPLEMENTED; };
+  return gdbpy_ref<> (f ());
+}
+
 #endif /* GDB_PYTHON_PYTHON_INTERNAL_H */

base-commit: 79fcceafcbfe9aa02441445eb9444718b17330df
-- 
2.51.0


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2026-05-15 19:09 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-15 13:58 [PATCH v3 1/5] [gdb/python] Introduce py_{none, true, false, notimplemented} functions Tom de Vries
2026-05-15 13:58 ` [PATCH v3 2/5] [gdb/python] Remove Py_RETURN_{NONE, TRUE, FALSE, NOTIMPLEMENTED} Tom de Vries
2026-05-15 13:58 ` [PATCH v3 3/5] [gdb/python] Undefine " Tom de Vries
2026-05-15 14:05   ` Tom de Vries
2026-05-15 17:40   ` Tom Tromey
2026-05-15 13:58 ` [PATCH v3 4/5] [gdb/python] Use py_{none,false} more often Tom de Vries
2026-05-15 17:39   ` Tom Tromey
2026-05-15 13:58 ` [PATCH v3 5/5] [gdb/python] Use py_{none,notimplemented} " Tom de Vries
2026-05-15 17:38   ` Tom Tromey
2026-05-15 17:36 ` [PATCH v3 1/5] [gdb/python] Introduce py_{none, true, false, notimplemented} functions Tom Tromey
2026-05-15 17:44   ` Tom de Vries
2026-05-15 18:36     ` Tom Tromey
2026-05-15 19:08       ` Tom de Vries

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox