diff options
| author | Matthieu Longo <matthieu.longo@arm.com> | 2026-02-26 16:08:27 +0000 |
|---|---|---|
| committer | Matthieu Longo <matthieu.longo@arm.com> | 2026-03-10 12:43:12 +0000 |
| commit | f295945e2689ef0c710c79e5ea11a23900019404 (patch) | |
| tree | 8e49f33683cb20eb3c407c2ce877b4f95fe778f6 /gdb/python | |
| parent | f0cfbf6ceda8396291d736858cfd713cc4a0e0da (diff) | |
| download | binutils-f295945e2689ef0c710c79e5ea11a23900019404.tar.gz binutils-f295945e2689ef0c710c79e5ea11a23900019404.tar.bz2 binutils-f295945e2689ef0c710c79e5ea11a23900019404.zip | |
gdb/python: flatten functions calling PyObject_New and use gdbpy_ref
This patch aims at systematically using gdbpy_ref<> at all call sites
of PyObject_New(). This prepares for future patches that expect
gdbby_ref<> parameters and affect return handling.
As part of this change, flattening the affected functions so that the
return logic becomes clearer and more flexible to adjust.
Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdb/python')
| -rw-r--r-- | gdb/python/py-corefile.c | 48 | ||||
| -rw-r--r-- | gdb/python/py-inferior.c | 35 | ||||
| -rw-r--r-- | gdb/python/py-progspace.c | 31 |
3 files changed, 57 insertions, 57 deletions
diff --git a/gdb/python/py-corefile.c b/gdb/python/py-corefile.c index 88fedbd718c..25af8710d5e 100644 --- a/gdb/python/py-corefile.c +++ b/gdb/python/py-corefile.c @@ -119,33 +119,33 @@ gdbpy_core_file_from_inferior (inferior *inf) return gdbpy_ref<>::new_reference (Py_None); PyObject *result = (PyObject *) cfpy_inferior_corefile_data_key.get (inf); - if (result == nullptr) - { - gdbpy_ref<corefile_object> object - (PyObject_New (corefile_object, &corefile_object_type)); - if (object == nullptr) - return nullptr; + if (result != nullptr) + return gdbpy_ref<>::new_reference (result); - /* Ensure the 'inferior' field is set to NULL. If the PyDict_New - call fails then the gdb.Corefile will be discarded and - cfpy_dealloc will be called, which requires that the 'inferior' be - set to NULL. */ - object->inferior = nullptr; - object->mapped_files = nullptr; - object->dict = PyDict_New (); - if (object->dict == nullptr) - return nullptr; + gdbpy_ref<corefile_object> object + (PyObject_New (corefile_object, &corefile_object_type)); + if (object == nullptr) + return nullptr; - /* Now that the gdb.Corefile has been successfully initialised and we - know that it is going to be passed back to the user, move it out - of the invalid state by setting the 'inferior' field to a non NULL - value. */ - object->inferior = inf; - cfpy_inferior_corefile_data_key.set (inf, object.get ()); - result = (PyObject *) object.release (); - } + /* Ensure the 'inferior' field is set to NULL. If the PyDict_New call fails + then the gdb.Corefile will be discarded and cfpy_dealloc will be called, + which requires that the 'inferior' be set to NULL. */ + object->inferior = nullptr; + object->mapped_files = nullptr; + object->dict = PyDict_New (); + if (object->dict == nullptr) + return nullptr; + + /* Now that the gdb.Corefile has been successfully initialised and we know + that it is going to be passed back to the user, move it out of the invalid + state by setting the 'inferior' field to a non NULL value. */ + object->inferior = inf; + + /* PyObject_New initializes the new object with a refcount of 1. This counts + for the reference we are keeping in the inferior corefile data. */ + cfpy_inferior_corefile_data_key.set (inf, object.get ()); - return gdbpy_ref<>::new_reference (result); + return gdbpy_ref<>::new_reference (object.release ()); } /* Return true if OBJ is valid. */ diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index ed28ccf3c07..f4fdd4df6cc 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -213,29 +213,26 @@ python_free_objfile (struct objfile *objfile) gdbpy_ref<inferior_object> inferior_to_inferior_object (struct inferior *inferior) { - inferior_object *inf_obj; + inferior_object *result = infpy_inf_data_key.get (inferior); + if (result != nullptr) + return gdbpy_ref<inferior_object>::new_reference (result); - inf_obj = infpy_inf_data_key.get (inferior); - if (!inf_obj) - { - inf_obj = PyObject_New (inferior_object, &inferior_object_type); - if (!inf_obj) - return NULL; + gdbpy_ref<inferior_object> inf_obj + (PyObject_New (inferior_object, &inferior_object_type)); + if (inf_obj == nullptr) + return nullptr; - inf_obj->inferior = inferior; - inf_obj->threads = new thread_map_t (); - inf_obj->dict = PyDict_New (); - if (inf_obj->dict == nullptr) - return nullptr; + inf_obj->inferior = inferior; + inf_obj->threads = new thread_map_t (); + inf_obj->dict = PyDict_New (); + if (inf_obj->dict == nullptr) + return nullptr; - /* PyObject_New initializes the new object with a refcount of 1. This - counts for the reference we are keeping in the inferior data. */ - infpy_inf_data_key.set (inferior, inf_obj); - } + /* PyObject_New initializes the new object with a refcount of 1. This counts + for the reference we are keeping in the inferior data. */ + infpy_inf_data_key.set (inferior, inf_obj.get ()); - /* We are returning a new reference. */ - gdb_assert (inf_obj != nullptr); - return gdbpy_ref<inferior_object>::new_reference (inf_obj); + return gdbpy_ref<inferior_object>::new_reference (inf_obj.release ()); } /* Called when a new inferior is created. Notifies any Python event diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c index 5a23c4c7177..f2585103346 100644 --- a/gdb/python/py-progspace.c +++ b/gdb/python/py-progspace.c @@ -585,21 +585,24 @@ gdbpy_ref<> pspace_to_pspace_object (struct program_space *pspace) { PyObject *result = (PyObject *) pspy_pspace_data_key.get (pspace); - if (result == NULL) - { - gdbpy_ref<pspace_object> object - ((pspace_object *) PyObject_New (pspace_object, &pspace_object_type)); - if (object == NULL) - return NULL; - if (!pspy_initialize (object)) - return NULL; - - object->pspace = pspace; - pspy_pspace_data_key.set (pspace, object.get ()); - result = (PyObject *) object.release (); - } + if (result != nullptr) + return gdbpy_ref<>::new_reference (result); + + gdbpy_ref<pspace_object> object + (PyObject_New (pspace_object, &pspace_object_type)); + if (object == nullptr) + return nullptr; + + if (!pspy_initialize (object)) + return nullptr; + + object->pspace = pspace; + + /* PyObject_New initializes the new object with a refcount of 1. This counts + for the reference we are keeping in the pspace data. */ + pspy_pspace_data_key.set (pspace, object.get ()); - return gdbpy_ref<>::new_reference (result); + return gdbpy_ref<>::new_reference (object.release ()); } /* See python-internal.h. */ |
