[RFA,v2,2/7] Turn gdbpy_ref into a template

Message ID 20170209212456.2385-3-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey Feb. 9, 2017, 9:24 p.m. UTC
  This turns gdbpy_ref into a template class, so that it can be used to
wrap subclasses of PyObject.  The default argument remains PyObject;
and this necessitated renaming uses of "gdbpy_ref" to "gdbpy_ref<>".

2017-02-09  Tom Tromey  <tom@tromey.com>

	* python/py-ref.h (gdbpy_ref_policy): Now a template.
	(gdbpy_ref): Now a template; allow subclasses of PyObject to be
	used.
	* python/py-arch.c, python/py-bpevent.c, python/py-breakpoint.c,
	python/py-cmd.c, python/py-continueevent.c, python/py-event.c,
	python/py-exitedevent.c, python/py-finishbreakpoint.c,
	python/py-framefilter.c, python/py-function.c,
	python/py-inferior.c, python/py-infevents.c,
	python/py-linetable.c, python/py-newobjfileevent.c,
	python/py-param.c, python/py-prettyprint.c, python/py-ref.h,
	python/py-signalevent.c, python/py-stopevent.c,
	python/py-symbol.c, python/py-threadevent.c, python/py-type.c,
	python/py-unwind.c, python/py-utils.c, python/py-value.c,
	python/py-varobj.c, python/py-xmethods.c, python/python.c,
	varobj.c: Change gdbpy_ref to gdbpy_ref<>.
---
 gdb/ChangeLog                    | 18 ++++++++++
 gdb/python/py-arch.c             |  4 +--
 gdb/python/py-bpevent.c          |  2 +-
 gdb/python/py-breakpoint.c       |  4 +--
 gdb/python/py-cmd.c              | 42 ++++++++++++------------
 gdb/python/py-continueevent.c    |  2 +-
 gdb/python/py-event.c            |  5 +--
 gdb/python/py-exitedevent.c      |  8 ++---
 gdb/python/py-finishbreakpoint.c |  4 +--
 gdb/python/py-framefilter.c      | 61 +++++++++++++++++-----------------
 gdb/python/py-function.c         | 14 ++++----
 gdb/python/py-inferior.c         |  6 ++--
 gdb/python/py-infevents.c        | 24 +++++++-------
 gdb/python/py-linetable.c        |  8 ++---
 gdb/python/py-newobjfileevent.c  |  8 ++---
 gdb/python/py-param.c            | 12 +++----
 gdb/python/py-prettyprint.c      | 55 ++++++++++++++++---------------
 gdb/python/py-ref.h              | 13 +++++---
 gdb/python/py-signalevent.c      |  4 +--
 gdb/python/py-stopevent.c        |  4 +--
 gdb/python/py-symbol.c           |  2 +-
 gdb/python/py-threadevent.c      |  2 +-
 gdb/python/py-type.c             | 18 +++++-----
 gdb/python/py-unwind.c           | 10 +++---
 gdb/python/py-utils.c            | 12 +++----
 gdb/python/py-value.c            | 12 +++----
 gdb/python/py-varobj.c           |  6 ++--
 gdb/python/py-xmethods.c         | 71 ++++++++++++++++++++--------------------
 gdb/python/python.c              | 51 +++++++++++++++--------------
 gdb/varobj.c                     | 12 +++----
 30 files changed, 260 insertions(+), 234 deletions(-)
  

Patch

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 4ec4dd5..43dc3ed 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,23 @@ 
 2017-02-09  Tom Tromey  <tom@tromey.com>
 
+	* python/py-ref.h (gdbpy_ref_policy): Now a template.
+	(gdbpy_ref): Now a template; allow subclasses of PyObject to be
+	used.
+	* python/py-arch.c, python/py-bpevent.c, python/py-breakpoint.c,
+	python/py-cmd.c, python/py-continueevent.c, python/py-event.c,
+	python/py-exitedevent.c, python/py-finishbreakpoint.c,
+	python/py-framefilter.c, python/py-function.c,
+	python/py-inferior.c, python/py-infevents.c,
+	python/py-linetable.c, python/py-newobjfileevent.c,
+	python/py-param.c, python/py-prettyprint.c, python/py-ref.h,
+	python/py-signalevent.c, python/py-stopevent.c,
+	python/py-symbol.c, python/py-threadevent.c, python/py-type.c,
+	python/py-unwind.c, python/py-utils.c, python/py-value.c,
+	python/py-varobj.c, python/py-xmethods.c, python/python.c,
+	varobj.c: Change gdbpy_ref to gdbpy_ref<>.
+
+2017-02-09  Tom Tromey  <tom@tromey.com>
+
 	* ui-out.h (ui_out_emit_type): New class.
 	(ui_out_emit_tuple, ui_out_emit_list): New typedefs.
 	* python/py-framefilter.c (py_print_single_arg): Use gdb::optional
diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c
index 8d0ec33..71d2989 100644
--- a/gdb/python/py-arch.c
+++ b/gdb/python/py-arch.c
@@ -178,7 +178,7 @@  archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
         }
     }
 
-  gdbpy_ref result_list (PyList_New (0));
+  gdbpy_ref<> result_list (PyList_New (0));
   if (result_list == NULL)
     return NULL;
 
