From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id wna7FrXT0mjSfw8AWB0awg (envelope-from ) for ; Tue, 23 Sep 2025 13:07:01 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=aSiAo1H/; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 4BFB31E0BC; Tue, 23 Sep 2025 13:07:01 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,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 server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (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 A048B1E0B8 for ; Tue, 23 Sep 2025 13:07:00 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1E8F3385AC2E for ; Tue, 23 Sep 2025 17:07:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1E8F3385AC2E Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=aSiAo1H/ Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 2B8EB3858031 for ; Tue, 23 Sep 2025 17:05:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2B8EB3858031 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2B8EB3858031 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1758647131; cv=none; b=MlLVg3R1Aot6pIsz5VlzwPi/cZViKTFyMtFu8U+9xe8AVl48ZwuCyqVBpZ8CF1Ed5+qEpFw7KpGlkSALX0wZhAgaVy996Pc6UnQlf1hdmYbmsNdV5rSJYvAumVqXdBDFQd7o4yQkefmBun3LIiES++HsErvThGdnGV0n9lkYGsI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1758647131; c=relaxed/simple; bh=bPDbq2mnMWa8goHUR9NN/UoIwsS/sl3JxIHFF+MP0IQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=hXYsr2pXCybNEj6ddn9/P7Q4CwuxraOa0NOjL2vMAGlFlHWR5E59kAvJtri/vruZZNDMvjDgisxk93fqnSS+FnQWN2RB7VxoBRzgj8AWMrsbzGVLoFDqGEkRS5JlGNE7AOM0j/cM1Lv1dvOiuEluvR9YtUj7v2MDClZotc48eWI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758647124; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=FOI4kuYWlOfIf8DBPTh6oaahD+WmS7WH8AI9I6nT7uY=; b=aSiAo1H/JhEEA2N7VCcZezJKfrvZ3VuSh7GMOMaM4IFy0MN1EfC3mO/Qi7T0OytJyaShv+ jQVRfICxAvHOam6atn1bFCO8N3rrTr0xvM1srDWbi9zpiqIUzuWjQvqf2bu+CrpQxkfw9S gMjshuDGNygxBL0HfRtGlRWB31BQ1aU= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-458-XT7Co1PtNTSy1WHrEGduWA-1; Tue, 23 Sep 2025 13:05:17 -0400 X-MC-Unique: XT7Co1PtNTSy1WHrEGduWA-1 X-Mimecast-MFC-AGG-ID: XT7Co1PtNTSy1WHrEGduWA_1758647116 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-3ef9218daf5so1643295f8f.1 for ; Tue, 23 Sep 2025 10:05:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758647116; x=1759251916; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FOI4kuYWlOfIf8DBPTh6oaahD+WmS7WH8AI9I6nT7uY=; b=o5I8wg1iLig5QneMk6r9qVdT47Cn+ihqszgbJw+mJYIvB2X2+q3/bqenLafMhx5jGL WKKutVocHR6sXEhz2rrspaIk1jZrE6uo3Zy4NWiYe1cQZhTl5xdoiwjHOtpdcDd/L6rB 4DDrtqkDSHFp5/K651kbMHVV2Y9AHqOr+05rwahxWXIHXO8gGtTsCGWJr+Qxx6W/QB6w 39diGWKSeclDQR3RNcY5c2a/uQh/oD1Bvmthzn6XXeJzy6fB1Ipud9tRyS05WGhgYYRX DlszAnyhvxFAg/smosstk/UR+P+vulh9QAcYQuD8U41svkDEIrHFdTFZ4HRWV3h0/TzL FFAw== X-Gm-Message-State: AOJu0YykhjbSrCOE06w+YnkmFKPGhhaFk7n3IO//UEO5zxlK2oXDaYJD CFA3AnTZXXeyskI2t0mxAWQKDeXMpm4kQjhpM+2v2FYW/je3VJsjN5NMMuqNqxx9kF7nZl7GAX8 vwiyvyyz/chOz2KTQvURlglh3CrT9qfKsoIIjJ8rDuRoyethyN4+tcVZ+/siOWnc= X-Gm-Gg: ASbGncsT2vLn7yAczlNXVaJOF5yFpOVP7nn1dBUBv9+QUP8fXCOedPWxxzW00yBMa0F R/AeJs8ScpyroNo7Sadtx7/LFEWV7YDraS3Yz0TaNsjcCQGmo4WiQkxkNz6fTCBYAB8W723GCeC 6+/kdl+Rt/c8iTH+A6CD0pRrxWplyBsOrtG8AurhuEEM/128IskesiduaeTeocYkd4YWKogNnAW PgmmrtDqjV22cTUqUPIIOyI6WQzsUdaruXN230TR4TBSkgEaBG46c4RcRs+KTi2tMxlcOWINNjV euL4upxoj2K8lcNEjwFNxgesyq7I5nbsI30= X-Received: by 2002:a05:6000:4024:b0:3eb:df84:62e with SMTP id ffacd0b85a97d-405c551b032mr2504972f8f.3.1758647115748; Tue, 23 Sep 2025 10:05:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGtXfyrV2mnc3GDuyE+IBOzkWBnra33O3UhiaH7pODz1Kc1AlJGiFzz0vW4VOJpsxLy1GGwfA== X-Received: by 2002:a05:6000:4024:b0:3eb:df84:62e with SMTP id ffacd0b85a97d-405c551b032mr2504953f8f.3.1758647115344; Tue, 23 Sep 2025 10:05:15 -0700 (PDT) Received: from localhost ([31.111.84.207]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3ee0fbc7188sm25703991f8f.37.2025.09.23.10.05.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Sep 2025 10:05:14 -0700 (PDT) From: Andrew Burgess To: Tom Tromey Cc: gdb-patches@sourceware.org, Eli Zaretskii Subject: Re: [PATCHv8 3/3] gdb/python: extend gdb.write to support styled output In-Reply-To: <87ikhil4ji.fsf@tromey.com> References: <31de42bf45326efa2c5a58bdb96c831923971ab1.1756294307.git.aburgess@redhat.com> <87ikhil4ji.fsf@tromey.com> Date: Tue, 23 Sep 2025 18:05:14 +0100 Message-ID: <87ms6lf6b9.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: R7bvPPTqxqcMwb3Top_6_X4Hhqoc8LFEgAUnb6UCmTE_1758647116 X-Mimecast-Originator: redhat.com Content-Type: text/plain 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 Tom Tromey writes: >>>>>> "Andrew" == Andrew Burgess writes: > > Andrew> Using gdb.write with a style object more closely matches how GDB > Andrew> handles styling internally, and has the benefit that the user doesn't > Andrew> need to remember to restore the default style when they are done. > > I'm mildly meh on this one since I tend to think users should use print > and that gdb.write is an implementation detail. But since it exists... > > Andrew> + return PyObject_TypeCheck (obj, &style_object_type); > > Andrew> +/* See python-internal.h. */ > Andrew> + > Andrew> +std::optional > Andrew> +gdbpy_style_object_to_ui_file_style (PyObject *obj) > Andrew> +{ > Andrew> + gdb_assert (obj != nullptr); > Andrew> + if (!gdbpy_is_style (obj)) > Andrew> + { > Andrew> + PyErr_Format > Andrew> + (PyExc_TypeError, _("argument must be a gdb.Style object, not %s."), > Andrew> + Py_TYPE (obj)->tp_name); > Andrew> + return {}; > Andrew> + } > Andrew> + > Andrew> + style_object *style_obj = (style_object *) obj; > Andrew> + return stylepy_to_style (style_obj); > > > Andrew> + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|iO", keywords, &arg, > Andrew> + &stream_type, &style_obj)) > Andrew> + return nullptr; > Andrew> + > Andrew> + if (style_obj != Py_None && !gdbpy_is_style (style_obj)) > Andrew> + { > Andrew> + PyErr_Format > Andrew> + (PyExc_TypeError, > Andrew> + _("'style' argument must be gdb.Style or None, not %s."), > Andrew> + Py_TYPE (style_obj)->tp_name); > Andrew> + return nullptr; > > FWIW if the type object is available there's a way to get Python to do > this bit when parsing the arguments. I think you're referring too 'O!' maybe? The reason that doesn't work is that the argument can be a gdb.Style _or_ None. It feels like this must be a common pattern, so I would have expected to find a modifier that takes a named type or accepts None, but I couldn't find anything. Is there another trick that I'm not aware of? Would love to know because I'm pretty sure I've had to do this elsewhere in GDB when an argument can also be None. > This explicit check is fine too, but it seems to me that this is > redundant with the check that gdbpy_style_object_to_ui_file_style does > and so this could just delegate to that. Given gdbpy_style_object_to_ui_file_style is only used in this one place, what I did in the end was to drop the gdbpy_is_style check from gdbpy_style_object_to_ui_file_style, replacing it with an assert. Then I've retained the check in gdbpy_write. The reason is that the error message in gdbpy_write is so much more descriptive, it names the argument, and says that the argument can be a gdb.Style or None. The error from gdbpy_style_object_to_ui_file_style was much more generic. I prefer accurate error messages wherever possible. So, after this change, there's only one gdbpy_is_style check (not counting the assert), and only one place where the error message is set if the argument is the wrong type. I posted a v9 series with these changes in, let me know what you think. Thanks, Andrew