Use gdbpy_enter in gdbpy_get_matching_xmethod_workers

Change gdbpy_get_matching_xmethod_workers to use gdbpy_enter and
gdbpy_ref.

2017-01-10  Tom Tromey  <tom@tromey.com>

	* python/py-xmethods.c (gdbpy_get_matching_xmethod_workers): use
	gdbpy_enter, gdbpy_ref.
This commit is contained in:
Tom Tromey 2016-11-08 15:34:55 -07:00
parent 396a78b627
commit 572a5524c1
2 changed files with 63 additions and 95 deletions

View file

@ -1,3 +1,8 @@
2017-01-10 Tom Tromey <tom@tromey.com>
* python/py-xmethods.c (gdbpy_get_matching_xmethod_workers): use
gdbpy_enter, gdbpy_ref.
2017-01-10 Tom Tromey <tom@tromey.com> 2017-01-10 Tom Tromey <tom@tromey.com>
* python/python.c (python_interactive_command): Use gdbpy_enter. * python/python.c (python_interactive_command): Use gdbpy_enter.

View file

@ -26,6 +26,7 @@
#include "python.h" #include "python.h"
#include "python-internal.h" #include "python-internal.h"
#include "py-ref.h"
static const char enabled_field_name[] = "enabled"; static const char enabled_field_name[] = "enabled";
static const char match_method_name[] = "match"; static const char match_method_name[] = "match";
@ -156,33 +157,26 @@ gdbpy_get_matching_xmethod_workers
struct type *obj_type, const char *method_name, struct type *obj_type, const char *method_name,
xmethod_worker_vec **dm_vec) xmethod_worker_vec **dm_vec)
{ {
struct cleanup *cleanups;
struct objfile *objfile; struct objfile *objfile;
VEC (xmethod_worker_ptr) *worker_vec = NULL; VEC (xmethod_worker_ptr) *worker_vec = NULL;
PyObject *py_type, *py_progspace; PyObject *py_progspace;
PyObject *py_xmethod_matcher_list = NULL, *list_iter, *matcher;
gdb_assert (obj_type != NULL && method_name != NULL); gdb_assert (obj_type != NULL && method_name != NULL);
cleanups = ensure_python_env (get_current_arch (), current_language); gdbpy_enter enter_py (get_current_arch (), current_language);
py_type = type_to_type_object (obj_type); gdbpy_ref py_type (type_to_type_object (obj_type));
if (py_type == NULL) if (py_type == NULL)
{ {
gdbpy_print_stack (); gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR; return EXT_LANG_RC_ERROR;
} }
make_cleanup_py_decref (py_type);
/* Create an empty list of debug methods. */ /* Create an empty list of debug methods. */
py_xmethod_matcher_list = PyList_New (0); gdbpy_ref py_xmethod_matcher_list (PyList_New (0));
if (py_xmethod_matcher_list == NULL) if (py_xmethod_matcher_list == NULL)
{ {
gdbpy_print_stack (); gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR; return EXT_LANG_RC_ERROR;
} }
@ -192,28 +186,23 @@ gdbpy_get_matching_xmethod_workers
ALL_OBJFILES (objfile) ALL_OBJFILES (objfile)
{ {
PyObject *py_objfile = objfile_to_objfile_object (objfile); PyObject *py_objfile = objfile_to_objfile_object (objfile);
PyObject *objfile_matchers, *temp = py_xmethod_matcher_list;
if (py_objfile == NULL) if (py_objfile == NULL)
{ {
gdbpy_print_stack (); gdbpy_print_stack ();
Py_DECREF (py_xmethod_matcher_list);
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR; return EXT_LANG_RC_ERROR;
} }
objfile_matchers = objfpy_get_xmethods (py_objfile, NULL); gdbpy_ref objfile_matchers (objfpy_get_xmethods (py_objfile, NULL));
py_xmethod_matcher_list = PySequence_Concat (temp, objfile_matchers); gdbpy_ref temp (PySequence_Concat (py_xmethod_matcher_list.get (),
Py_DECREF (temp); objfile_matchers.get ()));
Py_DECREF (objfile_matchers); if (temp == NULL)
if (py_xmethod_matcher_list == NULL)
{ {
gdbpy_print_stack (); gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR; return EXT_LANG_RC_ERROR;
} }
py_xmethod_matcher_list = std::move (temp);
} }
/* Gather debug methods matchers registered with the current program /* Gather debug methods matchers registered with the current program
@ -221,26 +210,21 @@ gdbpy_get_matching_xmethod_workers
py_progspace = pspace_to_pspace_object (current_program_space); py_progspace = pspace_to_pspace_object (current_program_space);
if (py_progspace != NULL) if (py_progspace != NULL)
{ {
PyObject *temp = py_xmethod_matcher_list; gdbpy_ref pspace_matchers (pspy_get_xmethods (py_progspace, NULL));
PyObject *pspace_matchers = pspy_get_xmethods (py_progspace, NULL);
py_xmethod_matcher_list = PySequence_Concat (temp, pspace_matchers); gdbpy_ref temp (PySequence_Concat (py_xmethod_matcher_list.get (),
Py_DECREF (temp); pspace_matchers.get ()));
Py_DECREF (pspace_matchers); if (temp == NULL)
if (py_xmethod_matcher_list == NULL)
{ {
gdbpy_print_stack (); gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR; return EXT_LANG_RC_ERROR;
} }
py_xmethod_matcher_list = std::move (temp);
} }
else else
{ {
gdbpy_print_stack (); gdbpy_print_stack ();
Py_DECREF (py_xmethod_matcher_list);
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR; return EXT_LANG_RC_ERROR;
} }
@ -248,117 +232,96 @@ gdbpy_get_matching_xmethod_workers
if (gdb_python_module != NULL if (gdb_python_module != NULL
&& PyObject_HasAttrString (gdb_python_module, matchers_attr_str)) && PyObject_HasAttrString (gdb_python_module, matchers_attr_str))
{ {
PyObject *gdb_matchers; gdbpy_ref gdb_matchers (PyObject_GetAttrString (gdb_python_module,
PyObject *temp = py_xmethod_matcher_list; matchers_attr_str));
gdb_matchers = PyObject_GetAttrString (gdb_python_module,
matchers_attr_str);
if (gdb_matchers != NULL) if (gdb_matchers != NULL)
{ {
py_xmethod_matcher_list = PySequence_Concat (temp, gdb_matchers); gdbpy_ref temp (PySequence_Concat (py_xmethod_matcher_list.get (),
Py_DECREF (temp); gdb_matchers.get ()));
Py_DECREF (gdb_matchers); if (temp == NULL)
if (py_xmethod_matcher_list == NULL)
{ {
gdbpy_print_stack (); gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR; return EXT_LANG_RC_ERROR;
} }
py_xmethod_matcher_list = std::move (temp);
} }
else else
{ {
gdbpy_print_stack (); gdbpy_print_stack ();
Py_DECREF (py_xmethod_matcher_list);
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR; return EXT_LANG_RC_ERROR;
} }
} }
/* Safe to make a cleanup for py_xmethod_matcher_list now as it gdbpy_ref list_iter (PyObject_GetIter (py_xmethod_matcher_list.get ()));
will not change any more. */
make_cleanup_py_decref (py_xmethod_matcher_list);
list_iter = PyObject_GetIter (py_xmethod_matcher_list);
if (list_iter == NULL) if (list_iter == NULL)
{ {
gdbpy_print_stack (); gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR; return EXT_LANG_RC_ERROR;
} }
while ((matcher = PyIter_Next (list_iter)) != NULL) while (true)
{ {
PyObject *match_result = invoke_match_method (matcher, py_type, gdbpy_ref matcher (PyIter_Next (list_iter.get ()));
method_name); if (matcher == NULL)
{
if (PyErr_Occurred ())
{
gdbpy_print_stack ();
return EXT_LANG_RC_ERROR;
}
break;
}
gdbpy_ref match_result (invoke_match_method (matcher.get (),
py_type.get (),
method_name));
if (match_result == NULL) if (match_result == NULL)
{ {
gdbpy_print_stack (); gdbpy_print_stack ();
Py_DECREF (matcher);
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR; return EXT_LANG_RC_ERROR;
} }
if (match_result == Py_None) if (match_result == Py_None)
; /* This means there was no match. */ ; /* This means there was no match. */
else if (PySequence_Check (match_result)) else if (PySequence_Check (match_result.get ()))
{ {
PyObject *iter = PyObject_GetIter (match_result); gdbpy_ref iter (PyObject_GetIter (match_result.get ()));
PyObject *py_worker;
if (iter == NULL) if (iter == NULL)
{ {
gdbpy_print_stack (); gdbpy_print_stack ();
Py_DECREF (matcher);
Py_DECREF (match_result);
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR; return EXT_LANG_RC_ERROR;
} }
while ((py_worker = PyIter_Next (iter)) != NULL) while (true)
{ {
struct xmethod_worker *worker; struct xmethod_worker *worker;
worker = new_python_xmethod_worker (py_worker, py_type); gdbpy_ref py_worker (PyIter_Next (iter.get ()));
VEC_safe_push (xmethod_worker_ptr, worker_vec, worker); if (py_worker == NULL)
Py_DECREF (py_worker); {
} if (PyErr_Occurred ())
Py_DECREF (iter); {
/* Report any error that could have occurred while iterating. */ gdbpy_print_stack ();
if (PyErr_Occurred ()) return EXT_LANG_RC_ERROR;
{ }
gdbpy_print_stack (); break;
Py_DECREF (matcher); }
Py_DECREF (match_result);
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR; worker = new_python_xmethod_worker (py_worker.get (),
py_type.get ());
VEC_safe_push (xmethod_worker_ptr, worker_vec, worker);
} }
} }
else else
{ {
struct xmethod_worker *worker; struct xmethod_worker *worker;
worker = new_python_xmethod_worker (match_result, py_type); worker = new_python_xmethod_worker (match_result.get (),
py_type.get ());
VEC_safe_push (xmethod_worker_ptr, worker_vec, worker); VEC_safe_push (xmethod_worker_ptr, worker_vec, worker);
} }
Py_DECREF (match_result);
Py_DECREF (matcher);
}
Py_DECREF (list_iter);
/* Report any error that could have occurred while iterating. */
if (PyErr_Occurred ())
{
gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
} }
do_cleanups (cleanups);
*dm_vec = worker_vec; *dm_vec = worker_vec;
return EXT_LANG_RC_OK; return EXT_LANG_RC_OK;