[PATCHv2,4/8] gdb/python: remove users ability to create gdb.Progspace objects

Message ID 8990b13a57adba8f48f0aef44364bacf305c7bca.1704901918.git.aburgess@redhat.com
State New
Headers
Series Python __repr__() methods and new __dict__ attributes |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 fail Testing failed
linaro-tcwg-bot/tcwg_gdb_check--master-arm fail Patch failed to apply

Commit Message

Andrew Burgess Jan. 10, 2024, 3:54 p.m. UTC
  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(-)
  

Patch

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
 }