From 830a493402da4a055bf2d34ab300a83305391095 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Sun, 20 Nov 2016 10:52:25 -0700 Subject: [PATCH] Use gdbpy_ref in py-utils.c This changes more places in py-utils.c to use gdbpy_ref. 2017-01-10 Tom Tromey * python/py-utils.c (unicode_to_encoded_string) (python_string_to_target_string) (python_string_to_target_python_string) (python_string_to_host_string, gdbpy_obj_to_string) (get_addr_from_python): Use gdbpy_ref. --- gdb/ChangeLog | 8 +++++++ gdb/python/py-utils.c | 49 +++++++++++++------------------------------ 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 68eaf566d5a..25ec3a0d0a8 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,11 @@ +2017-01-10 Tom Tromey + + * python/py-utils.c (unicode_to_encoded_string) + (python_string_to_target_string) + (python_string_to_target_python_string) + (python_string_to_host_string, gdbpy_obj_to_string) + (get_addr_from_python): Use gdbpy_ref. + 2017-01-10 Tom Tromey * python/py-unwind.c (pyuw_object_attribute_to_pointer): Use diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c index a4dadcd9de0..2cbf29eff1f 100644 --- a/gdb/python/py-utils.c +++ b/gdb/python/py-utils.c @@ -21,7 +21,7 @@ #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. */ @@ -111,21 +111,18 @@ static gdb::unique_xmalloc_ptr unicode_to_encoded_string (PyObject *unicode_str, const char *charset) { gdb::unique_xmalloc_ptr 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; } @@ -168,15 +165,11 @@ unicode_to_target_python_string (PyObject *unicode_str) gdb::unique_xmalloc_ptr 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 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 @@ -187,16 +180,11 @@ python_string_to_target_string (PyObject *obj) 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 @@ -205,16 +193,11 @@ python_string_to_target_python_string (PyObject *obj) gdb::unique_xmalloc_ptr 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 - 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. */ @@ -244,19 +227,18 @@ gdbpy_is_string (PyObject *obj) gdb::unique_xmalloc_ptr 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 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; } @@ -329,14 +311,13 @@ get_addr_from_python (PyObject *obj, CORE_ADDR *addr) } 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; -- 2.30.2