+2017-02-10  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-10  Tom Tromey  <tom@tromey.com>
 
        * ui-out.h (ui_out_emit_type): New class.
 
         }
     }
 
-  gdbpy_ref result_list (PyList_New (0));
+  gdbpy_ref<> result_list (PyList_New (0));
   if (result_list == NULL)
     return NULL;
 
        || (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;
 
 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)
 
   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;
 
 
   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)
 
 
   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)
     {
       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.  */
 
   /* 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)
 
   /* 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
     }
   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;
 
     }
   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 ()))
        {
   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;
 
 
       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)
 
   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;
 
      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;
 
       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)
        {
 
 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;
        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)
   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);
 
   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 ();
     }
 
             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;
 {
   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;
 {
   if (PyObject_HasAttrString (filter, func))
     {
-      gdbpy_ref result (PyObject_CallMethod (filter, func, NULL));
+      gdbpy_ref<> result (PyObject_CallMethod (filter, func, NULL));
 
       if (result != NULL)
        {
       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;
 
       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;
 
                       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;
 
                 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;
 
               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;
 
   /* 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;
 
         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;
       /* 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)
 
       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;
 
       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)
 
   {
     /* 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;
 
 
        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,
 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;
 
 
   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)
     {
 
   while (true)
     {
-      gdbpy_ref item (PyIter_Next (iterable.get ()));
+      gdbpy_ref<> item (PyIter_Next (iterable.get ()));
 
       if (item == NULL)
        {
 
 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 ());
      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 ->
 
   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."));
 
 
   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 ()))
 
   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;
 
 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;
 PyObject *
 gdbpy_inferiors (PyObject *unused, PyObject *unused2)
 {
-  gdbpy_ref list (PyList_New (0));
+  gdbpy_ref<> list (PyList_New (0));
   if (list == NULL)
     return NULL;
 
 
 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)
       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;
 
 create_register_changed_event_object (struct frame_info *frame, 
                                      int regnum)
 {
-  gdbpy_ref event (create_event_object (®ister_changed_event_object_type));
+  gdbpy_ref<> event (create_event_object (®ister_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;
 
 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;
 
   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;
   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;
   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;
 
   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;
       return NULL;
     }
 
-  gdbpy_ref source_dict (PyDict_New ());
+  gdbpy_ref<> source_dict (PyDict_New ());
   if (source_dict == NULL)
     return NULL;
 
         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;
 
 {
   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;
   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;
 {
   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;
   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;
 
 
   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 ()))
        {
 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;
   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)
     {
   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)
     {
 
   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)
        {
 
   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)
        {
 
       /* 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)
            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)
        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)
 
   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);
 }
 
   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;
 
 {
   /* 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 ();
 
   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 ()))
 
       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));
   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)
        }
       else
        {
-         gdbpy_ref string
+         gdbpy_ref<> string
            (python_string_to_target_python_string (py_str.get ()));
          if (string != NULL)
            {
 
   bool m_valid;
   PyFrameObject *m_saved_frame;
-  gdbpy_ref m_frame;
+  gdbpy_ref<> m_frame;
   PyThreadState *m_tstate;
 };
 
   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;
 
                     );
   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;
 
   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);
       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 ())
   /* 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);
     }
 
   /* 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);
     }
   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;
 
 
 /* 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.
 
 #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 */
 
 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)
 
   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 (),
 
 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;
 
 
     }
   END_CATCH
 
-  gdbpy_ref ret_tuple (PyTuple_New (2));
+  gdbpy_ref<> ret_tuple (PyTuple_New (2));
   if (ret_tuple == NULL)
     return NULL;
 
 
 {
   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;
 
 
 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)
     {
     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 ());
   /* 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;
 
       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;
 
 
 
   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)
         {
   /* 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 ();
       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)
 
   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;
 
 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;
 
 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;
 
 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;
 
 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)
     {
     }
   else
     {
-      gdbpy_ref num (PyNumber_Long (obj));
+      gdbpy_ref<> num (PyNumber_Long (obj));
       gdb_py_ulongest val;
 
       if (num == NULL)
 
 {
   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)
 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;
 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)
        }
       else
        {
-         gdbpy_ref name_obj (PyObject_GetAttrString (key, "name"));
+         gdbpy_ref<> name_obj (PyObject_GetAttrString (key, "name"));
 
          if (name_obj == NULL)
            return NULL;
 
                  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))
                  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)
 
 
   gdbpy_enter_varobj enter_py (self->var);
 
-  gdbpy_ref item (PyIter_Next (t->iter));
+  gdbpy_ref<> item (PyIter_Next (t->iter));
 
   if (item == NULL)
     {
   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 ();
 
 {
   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;
 
       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;
 
 
   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 ();
     }
 
   /* 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 ();
          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 ();
   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 ();
   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 ();
        }
     }
 
-  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 ();
     }
   while (true)
     {
-      gdbpy_ref matcher (PyIter_Next (list_iter.get ()));
+      gdbpy_ref<> matcher (PyIter_Next (list_iter.get ()));
       if (matcher == NULL)
        {
          if (PyErr_Occurred ())
          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)
        {
        ; /* 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)
            {
            {
              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 ())
   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)
     {
       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)
     {
       while (true)
        {
-         gdbpy_ref item (PyIter_Next (list_iter.get ()));
+         gdbpy_ref<> item (PyIter_Next (list_iter.get ()));
          if (item == NULL)
            {
              if (PyErr_Occurred ())
 
   /* 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)
     {
       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 ();
       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)
     {
       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 ();
       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 ();
 
   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;
 
   /* 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 ();
   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))
       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);
        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 ();
 
   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 ();
 
       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 ();
 {
   struct program_space *ps;
 
-  gdbpy_ref list (PyList_New (0));
+  gdbpy_ref<> list (PyList_New (0));
   if (list == NULL)
     return NULL;
 
 {
   struct objfile *objf;
 
-  gdbpy_ref list (PyList_New (0));
+  gdbpy_ref<> list (PyList_New (0));
   if (list == NULL)
     return NULL;
 
 
   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 ();
 
   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 ();
     }
   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;
     }
 
   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)
     {
            {
              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)