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.
This commit is contained in:
Tom Tromey 2016-11-20 10:52:25 -07:00
parent 4586d54305
commit 830a493402
2 changed files with 23 additions and 34 deletions

View File

@ -1,3 +1,11 @@
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.
2017-01-10 Tom Tromey <tom@tromey.com>
* python/py-unwind.c (pyuw_object_attribute_to_pointer): Use

View File

@ -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;