Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 2/4] Change objfile_to_objfile_object to return a new reference
Date: Thu, 13 Sep 2018 05:30:00 -0000	[thread overview]
Message-ID: <20180913053007.11780-3-tom@tromey.com> (raw)
In-Reply-To: <20180913053007.11780-1-tom@tromey.com>

This changes objfile_to_objfile_object to return a new references and
fixes up all the uses.

2018-09-12  Tom Tromey  <tom@tromey.com>

	* python/python-internal.h (objfile_to_objfile_object): Change
	return type.
	* python/py-newobjfileevent.c (create_new_objfile_event_object):
	Update.
	* python/py-xmethods.c (gdbpy_get_matching_xmethod_workers):
	Update.
	* python/python.c (gdbpy_get_current_objfile): Update.
	(gdbpy_objfiles): Update.
	* python/py-objfile.c (objfpy_get_owner, gdbpy_lookup_objfile):
	Update.
	(objfile_to_objfile_object): Return a new reference.
	* python/py-symtab.c (stpy_get_objfile): Update.
	* python/py-prettyprint.c (find_pretty_printer_from_objfiles):
	Update.
---
 gdb/ChangeLog                   | 17 +++++++++++++
 gdb/python/py-newobjfileevent.c | 10 +++-----
 gdb/python/py-objfile.c         | 43 +++++++++++++--------------------
 gdb/python/py-prettyprint.c     |  6 ++---
 gdb/python/py-symtab.c          |  5 +---
 gdb/python/py-xmethods.c        |  5 ++--
 gdb/python/python-internal.h    |  3 +--
 gdb/python/python.c             | 11 +++------
 8 files changed, 49 insertions(+), 51 deletions(-)

diff --git a/gdb/python/py-newobjfileevent.c b/gdb/python/py-newobjfileevent.c
index aa31fb4849b..55e884dcaf5 100644
--- a/gdb/python/py-newobjfileevent.c
+++ b/gdb/python/py-newobjfileevent.c
@@ -28,12 +28,10 @@ create_new_objfile_event_object (struct objfile *objfile)
   if (objfile_event == NULL)
     return NULL;
 
-  /* Note that objfile_to_objfile_object returns a borrowed reference,
-     so we don't need a decref here.  */
-  PyObject *py_objfile = objfile_to_objfile_object (objfile);
-  if (!py_objfile || evpy_add_attribute (objfile_event.get (),
-                                         "new_objfile",
-                                         py_objfile) < 0)
+  gdbpy_ref<> py_objfile = objfile_to_objfile_object (objfile);
+  if (py_objfile == NULL || evpy_add_attribute (objfile_event.get (),
+						"new_objfile",
+						py_objfile.get ()) < 0)
     return NULL;
 
   return objfile_event;
diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
index 8fd160c7bf8..cef34f1bcc2 100644
--- a/gdb/python/py-objfile.c
+++ b/gdb/python/py-objfile.c
@@ -115,12 +115,7 @@ objfpy_get_owner (PyObject *self, void *closure)
 
   owner = objfile->separate_debug_objfile_backlink;
   if (owner != NULL)
-    {
-      PyObject *result = objfile_to_objfile_object (owner);
-
-      Py_XINCREF (result);
-      return result;
-    }
+    return objfile_to_objfile_object (owner).release ();
   Py_RETURN_NONE;
 }
 
@@ -590,12 +585,7 @@ gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw)
     objfile = objfpy_lookup_objfile_by_name (name);
 
   if (objfile != NULL)
-    {
-      PyObject *result = objfile_to_objfile_object (objfile);
-
-      Py_XINCREF (result);
-      return result;
-    }
+    return objfile_to_objfile_object (objfile).release ();
 
   PyErr_SetString (PyExc_ValueError, _("Objfile not found."));
   return NULL;
@@ -613,30 +603,31 @@ py_free_objfile (struct objfile *objfile, void *datum)
   object->objfile = NULL;
 }
 
