From: Andrew Burgess Date: Tue, 26 Aug 2025 15:56:07 +0000 (+0100) Subject: gdb/python: return gdbpy_ref<> from gdbpy_create_ptid_object X-Git-Tag: gdb-17-branchpoint~168 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f5493d6c96da9faf5f16d748cf6c899f0836b12d;p=thirdparty%2Fbinutils-gdb.git gdb/python: return gdbpy_ref<> from gdbpy_create_ptid_object Update gdbpy_create_ptid_object (python/py-infthread.c) to return a gdbpy_ref<> rather than a 'PyObject *'. This reduces the chances that a caller will leak an object, though no such memory leaks are fixed in this commit, this is just a code improvement patch. There should be no user visible changes after this commit. Approved-By: Simon Marchi --- diff --git a/gdb/python/py-infevents.c b/gdb/python/py-infevents.c index e63ba52623f..f74fb017edc 100644 --- a/gdb/python/py-infevents.c +++ b/gdb/python/py-infevents.c @@ -40,7 +40,7 @@ create_inferior_call_event_object (inferior_call_kind flag, ptid_t ptid, gdb_assert_not_reached ("invalid inferior_call_kind"); } - gdbpy_ref<> ptid_obj (gdbpy_create_ptid_object (ptid)); + gdbpy_ref<> ptid_obj = gdbpy_create_ptid_object (ptid); if (ptid_obj == NULL) return NULL; diff --git a/gdb/python/py-infthread.c b/gdb/python/py-infthread.c index d78c3a15b87..08533fe2923 100644 --- a/gdb/python/py-infthread.c +++ b/gdb/python/py-infthread.c @@ -190,7 +190,7 @@ thpy_get_ptid (PyObject *self, void *closure) THPY_REQUIRE_VALID (thread_obj); - return gdbpy_create_ptid_object (thread_obj->thread->ptid); + return gdbpy_create_ptid_object (thread_obj->thread->ptid).release (); } /* Implement gdb.InferiorThread.ptid_string attribute. */ @@ -361,9 +361,9 @@ thpy_repr (PyObject *self) target_pid_to_str (thr->ptid).c_str ()); } -/* Return a reference to a new Python object representing a ptid_t. - The object is a tuple containing (pid, lwp, tid). */ -PyObject * +/* See python-internal.h. */ + +gdbpy_ref<> gdbpy_create_ptid_object (ptid_t ptid) { int pid = ptid.pid (); @@ -389,7 +389,7 @@ gdbpy_create_ptid_object (ptid_t ptid) PyTuple_SET_ITEM (ret.get (), 1, lwp_obj.release ()); PyTuple_SET_ITEM (ret.get (), 2, tid_obj.release ()); - return ret.release (); + return ret; } /* Implementation of gdb.selected_thread () -> gdb.InferiorThread. diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 7f4237eecc2..f61a1753ac4 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -503,7 +503,12 @@ PyObject *gdbpy_create_lazy_string_object (CORE_ADDR address, long length, const char *encoding, struct type *type); PyObject *gdbpy_inferiors (PyObject *unused, PyObject *unused2); -PyObject *gdbpy_create_ptid_object (ptid_t ptid); + +/* Return a reference to a new Python Tuple object representing a ptid_t. + The object is a tuple containing (pid, lwp, tid). */ + +extern gdbpy_ref<> gdbpy_create_ptid_object (ptid_t ptid); + PyObject *gdbpy_selected_thread (PyObject *self, PyObject *args); PyObject *gdbpy_selected_inferior (PyObject *self, PyObject *args); PyObject *gdbpy_string_to_argv (PyObject *self, PyObject *args);