From patchwork Fri Jan 5 11:48:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83394 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 18E8B385DC05 for ; Fri, 5 Jan 2024 11:49:57 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 1810E385DC03 for ; Fri, 5 Jan 2024 11:48:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1810E385DC03 Authentication-Results: sourceware.org; dmarc=pass (p=none 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 1810E385DC03 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=1704455331; cv=none; b=tzo9H8TDIseywr4l87VSz7CRihrgfnrCJcFAjLuyMAEz3B5NBC/2WRL6Zgf7IuCeRy731n+Y+fAqe387eteZnIIDQQFT0mbBdrwl4bZN2n28+SHSTQpjoLcuQ3b0dL7D/wWHVS7kc2wrZNN+cz7+9/KTCXZgQvdzyNGbU8noj9E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704455331; c=relaxed/simple; bh=kd5HBSqji9ep0geepKuVhxdGsHzfBPAQZIn6knz6zdM=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=gtbL1anaJuKsoIgn85dBCkPYX3vLMdtd8A8ZJahbdVI+vSB/LdmYfi14K/XqFpOQkBN86Sf+mB1HtgQDEWP00CSfWu5n5ImaOJNt1aw7aZdkT466mGadCu3sUpnG5kww2jRsGUTB94hKlXEHewxxTuv/Lvo8PStRQDUVsYyYyB4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704455329; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OvgdxGaYyhQU5eeq0gi3nBgC4K0280jDESofvQbbR8o=; b=VF+i0BqTcJOftfIbqhNRjBCdAkkS70Xkqdj+JlVpBzRaECxa8btfrOKnhwjFOwHMpFKB3U 1Xk1VAjF/c0EuH1xrerNvJS3SrTfYVgIxLHjiJaKv1fDKYfMvZwTJfGSyxzv4171qJltGE lMhudv/RzX+VtUAKQCvcfrpEPcM8bak= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-28-UCPj6DpvONOo1m2eVSlVDg-1; Fri, 05 Jan 2024 06:48:48 -0500 X-MC-Unique: UCPj6DpvONOo1m2eVSlVDg-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-a27eddc1c27so83289966b.3 for ; Fri, 05 Jan 2024 03:48:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704455327; x=1705060127; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OvgdxGaYyhQU5eeq0gi3nBgC4K0280jDESofvQbbR8o=; b=SLmGY/3kTbNMW5A/KD7PdVHCgFo789sCnr4EIlS/9RAVEWj8Ira9l5x/f/jtfyrQdd l21OCCDRrTx1vYLYrs+ZSNXQOR+kJvcTh/9UkR6wory+58WmyuUtzdASYgZheVVeBQv7 /Q12jCj0n12+X5yZzsStb8kffWT/1fejyiqYAvUn2E3kBm4HmlnI17BO+lg5I8EMTjnT vmK0HS+HkplP3im/gOOUUKDiN6xzlAOL9hoDd80G6JYMuJw3NQKTk+cHb4vdvk7rs8LW TuBU/lqWeL7upjyJHWRnCzOMnQtXmrqS2rOPDXR8Aea46WiIPLoKM9kYv9/94K39NRUN LEbw== X-Gm-Message-State: AOJu0YzMZDFD2Lax+SXfAmD6hVtv5NN7m3L3jp9U8FrkTou+N7tanEqS 3rCFpOD7BZVyYJCjvxNjrlh5CUUWLFdSjuoSvwXr/DBwa6/PhtnJZqlEVJaVXMbYOVzCvu/wysE h4q2Vc3UcAo738GOJkIFVuCOynhO8cG/ubiz7bCipCtOj4hWVmCMjmQ6XGoHXRsRHqtLAYjXqCZ ojPfnme1qNh0pIQQ== X-Received: by 2002:a17:906:3851:b0:a29:9225:8e4b with SMTP id w17-20020a170906385100b00a2992258e4bmr54919ejc.106.1704455326932; Fri, 05 Jan 2024 03:48:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IFoSzpoyHiSbvwXsHkhLY5MUIVvLggi+ffgOc1rft6g+vQO0g0zg3fhTvUsfqfcAvdJaSvPWA== X-Received: by 2002:a17:906:3851:b0:a29:9225:8e4b with SMTP id w17-20020a170906385100b00a2992258e4bmr54913ejc.106.1704455326701; Fri, 05 Jan 2024 03:48:46 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id x7-20020a170906440700b00a1d2b0d4500sm786000ejo.168.2024.01.05.03.48.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 03:48:45 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 4/6] gdb/python: remove users ability to create gdb.Progspace objects Date: Fri, 5 Jan 2024 11:48:33 +0000 Message-Id: <5d718380fd0499359cbdd909ae8459b314e4c6b8.1704455158.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org 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+patchwork=sourceware.org@sourceware.org I noticed that it is possible for the user to create a new gdb.Progspace object, like this: (gdb) pi >>> p = gdb.Progspace() >>> p >>> p.is_valid() False As the new gdb.Progspace object is not associated with an actual C++ program_space object within GDB core, then the new gdb.Progspace is created invalid, and there is no way in which the new object can ever become valid. Nor do I believe there's anywhere in the Python API where it makes sense to consume an invalid gdb.Progspace created in this way, for example, the gdb.Progspace could be passed as the locus to register_type_printer, but all that would happen is that the registered printer would never be used. In this commit I propose to remove the ability to create new gdb.Progspace objects. Attempting to do so now gives an error, like this: (gdb) pi >>> gdb.Progspace() Traceback (most recent call last): File "", line 1, in TypeError: cannot create 'gdb.Progspace' instances Of course, there is a small risk here that some existing user code might break ... but if that happens I don't believe the user code can have been doing anything useful, so I see this as a small risk. Reviewed-By: Eli Zaretskii --- gdb/NEWS | 5 +++++ gdb/python/py-progspace.c | 16 +--------------- gdb/testsuite/gdb.python/py-progspace.exp | 6 ++++++ 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index 11cd6c0663e..36443c38aca 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -87,6 +87,11 @@ show remote thread-options-packet ** New function gdb.interrupt(), that interrupts GDB as if the user typed control-c. + ** It is no longer possible to create new gdb.Progspace object using + 'gdb.Progspace()', this will result in a TypeError. Progspace + objects can still be obtained through calling other API + functions, for example 'gdb.current_progspace()'. + * Debugger Adapter Protocol changes ** GDB now emits the "process" event. diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c index 0797ef1fa6b..bfc6ff50f01 100644 --- a/gdb/python/py-progspace.c +++ b/gdb/python/py-progspace.c @@ -210,20 +210,6 @@ pspy_initialize (pspace_object *self) return 1; } -static PyObject * -pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords) -{ - gdbpy_ref self ((pspace_object *) type->tp_alloc (type, 0)); - - if (self != NULL) - { - if (!pspy_initialize (self.get ())) - return NULL; - } - - return (PyObject *) self.release (); -} - PyObject * pspy_get_printers (PyObject *o, void *ignore) { @@ -859,5 +845,5 @@ PyTypeObject pspace_object_type = offsetof (pspace_object, dict), /* tp_dictoffset */ 0, /* tp_init */ 0, /* tp_alloc */ - pspy_new, /* tp_new */ + 0, /* tp_new */ }; diff --git a/gdb/testsuite/gdb.python/py-progspace.exp b/gdb/testsuite/gdb.python/py-progspace.exp index befd6433e47..5f6a9577f82 100644 --- a/gdb/testsuite/gdb.python/py-progspace.exp +++ b/gdb/testsuite/gdb.python/py-progspace.exp @@ -53,6 +53,12 @@ gdb_py_test_silent_cmd "python progspace.random_attribute = 42" \ gdb_test "python print (progspace.random_attribute)" "42" \ "Verify set of random attribute in progspace" +# Check that we can't create new (invalid) gdb.Progspace objects. +gdb_test "python gdb.Progspace()" \ + [multi_line "TypeError: cannot create 'gdb.Progspace' instances" \ + "Error while executing Python code\\."] \ + "check for error when calling gdb.Progspace() directly" + if {![runto_main]} { return }