From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id /xWWBddkB2rlzD0AWB0awg (envelope-from ) for ; Fri, 15 May 2026 14:24:23 -0400 Authentication-Results: simark.ca; dkim=fail reason="signature verification failed" (768-bit key; unprotected) header.d=tromey.com header.i=@tromey.com header.a=rsa-sha256 header.s=default header.b=tBpbtkKT; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id ED91B1E067; Fri, 15 May 2026 14:24:22 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_INVALID,DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 881CB1E067 for ; Fri, 15 May 2026 14:24:21 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ED0154BB3BF1 for ; Fri, 15 May 2026 18:24:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ED0154BB3BF1 Authentication-Results: sourceware.org; dkim=fail reason="signature verification failed" (768-bit key, unprotected) header.d=tromey.com header.i=@tromey.com header.a=rsa-sha256 header.s=default header.b=tBpbtkKT Received: from omta38.uswest2.a.cloudfilter.net (omta38.uswest2.a.cloudfilter.net [35.89.44.37]) by sourceware.org (Postfix) with ESMTPS id AFC4D409FC90 for ; Fri, 15 May 2026 18:23:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AFC4D409FC90 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AFC4D409FC90 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=35.89.44.37 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778869406; cv=none; b=QfT4v8w8DRHRRBM7turCKsgafYdv/fu//nndJDOkbhHhhs0HvGFvzrnpGuZPXvM1ouyPg1rpw4BnEfNkNbw/Lps7mfKeMhFVz9n10Z7/0tchHRqVXI0XffQfIGiO7ChvzSVr9PrIlLroptJbIhDJBy/X++9HM2IYFzONCCFLW6s= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778869406; c=relaxed/simple; bh=db+kJ5nytsK6r9bLpM9N3aLEULaERLcGxNGNjrS3guI=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=VcasVdue60pVDCskip5+wvu3oYbdOQjIhpHi6fDQEBguMaxzv/nL+gDJRlgnwy+Msk+wQboIuyyUKhjtbFfZCgezYOBXIyDriYXWZOSxqC3HAW9G+gRpHY4cIkDoAjNZka7/FxaSPDnavfyktbQmcQxnPXXwzP4qWrOOeiQXrEA= ARC-Authentication-Results: i=1; sourceware.org; dkim=policy (768-bit key, unprotected) header.d=tromey.com header.i=@tromey.com header.a=rsa-sha256 header.s=default header.b=tBpbtkKT reason="signing key too small" DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AFC4D409FC90 Received: from eig-obgw-6006b.ext.cloudfilter.net ([10.0.30.211]) by cmsmtp with ESMTPS id NwzTwArk5jw8YNxBzwZxhz; Fri, 15 May 2026 18:23:23 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id NxBywM8eCSUGqNxBzwaFJ6; Fri, 15 May 2026 18:23:23 +0000 X-Authority-Analysis: v=2.4 cv=CoK/cm4D c=1 sm=1 tr=0 ts=6a07649b a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=IkcTkHD0fZMA:10 a=NGcC8JguVDcA:10 a=ItBw4LHWJt0A:10 a=idmLz7Bo26IfJ7ekqPEA:9 a=3ZKOabzyN94A:10 a=QEXdDO2ut3YA:10 a=DCx65vhANUyCzuf5D8fC:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Cc:To:In-Reply-To:References:Message-Id: Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From:Sender: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=kKegeGklEtDshKli21X4hOkncNMZLvCrF6ZDmusy41A=; b=tBpbtkKTFNnDRWa9MC4+Frce9K t15O3SKLJTBGLSakC2xfGA3Ogg6oGVGElhe3MMI0KuTd9D0R0Bc9fowbQV6Su4mRH0XDOgqGyWbhI qiTfo/4ytoOsLBJd5KRsYNKi1; Received: from 75-166-225-82.hlrn.qwest.net ([75.166.225.82]:46584 helo=[192.168.0.17]) by box5379.bluehost.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.2) (envelope-from ) id 1wNxBy-00000002ZaG-2Io9; Fri, 15 May 2026 12:23:22 -0600 From: Tom Tromey Date: Fri, 15 May 2026 12:23:22 -0600 Subject: [PATCH 4/4] Convert py-tui.c to the "python safety" approach MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260515-python-safety-initial-v1-4-8f155338df57@tromey.com> References: <20260515-python-safety-initial-v1-0-8f155338df57@tromey.com> In-Reply-To: <20260515-python-safety-initial-v1-0-8f155338df57@tromey.com> To: gdb-patches@sourceware.org Cc: Tom Tromey X-Mailer: b4 0.14.3 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 75.166.225.82 X-Source-L: No X-Exim-ID: 1wNxBy-00000002ZaG-2Io9 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 75-166-225-82.hlrn.qwest.net ([192.168.0.17]) [75.166.225.82]:46584 X-Source-Auth: tom+tromey.com X-Email-Count: 5 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfG3cjq53kE6A1I0U7gWkzMppU4yba37Tkzp7bvZxeycbhcV8oln7SiEfjXXgxNBHSXh/8DXL9KvOKyvg9coMdSeyajAnEm3QnXzhZLwZd3X8ZWCKpW1w +iWfma6itgVNaRUWTZl6Kn7YZSmSlsZqCnDUO9F6DLSeKTY2T6YZy0QPvsf3iL3Y+GJDomvuFe2IjvPoSeXWTAptXkcRvXMH0qw= X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org This patch mostly converts py-tui.c to use the new Python safety code. In particular: * All methods of gdb.TuiWindow are now implemented as straightforward methods of gdbpy_tui_window. * gdb.register_tui_window is converted and simply returns 'void'. I converted this particular file because it was relatively straightforward, while also demonstrating most of the features of the new approach. For example, explicit result checks aren't needed, try/catch can be removed, and the methods are now written in a natural style. Note that more conversion remains to be done here: * gdbpy_tui_enabled hasn't been converted and still does explicit checks. * There's one explicit check in gdbpy_tui_window::set_title. Fully implementing the safety approach means that some low-level things should eventually be converted to throw; but some work has to be deferred until a lot of the work is complete. --- gdb/python/py-tui.c | 208 ++++++++++++++++++------------------------- gdb/python/python-internal.h | 5 +- gdb/python/python.c | 5 +- 3 files changed, 92 insertions(+), 126 deletions(-) diff --git a/gdb/python/py-tui.c b/gdb/python/py-tui.c index be19193770f..0755d7b92e3 100644 --- a/gdb/python/py-tui.c +++ b/gdb/python/py-tui.c @@ -50,7 +50,34 @@ struct gdbpy_tui_window: public PyObject tui_py_window *window; /* Return true if this object is valid. */ - bool is_valid () const; + bool is_valid (); + + /* Require that this object be valid. Throws exception if not. */ + void require_valid () + { + if (!is_valid ()) + gdbpy_err_format (PyExc_RuntimeError, _("TUI window is invalid.")); + } + + /* Erase the TUI window. */ + void erase (); + + /* Python function that writes some text to a TUI window. */ + void write (gdbpy_borrowed_ref args, gdbpy_opt_borrowed_ref kw); + + /* Return the width of the TUI window. */ + int width (); + + /* Return the height of the TUI window. */ + int height (); + + /* Return the title of the TUI window. */ + const std::string &title (); + + /* Set the title of the TUI window. */ + void set_title (gdbpy_opt_borrowed_ref new_title); + + static PyTypeObject *corresponding_object_type; }; extern PyTypeObject gdbpy_tui_window_object_type; @@ -150,7 +177,7 @@ class tui_py_window : public tui_win_info /* See gdbpy_tui_window declaration above. */ bool -gdbpy_tui_window::is_valid () const +gdbpy_tui_window::is_valid () { return window != nullptr && tui_active; } @@ -406,173 +433,109 @@ gdbpy_tui_window_maker::operator() (const char *win_name) /* Implement "gdb.register_window_type". */ -PyObject * -gdbpy_register_tui_window (PyObject *self, PyObject *args, PyObject *kw) +void +gdbpy_register_tui_window (gdbpy_borrowed_ref args, + gdbpy_opt_borrowed_ref kw) { static const char *keywords[] = { "name", "constructor", nullptr }; - const char *name; PyObject *cons_obj; + gdbpy_arg_parse_tuple_and_keywords (args, kw, "sO", keywords, + &name, &cons_obj); - if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "sO", keywords, - &name, &cons_obj)) - return nullptr; - - try - { - gdbpy_tui_window_maker constr (gdbpy_ref<>::new_reference (cons_obj)); - tui_register_window (name, constr); - } - catch (const gdb_exception &except) - { - return gdbpy_handle_gdb_exception (nullptr, except); - } - - Py_RETURN_NONE; + gdbpy_tui_window_maker constr (gdbpy_ref<>::new_reference (cons_obj)); + tui_register_window (name, constr); } -/* Require that "Window" be a valid window. */ - -#define REQUIRE_WINDOW(Window) \ - do { \ - if (!(Window)->is_valid ()) \ - return PyErr_Format (PyExc_RuntimeError, \ - _("TUI window is invalid.")); \ - } while (0) - -/* Require that "Window" be a valid window. */ - -#define REQUIRE_WINDOW_FOR_SETTER(Window) \ - do { \ - if (!(Window)->is_valid ()) \ - { \ - PyErr_Format (PyExc_RuntimeError, \ - _("TUI window is invalid.")); \ - return -1; \ - } \ - } while (0) - -/* Python function which checks the validity of a TUI window - object. */ -static PyObject * -gdbpy_tui_is_valid (PyObject *self, PyObject *args) -{ - gdbpy_tui_window *win = (gdbpy_tui_window *) self; - - if (win->is_valid ()) - Py_RETURN_TRUE; - Py_RETURN_FALSE; -} - /* Python function that erases the TUI window. */ -static PyObject * -gdbpy_tui_erase (PyObject *self, PyObject *args) +void +gdbpy_tui_window::erase () { - gdbpy_tui_window *win = (gdbpy_tui_window *) self; - - REQUIRE_WINDOW (win); - - win->window->erase (); - - Py_RETURN_NONE; + require_valid (); + window->erase (); } /* Python function that writes some text to a TUI window. */ -static PyObject * -gdbpy_tui_write (PyObject *self, PyObject *args, PyObject *kw) +void +gdbpy_tui_window::write (gdbpy_borrowed_ref args, gdbpy_opt_borrowed_ref kw) { + require_valid (); + static const char *keywords[] = { "string", "full_window", nullptr }; - gdbpy_tui_window *win = (gdbpy_tui_window *) self; const char *text; int full_window = 0; - if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, - &text, &full_window)) - return nullptr; - - REQUIRE_WINDOW (win); + gdbpy_arg_parse_tuple_and_keywords (args, kw, "s|i", keywords, + &text, &full_window); - win->window->output (text, full_window); - - Py_RETURN_NONE; + window->output (text, full_window); } -/* Return the width of the TUI window. */ -static PyObject * -gdbpy_tui_width (PyObject *self, void *closure) +int +gdbpy_tui_window::width () { - gdbpy_tui_window *win = (gdbpy_tui_window *) self; - REQUIRE_WINDOW (win); - gdbpy_ref<> result - = gdb_py_object_from_longest (win->window->viewport_width ()); - return result.release (); + require_valid (); + return window->viewport_width (); } -/* Return the height of the TUI window. */ -static PyObject * -gdbpy_tui_height (PyObject *self, void *closure) +int +gdbpy_tui_window::height () { - gdbpy_tui_window *win = (gdbpy_tui_window *) self; - REQUIRE_WINDOW (win); - gdbpy_ref<> result - = gdb_py_object_from_longest (win->window->viewport_height ()); - return result.release (); + require_valid (); + return window->viewport_height (); } -/* Return the title of the TUI window. */ -static PyObject * -gdbpy_tui_title (PyObject *self, void *closure) +const std::string & +gdbpy_tui_window::title () { - gdbpy_tui_window *win = (gdbpy_tui_window *) self; - REQUIRE_WINDOW (win); - return host_string_to_python_string (win->window->title ().c_str ()).release (); + require_valid (); + return window->title (); } /* Set the title of the TUI window. */ -static int -gdbpy_tui_set_title (PyObject *self, PyObject *newvalue, void *closure) +void +gdbpy_tui_window::set_title (gdbpy_opt_borrowed_ref new_title) { - gdbpy_tui_window *win = (gdbpy_tui_window *) self; + require_valid (); - REQUIRE_WINDOW_FOR_SETTER (win); - - if (newvalue == nullptr) - { - PyErr_Format (PyExc_TypeError, _("Cannot delete \"title\" attribute.")); - return -1; - } + if (new_title == nullptr) + gdbpy_err_format (PyExc_TypeError, + _("Cannot delete \"title\" attribute.")); + /* FIXME: safety */ gdb::unique_xmalloc_ptr value - = python_string_to_host_string (newvalue); + = python_string_to_host_string (new_title); if (value == nullptr) - return -1; + throw gdb_python_exception (); - win->window->set_title (value.get ()); - return 0; + window->set_title (value.get ()); } static gdb_PyGetSetDef tui_object_getset[] = { - { "width", gdbpy_tui_width, NULL, "Width of the window.", NULL }, - { "height", gdbpy_tui_height, NULL, "Height of the window.", NULL }, - { "title", gdbpy_tui_title, gdbpy_tui_set_title, "Title of the window.", - NULL }, - { NULL } /* Sentinel */ + { "width", wrap_getter, nullptr, + "Width of the window.", nullptr }, + { "height", wrap_getter, nullptr, + "Height of the window.", nullptr }, + { "title", wrap_getter, + wrap_setter, + "Title of the window.", nullptr }, + { nullptr } /* Sentinel */ }; static PyMethodDef tui_object_methods[] = { - { "is_valid", gdbpy_tui_is_valid, METH_NOARGS, + noargs_method ("is_valid", "is_valid () -> Boolean\n\ -Return true if this TUI window is valid, false if not." }, - { "erase", gdbpy_tui_erase, METH_NOARGS, - "Erase the TUI window." }, - { "write", (PyCFunction) gdbpy_tui_write, METH_VARARGS | METH_KEYWORDS, - "Append a string to the TUI window." }, - { NULL } /* Sentinel. */ +Return true if this TUI window is valid, false if not."), + noargs_method + ("erase", "Erase the TUI window."), + varargs_method ("write", + "Append a string to the TUI window."), + { nullptr } /* Sentinel. */ }; PyTypeObject gdbpy_tui_window_object_type = @@ -616,6 +579,9 @@ PyTypeObject gdbpy_tui_window_object_type = 0, /* tp_alloc */ }; +PyTypeObject *gdbpy_tui_window::corresponding_object_type + = &gdbpy_tui_window_object_type; + /* Called when TUI is enabled or disabled. */ static void diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index ca667681cb7..e656ca73617 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -496,8 +496,9 @@ gdb::unique_xmalloc_ptr gdbpy_parse_command_name (const char *name, struct cmd_list_element ***base_list, struct cmd_list_element **start_list, struct cmd_list_element **prefix_cmd = nullptr); -PyObject *gdbpy_register_tui_window (PyObject *self, PyObject *args, - PyObject *kw); + +void gdbpy_register_tui_window (gdbpy_borrowed_ref args, + gdbpy_opt_borrowed_ref kw); gdbpy_ref<> symtab_and_line_to_sal_object (struct symtab_and_line sal); gdbpy_ref<> symtab_to_symtab_object (struct symtab *symtab); diff --git a/gdb/python/python.c b/gdb/python/python.c index 045626b70fd..689d121ce2d 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -3274,10 +3274,9 @@ or None if not set." }, Set the value of the convenience variable $NAME." }, #ifdef TUI - { "register_window_type", (PyCFunction) gdbpy_register_tui_window, - METH_VARARGS | METH_KEYWORDS, + varargs_function ("register_window_type", "register_window_type (NAME, CONSTRUCTOR) -> None\n\ -Register a TUI window constructor." }, +Register a TUI window constructor."), #endif /* TUI */ { "architecture_names", gdbpy_all_architecture_names, METH_NOARGS, -- 2.49.0