From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [RFA 08/13] Use gdbpy_ref in python.c
Date: Sun, 20 Nov 2016 20:42:00 -0000 [thread overview]
Message-ID: <1479674496-14000-9-git-send-email-tom@tromey.com> (raw)
In-Reply-To: <1479674496-14000-1-git-send-email-tom@tromey.com>
This changes more places in python.c to use gdbpy_ref.
Additionally, previously gdbpy_apply_type_printers would return
EXT_LANG_RC_ERROR if a type printer returned None. However, that
doesn't seem correct to me; this patch changes it to return
EXT_LANG_RC_NOP in this case.
2016-11-20 Tom Tromey <tom@tromey.com>
* python/python.c (eval_python_command, gdbpy_decode_line)
(gdbpy_run_events, gdbpy_start_type_printers)
(gdbpy_apply_type_printers): Use gdbpy_ref.
---
gdb/ChangeLog | 6 +++
gdb/python/python.c | 120 +++++++++++++++++++++++-----------------------------
2 files changed, 59 insertions(+), 67 deletions(-)
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 92fee8c..ab0cefb 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,11 @@
2016-11-20 Tom Tromey <tom@tromey.com>
+ * python/python.c (eval_python_command, gdbpy_decode_line)
+ (gdbpy_run_events, gdbpy_start_type_printers)
+ (gdbpy_apply_type_printers): Use gdbpy_ref.
+
+2016-11-20 Tom Tromey <tom@tromey.com>
+
* python/py-param.c (get_doc_string, compute_enum_values): Use
gdbpy_ref.
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 7a38ca2..83b9805 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -264,7 +264,7 @@ gdbpy_check_quit_flag (const struct extension_language_defn *extlang)
static int
eval_python_command (const char *command)
{
- PyObject *m, *d, *v;
+ PyObject *m, *d;
m = PyImport_AddModule ("__main__");
if (m == NULL)
@@ -273,11 +273,10 @@ eval_python_command (const char *command)
d = PyModule_GetDict (m);
if (d == NULL)
return -1;
- v = PyRun_StringFlags (command, Py_single_input, d, d, NULL);
+ gdbpy_ref v (PyRun_StringFlags (command, Py_single_input, d, d, NULL));
if (v == NULL)
return -1;
- Py_DECREF (v);
#ifndef IS_PY3K
if (Py_FlushLine ())
PyErr_Clear ();
@@ -672,9 +671,8 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
struct symtab_and_line sal;
char *arg = NULL;
struct cleanup *cleanups;
- PyObject *result = NULL;
- PyObject *return_result = NULL;
- PyObject *unparsed = NULL;
+ gdbpy_ref result;
+ gdbpy_ref unparsed;
struct event_location *location = NULL;
if (! PyArg_ParseTuple (args, "|s", &arg))
@@ -723,9 +721,12 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
{
int i;
- result = PyTuple_New (sals.nelts);
- if (! result)
- goto error;
+ result.reset (PyTuple_New (sals.nelts));
+ if (result == NULL)
+ {
+ do_cleanups (cleanups);
+ return NULL;
+ }
for (i = 0; i < sals.nelts; ++i)
{
PyObject *obj;
@@ -733,50 +734,47 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
obj = symtab_and_line_to_sal_object (sals.sals[i]);
if (! obj)
{
- Py_DECREF (result);
- goto error;
+ do_cleanups (cleanups);
+ return NULL;
}
- PyTuple_SetItem (result, i, obj);
+ PyTuple_SetItem (result.get (), i, obj);
}
}
else
{
- result = Py_None;
+ result.reset (Py_None);
Py_INCREF (Py_None);
}
- return_result = PyTuple_New (2);
- if (! return_result)
+ gdbpy_ref return_result (PyTuple_New (2));
+ if (return_result == NULL)
{
- Py_DECREF (result);
- goto error;
+ do_cleanups (cleanups);
+ return NULL;
}
if (arg != NULL && strlen (arg) > 0)
{
- unparsed = PyString_FromString (arg);
+ unparsed.reset (PyString_FromString (arg));
if (unparsed == NULL)
{
- Py_DECREF (result);
- Py_DECREF (return_result);
- return_result = NULL;
- goto error;
+ do_cleanups (cleanups);
+ return NULL;
}
}
else
{
- unparsed = Py_None;
+ unparsed.reset (Py_None);
Py_INCREF (Py_None);
}
- PyTuple_SetItem (return_result, 0, unparsed);
- PyTuple_SetItem (return_result, 1, result);
+ PyTuple_SetItem (return_result.get (), 0, unparsed.release ());
+ PyTuple_SetItem (return_result.get (), 1, result.release ());
- error:
do_cleanups (cleanups);
- return return_result;
+ return return_result.release ();
}
/* Parse a string and evaluate it as an expression. */
@@ -893,8 +891,6 @@ gdbpy_run_events (int error, gdb_client_data client_data)
while (gdbpy_event_list)
{
- PyObject *call_result;
-
/* Dispatching the event might push a new element onto the event
loop, so we update here "atomically enough". */
struct gdbpy_event *item = gdbpy_event_list;
@@ -903,11 +899,10 @@ gdbpy_run_events (int error, gdb_client_data client_data)
gdbpy_event_list_end = &gdbpy_event_list;
/* Ignore errors. */
- call_result = PyObject_CallObject (item->event, NULL);
+ gdbpy_ref call_result (PyObject_CallObject (item->event, NULL));
if (call_result == NULL)
PyErr_Clear ();
- Py_XDECREF (call_result);
Py_DECREF (item->event);
xfree (item);
}
@@ -1327,36 +1322,33 @@ static void
gdbpy_start_type_printers (const struct extension_language_defn *extlang,
struct ext_lang_type_printers *ext_printers)
{
- PyObject *type_module, *func = NULL, *printers_obj = NULL;
+ PyObject *printers_obj = NULL;
if (!gdb_python_initialized)
return;
gdbpy_enter enter_py (get_current_arch (), current_language);
- type_module = PyImport_ImportModule ("gdb.types");
+ gdbpy_ref type_module (PyImport_ImportModule ("gdb.types"));
if (type_module == NULL)
{
gdbpy_print_stack ();
- goto done;
+ return;
}
- func = PyObject_GetAttrString (type_module, "get_type_recognizers");
+ gdbpy_ref func (PyObject_GetAttrString (type_module.get (),
+ "get_type_recognizers"));
if (func == NULL)
{
gdbpy_print_stack ();
- goto done;
+ return;
}
- printers_obj = PyObject_CallFunctionObjArgs (func, (char *) NULL);
+ printers_obj = PyObject_CallFunctionObjArgs (func.get (), (char *) NULL);
if (printers_obj == NULL)
gdbpy_print_stack ();
else
ext_printers->py_type_printers = printers_obj;
-
- done:
- Py_XDECREF (type_module);
- Py_XDECREF (func);
}
/* If TYPE is recognized by some type printer, store in *PRETTIED_TYPE
@@ -1371,8 +1363,6 @@ gdbpy_apply_type_printers (const struct extension_language_defn *extlang,
const struct ext_lang_type_printers *ext_printers,
struct type *type, char **prettied_type)
{
- PyObject *type_obj, *type_module = NULL, *func = NULL;
- PyObject *result_obj = NULL;
PyObject *printers_obj = (PyObject *) ext_printers->py_type_printers;
gdb::unique_xmalloc_ptr<char> result;
@@ -1384,53 +1374,49 @@ gdbpy_apply_type_printers (const struct extension_language_defn *extlang,
gdbpy_enter enter_py (get_current_arch (), current_language);
- type_obj = type_to_type_object (type);
+ gdbpy_ref type_obj (type_to_type_object (type));
if (type_obj == NULL)
{
gdbpy_print_stack ();
- goto done;
+ return EXT_LANG_RC_ERROR;
}
- type_module = PyImport_ImportModule ("gdb.types");
+ gdbpy_ref type_module (PyImport_ImportModule ("gdb.types"));
if (type_module == NULL)
{
gdbpy_print_stack ();
- goto done;
+ return EXT_LANG_RC_ERROR;
}
- func = PyObject_GetAttrString (type_module, "apply_type_recognizers");
+ gdbpy_ref func (PyObject_GetAttrString (type_module.get (),
+ "apply_type_recognizers"));
if (func == NULL)
{
gdbpy_print_stack ();
- goto done;
+ return EXT_LANG_RC_ERROR;
}
- result_obj = PyObject_CallFunctionObjArgs (func, printers_obj,
- type_obj, (char *) NULL);
+ gdbpy_ref result_obj (PyObject_CallFunctionObjArgs (func.get (), printers_obj,
+ type_obj.get (),
+ (char *) NULL));
if (result_obj == NULL)
{
gdbpy_print_stack ();
- goto done;
+ return EXT_LANG_RC_ERROR;
}
- if (result_obj != Py_None)
- {
- result = python_string_to_host_string (result_obj);
- if (result == NULL)
- gdbpy_print_stack ();
- }
+ if (result_obj == Py_None)
+ return EXT_LANG_RC_NOP;
- done:
- Py_XDECREF (type_obj);
- Py_XDECREF (type_module);
- Py_XDECREF (func);
- Py_XDECREF (result_obj);
- if (result != NULL)
+ result = python_string_to_host_string (result_obj.get ());
+ if (result == NULL)
{
- *prettied_type = result.release ();
- return EXT_LANG_RC_OK;
+ gdbpy_print_stack ();
+ return EXT_LANG_RC_ERROR;
}
- return EXT_LANG_RC_ERROR;
+
+ *prettied_type = result.release ();
+ return EXT_LANG_RC_OK;
}
/* Free the result of start_type_printers.
--
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 02/13] Use gdbpy_ref in gdbpy_breakpoint_cond_says_stop 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 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 ` Tom Tromey [this message]
2016-11-22 17:21 ` [RFA 08/13] Use gdbpy_ref in python.c Pedro Alves
2016-11-28 23:08 ` 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 04/13] Use gdbpy_ref in bpfinishpy_out_of_scope Tom Tromey
2016-11-20 20:42 ` [RFA 10/13] Use gdbpy_ref in py-utils.c 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:42 ` [RFA 09/13] Use gdbpy_ref in pyuw_object_attribute_to_pointer Tom Tromey
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 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: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-9-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