From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH] Use the "O!" format more in the Python code
Date: Fri, 27 Feb 2026 09:48:43 -0700 [thread overview]
Message-ID: <20260227164843.2275912-1-tom@tromey.com> (raw)
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
next reply other threads:[~2026-02-27 16:49 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-27 16:48 Tom Tromey [this message]
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
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=20260227164843.2275912-1-tom@tromey.com \
--to=tom@tromey.com \
--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