#include "charset.h"
 #include "value.h"
 #include "python-internal.h"
-
+#include "py-ref.h"
 
 /* This is a cleanup function which decrements the refcount on a
    Python object.  */
 unicode_to_encoded_string (PyObject *unicode_str, const char *charset)
 {
   gdb::unique_xmalloc_ptr<char> result;
-  PyObject *string;
 
   /* Translate string to named charset.  */
-  string = PyUnicode_AsEncodedString (unicode_str, charset, NULL);
+  gdbpy_ref string (PyUnicode_AsEncodedString (unicode_str, charset, NULL));
   if (string == NULL)
     return NULL;
 
 #ifdef IS_PY3K
-  result.reset (xstrdup (PyBytes_AsString (string)));
+  result.reset (xstrdup (PyBytes_AsString (string.get ())));
 #else
-  result.reset (xstrdup (PyString_AsString (string)));
+  result.reset (xstrdup (PyString_AsString (string.get ())));
 #endif
 
-  Py_DECREF (string);
-
   return result;
 }
 
 gdb::unique_xmalloc_ptr<char>
 python_string_to_target_string (PyObject *obj)
 {
-  PyObject *str;
-
-  str = python_string_to_unicode (obj);
+  gdbpy_ref str (python_string_to_unicode (obj));
   if (str == NULL)
     return NULL;
 
-  gdb::unique_xmalloc_ptr<char> result (unicode_to_target_string (str));
-  Py_DECREF (str);
-  return result;
+  return unicode_to_target_string (str.get ());
 }
 
 /* Converts a python string (8-bit or unicode) to a target string in the
 PyObject *
 python_string_to_target_python_string (PyObject *obj)
 {
-  PyObject *str;
-  PyObject *result;
-
-  str = python_string_to_unicode (obj);
+  gdbpy_ref str (python_string_to_unicode (obj));
   if (str == NULL)
     return NULL;
 
-  result = unicode_to_target_python_string (str);
-  Py_DECREF (str);
-  return result;
+  return unicode_to_target_python_string (str.get ());
 }
 
 /* Converts a python string (8-bit or unicode) to a target string in
 gdb::unique_xmalloc_ptr<char>
 python_string_to_host_string (PyObject *obj)
 {
-  PyObject *str;
-
-  str = python_string_to_unicode (obj);
+  gdbpy_ref str (python_string_to_unicode (obj));
   if (str == NULL)
     return NULL;
 
-  gdb::unique_xmalloc_ptr<char>
-    result (unicode_to_encoded_string (str, host_charset ()));
-  Py_DECREF (str);
-  return result;
+  return unicode_to_encoded_string (str.get (), host_charset ());
 }
 
 /* Convert a host string to a python string.  */
 gdb::unique_xmalloc_ptr<char>
 gdbpy_obj_to_string (PyObject *obj)
 {
-  PyObject *str_obj = PyObject_Str (obj);
+  gdbpy_ref str_obj (PyObject_Str (obj));
 
   if (str_obj != NULL)
     {
       gdb::unique_xmalloc_ptr<char> msg;
 
 #ifdef IS_PY3K
-      msg = python_string_to_host_string (str_obj);
+      msg = python_string_to_host_string (str_obj.get ());
 #else
-      msg.reset (xstrdup (PyString_AsString (str_obj)));
+      msg.reset (xstrdup (PyString_AsString (str_obj.get ())));
 #endif
 
-      Py_DECREF (str_obj);
       return msg;
     }
 
     }
   else
     {
-      PyObject *num = PyNumber_Long (obj);
+      gdbpy_ref num (PyNumber_Long (obj));
       gdb_py_ulongest val;
 
       if (num == NULL)
        return -1;
 
-      val = gdb_py_long_as_ulongest (num);
-      Py_XDECREF (num);
+      val = gdb_py_long_as_ulongest (num.get ());
       if (PyErr_Occurred ())
        return -1;