public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/3] Use gdbpy_ref more in gdb/python
@ 2018-10-28 18:13 Tom Tromey
  2018-10-28 18:13 ` [PATCH 1/3] Return gdbpy_ref from gdb_py_object_from_*longest Tom Tromey
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Tom Tromey @ 2018-10-28 18:13 UTC (permalink / raw)
  To: gdb-patches

This changes a few more spots in the Python layer to return a
gdbpy_ref.  This is a small improvement because it makes the reference
ownership part of the type system rather than merely a convention.

Regression tested by the buildbot.

Tom


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/3] Return gdbpy_ref from gdbpy_get_varobj_pretty_printer
  2018-10-28 18:13 [PATCH 0/3] Use gdbpy_ref more in gdb/python Tom Tromey
  2018-10-28 18:13 ` [PATCH 1/3] Return gdbpy_ref from gdb_py_object_from_*longest Tom Tromey
  2018-10-28 18:13 ` [PATCH 2/3] Return gdbpy_ref from some Python string functions Tom Tromey
@ 2018-10-28 18:13 ` Tom Tromey
  2018-11-04 15:01 ` [PATCH 0/3] Use gdbpy_ref more in gdb/python Tom Tromey
  3 siblings, 0 replies; 5+ messages in thread
From: Tom Tromey @ 2018-10-28 18:13 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes gdbpy_get_varobj_pretty_printer to return a gdbpy_ref.

gdb/ChangeLog
2018-10-28  Tom Tromey  <tom@tromey.com>

	* varobj.c (install_default_visualizer): Update.
	* python/python-internal.h (gdbpy_get_varobj_pretty_printer):
	Return gdbpy_ref.
	* python/py-prettyprint.c (search_pp_list): Return gdbpy_ref.
	(find_pretty_printer_from_progspace)
	(find_pretty_printer_from_gdb, find_pretty_printer)
	(gdbpy_get_varobj_pretty_printer): Return gdbpy_ref.
	(gdbpy_get_varobj_pretty_printer, gdbpy_default_visualizer):
	Update.
---
 gdb/ChangeLog                | 12 ++++++++++++
 gdb/python/py-prettyprint.c  | 28 +++++++++++++---------------
 gdb/python/python-internal.h |  2 +-
 gdb/varobj.c                 | 13 +++++--------
 4 files changed, 31 insertions(+), 24 deletions(-)

diff --git a/gdb/python/py-prettyprint.c b/gdb/python/py-prettyprint.c
index 2386dccbbb..fa4107c30d 100644
--- a/gdb/python/py-prettyprint.c
+++ b/gdb/python/py-prettyprint.c
@@ -45,7 +45,7 @@ enum string_repr_result
    will return None.  On error, it will set the Python error and
    return NULL.  */
 
-static PyObject *
+static gdbpy_ref<>
 search_pp_list (PyObject *list, PyObject *value)
 {
   Py_ssize_t pp_list_size, list_index;
@@ -78,10 +78,10 @@ search_pp_list (PyObject *list, PyObject *value)
       if (printer == NULL)
 	return NULL;
       else if (printer != Py_None)
-	return printer.release ();
+	return printer;
     }
 
-  Py_RETURN_NONE;
+  return gdbpy_ref<>::new_reference (Py_None);
 }
 
 /* Subroutine of find_pretty_printer to simplify it.
@@ -125,7 +125,7 @@ find_pretty_printer_from_objfiles (PyObject *value)
    The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
    Otherwise the result is the pretty-printer function, suitably inc-ref'd.  */
 
-static PyObject *
+static gdbpy_ref<>
 find_pretty_printer_from_progspace (PyObject *value)
 {
   gdbpy_ref<> obj = pspace_to_pspace_object (current_program_space);
@@ -142,17 +142,17 @@ find_pretty_printer_from_progspace (PyObject *value)
    The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
    Otherwise the result is the pretty-printer function, suitably inc-ref'd.  */
 
-static PyObject *
+static gdbpy_ref<>
 find_pretty_printer_from_gdb (PyObject *value)
 {
   /* Fetch the global pretty printer list.  */
   if (gdb_python_module == NULL
       || ! PyObject_HasAttrString (gdb_python_module, "pretty_printers"))
-    Py_RETURN_NONE;
+    return gdbpy_ref<>::new_reference (Py_None);
   gdbpy_ref<> pp_list (PyObject_GetAttrString (gdb_python_module,
 					       "pretty_printers"));
   if (pp_list == NULL || ! PyList_Check (pp_list.get ()))
-    Py_RETURN_NONE;
+    return gdbpy_ref<>::new_reference (Py_None);
 
   return search_pp_list (pp_list.get (), value);
 }
@@ -161,19 +161,19 @@ find_pretty_printer_from_gdb (PyObject *value)
    pretty-printer exists, return None.  If one exists, return a new
    reference.  On error, set the Python error and return NULL.  */
 
-static PyObject *
+static gdbpy_ref<>
 find_pretty_printer (PyObject *value)
 {
   /* Look at the pretty-printer list for each objfile
      in the current program-space.  */
   gdbpy_ref<> function (find_pretty_printer_from_objfiles (value));
   if (function == NULL || function != Py_None)
-    return function.release ();
+    return function;
 
   /* Look at the pretty-printer list for the current program-space.  */
-  function.reset (find_pretty_printer_from_progspace (value));
+  function = find_pretty_printer_from_progspace (value);
   if (function == NULL || function != Py_None)
-    return function.release ();
+    return function;
 
   /* Look at the pretty-printer list in the gdb module.  */
   return find_pretty_printer_from_gdb (value);
@@ -744,7 +744,7 @@ apply_varobj_pretty_printer (PyObject *printer_obj,
    reference to the object if successful; returns NULL if not.  VALUE
    is the value for which a printer tests to determine if it
    can pretty-print the value.  */
-PyObject *
+gdbpy_ref<>
 gdbpy_get_varobj_pretty_printer (struct value *value)
 {
   TRY
@@ -772,7 +772,6 @@ PyObject *
 gdbpy_default_visualizer (PyObject *self, PyObject *args)
 {
   PyObject *val_obj;
-  PyObject *cons;
   struct value *value;
 
   if (! PyArg_ParseTuple (args, "O", &val_obj))
@@ -785,6 +784,5 @@ gdbpy_default_visualizer (PyObject *self, PyObject *args)
       return NULL;
     }
 
-  cons = find_pretty_printer (val_obj);
-  return cons;
+  return find_pretty_printer (val_obj).release ();
 }
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 582044ad8c..1ac54f9b57 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -680,7 +680,7 @@ int gdbpy_is_value_object (PyObject *obj);
 gdbpy_ref<> apply_varobj_pretty_printer (PyObject *print_obj,
 					 struct value **replacement,
 					 struct ui_file *stream);
-PyObject *gdbpy_get_varobj_pretty_printer (struct value *value);
+gdbpy_ref<> gdbpy_get_varobj_pretty_printer (struct value *value);
 gdb::unique_xmalloc_ptr<char> gdbpy_get_display_hint (PyObject *printer);
 PyObject *gdbpy_default_visualizer (PyObject *self, PyObject *args);
 
diff --git a/gdb/varobj.c b/gdb/varobj.c
index 3ae4de8bc1..17cfe4bdc3 100644
--- a/gdb/varobj.c
+++ b/gdb/varobj.c
@@ -1115,25 +1115,22 @@ install_default_visualizer (struct varobj *var)
 
   if (pretty_printing)
     {
-      PyObject *pretty_printer = NULL;
+      gdbpy_ref<> pretty_printer;
 
       if (var->value != nullptr)
 	{
 	  pretty_printer = gdbpy_get_varobj_pretty_printer (var->value.get ());
-	  if (! pretty_printer)
+	  if (pretty_printer == nullptr)
 	    {
 	      gdbpy_print_stack ();
 	      error (_("Cannot instantiate printer for default visualizer"));
 	    }
 	}
-      
+
       if (pretty_printer == Py_None)
-	{
-	  Py_DECREF (pretty_printer);
-	  pretty_printer = NULL;
-	}
+	pretty_printer.release ();
   
-      install_visualizer (var->dynamic, NULL, pretty_printer);
+      install_visualizer (var->dynamic, NULL, pretty_printer.release ());
     }
 }
 
-- 
2.17.1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/3] Return gdbpy_ref from gdb_py_object_from_*longest
  2018-10-28 18:13 [PATCH 0/3] Use gdbpy_ref more in gdb/python Tom Tromey
@ 2018-10-28 18:13 ` Tom Tromey
  2018-10-28 18:13 ` [PATCH 2/3] Return gdbpy_ref from some Python string functions Tom Tromey
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Tom Tromey @ 2018-10-28 18:13 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes gdb_py_object_from_longest and
gdb_py_object_from_ulongest to return a gdbpy_ref rather than a
PyObject*.

gdb/ChangeLog
2018-10-28  Tom Tromey  <tom@tromey.com>

	* python/python-internal.h (gdb_py_object_from_longest)
	(gdb_py_object_from_ulongest): Return gdbpy_ref.
	* python/py-value.c (valpy_int): Update.
	* python/py-utils.c (gdb_py_object_from_longest): Return
	gdbpy_ref.
	(gdb_py_object_from_ulongest): Likewise.
	* python/py-type.c (typy_get_alignof): Update.
	* python/py-linetable.c (ltpy_get_all_source_lines)
	(ltpy_entry_get_line, ltpy_entry_get_pc): Update.
	* python/py-block.c (blpy_get_start, blpy_get_end): Update.
---
 gdb/ChangeLog                | 13 +++++++++++++
 gdb/python/py-block.c        |  4 ++--
 gdb/python/py-linetable.c    |  6 +++---
 gdb/python/py-type.c         |  2 +-
 gdb/python/py-utils.c        | 22 +++++++++++-----------
 gdb/python/py-value.c        |  4 ++--
 gdb/python/python-internal.h |  4 ++--
 7 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c
index 5fc33f0f0f..f5870c662b 100644
--- a/gdb/python/py-block.c
+++ b/gdb/python/py-block.c
@@ -110,7 +110,7 @@ blpy_get_start (PyObject *self, void *closure)
 
   BLPY_REQUIRE_VALID (self, block);
 
-  return gdb_py_object_from_ulongest (BLOCK_START (block));
+  return gdb_py_object_from_ulongest (BLOCK_START (block)).release ();
 }
 
 static PyObject *
@@ -120,7 +120,7 @@ blpy_get_end (PyObject *self, void *closure)
 
   BLPY_REQUIRE_VALID (self, block);
 
-  return gdb_py_object_from_ulongest (BLOCK_END (block));
+  return gdb_py_object_from_ulongest (BLOCK_END (block)).release ();
 }
 
 static PyObject *
diff --git a/gdb/python/py-linetable.c b/gdb/python/py-linetable.c
index c4e80d02a5..97c81aacf0 100644
--- a/gdb/python/py-linetable.c
+++ b/gdb/python/py-linetable.c
@@ -244,7 +244,7 @@ ltpy_get_all_source_lines (PyObject *self, PyObject *args)
 	 include in the source set. */
       if (item->line > 0)
 	{
-	  gdbpy_ref<> line (gdb_py_object_from_longest (item->line));
+	  gdbpy_ref<> line = gdb_py_object_from_longest (item->line);
 
 	  if (line == NULL)
 	    return NULL;
@@ -327,7 +327,7 @@ ltpy_entry_get_line (PyObject *self, void *closure)
 {
   linetable_entry_object *obj = (linetable_entry_object *) self;
 
-  return gdb_py_object_from_longest (obj->line);
+  return gdb_py_object_from_longest (obj->line).release ();
 }
 
 /* Implementation of gdb.LineTableEntry.pc (self) -> Long.  Returns a
@@ -338,7 +338,7 @@ ltpy_entry_get_pc (PyObject *self, void *closure)
 {
   linetable_entry_object *obj = (linetable_entry_object *) self;
 
-  return  gdb_py_object_from_longest (obj->pc);
+  return  gdb_py_object_from_longest (obj->pc).release ();
 }
 
 /* LineTable iterator functions.  */
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index 897ad9374a..afc7635235 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -755,7 +755,7 @@ typy_get_alignof (PyObject *self, void *closure)
 
   /* Ignore exceptions.  */
 
-  return gdb_py_object_from_ulongest (align);
+  return gdb_py_object_from_ulongest (align).release ();
 }
 
 static struct type *
diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c
index 6ef0d7efd3..4dcac3daaf 100644
--- a/gdb/python/py-utils.c
+++ b/gdb/python/py-utils.c
@@ -295,47 +295,47 @@ get_addr_from_python (PyObject *obj, CORE_ADDR *addr)
 /* Convert a LONGEST to the appropriate Python object -- either an
    integer object or a long object, depending on its value.  */
 
-PyObject *
+gdbpy_ref<>
 gdb_py_object_from_longest (LONGEST l)
 {
 #ifdef IS_PY3K
   if (sizeof (l) > sizeof (long))
-    return PyLong_FromLongLong (l);
-  return PyLong_FromLong (l);
+    return gdbpy_ref<> (PyLong_FromLongLong (l));
+  return gdbpy_ref<> (PyLong_FromLong (l));
 #else
 #ifdef HAVE_LONG_LONG		/* Defined by Python.  */
   /* If we have 'long long', and the value overflows a 'long', use a
      Python Long; otherwise use a Python Int.  */
   if (sizeof (l) > sizeof (long)
       && (l > PyInt_GetMax () || l < (- (LONGEST) PyInt_GetMax ()) - 1))
-    return PyLong_FromLongLong (l);
+    return gdbpy_ref<> (PyLong_FromLongLong (l));
 #endif
-  return PyInt_FromLong (l);
+  return gdbpy_ref<> (PyInt_FromLong (l));
 #endif
 }
 
 /* Convert a ULONGEST to the appropriate Python object -- either an
    integer object or a long object, depending on its value.  */
 
-PyObject *
+gdbpy_ref<>
 gdb_py_object_from_ulongest (ULONGEST l)
 {
 #ifdef IS_PY3K
   if (sizeof (l) > sizeof (unsigned long))
-    return PyLong_FromUnsignedLongLong (l);
-  return PyLong_FromUnsignedLong (l);
+    return gdbpy_ref<> (PyLong_FromUnsignedLongLong (l));
+  return gdbpy_ref<> (PyLong_FromUnsignedLong (l));
 #else
 #ifdef HAVE_LONG_LONG		/* Defined by Python.  */
   /* If we have 'long long', and the value overflows a 'long', use a
      Python Long; otherwise use a Python Int.  */
   if (sizeof (l) > sizeof (unsigned long) && l > PyInt_GetMax ())
-    return PyLong_FromUnsignedLongLong (l);
+    return gdbpy_ref<> (PyLong_FromUnsignedLongLong (l));
 #endif
 
   if (l > PyInt_GetMax ())
-    return PyLong_FromUnsignedLong (l);
+    return gdbpy_ref<> (PyLong_FromUnsignedLong (l));
 
-  return PyInt_FromLong (l);
+  return gdbpy_ref<> (PyInt_FromLong (l));
 #endif
 }
 
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 26e91ff2b2..fe2adcc19c 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -1516,9 +1516,9 @@ valpy_int (PyObject *self)
   END_CATCH
 
   if (TYPE_UNSIGNED (type))
-    return gdb_py_object_from_ulongest (l);
+    return gdb_py_object_from_ulongest (l).release ();
   else
-    return gdb_py_object_from_longest (l);
+    return gdb_py_object_from_longest (l).release ();
 }
 #endif
 
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 1812abb5b7..5f15a33d5e 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -705,8 +705,8 @@ extern void gdbpy_convert_exception (struct gdb_exception)
 int get_addr_from_python (PyObject *obj, CORE_ADDR *addr)
     CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 
-PyObject *gdb_py_object_from_longest (LONGEST l);
-PyObject *gdb_py_object_from_ulongest (ULONGEST l);
+gdbpy_ref<> gdb_py_object_from_longest (LONGEST l);
+gdbpy_ref<> gdb_py_object_from_ulongest (ULONGEST l);
 int gdb_py_int_as_long (PyObject *, long *);
 
 PyObject *gdb_py_generic_dict (PyObject *self, void *closure);
-- 
2.17.1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/3] Return gdbpy_ref from some Python string functions
  2018-10-28 18:13 [PATCH 0/3] Use gdbpy_ref more in gdb/python Tom Tromey
  2018-10-28 18:13 ` [PATCH 1/3] Return gdbpy_ref from gdb_py_object_from_*longest Tom Tromey
