From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [RFA 10/13] Use gdbpy_ref in py-utils.c
Date: Sun, 20 Nov 2016 20:42:00 -0000 [thread overview]
Message-ID: <1479674496-14000-11-git-send-email-tom@tromey.com> (raw)
In-Reply-To: <1479674496-14000-1-git-send-email-tom@tromey.com>
This changes more places in py-utils.c to use gdbpy_ref.
2016-11-20 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.
---
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 3016d3a..4f8447c 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,13 @@
2016-11-20 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.
+
+2016-11-20 Tom Tromey <tom@tromey.com>
+
* python/py-unwind.c (pyuw_object_attribute_to_pointer): Use
gdbpy_ref.
diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c
index 9f99e29..6b7540c 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;
--
2.7.4
next prev parent reply other threads:[~2016-11-20 20:42 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-11-20 20:42 [RFA 00/13] series #4 of c++ in python Tom Tromey
2016-11-20 20:41 ` [RFA 05/13] Use gdbpy_ref in py_print_frame Tom Tromey
2016-11-20 20:41 ` [RFA 02/13] Use gdbpy_ref in gdbpy_breakpoint_cond_says_stop Tom Tromey
2016-11-20 20:41 ` [RFA 06/13] Use gdbpy_ref in py-inferior.c Tom Tromey
2016-11-20 20:41 ` [RFA 01/13] Use gdbpy_ref in archpy_disassemble Tom Tromey
2016-11-20 20:42 ` [RFA 07/13] Use gdbpy_ref in py-param.c Tom Tromey
2016-11-20 20:42 ` [RFA 08/13] Use gdbpy_ref in python.c Tom Tromey
2016-11-22 17:21 ` Pedro Alves
2016-11-28 23:08 ` Tom Tromey
2016-11-20 20:42 ` [RFA 04/13] Use gdbpy_ref in bpfinishpy_out_of_scope Tom Tromey
2016-11-20 20:42 ` Tom Tromey [this message]
2016-11-20 20:42 ` [RFA 13/13] Remove make_cleanup_py_decref and make_cleanup_py_xdecref Tom Tromey
2016-11-22 17:27 ` Pedro Alves
2016-11-20 20:42 ` [RFA 12/13] Use gdbpy_ref rather than make_cleanup_py_decref Tom Tromey
2016-11-20 20:42 ` [RFA 09/13] Use gdbpy_ref in pyuw_object_attribute_to_pointer Tom Tromey
2016-11-20 20:42 ` [RFA 03/13] Use gdbpy_ref in py-cmd.c Tom Tromey
2016-11-21 22:46 ` Tom Tromey
2016-11-20 20:48 ` [RFA 11/13] Use gdbpy_ref in enumerate_args Tom Tromey
2016-11-21 22:44 ` [RFA 00/13] series #4 of c++ in python Tom Tromey
2016-11-22 17:28 ` Pedro Alves
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1479674496-14000-11-git-send-email-tom@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox