const char *text, const char *word)
 {
   cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command);
-  PyObject *textobj, *wordobj;
-  PyObject *resultobj;
 
   if (obj == NULL)
     error (_("Invalid invocation of Python command object."));
       return NULL;
     }
 
-  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."));
-  wordobj = PyUnicode_Decode (word, strlen (word), host_charset (), NULL);
+  gdbpy_ref wordobj (PyUnicode_Decode (word, strlen (word), host_charset (),
+                                      NULL));
   if (wordobj == NULL)
-    {
-      Py_DECREF (textobj);
-      error (_("Could not convert argument to Python string."));
-    }
+    error (_("Could not convert argument to Python string."));
 
-  resultobj = PyObject_CallMethodObjArgs ((PyObject *) obj, complete_cst,
-                                         textobj, wordobj, NULL);
-  Py_DECREF (textobj);
-  Py_DECREF (wordobj);
-  if (!resultobj)
+  gdbpy_ref resultobj (PyObject_CallMethodObjArgs ((PyObject *) obj,
+                                                  complete_cst,
+                                                  textobj.get (),
+                                                  wordobj.get (), NULL));
+  if (resultobj == NULL)
     {
       /* Just swallow errors here.  */
       PyErr_Clear ();
     }
 
-  Py_XINCREF (resultobj);
-
-  return resultobj;
+  return resultobj.release ();
 }
 
 /* Python function called to determine the break characters of a
 cmdpy_completer_handle_brkchars (struct cmd_list_element *command,
                                 const char *text, const char *word)
 {
-  PyObject *resultobj = NULL;
-
   gdbpy_enter enter_py (get_current_arch (), current_language);
 
   /* Calling our helper to obtain the PyObject of the Python
      function.  */
-  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)
-    goto done;
+    return;
 
-  if (PyInt_Check (resultobj))
+  if (PyInt_Check (resultobj.get ()))
     {
       /* User code may also return one of the completion constants,
         thus requesting that sort of completion.  We are only
         interested in this kind of return.  */
       long value;
 
-      if (!gdb_py_int_as_long (resultobj, &value))
+      if (!gdb_py_int_as_long (resultobj.get (), &value))
        {
          /* Ignore.  */
          PyErr_Clear ();
            (completers[value].completer);
        }
     }
-
- done:
-
-  Py_XDECREF (resultobj);
 }
 
 /* Called by gdb for command completion.  */
 cmdpy_completer (struct cmd_list_element *command,
                 const char *text, const char *word)
 {
-  PyObject *resultobj = NULL;
   VEC (char_ptr) *result = NULL;
 
   gdbpy_enter enter_py (get_current_arch (), current_language);
 
   /* Calling our helper to obtain the PyObject of the Python
      function.  */
-  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
      return NULL.  */
   if (resultobj == NULL)
-    goto done;
+    return NULL;
 
   result = NULL;
-  if (PyInt_Check (resultobj))
+  if (PyInt_Check (resultobj.get ()))
     {
       /* User code may also return one of the completion constants,
         thus requesting that sort of completion.  */
       long value;
 
-      if (! gdb_py_int_as_long (resultobj, &value))
+      if (! gdb_py_int_as_long (resultobj.get (), &value))
        {
          /* Ignore.  */
          PyErr_Clear ();
     }
   else
     {
-      PyObject *iter = PyObject_GetIter (resultobj);
-      PyObject *elt;
+      gdbpy_ref iter (PyObject_GetIter (resultobj.get ()));
 
       if (iter == NULL)
-       goto done;
+       return NULL;
 
-      while ((elt = PyIter_Next (iter)) != NULL)
+      while (true)
        {
+         gdbpy_ref elt (PyIter_Next (iter.get ()));
+         if (elt == NULL)
+           break;
 
-         if (! gdbpy_is_string (elt))
+         if (! gdbpy_is_string (elt.get ()))
            {
              /* Skip problem elements.  */
-             Py_DECREF (elt);
              continue;
            }
          gdb::unique_xmalloc_ptr<char>
-           item (python_string_to_host_string (elt));
-         Py_DECREF (elt);
+           item (python_string_to_host_string (elt.get ()));
          if (item == NULL)
            {
              /* Skip problem elements.  */
          VEC_safe_push (char_ptr, result, item.release ());
        }
 
-      Py_DECREF (iter);
-
       /* If we got some results, ignore problems.  Otherwise, report
         the problem.  */
       if (result != NULL && PyErr_Occurred ())
        PyErr_Clear ();
     }
 
- done:
-
-  Py_XDECREF (resultobj);
-
   return result;
 }
 
     }
   if (PyObject_HasAttr (self, gdbpy_doc_cst))
     {
-      PyObject *ds_obj = PyObject_GetAttr (self, gdbpy_doc_cst);
+      gdbpy_ref ds_obj (PyObject_GetAttr (self, gdbpy_doc_cst));
 
-      if (ds_obj && gdbpy_is_string (ds_obj))
+      if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ()))
        {
-         docstring = python_string_to_host_string (ds_obj).release ();
+         docstring = python_string_to_host_string (ds_obj.get ()).release ();
          if (docstring == NULL)
            {
              xfree (cmd_name);
              xfree (pfx_name);
-             Py_DECREF (ds_obj);
              return -1;
            }
        }
-
-      Py_XDECREF (ds_obj);
     }
   if (! docstring)
     docstring = xstrdup (_("This command is not documented."));