Use gdbpy_ref in python.c

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.

2017-01-10  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.
This commit is contained in:
Tom Tromey 2016-11-20 10:46:23 -07:00
parent 97d83487d5
commit 59876f8f9f
2 changed files with 59 additions and 67 deletions

View file

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

View file

@ -264,7 +264,7 @@ gdbpy_check_quit_flag (const struct extension_language_defn *extlang)
static int static int
eval_python_command (const char *command) eval_python_command (const char *command)
{ {
PyObject *m, *d, *v; PyObject *m, *d;
m = PyImport_AddModule ("__main__"); m = PyImport_AddModule ("__main__");
if (m == NULL) if (m == NULL)
@ -273,11 +273,10 @@ eval_python_command (const char *command)
d = PyModule_GetDict (m); d = PyModule_GetDict (m);
if (d == NULL) if (d == NULL)
return -1; 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) if (v == NULL)
return -1; return -1;
Py_DECREF (v);
#ifndef IS_PY3K #ifndef IS_PY3K
if (Py_FlushLine ()) if (Py_FlushLine ())
PyErr_Clear (); PyErr_Clear ();
@ -672,9 +671,8 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
struct symtab_and_line sal; struct symtab_and_line sal;
char *arg = NULL; char *arg = NULL;
struct cleanup *cleanups; struct cleanup *cleanups;
PyObject *result = NULL; gdbpy_ref result;
PyObject *return_result = NULL; gdbpy_ref unparsed;
PyObject *unparsed = NULL;
struct event_location *location = NULL; struct event_location *location = NULL;
if (! PyArg_ParseTuple (args, "|s", &arg)) if (! PyArg_ParseTuple (args, "|s", &arg))
@ -723,9 +721,12 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
{ {
int i; int i;
result = PyTuple_New (sals.nelts); result.reset (PyTuple_New (sals.nelts));
if (! result) if (result == NULL)
goto error; {
do_cleanups (cleanups);
return NULL;
}
for (i = 0; i < sals.nelts; ++i) for (i = 0; i < sals.nelts; ++i)
{ {
PyObject *obj; PyObject *obj;
@ -733,50 +734,47 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
obj = symtab_and_line_to_sal_object (sals.sals[i]); obj = symtab_and_line_to_sal_object (sals.sals[i]);
if (! obj) if (! obj)
{ {
Py_DECREF (result); do_cleanups (cleanups);
goto error; return NULL;
} }
PyTuple_SetItem (result, i, obj); PyTuple_SetItem (result.get (), i, obj);
} }
} }
else else
{ {
result = Py_None; result.reset (Py_None);
Py_INCREF (Py_None); Py_INCREF (Py_None);
} }
return_result = PyTuple_New (2); gdbpy_ref return_result (PyTuple_New (2));
if (! return_result) if (return_result == NULL)
{ {
Py_DECREF (result); do_cleanups (cleanups);
goto error; return NULL;
} }
if (arg != NULL && strlen (arg) > 0) if (arg != NULL && strlen (arg) > 0)
{ {
unparsed = PyString_FromString (arg); unparsed.reset (PyString_FromString (arg));
if (unparsed == NULL) if (unparsed == NULL)
{ {
Py_DECREF (result); do_cleanups (cleanups);
Py_DECREF (return_result); return NULL;
return_result = NULL;
goto error;
} }
} }
else else
{ {
unparsed = Py_None; unparsed.reset (Py_None);
Py_INCREF (Py_None); Py_INCREF (Py_None);
} }
PyTuple_SetItem (return_result, 0, unparsed); PyTuple_SetItem (return_result.get (), 0, unparsed.release ());
PyTuple_SetItem (return_result, 1, result); PyTuple_SetItem (return_result.get (), 1, result.release ());
error:
do_cleanups (cleanups); do_cleanups (cleanups);
return return_result; return return_result.release ();
} }
/* Parse a string and evaluate it as an expression. */ /* 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) while (gdbpy_event_list)
{ {
PyObject *call_result;
/* Dispatching the event might push a new element onto the event /* Dispatching the event might push a new element onto the event
loop, so we update here "atomically enough". */ loop, so we update here "atomically enough". */
struct gdbpy_event *item = gdbpy_event_list; 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; gdbpy_event_list_end = &gdbpy_event_list;
/* Ignore errors. */ /* Ignore errors. */
call_result = PyObject_CallObject (item->event, NULL); gdbpy_ref call_result (PyObject_CallObject (item->event, NULL));
if (call_result == NULL) if (call_result == NULL)
PyErr_Clear (); PyErr_Clear ();
Py_XDECREF (call_result);
Py_DECREF (item->event); Py_DECREF (item->event);
xfree (item); xfree (item);
} }
@ -1327,36 +1322,33 @@ static void
gdbpy_start_type_printers (const struct extension_language_defn *extlang, gdbpy_start_type_printers (const struct extension_language_defn *extlang,
struct ext_lang_type_printers *ext_printers) struct ext_lang_type_printers *ext_printers)
{ {
PyObject *type_module, *func = NULL, *printers_obj = NULL; PyObject *printers_obj = NULL;
if (!gdb_python_initialized) if (!gdb_python_initialized)
return; return;
gdbpy_enter enter_py (get_current_arch (), current_language); 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) if (type_module == NULL)
{ {
gdbpy_print_stack (); 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) if (func == NULL)
{ {
gdbpy_print_stack (); 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) if (printers_obj == NULL)
gdbpy_print_stack (); gdbpy_print_stack ();
else else
ext_printers->py_type_printers = printers_obj; 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 /* 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, const struct ext_lang_type_printers *ext_printers,
struct type *type, char **prettied_type) 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; PyObject *printers_obj = (PyObject *) ext_printers->py_type_printers;
gdb::unique_xmalloc_ptr<char> result; 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); 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) if (type_obj == NULL)
{ {
gdbpy_print_stack (); 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) if (type_module == NULL)
{ {
gdbpy_print_stack (); 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) if (func == NULL)
{ {
gdbpy_print_stack (); gdbpy_print_stack ();
goto done; return EXT_LANG_RC_ERROR;
} }
result_obj = PyObject_CallFunctionObjArgs (func, printers_obj, gdbpy_ref result_obj (PyObject_CallFunctionObjArgs (func.get (), printers_obj,
type_obj, (char *) NULL); type_obj.get (),
(char *) NULL));
if (result_obj == NULL) if (result_obj == NULL)
{ {
gdbpy_print_stack (); gdbpy_print_stack ();
goto done; return EXT_LANG_RC_ERROR;
} }
if (result_obj != Py_None) if (result_obj == Py_None)
return EXT_LANG_RC_NOP;
result = python_string_to_host_string (result_obj.get ());
if (result == NULL)
{ {
result = python_string_to_host_string (result_obj); gdbpy_print_stack ();
if (result == NULL) return EXT_LANG_RC_ERROR;
gdbpy_print_stack ();
} }
done: *prettied_type = result.release ();
Py_XDECREF (type_obj); return EXT_LANG_RC_OK;
Py_XDECREF (type_module);
Py_XDECREF (func);
Py_XDECREF (result_obj);
if (result != NULL)
{
*prettied_type = result.release ();
return EXT_LANG_RC_OK;
}
return EXT_LANG_RC_ERROR;
} }
/* Free the result of start_type_printers. /* Free the result of start_type_printers.