1 /* Copyright (C) 2013-2015 Free Software Foundation, Inc.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 3 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 #include "python-internal.h"
19 #include "varobj-iter.h"
21 /* A dynamic varobj iterator "class" for python pretty-printed
22 varobjs. This inherits struct varobj_iter. */
26 /* The 'base class'. */
27 struct varobj_iter base
;
29 /* The python iterator returned by the printer's 'children' method,
30 or NULL if not available. */
34 /* Implementation of the 'dtor' method of pretty-printed varobj
38 py_varobj_iter_dtor (struct varobj_iter
*self
)
40 struct py_varobj_iter
*dis
= (struct py_varobj_iter
*) self
;
41 struct cleanup
*back_to
= varobj_ensure_python_env (self
->var
);
43 Py_XDECREF (dis
->iter
);
45 do_cleanups (back_to
);
48 /* Implementation of the 'next' method of pretty-printed varobj
52 py_varobj_iter_next (struct varobj_iter
*self
)
54 struct py_varobj_iter
*t
= (struct py_varobj_iter
*) self
;
55 struct cleanup
*back_to
;
59 const char *name
= NULL
;
61 if (!gdb_python_initialized
)
64 back_to
= varobj_ensure_python_env (self
->var
);
66 item
= PyIter_Next (t
->iter
);
70 /* Normal end of iteration. */
71 if (!PyErr_Occurred ())
74 /* If we got a memory error, just use the text as the item. */
75 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error
))
77 PyObject
*type
, *value
, *trace
;
78 char *name_str
, *value_str
;
80 PyErr_Fetch (&type
, &value
, &trace
);
81 value_str
= gdbpy_exception_to_string (type
, value
);
85 if (value_str
== NULL
)
91 name_str
= xstrprintf ("<error at %d>",
92 self
->next_raw_index
++);
93 item
= Py_BuildValue ("(ss)", name_str
, value_str
);
104 /* Any other kind of error. */
105 gdbpy_print_stack ();
110 if (!PyArg_ParseTuple (item
, "sO", &name
, &py_v
))
112 gdbpy_print_stack ();
113 error (_("Invalid item from the child list"));
116 vitem
= xmalloc (sizeof *vitem
);
117 vitem
->value
= convert_value_from_python (py_v
);
118 if (vitem
->value
== NULL
)
119 gdbpy_print_stack ();
120 vitem
->name
= xstrdup (name
);
122 self
->next_raw_index
++;
123 do_cleanups (back_to
);
127 /* The 'vtable' of pretty-printed python varobj iterators. */
129 static const struct varobj_iter_ops py_varobj_iter_ops
=
135 /* Constructor of pretty-printed varobj iterators. VAR is the varobj
136 whose children the iterator will be iterating over. PYITER is the
137 python iterator actually responsible for the iteration. */
139 static void CPYCHECKER_STEALS_REFERENCE_TO_ARG (3)
140 py_varobj_iter_ctor (struct py_varobj_iter
*self
,
141 struct varobj
*var
, PyObject
*pyiter
)
143 self
->base
.var
= var
;
144 self
->base
.ops
= &py_varobj_iter_ops
;
145 self
->base
.next_raw_index
= 0;
149 /* Allocate and construct a pretty-printed varobj iterator. VAR is
150 the varobj whose children the iterator will be iterating over.
151 PYITER is the python iterator actually responsible for the
154 static struct py_varobj_iter
* CPYCHECKER_STEALS_REFERENCE_TO_ARG (2)
155 py_varobj_iter_new (struct varobj
*var
, PyObject
*pyiter
)
157 struct py_varobj_iter
*self
;
159 self
= XNEW (struct py_varobj_iter
);
160 py_varobj_iter_ctor (self
, var
, pyiter
);
164 /* Return a new pretty-printed varobj iterator suitable to iterate
165 over VAR's children. */
168 py_varobj_get_iterator (struct varobj
*var
, PyObject
*printer
)
173 struct py_varobj_iter
*py_iter
;
174 struct cleanup
*back_to
= varobj_ensure_python_env (var
);
176 if (!PyObject_HasAttr (printer
, gdbpy_children_cst
))
178 do_cleanups (back_to
);
182 children
= PyObject_CallMethodObjArgs (printer
, gdbpy_children_cst
,
184 if (children
== NULL
)
186 gdbpy_print_stack ();
187 error (_("Null value returned for children"));
190 make_cleanup_py_decref (children
);
192 iter
= PyObject_GetIter (children
);
195 gdbpy_print_stack ();
196 error (_("Could not get children iterator"));
199 py_iter
= py_varobj_iter_new (var
, iter
);
201 do_cleanups (back_to
);
203 return &py_iter
->base
;