public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>, Eli Zaretskii <eliz@gnu.org>
Subject: [PATCHv2 4/8] gdb/python: remove users ability to create gdb.Progspace objects
Date: Wed, 10 Jan 2024 15:54:41 +0000	[thread overview]
Message-ID: <8990b13a57adba8f48f0aef44364bacf305c7bca.1704901918.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1704901918.git.aburgess@redhat.com>

I noticed that it is possible for the user to create a new
gdb.Progspace object, like this:

  (gdb) pi
  >>> p = gdb.Progspace()
  >>> p
  <gdb.Progspace object at 0x7ffad4219c10>
  >>> 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 "<stdin>", line 1, in <module>
  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 <eliz@gnu.org>
---
 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<pspace_object> 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
 }
-- 
2.25.4


  parent reply	other threads:[~2024-01-10 15:55 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-05 11:48 [PATCH 0/6] Python __repr__() methods and new __dict__ attributes Andrew Burgess
2024-01-05 11:48 ` [PATCH 1/6] gdb/python: hoist common invalid object repr code into py-utils.c Andrew Burgess
2024-01-09 19:19   ` Tom Tromey
2024-01-05 11:48 ` [PATCH 2/6] gdb/python: add gdb.InferiorThread.__repr__() method Andrew Burgess
2024-01-05 11:48 ` [PATCH 3/6] gdb/python: add gdb.Frame.__repr__() method Andrew Burgess
2024-01-05 11:48 ` [PATCH 4/6] gdb/python: remove users ability to create gdb.Progspace objects Andrew Burgess
2024-01-05 13:27   ` Eli Zaretskii
2024-01-05 11:48 ` [PATCH 5/6] gdb/python: Add gdb.Inferior.__dict__ attribute Andrew Burgess
2024-01-05 13:33   ` Eli Zaretskii
2024-01-09 20:05   ` Tom Tromey
2024-01-05 11:48 ` [PATCH 6/6] gdb/python: Add gdb.InferiorThread.__dict__ attribute Andrew Burgess
2024-01-05 13:31   ` Eli Zaretskii
2024-01-09 20:11   ` Tom Tromey
2024-01-10 10:38     ` Andrew Burgess
2024-01-10 15:54 ` [PATCHv2 0/8] Python __repr__() methods and new __dict__ attributes Andrew Burgess
2024-01-09 17:32   ` [PATCH] gdb/python: New InferiorThread.ptid_string attribute Andrew Burgess
2024-01-09 18:50     ` Eli Zaretskii
2024-01-09 19:10     ` Tom Tromey
2024-01-12  9:39       ` [PUSHED] " Andrew Burgess
2024-01-10 15:54     ` [PATCH] " Andrew Burgess
2024-01-10 15:54   ` [PATCHv2 1/8] gdb/python: hoist common invalid object repr code into py-utils.c Andrew Burgess
2024-01-10 15:54   ` [PATCHv2 2/8] gdb/python: add gdb.InferiorThread.__repr__() method Andrew Burgess
2024-01-10 15:54   ` [PATCHv2 3/8] gdb/python: add gdb.Frame.__repr__() method Andrew Burgess
2024-01-10 15:54   ` Andrew Burgess [this message]
2024-01-10 15:54   ` [PATCHv2 5/8] gdb/python: Add gdb.Inferior.__dict__ attribute Andrew Burgess
2024-01-10 15:54   ` [PATCHv2 6/8] gdb/python: Add gdb.InferiorThread.__dict__ attribute Andrew Burgess
2024-01-10 15:54   ` [PATCHv2 7/8] gdb/doc: add some notes on selecting suitable attribute names Andrew Burgess
2024-01-10 16:35     ` Eli Zaretskii
2024-01-11 10:48       ` Andrew Burgess
2024-01-11 10:56         ` Eli Zaretskii
2024-01-10 15:54   ` [PATCHv2 8/8] gdb/doc: update examples in gdb.Progspace and gdb.Objfile docs Andrew Burgess
2024-01-10 16:36     ` Eli Zaretskii
2024-01-10 18:08   ` [PATCHv2 0/8] Python __repr__() methods and new __dict__ attributes Tom Tromey
2024-01-12 13:44     ` Andrew Burgess
2024-01-12 14:57       ` Tom de Vries
2024-01-12 16:20         ` Andrew Burgess

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=8990b13a57adba8f48f0aef44364bacf305c7bca.1704901918.git.aburgess@redhat.com \
    --to=aburgess@redhat.com \
    --cc=eliz@gnu.org \
    --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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).