@@ -193,7 +193,7 @@  archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
        || (end_obj == NULL && count_obj == NULL && pc == start);)
     {
       int insn_len = 0;
-      gdbpy_ref insn_dict (PyDict_New ());
+      gdbpy_ref<> insn_dict (PyDict_New ());
 
       if (insn_dict == NULL)
 	return NULL;
diff --git a/gdb/python/py-bpevent.c b/gdb/python/py-bpevent.c
index c9b53b5..c5a85b6 100644
--- a/gdb/python/py-bpevent.c
+++ b/gdb/python/py-bpevent.c
@@ -30,7 +30,7 @@  extern PyTypeObject breakpoint_event_object_type
 PyObject *
 create_breakpoint_event_object (PyObject *breakpoint_list, PyObject *first_bp)
 {
-  gdbpy_ref breakpoint_event_obj
+  gdbpy_ref<> breakpoint_event_obj
     (create_stop_event_object (&breakpoint_event_object_type));
 
   if (breakpoint_event_obj == NULL)
diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index ac64900..507b866 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -758,7 +758,7 @@  gdbpy_breakpoints (PyObject *self, PyObject *args)
   if (bppy_live == 0)
     return PyTuple_New (0);
 
-  gdbpy_ref list (PyList_New (0));
+  gdbpy_ref<> list (PyList_New (0));
   if (list == NULL)
     return NULL;
 
@@ -798,7 +798,7 @@  gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
 
   if (PyObject_HasAttrString (py_bp, stop_func))
     {
-      gdbpy_ref result (PyObject_CallMethod (py_bp, stop_func, NULL));
+      gdbpy_ref<> result (PyObject_CallMethod (py_bp, stop_func, NULL));
 
       stop = 1;
       if (result != NULL)
diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c
index 28c391c..cd2a9bb 100644
--- a/gdb/python/py-cmd.c
+++ b/gdb/python/py-cmd.c
@@ -134,19 +134,19 @@  cmdpy_function (struct cmd_list_element *command, char *args, int from_tty)
 
   if (! args)
     args = "";
-  gdbpy_ref argobj (PyUnicode_Decode (args, strlen (args), host_charset (),
-				      NULL));
+  gdbpy_ref<> argobj (PyUnicode_Decode (args, strlen (args), host_charset (),
+					NULL));
   if (argobj == NULL)
     {
       gdbpy_print_stack ();
       error (_("Could not convert arguments to Python string."));
     }
 
-  gdbpy_ref ttyobj (from_tty ? Py_True : Py_False);
+  gdbpy_ref<> ttyobj (from_tty ? Py_True : Py_False);
   Py_INCREF (ttyobj.get ());
-  gdbpy_ref result (PyObject_CallMethodObjArgs ((PyObject *) obj, invoke_cst,
-						argobj.get (), ttyobj.get (),
-						NULL));
+  gdbpy_ref<> result (PyObject_CallMethodObjArgs ((PyObject *) obj, invoke_cst,
+						  argobj.get (), ttyobj.get (),
+						  NULL));
 
   if (result == NULL)
     {
@@ -241,19 +241,19 @@  cmdpy_completer_helper (struct cmd_list_element *command,
       return NULL;
     }
 
-  gdbpy_ref textobj (PyUnicode_Decode (text, strlen (text), host_charset (),
-				       NULL));
+  gdbpy_ref<> textobj (PyUnicode_Decode (text, strlen (text), host_charset (),
+					 NULL));
   if (textobj == NULL)
     error (_("Could not convert argument to Python string."));
-  gdbpy_ref wordobj (PyUnicode_Decode (word, strlen (word), host_charset (),
-				       NULL));
+  gdbpy_ref<> wordobj (PyUnicode_Decode (word, strlen (word), host_charset (),
+					 NULL));
   if (wordobj == NULL)
     error (_("Could not convert argument to Python string."));
 
-  gdbpy_ref resultobj (PyObject_CallMethodObjArgs ((PyObject *) obj,
-						   complete_cst,
-						   textobj.get (),
-						   wordobj.get (), NULL));
+  gdbpy_ref<> resultobj (PyObject_CallMethodObjArgs ((PyObject *) obj,
+						     complete_cst,
+						     textobj.get (),
+						     wordobj.get (), NULL));
   if (resultobj == NULL)
     {
       /* Just swallow errors here.  */
@@ -276,7 +276,7 @@  cmdpy_completer_handle_brkchars (struct cmd_list_element *command,
 
   /* Calling our helper to obtain the PyObject of the Python
      function.  */
-  gdbpy_ref resultobj (cmdpy_completer_helper (command, text, word));
+  gdbpy_ref<> resultobj (cmdpy_completer_helper (command, text, word));
 
   /* Check if there was an error.  */
   if (resultobj == NULL)
@@ -317,7 +317,7 @@  cmdpy_completer (struct cmd_list_element *command,
 
   /* Calling our helper to obtain the PyObject of the Python
      function.  */
-  gdbpy_ref resultobj (cmdpy_completer_helper (command, text, word));
+  gdbpy_ref<> resultobj (cmdpy_completer_helper (command, text, word));
 
   /* If the result object of calling the Python function is NULL, it
      means that there was an error.  In this case, just give up and
@@ -342,14 +342,14 @@  cmdpy_completer (struct cmd_list_element *command,
     }
   else
     {
-      gdbpy_ref iter (PyObject_GetIter (resultobj.get ()));
+      gdbpy_ref<> iter (PyObject_GetIter (resultobj.get ()));
 
       if (iter == NULL)
 	return NULL;
 
       while (true)
 	{
-	  gdbpy_ref elt (PyIter_Next (iter.get ()));
+	  gdbpy_ref<> elt (PyIter_Next (iter.get ()));
 	  if (elt == NULL)
 	    break;
 
@@ -569,7 +569,7 @@  cmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
     }
   if (PyObject_HasAttr (self, gdbpy_doc_cst))
     {
-      gdbpy_ref ds_obj (PyObject_GetAttr (self, gdbpy_doc_cst));
+      gdbpy_ref<> ds_obj (PyObject_GetAttr (self, gdbpy_doc_cst));
 
       if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ()))
 	{
@@ -756,7 +756,7 @@  gdbpy_string_to_argv (PyObject *self, PyObject *args)
   if (!PyArg_ParseTuple (args, "s", &input))
     return NULL;
 
-  gdbpy_ref py_argv (PyList_New (0));
+  gdbpy_ref<> py_argv (PyList_New (0));
   if (py_argv == NULL)
     return NULL;
 
@@ -771,7 +771,7 @@  gdbpy_string_to_argv (PyObject *self, PyObject *args)
 
       for (i = 0; c_argv[i] != NULL; ++i)
 	{
-	  gdbpy_ref argp (PyString_FromString (c_argv[i]));
+	  gdbpy_ref<> argp (PyString_FromString (c_argv[i]));
 
 	  if (argp == NULL
 	      || PyList_Append (py_argv.get (), argp.get ()) < 0)
diff --git a/gdb/python/py-continueevent.c b/gdb/python/py-continueevent.c
index 84d2352..8be28c2 100644
--- a/gdb/python/py-continueevent.c
+++ b/gdb/python/py-continueevent.c
@@ -40,7 +40,7 @@  emit_continue_event (ptid_t ptid)
   if (evregpy_no_listeners_p (gdb_py_events.cont))
     return 0;
 
-  gdbpy_ref event (create_continue_event_object ());
+  gdbpy_ref<> event (create_continue_event_object ());
   if (event != NULL)
     return evpy_emit_event (event.get (), gdb_py_events.cont);
   return -1;
diff --git a/gdb/python/py-event.c b/gdb/python/py-event.c
index a50da1f..161a5e1 100644
--- a/gdb/python/py-event.c
+++ b/gdb/python/py-event.c
@@ -95,7 +95,7 @@  evpy_emit_event (PyObject *event,
      notifying listeners to avoid skipping callbacks
      in the case of a callback being disconnected during
      a notification.  */
-  gdbpy_ref callback_list_copy (PySequence_List (registry->callbacks));
+  gdbpy_ref<> callback_list_copy (PySequence_List (registry->callbacks));
   if (callback_list_copy == NULL)
     return -1;
 
@@ -106,7 +106,8 @@  evpy_emit_event (PyObject *event,
       if (func == NULL)
 	return -1;
 
-      gdbpy_ref func_result (PyObject_CallFunctionObjArgs (func, event, NULL));
+      gdbpy_ref<> func_result (PyObject_CallFunctionObjArgs (func, event,
+							     NULL));
 
       if (func_result == NULL)
 	{
diff --git a/gdb/python/py-exitedevent.c b/gdb/python/py-exitedevent.c
index 4590077..967fb1b 100644
--- a/gdb/python/py-exitedevent.c
+++ b/gdb/python/py-exitedevent.c
@@ -26,14 +26,14 @@  extern PyTypeObject exited_event_object_type
 static PyObject *
 create_exited_event_object (const LONGEST *exit_code, struct inferior *inf)
 {
-  gdbpy_ref exited_event (create_event_object (&exited_event_object_type));
+  gdbpy_ref<> exited_event (create_event_object (&exited_event_object_type));
 
   if (exited_event == NULL)
     return NULL;
 
   if (exit_code)
     {
-      gdbpy_ref exit_code_obj (PyLong_FromLongLong (*exit_code));
+      gdbpy_ref<> exit_code_obj (PyLong_FromLongLong (*exit_code));
 
       if (exit_code_obj == NULL)
 	return NULL;
@@ -42,7 +42,7 @@  create_exited_event_object (const LONGEST *exit_code, struct inferior *inf)
 	return NULL;
     }
 
-  gdbpy_ref inf_obj (inferior_to_inferior_object (inf));
+  gdbpy_ref<> inf_obj (inferior_to_inferior_object (inf));
   if (inf_obj == NULL || evpy_add_attribute (exited_event.get (),
 					     "inferior",
 					     inf_obj.get ()) < 0)
@@ -60,7 +60,7 @@  emit_exited_event (const LONGEST *exit_code, struct inferior *inf)
   if (evregpy_no_listeners_p (gdb_py_events.exited))
     return 0;
 
-  gdbpy_ref event (create_exited_event_object (exit_code, inf));
+  gdbpy_ref<> event (create_exited_event_object (exit_code, inf));
 
   if (event != NULL)
     return evpy_emit_event (event.get (), gdb_py_events.exited);
diff --git a/gdb/python/py-finishbreakpoint.c b/gdb/python/py-finishbreakpoint.c
index 7f30d86..106fe34 100644
--- a/gdb/python/py-finishbreakpoint.c
+++ b/gdb/python/py-finishbreakpoint.c
@@ -338,8 +338,8 @@  bpfinishpy_out_of_scope (struct finish_breakpoint_object *bpfinish_obj)
   if (bpfinish_obj->py_bp.bp->enable_state == bp_enabled
       && PyObject_HasAttrString (py_obj, outofscope_func))
     {
-      gdbpy_ref meth_result (PyObject_CallMethod (py_obj, outofscope_func,
-						  NULL));
+      gdbpy_ref<> meth_result (PyObject_CallMethod (py_obj, outofscope_func,
+						    NULL));
       if (meth_result == NULL)
 	gdbpy_print_stack ();
     }
diff --git a/gdb/python/py-framefilter.c b/gdb/python/py-framefilter.c
index a4a22a0..6db4aab 100644
--- a/gdb/python/py-framefilter.c
+++ b/gdb/python/py-framefilter.c
@@ -58,7 +58,7 @@  extract_sym (PyObject *obj, gdb::unique_xmalloc_ptr<char> *name,
 	     struct symbol **sym, struct block **sym_block,
 	     const struct language_defn **language)
 {
-  gdbpy_ref result (PyObject_CallMethod (obj, "symbol", NULL));
+  gdbpy_ref<> result (PyObject_CallMethod (obj, "symbol", NULL));
 
   if (result == NULL)
     return EXT_LANG_BT_ERROR;
@@ -129,7 +129,7 @@  extract_value (PyObject *obj, struct value **value)
 {
   if (PyObject_HasAttrString (obj, "value"))
     {
-      gdbpy_ref vresult (PyObject_CallMethod (obj, "value", NULL));
+      gdbpy_ref<> vresult (PyObject_CallMethod (obj, "value", NULL));
 
       if (vresult == NULL)
 	return EXT_LANG_BT_ERROR;
@@ -305,7 +305,7 @@  get_py_iter_from_func (PyObject *filter, char *func)
 {
   if (PyObject_HasAttrString (filter, func))
     {
-      gdbpy_ref result (PyObject_CallMethod (filter, func, NULL));
+      gdbpy_ref<> result (PyObject_CallMethod (filter, func, NULL));
 
       if (result != NULL)
 	{
@@ -508,7 +508,7 @@  enumerate_args (PyObject *iter,
       commas in the argument output is correct.  At the end of the
       loop block collect another item from the iterator, and, if it is
       not null emit a comma.  */
-  gdbpy_ref item (PyIter_Next (iter));
+  gdbpy_ref<> item (PyIter_Next (iter));
   if (item == NULL && PyErr_Occurred ())
     return EXT_LANG_BT_ERROR;
 
@@ -693,7 +693,7 @@  enumerate_locals (PyObject *iter,
       int local_indent = 8 + (8 * indent);
       gdb::optional<ui_out_emit_tuple> tuple;
 
-      gdbpy_ref item (PyIter_Next (iter));
+      gdbpy_ref<> item (PyIter_Next (iter));
       if (item == NULL)
 	break;
 
@@ -806,11 +806,11 @@  py_mi_print_variables (PyObject *filter, struct ui_out *out,
 		       enum ext_lang_frame_args args_type,
 		       struct frame_info *frame)
 {
-  gdbpy_ref args_iter (get_py_iter_from_func (filter, "frame_args"));
+  gdbpy_ref<> args_iter (get_py_iter_from_func (filter, "frame_args"));
   if (args_iter == NULL)
     return EXT_LANG_BT_ERROR;
 
-  gdbpy_ref locals_iter (get_py_iter_from_func (filter, "frame_locals"));
+  gdbpy_ref<> locals_iter (get_py_iter_from_func (filter, "frame_locals"));
   if (locals_iter == NULL)
     return EXT_LANG_BT_ERROR;
 
@@ -840,7 +840,7 @@  py_print_locals (PyObject *filter,
 		 int indent,
 		 struct frame_info *frame)
 {
-  gdbpy_ref locals_iter (get_py_iter_from_func (filter, "frame_locals"));
+  gdbpy_ref<> locals_iter (get_py_iter_from_func (filter, "frame_locals"));
   if (locals_iter == NULL)
     return EXT_LANG_BT_ERROR;
 
@@ -865,7 +865,7 @@  py_print_args (PyObject *filter,
 	       enum ext_lang_frame_args args_type,
 	       struct frame_info *frame)
 {
-  gdbpy_ref args_iter (get_py_iter_from_func (filter, "frame_args"));
+  gdbpy_ref<> args_iter (get_py_iter_from_func (filter, "frame_args"));
   if (args_iter == NULL)
     return EXT_LANG_BT_ERROR;
 
@@ -943,7 +943,8 @@  py_print_frame (PyObject *filter, int flags,
   /* Get the underlying frame.  This is needed to determine GDB
   architecture, and also, in the cases of frame variables/arguments to
   read them if they returned filter object requires us to do so.  */
-  gdbpy_ref py_inf_frame (PyObject_CallMethod (filter, "inferior_frame", NULL));
+  gdbpy_ref<> py_inf_frame (PyObject_CallMethod (filter, "inferior_frame",
+						 NULL));
   if (py_inf_frame == NULL)
     return EXT_LANG_BT_ERROR;
 
@@ -1000,7 +1001,7 @@  py_print_frame (PyObject *filter, int flags,
 	 address printing.  */
       if (PyObject_HasAttrString (filter, "address"))
 	{
-	  gdbpy_ref paddr (PyObject_CallMethod (filter, "address", NULL));
+	  gdbpy_ref<> paddr (PyObject_CallMethod (filter, "address", NULL));
 
 	  if (paddr == NULL)
 	    return EXT_LANG_BT_ERROR;
@@ -1076,7 +1077,7 @@  py_print_frame (PyObject *filter, int flags,
       /* Print frame function name.  */
       if (PyObject_HasAttrString (filter, "function"))
 	{
-	  gdbpy_ref py_func (PyObject_CallMethod (filter, "function", NULL));
+	  gdbpy_ref<> py_func (PyObject_CallMethod (filter, "function", NULL));
 	  const char *function = NULL;
 
 	  if (py_func == NULL)
@@ -1153,7 +1154,7 @@  py_print_frame (PyObject *filter, int flags,
 
       if (PyObject_HasAttrString (filter, "filename"))
 	{
-	  gdbpy_ref py_fn (PyObject_CallMethod (filter, "filename", NULL));
+	  gdbpy_ref<> py_fn (PyObject_CallMethod (filter, "filename", NULL));
 
 	  if (py_fn == NULL)
 	    return EXT_LANG_BT_ERROR;
@@ -1185,7 +1186,7 @@  py_print_frame (PyObject *filter, int flags,
 
       if (PyObject_HasAttrString (filter, "line"))
 	{
-	  gdbpy_ref py_line (PyObject_CallMethod (filter, "line", NULL));
+	  gdbpy_ref<> py_line (PyObject_CallMethod (filter, "line", NULL));
 	  int line;
 
 	  if (py_line == NULL)
@@ -1239,7 +1240,7 @@  py_print_frame (PyObject *filter, int flags,
 
   {
     /* Finally recursively print elided frames, if any.  */
-    gdbpy_ref elided (get_py_iter_from_func (filter, "elided"));
+    gdbpy_ref<> elided (get_py_iter_from_func (filter, "elided"));
     if (elided == NULL)
       return EXT_LANG_BT_ERROR;
 
@@ -1254,7 +1255,7 @@  py_print_frame (PyObject *filter, int flags,
 
 	while ((item = PyIter_Next (elided.get ())))
 	  {
-	    gdbpy_ref item_ref (item);
+	    gdbpy_ref<> item_ref (item);
 
 	    enum ext_lang_bt_status success = py_print_frame (item, flags,
 							      args_type, out,
@@ -1279,32 +1280,32 @@  static PyObject *
 bootstrap_python_frame_filters (struct frame_info *frame,
 				int frame_low, int frame_high)
 {
-  gdbpy_ref frame_obj (frame_info_to_frame_object (frame));
+  gdbpy_ref<> frame_obj (frame_info_to_frame_object (frame));
   if (frame_obj == NULL)
     return NULL;
 
-  gdbpy_ref module (PyImport_ImportModule ("gdb.frames"));
+  gdbpy_ref<> module (PyImport_ImportModule ("gdb.frames"));
   if (module == NULL)
     return NULL;
 
-  gdbpy_ref sort_func (PyObject_GetAttrString (module.get (),
-					       "execute_frame_filters"));
+  gdbpy_ref<> sort_func (PyObject_GetAttrString (module.get (),
+						 "execute_frame_filters"));
   if (sort_func == NULL)
     return NULL;
 
-  gdbpy_ref py_frame_low (PyInt_FromLong (frame_low));
+  gdbpy_ref<> py_frame_low (PyInt_FromLong (frame_low));
   if (py_frame_low == NULL)
     return NULL;
 
-  gdbpy_ref py_frame_high (PyInt_FromLong (frame_high));
+  gdbpy_ref<> py_frame_high (PyInt_FromLong (frame_high));
   if (py_frame_high == NULL)
     return NULL;
 
-  gdbpy_ref iterable (PyObject_CallFunctionObjArgs (sort_func.get (),
-						    frame_obj.get (),
-						    py_frame_low.get (),
-						    py_frame_high.get (),
-						    NULL));
+  gdbpy_ref<> iterable (PyObject_CallFunctionObjArgs (sort_func.get (),
+						      frame_obj.get (),
+						      py_frame_low.get (),
+						      py_frame_high.get (),
+						      NULL));
   if (iterable == NULL)
     return NULL;
 
@@ -1354,8 +1355,8 @@  gdbpy_apply_frame_filter (const struct extension_language_defn *extlang,
 
   gdbpy_enter enter_py (gdbarch, current_language);
 
-  gdbpy_ref iterable (bootstrap_python_frame_filters (frame, frame_low,
-						      frame_high));
+  gdbpy_ref<> iterable (bootstrap_python_frame_filters (frame, frame_low,
+							frame_high));
 
   if (iterable == NULL)
     {
@@ -1389,7 +1390,7 @@  gdbpy_apply_frame_filter (const struct extension_language_defn *extlang,
 
   while (true)
     {
-      gdbpy_ref item (PyIter_Next (iterable.get ()));
+      gdbpy_ref<> item (PyIter_Next (iterable.get ()));
 
       if (item == NULL)
 	{
diff --git a/gdb/python/py-function.c b/gdb/python/py-function.c
index 6762a6d..0b2a934 100644
--- a/gdb/python/py-function.c
+++ b/gdb/python/py-function.c
@@ -38,14 +38,14 @@  static PyObject *
 convert_values_to_python (int argc, struct value **argv)
 {
   int i;
-  gdbpy_ref result (PyTuple_New (argc));
+  gdbpy_ref<> result (PyTuple_New (argc));
 
   if (result == NULL)
     return NULL;
 
   for (i = 0; i < argc; ++i)
     {
-      gdbpy_ref elt (value_to_value_object (argv[i]));
+      gdbpy_ref<> elt (value_to_value_object (argv[i]));
       if (elt == NULL)
 	return NULL;
       PyTuple_SetItem (result.get (), i, elt.release ());
@@ -63,8 +63,8 @@  fnpy_call (struct gdbarch *gdbarch, const struct language_defn *language,
      be destroyed.  */
   gdbpy_enter enter_py (gdbarch, language);
   struct value *value;
-  gdbpy_ref result;
-  gdbpy_ref args (convert_values_to_python (argc, argv));
+  gdbpy_ref<> result;
+  gdbpy_ref<> args (convert_values_to_python (argc, argv));
 
   /* convert_values_to_python can return NULL on error.  If we
      encounter this, do not call the function, but allow the Python ->
@@ -74,8 +74,8 @@  fnpy_call (struct gdbarch *gdbarch, const struct language_defn *language,
 
   if (args != NULL)
     {
-      gdbpy_ref callable (PyObject_GetAttrString ((PyObject *) cookie,
-						  "invoke"));
+      gdbpy_ref<> callable (PyObject_GetAttrString ((PyObject *) cookie,
+						    "invoke"));
       if (callable == NULL)
 	error (_("No method named 'invoke' in object."));
 
@@ -159,7 +159,7 @@  fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
 
   if (PyObject_HasAttrString (self, "__doc__"))
     {
-      gdbpy_ref ds_obj (PyObject_GetAttrString (self, "__doc__"));
+      gdbpy_ref<> ds_obj (PyObject_GetAttrString (self, "__doc__"));
       if (ds_obj != NULL)
 	{
 	  if (gdbpy_is_string (ds_obj.get ()))
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index b2aaf25..0118caa 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -256,7 +256,7 @@  find_thread_object (ptid_t ptid)
   if (pid == 0)
     return NULL;
 
-  gdbpy_ref inf_obj (find_inferior_object (pid));
+  gdbpy_ref<> inf_obj (find_inferior_object (pid));
   if (inf_obj == NULL)
     return NULL;
 
@@ -405,7 +405,7 @@  static int
 build_inferior_list (struct inferior *inf, void *arg)
 {
   PyObject *list = (PyObject *) arg;
-  gdbpy_ref inferior (inferior_to_inferior_object (inf));
+  gdbpy_ref<> inferior (inferior_to_inferior_object (inf));
 
   if (inferior  == NULL)
     return 0;
@@ -418,7 +418,7 @@  build_inferior_list (struct inferior *inf, void *arg)
 PyObject *
 gdbpy_inferiors (PyObject *unused, PyObject *unused2)
 {
-  gdbpy_ref list (PyList_New (0));
+  gdbpy_ref<> list (PyList_New (0));
   if (list == NULL)
     return NULL;
 
diff --git a/gdb/python/py-infevents.c b/gdb/python/py-infevents.c
index 23bf57b..6e3c8c8 100644
--- a/gdb/python/py-infevents.c
+++ b/gdb/python/py-infevents.c
@@ -37,7 +37,7 @@  static PyObject *
 create_inferior_call_event_object (inferior_call_kind flag, ptid_t ptid,
 				   CORE_ADDR addr)
 {
-  gdbpy_ref event;
+  gdbpy_ref<> event;
   int failed;
 
   switch (flag)
@@ -52,14 +52,14 @@  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;
 
   if (evpy_add_attribute (event.get (), "ptid", ptid_obj.get ()) < 0)
     return NULL;
 
-  gdbpy_ref addr_obj (PyLong_FromLongLong (addr));
+  gdbpy_ref<> addr_obj (PyLong_FromLongLong (addr));
   if (addr_obj == NULL)
     return NULL;
 
@@ -76,18 +76,18 @@  static PyObject *
 create_register_changed_event_object (struct frame_info *frame, 
 				      int regnum)
 {
-  gdbpy_ref event (create_event_object (&register_changed_event_object_type));
+  gdbpy_ref<> event (create_event_object (&register_changed_event_object_type));
   if (event == NULL)
     return NULL;
 
-  gdbpy_ref frame_obj (frame_info_to_frame_object (frame));
+  gdbpy_ref<> frame_obj (frame_info_to_frame_object (frame));
   if (frame_obj == NULL)
     return NULL;
 
   if (evpy_add_attribute (event.get (), "frame", frame_obj.get ()) < 0)
     return NULL;
 
-  gdbpy_ref regnum_obj (PyLong_FromLongLong (regnum));
+  gdbpy_ref<> regnum_obj (PyLong_FromLongLong (regnum));
   if (regnum_obj == NULL)
     return NULL;
 
@@ -103,19 +103,19 @@  create_register_changed_event_object (struct frame_info *frame,
 static PyObject *
 create_memory_changed_event_object (CORE_ADDR addr, ssize_t len)
 {
-  gdbpy_ref event (create_event_object (&memory_changed_event_object_type));
+  gdbpy_ref<> event (create_event_object (&memory_changed_event_object_type));
 
   if (event == NULL)
     return NULL;
 
-  gdbpy_ref addr_obj (PyLong_FromLongLong (addr));
+  gdbpy_ref<> addr_obj (PyLong_FromLongLong (addr));
   if (addr_obj == NULL)
     return NULL;
 
   if (evpy_add_attribute (event.get (), "address", addr_obj.get ()) < 0)
     return NULL;
 
-  gdbpy_ref len_obj (PyLong_FromLong (len));
+  gdbpy_ref<> len_obj (PyLong_FromLong (len));
   if (len_obj == NULL)
     return NULL;
 
@@ -137,7 +137,7 @@  emit_inferior_call_event (inferior_call_kind flag, ptid_t thread,
   if (evregpy_no_listeners_p (gdb_py_events.inferior_call))
     return 0;
 
-  gdbpy_ref event (create_inferior_call_event_object (flag, thread, addr));
+  gdbpy_ref<> event (create_inferior_call_event_object (flag, thread, addr));
   if (event != NULL)
     return evpy_emit_event (event.get (), gdb_py_events.inferior_call);
   return -1;
@@ -152,7 +152,7 @@  emit_memory_changed_event (CORE_ADDR addr, ssize_t len)
   if (evregpy_no_listeners_p (gdb_py_events.memory_changed))
     return 0;
 
-  gdbpy_ref event (create_memory_changed_event_object (addr, len));
+  gdbpy_ref<> event (create_memory_changed_event_object (addr, len));
   if (event != NULL)
     return evpy_emit_event (event.get (), gdb_py_events.memory_changed);
   return -1;
@@ -167,7 +167,7 @@  emit_register_changed_event (struct frame_info* frame, int regnum)
   if (evregpy_no_listeners_p (gdb_py_events.register_changed))
     return 0;
 
-  gdbpy_ref event (create_register_changed_event_object (frame, regnum));
+  gdbpy_ref<> event (create_register_changed_event_object (frame, regnum));
   if (event != NULL)
     return evpy_emit_event (event.get (), gdb_py_events.register_changed);
   return -1;
diff --git a/gdb/python/py-linetable.c b/gdb/python/py-linetable.c
index 1f73ff7..d8597e4 100644
--- a/gdb/python/py-linetable.c
+++ b/gdb/python/py-linetable.c
@@ -132,14 +132,14 @@  build_line_table_tuple_from_pcs (int line, VEC (CORE_ADDR) *vec)
   if (vec_len < 1)
     Py_RETURN_NONE;
 
-  gdbpy_ref tuple (PyTuple_New (vec_len));
+  gdbpy_ref<> tuple (PyTuple_New (vec_len));
 
   if (tuple == NULL)
     return NULL;
 
   for (i = 0; VEC_iterate (CORE_ADDR, vec, i, pc); ++i)
     {
-      gdbpy_ref obj (build_linetable_entry (line, pc));
+      gdbpy_ref<> obj (build_linetable_entry (line, pc));
 
       if (obj == NULL)
 	return NULL;
@@ -238,7 +238,7 @@  ltpy_get_all_source_lines (PyObject *self, PyObject *args)
       return NULL;
     }
 
-  gdbpy_ref source_dict (PyDict_New ());
+  gdbpy_ref<> source_dict (PyDict_New ());
   if (source_dict == NULL)
     return NULL;
 
@@ -250,7 +250,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;
diff --git a/gdb/python/py-newobjfileevent.c b/gdb/python/py-newobjfileevent.c
index b8f9774..dc09e0f 100644
--- a/gdb/python/py-newobjfileevent.c
+++ b/gdb/python/py-newobjfileevent.c
@@ -30,7 +30,7 @@  create_new_objfile_event_object (struct objfile *objfile)
 {
   PyObject *py_objfile;
 
-  gdbpy_ref objfile_event
+  gdbpy_ref<> objfile_event
     (create_event_object (&new_objfile_event_object_type));
   if (objfile_event == NULL)
     return NULL;
@@ -56,7 +56,7 @@  emit_new_objfile_event (struct objfile *objfile)
   if (evregpy_no_listeners_p (gdb_py_events.new_objfile))
     return 0;
 
-  gdbpy_ref event (create_new_objfile_event_object (objfile));
+  gdbpy_ref<> event (create_new_objfile_event_object (objfile));
   if (event != NULL)
     return evpy_emit_event (event.get (), gdb_py_events.new_objfile);
   return -1;
@@ -75,7 +75,7 @@  create_clear_objfiles_event_object (void)
 {
   PyObject *py_progspace;
 
-  gdbpy_ref objfile_event
+  gdbpy_ref<> objfile_event
     (create_event_object (&clear_objfiles_event_object_type));
   if (objfile_event == NULL)
     return NULL;
@@ -102,7 +102,7 @@  emit_clear_objfiles_event (void)
   if (evregpy_no_listeners_p (gdb_py_events.clear_objfiles))
     return 0;
 
-  gdbpy_ref event (create_clear_objfiles_event_object ());
+  gdbpy_ref<> event (create_clear_objfiles_event_object ());
   if (event != NULL)
     return evpy_emit_event (event.get (), gdb_py_events.clear_objfiles);
   return -1;
diff --git a/gdb/python/py-param.c b/gdb/python/py-param.c
index c285bb5..d80b731 100644
--- a/gdb/python/py-param.c
+++ b/gdb/python/py-param.c
@@ -307,7 +307,7 @@  get_doc_string (PyObject *object, PyObject *attr)
 
   if (PyObject_HasAttr (object, attr))
     {
-      gdbpy_ref ds_obj (PyObject_GetAttr (object, attr));
+      gdbpy_ref<> ds_obj (PyObject_GetAttr (object, attr));
 
       if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ()))
 	{
@@ -329,7 +329,7 @@  static gdb::unique_xmalloc_ptr<char>
 call_doc_function (PyObject *obj, PyObject *method, PyObject *arg)
 {
   gdb::unique_xmalloc_ptr<char> data;
-  gdbpy_ref result (PyObject_CallMethodObjArgs (obj, method, arg, NULL));
+  gdbpy_ref<> result (PyObject_CallMethodObjArgs (obj, method, arg, NULL));
 
   if (result == NULL)
     return NULL;
@@ -364,7 +364,7 @@  get_set_value (char *args, int from_tty,
   gdb::unique_xmalloc_ptr<char> set_doc_string;
 
   gdbpy_enter enter_py (get_current_arch (), current_language);
-  gdbpy_ref set_doc_func (PyString_FromString ("get_set_string"));
+  gdbpy_ref<> set_doc_func (PyString_FromString ("get_set_string"));
 
   if (set_doc_func == NULL)
     {
@@ -407,7 +407,7 @@  get_show_value (struct ui_file *file, int from_tty,
   gdb::unique_xmalloc_ptr<char> show_doc_string;
 
   gdbpy_enter enter_py (get_current_arch (), current_language);
-  gdbpy_ref show_doc_func (PyString_FromString ("get_show_string"));
+  gdbpy_ref<> show_doc_func (PyString_FromString ("get_show_string"));
 
   if (show_doc_func == NULL)
     {
@@ -417,7 +417,7 @@  get_show_value (struct ui_file *file, int from_tty,
 
   if (PyObject_HasAttr (obj, show_doc_func.get ()))
     {
-      gdbpy_ref val_obj (PyString_FromString (value));
+      gdbpy_ref<> val_obj (PyString_FromString (value));
 
       if (val_obj == NULL)
 	{
@@ -586,7 +586,7 @@  compute_enum_values (parmpy_object *self, PyObject *enum_values)
 
   for (i = 0; i < size; ++i)
     {
-      gdbpy_ref item (PySequence_GetItem (enum_values, i));
+      gdbpy_ref<> item (PySequence_GetItem (enum_values, i));
 
       if (item == NULL)
 	{
diff --git a/gdb/python/py-prettyprint.c b/gdb/python/py-prettyprint.c
index d6d157a..e0d2110 100644
--- a/gdb/python/py-prettyprint.c
+++ b/gdb/python/py-prettyprint.c
@@ -60,7 +60,7 @@  search_pp_list (PyObject *list, PyObject *value)
       /* Skip if disabled.  */
       if (PyObject_HasAttr (function, gdbpy_enabled_cst))
 	{
-	  gdbpy_ref attr (PyObject_GetAttr (function, gdbpy_enabled_cst));
+	  gdbpy_ref<> attr (PyObject_GetAttr (function, gdbpy_enabled_cst));
 	  int cmp;
 
 	  if (attr == NULL)
@@ -73,7 +73,8 @@  search_pp_list (PyObject *list, PyObject *value)
 	    continue;
 	}
 
-      gdbpy_ref printer (PyObject_CallFunctionObjArgs (function, value, NULL));
+      gdbpy_ref<> printer (PyObject_CallFunctionObjArgs (function, value,
+							 NULL));
       if (printer == NULL)
 	return NULL;
       else if (printer != Py_None)
@@ -104,8 +105,8 @@  find_pretty_printer_from_objfiles (PyObject *value)
 	continue;
       }
 
-    gdbpy_ref pp_list (objfpy_get_printers (objf, NULL));
-    gdbpy_ref function (search_pp_list (pp_list.get (), value));
+    gdbpy_ref<> pp_list (objfpy_get_printers (objf, 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.  */
     if (function == NULL)
@@ -131,7 +132,7 @@  find_pretty_printer_from_progspace (PyObject *value)
 
   if (!obj)
     return NULL;
-  gdbpy_ref pp_list (pspy_get_printers (obj, NULL));
+  gdbpy_ref<> pp_list (pspy_get_printers (obj, NULL));
   return search_pp_list (pp_list.get (), value);
 }
 
@@ -148,8 +149,8 @@  find_pretty_printer_from_gdb (PyObject *value)
   if (gdb_python_module == NULL
       || ! PyObject_HasAttrString (gdb_python_module, "pretty_printers"))
     Py_RETURN_NONE;
-  gdbpy_ref pp_list (PyObject_GetAttrString (gdb_python_module,
-					     "pretty_printers"));
+  gdbpy_ref<> pp_list (PyObject_GetAttrString (gdb_python_module,
+					       "pretty_printers"));
   if (pp_list == NULL || ! PyList_Check (pp_list.get ()))
     Py_RETURN_NONE;
 
@@ -165,7 +166,7 @@  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));
+  gdbpy_ref<> function (find_pretty_printer_from_objfiles (value));
   if (function == NULL || function != Py_None)
     return function.release ();
 
@@ -228,8 +229,8 @@  gdbpy_get_display_hint (PyObject *printer)
   if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst))
     return NULL;
 
-  gdbpy_ref hint (PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst,
-					      NULL));
+  gdbpy_ref<> hint (PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst,
+						NULL));
   if (hint != NULL)
     {
       if (gdbpy_is_string (hint.get ()))
@@ -256,9 +257,9 @@  print_stack_unless_memory_error (struct ui_file *stream)
 
       PyErr_Fetch (&type, &value, &trace);
 
-      gdbpy_ref type_ref (type);
-      gdbpy_ref value_ref (value);
-      gdbpy_ref trace_ref (trace);
+      gdbpy_ref<> type_ref (type);
+      gdbpy_ref<> value_ref (value);
+      gdbpy_ref<> trace_ref (trace);
 
       gdb::unique_xmalloc_ptr<char>
 	msg (gdbpy_exception_to_string (type, value));
@@ -286,7 +287,7 @@  print_string_repr (PyObject *printer, const char *hint,
   struct value *replacement = NULL;
   enum string_repr_result result = string_repr_ok;
 
-  gdbpy_ref py_str (pretty_print_one_value (printer, &replacement));
+  gdbpy_ref<> py_str (pretty_print_one_value (printer, &replacement));
   if (py_str != NULL)
     {
       if (py_str == Py_None)
@@ -308,7 +309,7 @@  print_string_repr (PyObject *printer, const char *hint,
 	}
       else
 	{
-	  gdbpy_ref string
+	  gdbpy_ref<> string
 	    (python_string_to_target_python_string (py_str.get ()));
 	  if (string != NULL)
 	    {
@@ -379,7 +380,7 @@  class dummy_python_frame
 
   bool m_valid;
   PyFrameObject *m_saved_frame;
-  gdbpy_ref m_frame;
+  gdbpy_ref<> m_frame;
   PyThreadState *m_tstate;
 };
 
@@ -391,11 +392,11 @@  dummy_python_frame::dummy_python_frame ()
   PyCodeObject *code;
   PyFrameObject *frame;
 
-  gdbpy_ref empty_string (PyString_FromString (""));
+  gdbpy_ref<> empty_string (PyString_FromString (""));
   if (empty_string == NULL)
     return;
 
-  gdbpy_ref null_tuple (PyTuple_New (0));
+  gdbpy_ref<> null_tuple (PyTuple_New (0));
   if (null_tuple == NULL)
     return;
 
@@ -418,9 +419,9 @@  dummy_python_frame::dummy_python_frame ()
 		     );
   if (code == NULL)
     return;
-  gdbpy_ref code_holder ((PyObject *) code);
+  gdbpy_ref<> code_holder ((PyObject *) code);
 
-  gdbpy_ref globals (PyDict_New ());
+  gdbpy_ref<> globals (PyDict_New ());
   if (globals == NULL)
     return;
 
@@ -457,15 +458,15 @@  print_children (PyObject *printer, const char *hint,
   is_map = hint && ! strcmp (hint, "map");
   is_array = hint && ! strcmp (hint, "array");
 
-  gdbpy_ref children (PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
-						  NULL));
+  gdbpy_ref<> children (PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
+						    NULL));
   if (children == NULL)
     {
       print_stack_unless_memory_error (stream);
       return;
     }
 
-  gdbpy_ref iter (PyObject_GetIter (children.get ()));
+  gdbpy_ref<> iter (PyObject_GetIter (children.get ()));
   if (iter == NULL)
     {
       print_stack_unless_memory_error (stream);
@@ -502,7 +503,7 @@  print_children (PyObject *printer, const char *hint,
       PyObject *py_v;
       const char *name;
 
-      gdbpy_ref item (PyIter_Next (iter.get ()));
+      gdbpy_ref<> item (PyIter_Next (iter.get ()));
       if (item == NULL)
 	{
 	  if (PyErr_Occurred ())
@@ -674,7 +675,7 @@  gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang,
   /* Instantiate the printer.  */
   value = value_from_component (val, type, embedded_offset);
 
-  gdbpy_ref val_obj (value_to_value_object (value));
+  gdbpy_ref<> val_obj (value_to_value_object (value));
   if (val_obj == NULL)
     {
       print_stack_unless_memory_error (stream);
@@ -682,7 +683,7 @@  gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang,
     }
 
   /* Find the constructor.  */
-  gdbpy_ref printer (find_pretty_printer (val_obj.get ()));
+  gdbpy_ref<> printer (find_pretty_printer (val_obj.get ()));
   if (printer == NULL)
     {
       print_stack_unless_memory_error (stream);
@@ -750,7 +751,7 @@  gdbpy_get_varobj_pretty_printer (struct value *value)
     }
   END_CATCH
 
-  gdbpy_ref val_obj (value_to_value_object (value));
+  gdbpy_ref<> val_obj (value_to_value_object (value));
   if (val_obj == NULL)
     return NULL;
 
diff --git a/gdb/python/py-ref.h b/gdb/python/py-ref.h
index b2479bf..8568e5c 100644
--- a/gdb/python/py-ref.h
+++ b/gdb/python/py-ref.h
@@ -1,6 +1,6 @@ 
 /* Python reference-holding class
 
-   Copyright (C) 2016 Free Software Foundation, Inc.
+   Copyright (C) 2016-2017 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -23,20 +23,23 @@ 
 #include "common/gdb_ref_ptr.h"
 
 /* A policy class for gdb::ref_ptr for Python reference counting.  */
+template<typename T>
 struct gdbpy_ref_policy
 {
-  static void incref (PyObject *ptr)
+  static void incref (T *ptr)
   {
     Py_INCREF (ptr);
   }
 
-  static void decref (PyObject *ptr)
+  static void decref (T *ptr)
   {
     Py_DECREF (ptr);
   }
 };
 
-/* A gdb::ref_ptr that has been specialized for Python objects.  */
-typedef gdb::ref_ptr<PyObject, gdbpy_ref_policy> gdbpy_ref;
+/* A gdb::ref_ptr that has been specialized for Python objects or
+   their "subclasses".  */
+template<typename T = PyObject> using gdbpy_ref
+  = gdb::ref_ptr<T, gdbpy_ref_policy<T>>;
 
 #endif /* GDB_PYTHON_REF_H */
diff --git a/gdb/python/py-signalevent.c b/gdb/python/py-signalevent.c
index e4e9768..a8f41c2 100644
--- a/gdb/python/py-signalevent.c
+++ b/gdb/python/py-signalevent.c
@@ -27,7 +27,7 @@  PyObject *
 create_signal_event_object (enum gdb_signal stop_signal)
 {
   const char *signal_name;
-  gdbpy_ref signal_event_obj
+  gdbpy_ref<> signal_event_obj
     (create_stop_event_object (&signal_event_object_type));
 
   if (signal_event_obj == NULL)
@@ -35,7 +35,7 @@  create_signal_event_object (enum gdb_signal stop_signal)
 
   signal_name = gdb_signal_to_name (stop_signal);
 
-  gdbpy_ref signal_name_obj (PyString_FromString (signal_name));
+  gdbpy_ref<> signal_name_obj (PyString_FromString (signal_name));
   if (signal_name_obj == NULL)
     return NULL;
   if (evpy_add_attribute (signal_event_obj.get (),
diff --git a/gdb/python/py-stopevent.c b/gdb/python/py-stopevent.c
index b87ebda..cfa4591 100644
--- a/gdb/python/py-stopevent.c
+++ b/gdb/python/py-stopevent.c
@@ -36,8 +36,8 @@  create_stop_event_object (PyTypeObject *py_type)
 int
 emit_stop_event (struct bpstats *bs, enum gdb_signal stop_signal)
 {
-  gdbpy_ref stop_event_obj;
-  gdbpy_ref list;
+  gdbpy_ref<> stop_event_obj;
+  gdbpy_ref<> list;
   PyObject *first_bp = NULL;
   struct bpstats *current_bs;
 
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index 34e953f..b71cfb4 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -411,7 +411,7 @@  gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
     }
   END_CATCH
 
-  gdbpy_ref ret_tuple (PyTuple_New (2));
+  gdbpy_ref<> ret_tuple (PyTuple_New (2));
   if (ret_tuple == NULL)
     return NULL;
 
diff --git a/gdb/python/py-threadevent.c b/gdb/python/py-threadevent.c
index bba974b..9217444 100644
--- a/gdb/python/py-threadevent.c
+++ b/gdb/python/py-threadevent.c
@@ -52,7 +52,7 @@  create_thread_event_object (PyTypeObject *py_type)
 {
   PyObject *thread = NULL;
 
-  gdbpy_ref thread_event_obj (create_event_object (py_type));
+  gdbpy_ref<> thread_event_obj (create_event_object (py_type));
   if (thread_event_obj == NULL)
     return NULL;
 
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index f6e8dd6..857c4e2 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -169,12 +169,12 @@  typy_get_code (PyObject *self, void *closure)
 static PyObject *
 convert_field (struct type *type, int field)
 {
-  gdbpy_ref result (field_new ());
+  gdbpy_ref<> result (field_new ());
 
   if (result == NULL)
     return NULL;
 
-  gdbpy_ref arg (type_to_type_object (type));
+  gdbpy_ref<> arg (type_to_type_object (type));
   if (arg == NULL)
     return NULL;
   if (PyObject_SetAttrString (result.get (), "parent_type", arg.get ()) < 0)
@@ -292,13 +292,13 @@  make_fielditem (struct type *type, int i, enum gdbpy_iter_kind kind)
     {
     case iter_items:
       {
-	gdbpy_ref key (field_name (type, i));
+	gdbpy_ref<> key (field_name (type, i));
 	if (key == NULL)
 	  return NULL;
-	gdbpy_ref value (convert_field (type, i));
+	gdbpy_ref<> value (convert_field (type, i));
 	if (value == NULL)
 	  return NULL;
-	gdbpy_ref item (PyTuple_New (2));
+	gdbpy_ref<> item (PyTuple_New (2));
 	if (item == NULL)
 	  return NULL;
 	PyTuple_SET_ITEM (item.get (), 0, key.release ());
@@ -376,7 +376,7 @@  typy_fields (PyObject *self, PyObject *args)
   /* Array type.  Handle this as a special case because the common
      machinery wants struct or union or enum types.  Build a list of
      one entry which is the range for the array.  */
-  gdbpy_ref r (convert_field (type, 0));
+  gdbpy_ref<> r (convert_field (type, 0));
   if (r == NULL)
     return NULL;
 
@@ -602,15 +602,15 @@  typy_range (PyObject *self, PyObject *args)
       break;
     }
 
-  gdbpy_ref low_bound (PyLong_FromLong (low));
+  gdbpy_ref<> low_bound (PyLong_FromLong (low));
   if (low_bound == NULL)
     return NULL;
 
-  gdbpy_ref high_bound (PyLong_FromLong (high));
+  gdbpy_ref<> high_bound (PyLong_FromLong (high));
   if (high_bound == NULL)
     return NULL;
 
-  gdbpy_ref result (PyTuple_New (2));
+  gdbpy_ref<> result (PyTuple_New (2));
   if (result == NULL)
     return NULL;
 
diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
index d1b63ad..b7821b5 100644
--- a/gdb/python/py-unwind.c
+++ b/gdb/python/py-unwind.c
@@ -177,7 +177,7 @@  pyuw_object_attribute_to_pointer (PyObject *pyo, const char *attr_name,
 
   if (PyObject_HasAttrString (pyo, attr_name))
     {
-      gdbpy_ref pyo_value (PyObject_GetAttrString (pyo, attr_name));
+      gdbpy_ref<> pyo_value (PyObject_GetAttrString (pyo, attr_name));
 
       if (pyo_value != NULL && pyo_value != Py_None)
         {
@@ -515,7 +515,7 @@  pyuw_sniffer (const struct frame_unwind *self, struct frame_info *this_frame,
   /* Create PendingFrame instance to pass to sniffers.  */
   pending_frame_object *pfo = PyObject_New (pending_frame_object,
 					    &pending_frame_object_type);
-  gdbpy_ref pyo_pending_frame ((PyObject *) pfo);
+  gdbpy_ref<> pyo_pending_frame ((PyObject *) pfo);
   if (pyo_pending_frame == NULL)
     {
       gdbpy_print_stack ();
@@ -535,15 +535,15 @@  pyuw_sniffer (const struct frame_unwind *self, struct frame_info *this_frame,
       gdbpy_print_stack ();
       return 0;
     }
-  gdbpy_ref pyo_execute (PyObject_GetAttrString (gdb_python_module,
-						 "execute_unwinders"));
+  gdbpy_ref<> pyo_execute (PyObject_GetAttrString (gdb_python_module,
+						   "execute_unwinders"));
   if (pyo_execute == NULL)
     {
       gdbpy_print_stack ();
       return 0;
     }
 
-  gdbpy_ref pyo_unwind_info
+  gdbpy_ref<> pyo_unwind_info
     (PyObject_CallFunctionObjArgs (pyo_execute.get (),
 				   pyo_pending_frame.get (), NULL));
   if (pyo_unwind_info == NULL)
diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c
index e26de41..890220a 100644
--- a/gdb/python/py-utils.c
+++ b/gdb/python/py-utils.c
@@ -71,7 +71,7 @@  unicode_to_encoded_string (PyObject *unicode_str, const char *charset)
   gdb::unique_xmalloc_ptr<char> result;
 
   /* Translate string to named charset.  */
-  gdbpy_ref string (PyUnicode_AsEncodedString (unicode_str, charset, NULL));
+  gdbpy_ref<> string (PyUnicode_AsEncodedString (unicode_str, charset, NULL));
   if (string == NULL)
     return NULL;
 
@@ -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;
 
@@ -138,7 +138,7 @@  python_string_to_target_string (PyObject *obj)
 PyObject *
 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;
 
@@ -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;
 
@@ -185,7 +185,7 @@  gdbpy_is_string (PyObject *obj)
 gdb::unique_xmalloc_ptr<char>
 gdbpy_obj_to_string (PyObject *obj)
 {
-  gdbpy_ref str_obj (PyObject_Str (obj));
+  gdbpy_ref<> str_obj (PyObject_Str (obj));
 
   if (str_obj != NULL)
     {
@@ -269,7 +269,7 @@  get_addr_from_python (PyObject *obj, CORE_ADDR *addr)
     }
   else
     {
-      gdbpy_ref num (PyNumber_Long (obj));
+      gdbpy_ref<> num (PyNumber_Long (obj));
       gdb_py_ulongest val;
 
       if (num == NULL)
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 8e4d06d..b9b012b 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -566,7 +566,7 @@  value_has_field (struct value *v, PyObject *field)
 {
   struct type *parent_type, *val_type;
   enum type_code type_code;
-  gdbpy_ref type_object (PyObject_GetAttrString (field, "parent_type"));
+  gdbpy_ref<> type_object (PyObject_GetAttrString (field, "parent_type"));
   int has_field = 0;
 
   if (type_object == NULL)
@@ -612,7 +612,7 @@  value_has_field (struct value *v, PyObject *field)
 static int
 get_field_flag (PyObject *field, const char *flag_name)
 {
-  gdbpy_ref flag_object (PyObject_GetAttrString (field, flag_name));
+  gdbpy_ref<> flag_object (PyObject_GetAttrString (field, flag_name));
 
   if (flag_object == NULL)
     return -1;
@@ -626,7 +626,7 @@  get_field_flag (PyObject *field, const char *flag_name)
 static struct type *
 get_field_type (PyObject *field)
 {
-  gdbpy_ref ftype_obj (PyObject_GetAttrString (field, "type"));
+  gdbpy_ref<> ftype_obj (PyObject_GetAttrString (field, "type"));
   struct type *ftype;
 
   if (ftype_obj == NULL)
@@ -687,7 +687,7 @@  valpy_getitem (PyObject *self, PyObject *key)
 	}
       else
 	{
-	  gdbpy_ref name_obj (PyObject_GetAttrString (key, "name"));
+	  gdbpy_ref<> name_obj (PyObject_GetAttrString (key, "name"));
 
 	  if (name_obj == NULL)
 	    return NULL;
@@ -708,7 +708,7 @@  valpy_getitem (PyObject *self, PyObject *key)
 
 		  return NULL;
 		}
-	      gdbpy_ref bitpos_obj (PyObject_GetAttrString (key, "bitpos"));
+	      gdbpy_ref<> bitpos_obj (PyObject_GetAttrString (key, "bitpos"));
 	      if (bitpos_obj == NULL)
 		return NULL;
 	      if (!gdb_py_int_as_long (bitpos_obj.get (), &bitpos))
@@ -1576,7 +1576,7 @@  convert_value_from_python (PyObject *obj)
 		  PyObject *etype, *evalue, *etraceback;
 
 		  PyErr_Fetch (&etype, &evalue, &etraceback);
-		  gdbpy_ref zero (PyInt_FromLong (0));
+		  gdbpy_ref<> zero (PyInt_FromLong (0));
 
 		  /* Check whether obj is positive.  */
 		  if (PyObject_RichCompareBool (obj, zero.get (), Py_GT) > 0)
diff --git a/gdb/python/py-varobj.c b/gdb/python/py-varobj.c
index bac226f..e858556 100644
--- a/gdb/python/py-varobj.c
+++ b/gdb/python/py-varobj.c
@@ -59,7 +59,7 @@  py_varobj_iter_next (struct varobj_iter *self)
 
   gdbpy_enter_varobj enter_py (self->var);
 
-  gdbpy_ref item (PyIter_Next (t->iter));
+  gdbpy_ref<> item (PyIter_Next (t->iter));
 
   if (item == NULL)
     {
@@ -170,8 +170,8 @@  py_varobj_get_iterator (struct varobj *var, PyObject *printer)
   if (!PyObject_HasAttr (printer, gdbpy_children_cst))
     return NULL;
 
-  gdbpy_ref children (PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
-						  NULL));
+  gdbpy_ref<> children (PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
+						    NULL));
   if (children == NULL)
     {
       gdbpy_print_stack ();
diff --git a/gdb/python/py-xmethods.c b/gdb/python/py-xmethods.c
index 6505d06..d293c71 100644
--- a/gdb/python/py-xmethods.c
+++ b/gdb/python/py-xmethods.c
@@ -96,8 +96,8 @@  invoke_match_method (PyObject *matcher, PyObject *py_obj_type,
 {
   int enabled;
 
-  gdbpy_ref enabled_field (PyObject_GetAttrString (matcher,
-						   enabled_field_name));
+  gdbpy_ref<> enabled_field (PyObject_GetAttrString (matcher,
+						     enabled_field_name));
   if (enabled_field == NULL)
     return NULL;
 
@@ -110,11 +110,12 @@  invoke_match_method (PyObject *matcher, PyObject *py_obj_type,
       Py_RETURN_NONE;
     }
 
-  gdbpy_ref match_method (PyObject_GetAttrString (matcher, match_method_name));
+  gdbpy_ref<> match_method (PyObject_GetAttrString (matcher,
+						    match_method_name));
   if (match_method == NULL)
     return NULL;
 
-  gdbpy_ref py_xmethod_name (PyString_FromString (xmethod_name));
+  gdbpy_ref<> py_xmethod_name (PyString_FromString (xmethod_name));
   if (py_xmethod_name == NULL)
     return NULL;
 
@@ -139,7 +140,7 @@  gdbpy_get_matching_xmethod_workers
 
   gdbpy_enter enter_py (get_current_arch (), current_language);
 
-  gdbpy_ref py_type (type_to_type_object (obj_type));
+  gdbpy_ref<> py_type (type_to_type_object (obj_type));
   if (py_type == NULL)
     {
       gdbpy_print_stack ();
@@ -147,7 +148,7 @@  gdbpy_get_matching_xmethod_workers
     }
 
   /* Create an empty list of debug methods.  */
-  gdbpy_ref py_xmethod_matcher_list (PyList_New (0));
+  gdbpy_ref<> py_xmethod_matcher_list (PyList_New (0));
   if (py_xmethod_matcher_list == NULL)
     {
       gdbpy_print_stack ();
@@ -167,9 +168,9 @@  gdbpy_get_matching_xmethod_workers
 	  return EXT_LANG_RC_ERROR;
 	}
 
-      gdbpy_ref objfile_matchers (objfpy_get_xmethods (py_objfile, NULL));
-      gdbpy_ref temp (PySequence_Concat (py_xmethod_matcher_list.get (),
-					 objfile_matchers.get ()));
+      gdbpy_ref<> objfile_matchers (objfpy_get_xmethods (py_objfile, NULL));
+      gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
+					   objfile_matchers.get ()));
       if (temp == NULL)
 	{
 	  gdbpy_print_stack ();
@@ -184,10 +185,10 @@  gdbpy_get_matching_xmethod_workers
   py_progspace = pspace_to_pspace_object (current_program_space);
   if (py_progspace != NULL)
     {
-      gdbpy_ref pspace_matchers (pspy_get_xmethods (py_progspace, NULL));
+      gdbpy_ref<> pspace_matchers (pspy_get_xmethods (py_progspace, NULL));
 
-      gdbpy_ref temp (PySequence_Concat (py_xmethod_matcher_list.get (),
-					 pspace_matchers.get ()));
+      gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
+					   pspace_matchers.get ()));
       if (temp == NULL)
 	{
 	  gdbpy_print_stack ();
@@ -206,12 +207,12 @@  gdbpy_get_matching_xmethod_workers
   if (gdb_python_module != NULL
       && PyObject_HasAttrString (gdb_python_module, matchers_attr_str))
     {
-      gdbpy_ref gdb_matchers (PyObject_GetAttrString (gdb_python_module,
-						      matchers_attr_str));
+      gdbpy_ref<> gdb_matchers (PyObject_GetAttrString (gdb_python_module,
+							matchers_attr_str));
       if (gdb_matchers != NULL)
 	{
-	  gdbpy_ref temp (PySequence_Concat (py_xmethod_matcher_list.get (),
-					     gdb_matchers.get ()));
+	  gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
+					       gdb_matchers.get ()));
 	  if (temp == NULL)
 	    {
 	      gdbpy_print_stack ();
@@ -227,7 +228,7 @@  gdbpy_get_matching_xmethod_workers
 	}
     }
 
-  gdbpy_ref list_iter (PyObject_GetIter (py_xmethod_matcher_list.get ()));
+  gdbpy_ref<> list_iter (PyObject_GetIter (py_xmethod_matcher_list.get ()));
   if (list_iter == NULL)
     {
       gdbpy_print_stack ();
@@ -235,7 +236,7 @@  gdbpy_get_matching_xmethod_workers
     }
   while (true)
     {
-      gdbpy_ref matcher (PyIter_Next (list_iter.get ()));
+      gdbpy_ref<> matcher (PyIter_Next (list_iter.get ()));
       if (matcher == NULL)
 	{
 	  if (PyErr_Occurred ())
@@ -246,9 +247,9 @@  gdbpy_get_matching_xmethod_workers
 	  break;
 	}
 
-      gdbpy_ref match_result (invoke_match_method (matcher.get (),
-						   py_type.get (),
-						   method_name));
+      gdbpy_ref<> match_result (invoke_match_method (matcher.get (),
+						     py_type.get (),
+						     method_name));
 
       if (match_result == NULL)
 	{
@@ -259,7 +260,7 @@  gdbpy_get_matching_xmethod_workers
 	; /* This means there was no match.  */
       else if (PySequence_Check (match_result.get ()))
 	{
-	  gdbpy_ref iter (PyObject_GetIter (match_result.get ()));
+	  gdbpy_ref<> iter (PyObject_GetIter (match_result.get ()));
 
 	  if (iter == NULL)
 	    {
@@ -270,7 +271,7 @@  gdbpy_get_matching_xmethod_workers
 	    {
 	      struct xmethod_worker *worker;
 
-	      gdbpy_ref py_worker (PyIter_Next (iter.get ()));
+	      gdbpy_ref<> py_worker (PyIter_Next (iter.get ()));
 	      if (py_worker == NULL)
 		{
 		  if (PyErr_Occurred ())
@@ -316,13 +317,13 @@  gdbpy_get_xmethod_arg_types (const struct extension_language_defn *extlang,
   PyObject *py_worker = worker_data->worker;
   struct type *obj_type;
   int i = 1, arg_count;
-  gdbpy_ref list_iter;
+  gdbpy_ref<> list_iter;
 
   /* Set nargs to -1 so that any premature return from this function returns
      an invalid/unusable number of arg types.  */
   *nargs = -1;
 
-  gdbpy_ref get_arg_types_method
+  gdbpy_ref<> get_arg_types_method
     (PyObject_GetAttrString (py_worker, get_arg_types_method_name));
   if (get_arg_types_method == NULL)
     {
@@ -330,7 +331,7 @@  gdbpy_get_xmethod_arg_types (const struct extension_language_defn *extlang,
       return EXT_LANG_RC_ERROR;
     }
 
-  gdbpy_ref py_argtype_list
+  gdbpy_ref<> py_argtype_list
     (PyObject_CallMethodObjArgs (py_worker, py_get_arg_types_method_name,
 				 NULL));
   if (py_argtype_list == NULL)
@@ -368,7 +369,7 @@  gdbpy_get_xmethod_arg_types (const struct extension_language_defn *extlang,
     {
       while (true)
 	{
-	  gdbpy_ref item (PyIter_Next (list_iter.get ()));
+	  gdbpy_ref<> item (PyIter_Next (list_iter.get ()));
 	  if (item == NULL)
 	    {
 	      if (PyErr_Occurred ())
@@ -445,7 +446,7 @@  gdbpy_get_xmethod_result_type (const struct extension_language_defn *extlang,
 
   /* First see if there is a get_result_type method.
      If not this could be an old xmethod (pre 7.9.1).  */
-  gdbpy_ref get_result_type_method
+  gdbpy_ref<> get_result_type_method
     (PyObject_GetAttrString (py_worker, get_result_type_method_name));
   if (get_result_type_method == NULL)
     {
@@ -475,14 +476,14 @@  gdbpy_get_xmethod_result_type (const struct extension_language_defn *extlang,
       if (!types_equal (obj_type, this_type))
 	obj = value_cast (this_type, obj);
     }
-  gdbpy_ref py_value_obj (value_to_value_object (obj));
+  gdbpy_ref<> py_value_obj (value_to_value_object (obj));
   if (py_value_obj == NULL)
     {
       gdbpy_print_stack ();
       return EXT_LANG_RC_ERROR;
     }
 
-  gdbpy_ref py_arg_tuple (PyTuple_New (nargs + 1));
+  gdbpy_ref<> py_arg_tuple (PyTuple_New (nargs + 1));
   if (py_arg_tuple == NULL)
     {
       gdbpy_print_stack ();
@@ -505,7 +506,7 @@  gdbpy_get_xmethod_result_type (const struct extension_language_defn *extlang,
       PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg);
     }
 
-  gdbpy_ref py_result_type
+  gdbpy_ref<> py_result_type
     (PyObject_CallObject (get_result_type_method.get (), py_arg_tuple.get ()));
   if (py_result_type == NULL)
     {
@@ -563,14 +564,14 @@  gdbpy_invoke_xmethod (const struct extension_language_defn *extlang,
       if (!types_equal (obj_type, this_type))
 	obj = value_cast (this_type, obj);
     }
-  gdbpy_ref py_value_obj (value_to_value_object (obj));
+  gdbpy_ref<> py_value_obj (value_to_value_object (obj));
   if (py_value_obj == NULL)
     {
       gdbpy_print_stack ();
       error (_("Error while executing Python code."));
     }
 
-  gdbpy_ref py_arg_tuple (PyTuple_New (nargs + 1));
+  gdbpy_ref<> py_arg_tuple (PyTuple_New (nargs + 1));
   if (py_arg_tuple == NULL)
     {
       gdbpy_print_stack ();
@@ -594,8 +595,8 @@  gdbpy_invoke_xmethod (const struct extension_language_defn *extlang,
       PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg);
     }
 
-  gdbpy_ref py_result (PyObject_CallObject (xmethod_worker,
-					    py_arg_tuple.get ()));
+  gdbpy_ref<> py_result (PyObject_CallObject (xmethod_worker,
+					      py_arg_tuple.get ()));
   if (py_result == NULL)
     {
       gdbpy_print_stack ();
diff --git a/gdb/python/python.c b/gdb/python/python.c
index ab5a6a4..74a6f42 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -273,7 +273,7 @@  eval_python_command (const char *command)
   d = PyModule_GetDict (m);
   if (d == NULL)
     return -1;
-  gdbpy_ref v (PyRun_StringFlags (command, Py_single_input, d, d, NULL));
+  gdbpy_ref<> v (PyRun_StringFlags (command, Py_single_input, d, d, NULL));
   if (v == NULL)
     return -1;
 
@@ -351,7 +351,7 @@  python_run_simple_file (FILE *file, const char *filename)
   /* Because we have a string for a filename, and are using Python to
      open the file, we need to expand any tilde in the path first.  */
   gdb::unique_xmalloc_ptr<char> full_path (tilde_expand (filename));
-  gdbpy_ref python_file (PyFile_FromString (full_path.get (), "r"));
+  gdbpy_ref<> python_file (PyFile_FromString (full_path.get (), "r"));
   if (python_file == NULL)
     {
       gdbpy_print_stack ();
@@ -669,8 +669,8 @@  gdbpy_decode_line (PyObject *self, PyObject *args)
   struct symtab_and_line sal;
   char *arg = NULL;
   struct cleanup *cleanups;
-  gdbpy_ref result;
-  gdbpy_ref unparsed;
+  gdbpy_ref<> result;
+  gdbpy_ref<> unparsed;
   struct event_location *location = NULL;
 
   if (! PyArg_ParseTuple (args, "|s", &arg))
@@ -745,7 +745,7 @@  gdbpy_decode_line (PyObject *self, PyObject *args)
       Py_INCREF (Py_None);
     }
 
-  gdbpy_ref return_result (PyTuple_New (2));
+  gdbpy_ref<> return_result (PyTuple_New (2));
   if (return_result == NULL)
     {
       do_cleanups (cleanups);
@@ -897,7 +897,7 @@  gdbpy_run_events (int error, gdb_client_data client_data)
 	gdbpy_event_list_end = &gdbpy_event_list;
 
       /* Ignore errors.  */
-      gdbpy_ref call_result (PyObject_CallObject (item->event, NULL));
+      gdbpy_ref<> call_result (PyObject_CallObject (item->event, NULL));
       if (call_result == NULL)
 	PyErr_Clear ();
 
@@ -972,8 +972,8 @@  gdbpy_before_prompt_hook (const struct extension_language_defn *extlang,
   if (gdb_python_module
       && PyObject_HasAttrString (gdb_python_module, "prompt_hook"))
     {
-      gdbpy_ref hook (PyObject_GetAttrString (gdb_python_module,
-					      "prompt_hook"));
+      gdbpy_ref<> hook (PyObject_GetAttrString (gdb_python_module,
+						"prompt_hook"));
       if (hook == NULL)
 	{
 	  gdbpy_print_stack ();
@@ -982,16 +982,16 @@  gdbpy_before_prompt_hook (const struct extension_language_defn *extlang,
 
       if (PyCallable_Check (hook.get ()))
 	{
-	  gdbpy_ref current_prompt (PyString_FromString (current_gdb_prompt));
+	  gdbpy_ref<> current_prompt (PyString_FromString (current_gdb_prompt));
 	  if (current_prompt == NULL)
 	    {
 	      gdbpy_print_stack ();
 	      return EXT_LANG_RC_ERROR;
 	    }
 
-	  gdbpy_ref result (PyObject_CallFunctionObjArgs (hook.get (),
-							  current_prompt.get (),
-							  NULL));
+	  gdbpy_ref<> result
+	    (PyObject_CallFunctionObjArgs (hook.get (), current_prompt.get (),
+					   NULL));
 	  if (result == NULL)
 	    {
 	      gdbpy_print_stack ();
@@ -1206,7 +1206,7 @@  gdbpy_progspaces (PyObject *unused1, PyObject *unused2)
 {
   struct program_space *ps;
 
-  gdbpy_ref list (PyList_New (0));
+  gdbpy_ref<> list (PyList_New (0));
   if (list == NULL)
     return NULL;
 
@@ -1296,7 +1296,7 @@  gdbpy_objfiles (PyObject *unused1, PyObject *unused2)
 {
   struct objfile *objf;
 
-  gdbpy_ref list (PyList_New (0));
+  gdbpy_ref<> list (PyList_New (0));
   if (list == NULL)
     return NULL;
 
@@ -1327,15 +1327,15 @@  gdbpy_start_type_printers (const struct extension_language_defn *extlang,
 
   gdbpy_enter enter_py (get_current_arch (), current_language);
 
-  gdbpy_ref type_module (PyImport_ImportModule ("gdb.types"));
+  gdbpy_ref<> type_module (PyImport_ImportModule ("gdb.types"));
   if (type_module == NULL)
     {
       gdbpy_print_stack ();
       return;
     }
 
-  gdbpy_ref func (PyObject_GetAttrString (type_module.get (),
-					  "get_type_recognizers"));
+  gdbpy_ref<> func (PyObject_GetAttrString (type_module.get (),
+					    "get_type_recognizers"));
   if (func == NULL)
     {
       gdbpy_print_stack ();
@@ -1372,31 +1372,32 @@  gdbpy_apply_type_printers (const struct extension_language_defn *extlang,
 
   gdbpy_enter enter_py (get_current_arch (), current_language);
 
-  gdbpy_ref type_obj (type_to_type_object (type));
+  gdbpy_ref<> type_obj (type_to_type_object (type));
   if (type_obj == NULL)
     {
       gdbpy_print_stack ();
       return EXT_LANG_RC_ERROR;
     }
 
-  gdbpy_ref type_module (PyImport_ImportModule ("gdb.types"));
+  gdbpy_ref<> type_module (PyImport_ImportModule ("gdb.types"));
   if (type_module == NULL)
     {
       gdbpy_print_stack ();
       return EXT_LANG_RC_ERROR;
     }
 
-  gdbpy_ref func (PyObject_GetAttrString (type_module.get (),
-					  "apply_type_recognizers"));
+  gdbpy_ref<> func (PyObject_GetAttrString (type_module.get (),
+					    "apply_type_recognizers"));
   if (func == NULL)
     {
       gdbpy_print_stack ();
       return EXT_LANG_RC_ERROR;
     }
 
-  gdbpy_ref result_obj (PyObject_CallFunctionObjArgs (func.get (), printers_obj,
-						      type_obj.get (),
-						      (char *) NULL));
+  gdbpy_ref<> result_obj (PyObject_CallFunctionObjArgs (func.get (),
+							printers_obj,
+							type_obj.get (),
+							(char *) NULL));
   if (result_obj == NULL)
     {
       gdbpy_print_stack ();
@@ -1798,7 +1799,7 @@  do_finish_initialization (const struct extension_language_defn *extlang)
     }
   if (sys_path && PyList_Check (sys_path))
     {
-      gdbpy_ref pythondir (PyString_FromString (gdb_pythondir.c_str ()));
+      gdbpy_ref<> pythondir (PyString_FromString (gdb_pythondir.c_str ()));
       if (pythondir == NULL || PyList_Insert (sys_path, 0, pythondir.get ()))
 	return false;
     }
diff --git a/gdb/varobj.c b/gdb/varobj.c
index 3b2771f5..4b12826 100644
--- a/gdb/varobj.c
+++ b/gdb/varobj.c
@@ -1478,11 +1478,11 @@  varobj_set_visualizer (struct varobj *var, const char *visualizer)
   gdbpy_enter_varobj enter_py (var);
 
   mainmod = PyImport_AddModule ("__main__");
-  gdbpy_ref globals (PyModule_GetDict (mainmod));
+  gdbpy_ref<> globals (PyModule_GetDict (mainmod));
   Py_INCREF (globals.get ());
 
-  gdbpy_ref constructor (PyRun_String (visualizer, Py_eval_input,
-				       globals.get (), globals.get ()));
+  gdbpy_ref<> constructor (PyRun_String (visualizer, Py_eval_input,
+					 globals.get (), globals.get ()));
 
   if (constructor == NULL)
     {
@@ -2432,9 +2432,9 @@  varobj_value_get_print_value (struct value *value,
 	    {
 	      struct value *replacement;
 
-	      gdbpy_ref output (apply_varobj_pretty_printer (value_formatter,
-							     &replacement,
-							     &stb));
+	      gdbpy_ref<> output (apply_varobj_pretty_printer (value_formatter,
+							       &replacement,
+							       &stb));
 
 	      /* If we have string like output ...  */
 	      if (output != NULL)