From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id Ke7JBgDS0mjseg8AWB0awg (envelope-from ) for ; Tue, 23 Sep 2025 12:59:44 -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=UfQhy4yV; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 154B81E0BA; Tue, 23 Sep 2025 12:59:44 -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 3D45E1E04C for ; Tue, 23 Sep 2025 12:59:43 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D8031385840F for ; Tue, 23 Sep 2025 16:59:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D8031385840F 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=UfQhy4yV Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 8B3D63858D26 for ; Tue, 23 Sep 2025 16:59:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8B3D63858D26 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 8B3D63858D26 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1758646750; cv=none; b=LU4VjJRdzBIKu0M9x9aq+iy1AQqrJawpg9a/x1n4JEtr7pY77sOfXyCpS2RXxemNhCbeH21rs2pkkMp0vni/cYfr9qPzivyFbNjeO6SLSzucumby+icziGXsmLBJSCp5IQgKVYrAladLxYe6krETyicmOgGX07VzpGaAeIz7r6Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1758646750; c=relaxed/simple; bh=ASTWMTX0RfCB0zfaIXRsDUR6UD9vCIOlNHZjF8cwd50=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=OwplG8t5JxXyWFmeFF2mH4Qest4fVmuiit3coKtYHmhsFaoh4RNhYqgiQUqwH+hekUwdNJV+QrFYBC75ZtWeyF1SbSQR7BLrB61cWHrlEU9OOE7ybRzAJHBe+aZ9K4V3rBKnTqQF7Lg3CC3ytL/y3OQQkjCc/2F5nA7DVBcLmjE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8B3D63858D26 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758646750; 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=krMsrADaAbLFX3LxMgaq9oLGSBr4IxVIr+nAZd0OvIw=; b=UfQhy4yVKul8xiKV2nCqoF0LuvJZVFr1qmGO4hCBeXSDxT3QaB6/soGuV4a9V3YE2h83eU d9aJo7wp/5gCrjdexHyrh4AxO/Okwcrpv3WEbhZ3UZF5ds73XHrR4Zg+aDpPwgst3smGwQ z9BpgN8vlu1p4jMX0o1PI9v1dUctwyI= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-151-zuPBwJK4PHmgDvAjMFNBvw-1; Tue, 23 Sep 2025 12:59:09 -0400 X-MC-Unique: zuPBwJK4PHmgDvAjMFNBvw-1 X-Mimecast-MFC-AGG-ID: zuPBwJK4PHmgDvAjMFNBvw_1758646748 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-3fba0d9eb87so1212026f8f.0 for ; Tue, 23 Sep 2025 09:59:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758646747; x=1759251547; 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=krMsrADaAbLFX3LxMgaq9oLGSBr4IxVIr+nAZd0OvIw=; b=K8idu5pLEBZ/VYXImS+50n4el+adxlKvJYK16+hWjIl6xeRHxmI3VhKJghPQGTSXgF +Tq/16UcJTH1SwIrCSKvXfXVRnDVkhlKWzp8RlGOT6saxwFyOF7X8hpX8ImY+sOKJ6Zg Zm2pe9EsppqtVaB8Ern4LUoi2PJM14UOCeHWeC+9OKor289z19Gr4NDW4iuuQfpF8qaq EJ6fT4Ge+FO/ZRtAVaKGJk+Z/B3ZGVxaDlF/gs0HCoYy0j6ETT94rU6gfC5Q/FTQQXgw TfE+rjQlYoCEs76V0vE6C/pmEOXnl0mZOcIDlgdXSGhGwR+CDEAtHFk5tcsWmGhZtA5S YM7Q== X-Gm-Message-State: AOJu0YwHVVBI7gS2SKNnxR7lmml1LS+FMVpN9RCt/XxbnfFePf652rAm cmQLupNjtTipwZ8L5g5wvCRRcsqWaylMV4ioVnKADm7j3ZnYsXA+sEY0XdmOjVkAfW+Q7TXj017 KYRzIsXjE6YCXgusBfyoB5PXGyqz2eRuT3YAoiePxNTiJPTqV+A3/83SnHeXb3mtgFbf6WlM= X-Gm-Gg: ASbGncsWDg5pnj//VqpVgCQr6O27QPun2GLKcx8ZbJO2ii49+f/T0yNw8X6+iIPIEDx BJAwXNKTlgN0uv8UYDOhAvCxIGjquo2tNkx5OssRgW5pW1YTVLO14Q/JDyqYACSUGhlnI45nuYk R509YRQM+VGZft7WhCre4HSIHBHYXOL/d2iNoFBpMcxEl4K4aUoHIe5KkJOW6YZnwr1HnQatKSc COhtJ4YSu90NU01gtepfIoaNQmpDdZW5Fpf0FPtuhjL4Ra0tppqXxaDmh8+IlF30Ko/Ha1UKw+X XY2IVYDsWKZFvf+0HJlVAL9WMeltKAUNzmA= X-Received: by 2002:a05:6000:2893:b0:404:c253:4dc with SMTP id ffacd0b85a97d-405ca586a7amr2899933f8f.53.1758646747321; Tue, 23 Sep 2025 09:59:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFk0RRZlAQhODmq0Pg36y9THjFjgwQdtjxcw1geym+NmILUotJkeboxHjDTO4fRSjZ45v4y/w== X-Received: by 2002:a05:6000:2893:b0:404:c253:4dc with SMTP id ffacd0b85a97d-405ca586a7amr2899910f8f.53.1758646746826; Tue, 23 Sep 2025 09:59:06 -0700 (PDT) Received: from localhost ([31.111.84.207]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3ee0fbc7188sm25685320f8f.37.2025.09.23.09.59.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Sep 2025 09:59:06 -0700 (PDT) From: Andrew Burgess To: Tom Tromey Cc: gdb-patches@sourceware.org, Eli Zaretskii Subject: Re: [PATCHv8 1/3] gdb/python: add gdb.Style class In-Reply-To: <87qzw6l4y6.fsf@tromey.com> References: <87qzw6l4y6.fsf@tromey.com> Date: Tue, 23 Sep 2025 17:59:05 +0100 Message-ID: <87plbhf6li.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: s1kVToLZu3gheAzcEdnSOpL-m0XmCz-GigkT90hISHo_1758646748 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> +static std::optional > Andrew> +stylepy_style_from_name (const char *name, bool *has_intensity_ptr = nullptr, > Andrew> + const cmd_list_element **found_cmd_ptr = nullptr) > Andrew> +{ > Andrew> + std::string cmd_str = std::string ("show style ") + name; > Andrew> + > Andrew> + struct cmd_list_element *cmd = nullptr; > Andrew> + struct cmd_list_element *alias = nullptr; > Andrew> + int found; > Andrew> + try > Andrew> + { > Andrew> + struct cmd_list_element *prefix; > Andrew> + found = lookup_cmd_composition (cmd_str.c_str (), &alias, &prefix, &cmd); > Andrew> + } > Andrew> + catch (const gdb_exception &ex) > Andrew> + { > Andrew> + PyErr_Format (PyExc_RuntimeError, > Andrew> + _("style '%s' cannot be found."), name); > Andrew> + return {}; > > Noting this for a later comment... > > Andrew> +static PyObject * > Andrew> +stylepy_apply (PyObject *self, PyObject *args, PyObject *kw) > Andrew> +{ > Andrew> + style_object *style_obj = (style_object *) self; > Andrew> + > Andrew> + static const char *keywords[] = { "string", nullptr }; > Andrew> + PyObject *input_obj; > Andrew> + > Andrew> + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O!", keywords, > Andrew> + &PyUnicode_Type, &input_obj)) > Andrew> + return nullptr; > > Why "O!" and not "s" here? This is possibly a little premature optimisation, but capturing the string as O! means I get a 'PyObject *'. In the case where styling is not applied I can then just Py_INCREF that object and return it. If I capture the argument with "s" then I get a 'char *' back, so in the case where no styling is being added I have to create a new PyObject by converting the string _back_ again. I've left this code as it is for now, but I have added a comment. If you dislike this then just let me know, it's not essential, and I can switch to using "s". > > Andrew> + gdb::unique_xmalloc_ptr > Andrew> + input (python_string_to_target_string (input_obj)); > > target string seems like an unusual choice here. Yeah, especially as I use host_string_to_python_string about 5 lines later to convert the adjusted string back to a Python object. I've fixed this to use python_string_to_host_string. > > Andrew> + std::string output > Andrew> + = string_printf ("%s%s%s", style->to_ansi ().c_str (), input.get (), > Andrew> + ui_file_style ().to_ansi ().c_str ()); > > This can just using "+" which seems a little cleaner. Done. > > Andrew> + /* Handle named styles. This is harder, we need to write to the actual > Andrew> + parameter. First though, look up the named style to see if it has an > Andrew> + intensity. HAS_INTENSITY will be set true only if the style exists, > Andrew> + and has an 'intensity' setting. */ > Andrew> + > Andrew> + bool has_intensity = false; > Andrew> + (void) stylepy_style_from_name (style_obj->style_name, &has_intensity); > Andrew> + if (!has_intensity) > Andrew> + { > Andrew> + PyErr_Format > Andrew> + (PyExc_ValueError, "the intensity of style '%s' is not writable.", > Andrew> + style_obj->style_name); > Andrew> + return -1; > > I guess I don't know the Python rule here, but it seems like if > stylepy_style_from_name returns {}, then this can set an exception while > one is already set. See the snippet above. > > Instead of the (void) I think it would be a bit better to just check the > return result of stylepy_style_from_name, and if it returns an error, > just return -1 directly; and only then do the has_intensity check. You are absolutely correct. I fixed this, capturing the return value and checking we found a style. I've posted a v9 series with all these fixes. Thanks, Andrew