Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] Use the "O!" format more in the Python code
@ 2026-02-27 16:48 Tom Tromey
  2026-02-27 18:23 ` Tom de Vries
  0 siblings, 1 reply; 8+ messages in thread
From: Tom Tromey @ 2026-02-27 16:48 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

I noticed a few spots in the Python code that were decoding method
arguments and then immediately type-checking an argument.  This can be
done more concisely using the "O!" format.
---
 gdb/python/py-disasm.c        | 11 ++---------
 gdb/python/py-prettyprint.c   | 11 ++++-------
 gdb/python/py-record-btrace.c |  4 +---
 gdb/python/py-symbol.c        |  8 +-------
 4 files changed, 8 insertions(+), 26 deletions(-)

diff --git a/gdb/python/py-disasm.c b/gdb/python/py-disasm.c
index 748ffd5e6a3..7635e45db56 100644
--- a/gdb/python/py-disasm.c
+++ b/gdb/python/py-disasm.c
@@ -620,17 +620,10 @@ disasmpy_set_enabled (PyObject *self, PyObject *args, PyObject *kw)
 {
   PyObject *newstate;
   static const char *keywords[] = { "state", nullptr };
-  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O", keywords,
-					&newstate))
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O!", keywords,
+					&PyBool_Type, &newstate))
     return nullptr;
 
-  if (!PyBool_Check (newstate))
-    {
-      PyErr_SetString (PyExc_TypeError,
-		       _("The value passed to `_set_enabled' must be a boolean."));
-      return nullptr;
-    }
-
   python_print_insn_enabled = newstate == Py_True;
   Py_RETURN_NONE;
 }
diff --git a/gdb/python/py-prettyprint.c b/gdb/python/py-prettyprint.c
index 0aa11d29094..eb7ac2c048f 100644
--- a/gdb/python/py-prettyprint.c
+++ b/gdb/python/py-prettyprint.c
@@ -679,15 +679,12 @@ gdbpy_default_visualizer (PyObject *self, PyObject *args)
   PyObject *val_obj;
   struct value *value;
 
-  if (! PyArg_ParseTuple (args, "O", &val_obj))
+  if (! PyArg_ParseTuple (args, "O!", &value_object_type, &val_obj))
     return NULL;
   value = value_object_to_value (val_obj);
-  if (! value)
-    {
-      PyErr_SetString (PyExc_TypeError,
-		       _("Argument must be a gdb.Value."));
-      return NULL;
-    }
+  /* This was ensured by the type-checking during argument
+     parsing.  */
+  gdb_assert (value != nullptr);
 
   return find_pretty_printer (val_obj).release ();
 }
diff --git a/gdb/python/py-record-btrace.c b/gdb/python/py-record-btrace.c
index 1974dd3e939..6026de91e67 100644
--- a/gdb/python/py-record-btrace.c
+++ b/gdb/python/py-record-btrace.c
@@ -922,11 +922,9 @@ recpy_bt_goto (PyObject *self, PyObject *args)
   if (tinfo == NULL || btrace_is_empty (tinfo))
 	return PyErr_Format (gdbpy_gdb_error, _("Empty branch trace."));
 
-  if (!PyArg_ParseTuple (args, "O", &parse_obj))
+  if (!PyArg_ParseTuple (args, "O!", &recpy_insn_type, &parse_obj))
     return NULL;
 
-  if (Py_TYPE (parse_obj) != &recpy_insn_type)
-    return PyErr_Format (PyExc_TypeError, _("Argument must be instruction."));
   obj = (const recpy_element_object *) parse_obj;
 
   try
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index e8ff2dfe28a..15fcbeea9be 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -266,15 +266,9 @@ sympy_value (PyObject *self, PyObject *args)
   frame_info_ptr frame_info = NULL;
   PyObject *frame_obj = NULL;
 
-  if (!PyArg_ParseTuple (args, "|O", &frame_obj))
+  if (!PyArg_ParseTuple (args, "|O!", &frame_object_type, &frame_obj))
     return NULL;
 
-  if (frame_obj != NULL && !PyObject_TypeCheck (frame_obj, &frame_object_type))
-    {
-      PyErr_SetString (PyExc_TypeError, "argument is not a frame");
-      return NULL;
-    }
-
   SYMPY_REQUIRE_VALID (self, symbol);
   if (symbol->loc_class () == LOC_TYPEDEF)
     {
-- 
2.49.0


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

end of thread, other threads:[~2026-02-27 20:26 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-02-27 16:48 [PATCH] Use the "O!" format more in the Python code Tom Tromey
2026-02-27 18:23 ` Tom de Vries
2026-02-27 18:31   ` Tom Tromey
2026-02-27 19:01     ` Tom Tromey
2026-02-27 19:06       ` Tom de Vries
2026-02-27 19:35     ` Paul Koning
2026-02-27 19:58       ` Tom Tromey
2026-02-27 20:24         ` Paul Koning

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