-/* Return a borrowed reference to the Python object of type Objfile
+/* Return a new reference to the Python object of type Objfile
    representing OBJFILE.  If the object has already been created,
    return it.  Otherwise, create it.  Return NULL and set the Python
    error on failure.  */
 
-PyObject *
+gdbpy_ref<>
 objfile_to_objfile_object (struct objfile *objfile)
 {
-  gdbpy_ref<objfile_object> object
-    ((objfile_object *) objfile_data (objfile, objfpy_objfile_data_key));
-  if (object == NULL)
+  PyObject *result
+    = ((PyObject *) objfile_data (objfile, objfpy_objfile_data_key));
+  if (result == NULL)
     {
-      object.reset (PyObject_New (objfile_object, &objfile_object_type));
-      if (object != NULL)
-	{
-	  if (!objfpy_initialize (object.get ()))
-	    return NULL;
+      gdbpy_ref<objfile_object> object
+	((objfile_object *) PyObject_New (objfile_object, &objfile_object_type));
+      if (object == NULL)
+	return NULL;
+      if (!objfpy_initialize (object.get ()))
+	return NULL;
 
-	  object->objfile = objfile;
-	  set_objfile_data (objfile, objfpy_objfile_data_key, object.get ());
-	}
+      object->objfile = objfile;
+      set_objfile_data (objfile, objfpy_objfile_data_key, object.get ());
+      result = (PyObject *) object.release ();
     }
 
-  return (PyObject *) object.release ();
+  return gdbpy_ref<>::new_reference (result);
 }
 
 int
diff --git a/gdb/python/py-prettyprint.c b/gdb/python/py-prettyprint.c
index 9aadd3b27c9..a7062225816 100644
--- a/gdb/python/py-prettyprint.c
+++ b/gdb/python/py-prettyprint.c
@@ -97,15 +97,15 @@ find_pretty_printer_from_objfiles (PyObject *value)
 
   ALL_OBJFILES (obj)
   {
-    PyObject *objf = objfile_to_objfile_object (obj);
-    if (!objf)
+    gdbpy_ref<> objf = objfile_to_objfile_object (obj);
+    if (objf == NULL)
       {
 	/* Ignore the error and continue.  */
 	PyErr_Clear ();
 	continue;
       }
 
-    gdbpy_ref<> pp_list (objfpy_get_printers (objf, NULL));
+    gdbpy_ref<> pp_list (objfpy_get_printers (objf.get (), NULL));
     gdbpy_ref<> function (search_pp_list (pp_list.get (), value));
 
     /* If there is an error in any objfile list, abort the search and exit.  */
diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c
index be7fb3ec471..9bb20dab31f 100644
--- a/gdb/python/py-symtab.c
+++ b/gdb/python/py-symtab.c
@@ -117,13 +117,10 @@ static PyObject *
 stpy_get_objfile (PyObject *self, void *closure)
 {
   struct symtab *symtab = NULL;
-  PyObject *result;
 
   STPY_REQUIRE_VALID (self, symtab);
 
-  result = objfile_to_objfile_object (SYMTAB_OBJFILE (symtab));
-  Py_XINCREF (result);
-  return result;
+  return objfile_to_objfile_object (SYMTAB_OBJFILE (symtab)).release ();
 }
 
 /* Getter function for symtab.producer.  */
diff --git a/gdb/python/py-xmethods.c b/gdb/python/py-xmethods.c
index c568295a165..8e616cd4e2d 100644
--- a/gdb/python/py-xmethods.c
+++ b/gdb/python/py-xmethods.c
@@ -147,7 +147,7 @@ gdbpy_get_matching_xmethod_workers
      list individually, but there's no data yet to show it's needed.  */
   ALL_OBJFILES (objfile)
     {
-      PyObject *py_objfile = objfile_to_objfile_object (objfile);
+      gdbpy_ref<> py_objfile = objfile_to_objfile_object (objfile);
 
       if (py_objfile == NULL)
 	{
@@ -155,7 +155,8 @@ gdbpy_get_matching_xmethod_workers
 	  return EXT_LANG_RC_ERROR;
 	}
 
-      gdbpy_ref<> objfile_matchers (objfpy_get_xmethods (py_objfile, NULL));
+      gdbpy_ref<> objfile_matchers (objfpy_get_xmethods (py_objfile.get (),
+							 NULL));
       gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
 					   objfile_matchers.get ()));
       if (temp == NULL)
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index a6dc5f090b5..cbba3ff8ef6 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -523,8 +523,7 @@ PyObject *pspy_get_frame_filters (PyObject *, void *);
 PyObject *pspy_get_frame_unwinders (PyObject *, void *);
 PyObject *pspy_get_xmethods (PyObject *, void *);
 
