diff options
author | Tom Tromey <tom@tromey.com> | 2016-11-20 10:52:25 -0700 |
---|---|---|
committer | Tom Tromey <tom@tromey.com> | 2017-01-10 19:14:06 -0700 |
commit | 830a493402da4a055bf2d34ab300a83305391095 (patch) | |
tree | 872bb0d7658b203319f01a092f6973f7f6bec87f /gdb/python/py-utils.c | |
parent | 4586d54305ed275bb909f3373a7372c02d7e579e (diff) | |
download | gdb-830a493402da4a055bf2d34ab300a83305391095.zip gdb-830a493402da4a055bf2d34ab300a83305391095.tar.gz gdb-830a493402da4a055bf2d34ab300a83305391095.tar.bz2 |
Use gdbpy_ref in py-utils.c
This changes more places in py-utils.c to use gdbpy_ref.
2017-01-10 Tom Tromey <tom@tromey.com>
* 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.
Diffstat (limited to 'gdb/python/py-utils.c')
-rw-r--r-- | gdb/python/py-utils.c | 49 |
1 files changed, 15 insertions, 34 deletions
diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c index a4dadcd..2cbf29e 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<char> 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; } @@ -168,15 +165,11 @@ unicode_to_target_python_string (PyObject *unicode_str) 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 @@ -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<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. */ @@ -244,19 +227,18 @@ gdbpy_is_string (PyObject *obj) 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; } @@ -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; |