@ 2018-10-28 18:13 ` Tom Tromey
  2018-10-28 18:13 ` [PATCH 3/3] Return gdbpy_ref from gdbpy_get_varobj_pretty_printer Tom Tromey
  2018-11-04 15:01 ` [PATCH 0/3] Use gdbpy_ref more in gdb/python Tom Tromey
  3 siblings, 0 replies; 5+ messages in thread
From: Tom Tromey @ 2018-10-28 18:13 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes python_string_to_unicode,
python_string_to_target_python_string, and
host_string_to_python_string to return gdbpy_ref.

gdb/ChangeLog
2018-10-28  Tom Tromey  <tom@tromey.com>

	* python/python.c (gdbpy_parameter_value): Update.
	* python/python-internal.h (python_string_to_unicode)
	(python_string_to_target_python_string)
	(host_string_to_python_string): Return gdbpy_ref.
	* python/py-utils.c (python_string_to_unicode)
	(unicode_to_encoded_python_string)
	(unicode_to_target_python_string)
	(python_string_to_target_string)
	(python_string_to_target_python_string): Return gdbpy_ref.
	(python_string_to_host_string): Update.
	(host_string_to_python_string): Return gdbpy_ref.
	* python/py-symtab.c (stpy_get_filename, stpy_get_producer)
	(stpy_fullname): Update.
	* python/py-progspace.c (pspy_get_filename, pspy_solib_name):
	Update.
	* python/py-prettyprint.c (print_string_repr): Update.
	* python/py-objfile.c (objfpy_get_filename, objfpy_get_username)
	(objfpy_get_build_id): Update.
	* python/py-breakpoint.c (bppy_get_location)
	(bppy_get_expression, bppy_get_condition, bppy_get_commands):
	Update.
---
 gdb/ChangeLog                | 24 ++++++++++++++++++++++++
 gdb/python/py-breakpoint.c   |  8 ++++----
 gdb/python/py-objfile.c      |  7 ++++---
 gdb/python/py-prettyprint.c  |  2 +-
 gdb/python/py-progspace.c    |  5 +++--
 gdb/python/py-symtab.c       |  6 +++---
 gdb/python/py-utils.c        | 25 +++++++++++++------------
 gdb/python/python-internal.h |  6 +++---
 gdb/python/python.c          |  2 +-
 9 files changed, 56 insertions(+), 29 deletions(-)

diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index 94afd503e9..d144bcf2e6 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -399,7 +399,7 @@ bppy_get_location (PyObject *self, void *closure)
   str = event_location_to_string (location);
   if (! str)
     str = "";
-  return host_string_to_python_string (str);
+  return host_string_to_python_string (str).release ();
 }
 
 /* Python function to get the breakpoint expression.  */
@@ -421,7 +421,7 @@ bppy_get_expression (PyObject *self, void *closure)
   if (! str)
     str = "";
 
-  return host_string_to_python_string (str);
+  return host_string_to_python_string (str).release ();
 }
 
 /* Python function to get the condition expression of a breakpoint.  */
@@ -437,7 +437,7 @@ bppy_get_condition (PyObject *self, void *closure)
   if (! str)
     Py_RETURN_NONE;
 
-  return host_string_to_python_string (str);
+  return host_string_to_python_string (str).release ();
 }
 
 /* Returns 0 on success.  Returns -1 on error, with a python exception set.
@@ -512,7 +512,7 @@ bppy_get_commands (PyObject *self, void *closure)
   END_CATCH
 
   current_uiout->redirect (NULL);
-  return host_string_to_python_string (stb.c_str ());
+  return host_string_to_python_string (stb.c_str ()).release ();
 }
 
 /* Set the commands attached to a breakpoint.  Returns 0 on success.
diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
index 2e24d0f3ff..dc7f342aef 100644
--- a/gdb/python/py-objfile.c
+++ b/gdb/python/py-objfile.c
@@ -79,7 +79,8 @@ objfpy_get_filename (PyObject *self, void *closure)
   objfile_object *obj = (objfile_object *) self;
 
   if (obj->objfile)
-    return host_string_to_python_string (objfile_name (obj->objfile));
+    return (host_string_to_python_string (objfile_name (obj->objfile))
+	    .release ());
   Py_RETURN_NONE;
 }
 
@@ -95,7 +96,7 @@ objfpy_get_username (PyObject *self, void *closure)
     {
       const char *username = obj->objfile->original_name;
 
-      return host_string_to_python_string (username);
+      return host_string_to_python_string (username).release ();
     }
 
   Py_RETURN_NONE;
@@ -145,7 +146,7 @@ objfpy_get_build_id (PyObject *self, void *closure)
       char *hex_form = make_hex_string (build_id->data, build_id->size);
       PyObject *result;
 
-      result = host_string_to_python_string (hex_form);
+      result = host_string_to_python_string (hex_form).release ();
       xfree (hex_form);
       return result;
     }
diff --git a/gdb/python/py-prettyprint.c b/gdb/python/py-prettyprint.c
index a706222581..2386dccbbb 100644
--- a/gdb/python/py-prettyprint.c
+++ b/gdb/python/py-prettyprint.c
@@ -316,7 +316,7 @@ print_string_repr (PyObject *printer, const char *hint,
       else
 	{
 	  gdbpy_ref<> string
-	    (python_string_to_target_python_string (py_str.get ()));
+	    = python_string_to_target_python_string (py_str.get ());
 	  if (string != NULL)
 	    {
 	      char *output;
diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c
index 6395d5bb15..bd195a54c1 100644
--- a/gdb/python/py-progspace.c
+++ b/gdb/python/py-progspace.c
@@ -83,7 +83,8 @@ pspy_get_filename (PyObject *self, void *closure)
       struct objfile *objfile = obj->pspace->symfile_object_file;
 
       if (objfile)
-	return host_string_to_python_string (objfile_name (objfile));
+	return (host_string_to_python_string (objfile_name (objfile))
+		.release ());
     }
   Py_RETURN_NONE;
 }
@@ -373,7 +374,7 @@ pspy_solib_name (PyObject *o, PyObject *args)
   soname = solib_name_from_address (self->pspace, pc);
   if (soname == nullptr)
     Py_RETURN_NONE;
-  return host_string_to_python_string (soname);
+  return host_string_to_python_string (soname).release ();
 }
 
 /* Return the innermost lexical block containing the specified pc value,
diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c
index 9bb20dab31..4b29299bf9 100644
--- a/gdb/python/py-symtab.c
+++ b/gdb/python/py-symtab.c
@@ -109,7 +109,7 @@ stpy_get_filename (PyObject *self, void *closure)
   STPY_REQUIRE_VALID (self, symtab);
   filename = symtab_to_filename_for_display (symtab);
 
-  str_obj = host_string_to_python_string (filename);
+  str_obj = host_string_to_python_string (filename).release ();
   return str_obj;
 }
 
@@ -137,7 +137,7 @@ stpy_get_producer (PyObject *self, void *closure)
     {
       const char *producer = COMPUNIT_PRODUCER (cust);
 
-      return host_string_to_python_string (producer);
+      return host_string_to_python_string (producer).release ();
     }
 
   Py_RETURN_NONE;
@@ -153,7 +153,7 @@ stpy_fullname (PyObject *self, PyObject *args)
 
   fullname = symtab_to_fullname (symtab);
 
-  return host_string_to_python_string (fullname);
+  return host_string_to_python_string (fullname).release ();
 }
 
 /* Implementation of gdb.Symtab.is_valid (self) -> Boolean.
diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c
index 4dcac3daaf..2b4779097b 100644
--- a/gdb/python/py-utils.c
+++ b/gdb/python/py-utils.c
@@ -34,7 +34,7 @@
 
    If the given object is not one of the mentioned string types, NULL is
    returned, with the TypeError python exception set.  */
-PyObject *
+gdbpy_ref<>
 python_string_to_unicode (PyObject *obj)
 {
   PyObject *unicode_str;
@@ -57,7 +57,7 @@ python_string_to_unicode (PyObject *obj)
       unicode_str = NULL;
     }
 
-  return unicode_str;
+  return gdbpy_ref<> (unicode_str);
 }
 
 /* Returns a newly allocated string with the contents of the given unicode
@@ -88,11 +88,11 @@ unicode_to_encoded_string (PyObject *unicode_str, const char *charset)
    object converted to a named charset.  If an error occurs during
    the conversion, NULL will be returned and a python exception will
    be set.  */
-static PyObject *
+static gdbpy_ref<>
 unicode_to_encoded_python_string (PyObject *unicode_str, const char *charset)
 {
   /* Translate string to named charset.  */
-  return PyUnicode_AsEncodedString (unicode_str, charset, NULL);
+  return gdbpy_ref<> (PyUnicode_AsEncodedString (unicode_str, charset, NULL));
 }
 
 /* Returns a newly allocated string with the contents of the given
@@ -110,7 +110,7 @@ unicode_to_target_string (PyObject *unicode_str)
    object converted to the target's charset.  If an error occurs
    during the conversion, NULL will be returned and a python exception
    will be set.  */
-static PyObject *
+static gdbpy_ref<>
 unicode_to_target_python_string (PyObject *unicode_str)
 {
   return unicode_to_encoded_python_string (unicode_str,
@@ -123,7 +123,7 @@ unicode_to_target_python_string (PyObject *unicode_str)
 gdb::unique_xmalloc_ptr<char>
 python_string_to_target_string (PyObject *obj)
 {
-  gdbpy_ref<> str (python_string_to_unicode (obj));
+  gdbpy_ref<> str = python_string_to_unicode (obj);
   if (str == NULL)
     return NULL;
 
@@ -135,12 +135,12 @@ python_string_to_target_string (PyObject *obj)
    set.
 
    In Python 3, the returned object is a "bytes" object (not a string).  */
-PyObject *
+gdbpy_ref<>
 python_string_to_target_python_string (PyObject *obj)
 {
-  gdbpy_ref<> str (python_string_to_unicode (obj));
+  gdbpy_ref<> str = python_string_to_unicode (obj);
   if (str == NULL)
-    return NULL;
+    return str;
 
   return unicode_to_target_python_string (str.get ());
 }
@@ -151,7 +151,7 @@ python_string_to_target_python_string (PyObject *obj)
 gdb::unique_xmalloc_ptr<char>
 python_string_to_host_string (PyObject *obj)
 {
-  gdbpy_ref<> str (python_string_to_unicode (obj));
+  gdbpy_ref<> str = python_string_to_unicode (obj);
   if (str == NULL)
     return NULL;
 
@@ -160,10 +160,11 @@ python_string_to_host_string (PyObject *obj)
 
 /* Convert a host string to a python string.  */
 
-PyObject *
+gdbpy_ref<>
 host_string_to_python_string (const char *str)
 {
-  return PyString_Decode (str, strlen (str), host_charset (), NULL);
+  return gdbpy_ref<> (PyString_Decode (str, strlen (str), host_charset (),
+				       NULL));
 }
 
 /* Return true if OBJ is a Python string or unicode object, false
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 5f15a33d5e..582044ad8c 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -656,12 +656,12 @@ int gdbpy_print_python_errors_p (void);
 void gdbpy_print_stack (void);
 void gdbpy_handle_exception () ATTRIBUTE_NORETURN;
 
-PyObject *python_string_to_unicode (PyObject *obj);
+gdbpy_ref<> python_string_to_unicode (PyObject *obj);
 gdb::unique_xmalloc_ptr<char> unicode_to_target_string (PyObject *unicode_str);
 gdb::unique_xmalloc_ptr<char> python_string_to_target_string (PyObject *obj);
-PyObject *python_string_to_target_python_string (PyObject *obj);
+gdbpy_ref<> python_string_to_target_python_string (PyObject *obj);
 gdb::unique_xmalloc_ptr<char> python_string_to_host_string (PyObject *obj);
-PyObject *host_string_to_python_string (const char *str);
+gdbpy_ref<> host_string_to_python_string (const char *str);
 int gdbpy_is_string (PyObject *obj);
 gdb::unique_xmalloc_ptr<char> gdbpy_obj_to_string (PyObject *obj);
 gdb::unique_xmalloc_ptr<char> gdbpy_exception_to_string (PyObject *ptype,
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 348405e205..a37ed10bcf 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -438,7 +438,7 @@ gdbpy_parameter_value (enum var_types type, void *var)
 
 	if (! str)
 	  str = "";
-	return host_string_to_python_string (str);
+	return host_string_to_python_string (str).release ();
       }
 
     case var_boolean:
-- 
2.17.1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 0/3] Use gdbpy_ref more in gdb/python
  2018-10-28 18:13 [PATCH 0/3] Use gdbpy_ref more in gdb/python Tom Tromey
                   ` (2 preceding siblings ...)
  2018-10-28 18:13 ` [PATCH 3/3] Return gdbpy_ref from gdbpy_get_varobj_pretty_printer Tom Tromey
@ 2018-11-04 15:01 ` Tom Tromey
  3 siblings, 0 replies; 5+ messages in thread
From: Tom Tromey @ 2018-11-04 15:01 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:

Tom> This changes a few more spots in the Python layer to return a
Tom> gdbpy_ref.  This is a small improvement because it makes the reference
Tom> ownership part of the type system rather than merely a convention.

Tom> Regression tested by the buildbot.

I'm checking this in.

Tom

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-11-04 15:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-28 18:13 [PATCH 0/3] Use gdbpy_ref more in gdb/python Tom Tromey
2018-10-28 18:13 ` [PATCH 1/3] Return gdbpy_ref from gdb_py_object_from_*longest Tom Tromey
2018-10-28 18:13 ` [PATCH 2/3] Return gdbpy_ref from some Python string functions Tom Tromey
2018-10-28 18:13 ` [PATCH 3/3] Return gdbpy_ref from gdbpy_get_varobj_pretty_printer Tom Tromey
2018-11-04 15:01 ` [PATCH 0/3] Use gdbpy_ref more in gdb/python Tom Tromey

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).