-PyObject *objfile_to_objfile_object (struct objfile *)
-    CPYCHECKER_RETURNS_BORROWED_REF;
+gdbpy_ref<> objfile_to_objfile_object (struct objfile *);
 PyObject *objfpy_get_printers (PyObject *, void *);
 PyObject *objfpy_get_frame_filters (PyObject *, void *);
 PyObject *objfpy_get_frame_unwinders (PyObject *, void *);
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 93d6d1156d8..778ddb9f342 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1421,15 +1421,10 @@ gdbpy_execute_objfile_script (const struct extension_language_defn *extlang,
 static PyObject *
 gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2)
 {
-  PyObject *result;
-
   if (! gdbpy_current_objfile)
     Py_RETURN_NONE;
 
-  result = objfile_to_objfile_object (gdbpy_current_objfile);
-  if (result)
-    Py_INCREF (result);
-  return result;
+  return objfile_to_objfile_object (gdbpy_current_objfile).release ();
 }
 
 /* Return a sequence holding all the Objfiles.  */
@@ -1445,9 +1440,9 @@ gdbpy_objfiles (PyObject *unused1, PyObject *unused2)
 
   ALL_OBJFILES (objf)
   {
-    PyObject *item = objfile_to_objfile_object (objf);
+    gdbpy_ref<> item = objfile_to_objfile_object (objf);
 
-    if (!item || PyList_Append (list.get (), item) == -1)
+    if (item == NULL || PyList_Append (list.get (), item.get ()) == -1)
       return NULL;
   }
 
-- 
2.17.1


  reply	other threads:[~2018-09-13  5:30 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-13  5:30 [PATCH 0/4] Disallow the return of borrowed references Tom Tromey
2018-09-13  5:30 ` Tom Tromey [this message]
2018-09-16  1:28   ` [PATCH 2/4] Change objfile_to_objfile_object to return a new reference Simon Marchi
2018-09-16  2:00     ` Simon Marchi
2018-09-13  5:30 ` [PATCH 4/4] Remove CPYCHECKER_RETURNS_BORROWED_REF Tom Tromey
2018-09-13  5:30 ` [PATCH 3/4] Change thread_to_thread_object to return a new reference Tom Tromey
2018-09-16  2:11   ` Simon Marchi
2018-09-16 13:32     ` Tom Tromey
2018-09-16 14:05     ` Tom Tromey
2018-09-16 15:35       ` Tom Tromey
2018-09-17  0:52       ` Simon Marchi
2018-09-17  5:31         ` Tom Tromey
2018-09-13  5:30 ` [PATCH 1/4] Change pspace_to_pspace_object " Tom Tromey
2018-09-16  0:57   ` Simon Marchi
2018-09-16 12:59     ` Tom Tromey
2018-09-16  1:19   ` Simon Marchi
2018-09-16  1:58     ` Simon Marchi
2018-09-16 13:01       ` Tom Tromey
2018-09-16  0:56 ` [PATCH 0/4] Disallow the return of borrowed references Simon Marchi

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=20180913053007.11780-3-tom@tromey.com \
    --to=tom@tromey.com \
    --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