1 /* Copyright (C) 2013-2017 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"
22 /* A dynamic varobj iterator "class" for python pretty-printed
23 varobjs. This inherits struct varobj_iter. */
27 /* The 'base class'. */
28 struct varobj_iter base
;
30 /* The python iterator returned by the printer's 'children' method,
31 or NULL if not available. */
35 /* Implementation of the 'dtor' method of pretty-printed varobj
39 py_varobj_iter_dtor (struct varobj_iter
*self
)
41 struct py_varobj_iter
*dis
= (struct py_varobj_iter
*) self
;
42 struct cleanup
*back_to
= varobj_ensure_python_env (self
->var
);
44 Py_XDECREF (dis
->iter
);
46 do_cleanups (back_to
);
49 /* Implementation of the 'next' method of pretty-printed varobj
53 py_varobj_iter_next (struct varobj_iter
*self
)
55 struct py_varobj_iter
*t
= (struct py_varobj_iter
*) self
;
56 struct cleanup
*back_to
;
60 const char *name
= NULL
;
62 if (!gdb_python_initialized
)
65 back_to
= varobj_ensure_python_env (self
->var
);
67 item
= PyIter_Next (t
->iter
);
71 /* Normal end of iteration. */
72 if (!PyErr_Occurred ())
75 /* If we got a memory error, just use the text as the item. */
76 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error
))
78 PyObject
*type
, *value
, *trace
;
81 PyErr_Fetch (&type
, &value
, &trace
);
82 gdb::unique_xmalloc_ptr
<char>
83 value_str (gdbpy_exception_to_string (type
, value
));
87 if (value_str
== NULL
)
93 name_str
= xstrprintf ("<error at %d>",
94 self
->next_raw_index
++);
95 item
= Py_BuildValue ("(ss)", name_str
, value_str
.get ());
105 /* Any other kind of error. */
106 gdbpy_print_stack ();
111 if (!PyArg_ParseTuple (item
, "sO", &name
, &py_v
))
113 gdbpy_print_stack ();
114 error (_("Invalid item from the child list"));
117 vitem
= new varobj_item ();
118 vitem
->value
= convert_value_from_python (py_v
);
119 if (vitem
->value
== NULL
)
120 gdbpy_print_stack ();
123 self
->next_raw_index
++;
124 do_cleanups (back_to
);
128 /* The 'vtable' of pretty-printed python varobj iterators. */
130 static const struct varobj_iter_ops py_varobj_iter_ops
=
136 /* Constructor of pretty-printed varobj iterators. VAR is the varobj
137 whose children the iterator will be iterating over. PYITER is the
138 python iterator actually responsible for the iteration. */
140 static void CPYCHECKER_STEALS_REFERENCE_TO_ARG (3)
141 py_varobj_iter_ctor (struct py_varobj_iter
*self
,
142 struct varobj
*var
, PyObject
*pyiter
)
144 self
->base
.var
= var
;
145 self
->base
.ops
= &py_varobj_iter_ops
;
146 self
->base
.next_raw_index
= 0;
150 /* Allocate and construct a pretty-printed varobj iterator. VAR is
151 the varobj whose children the iterator will be iterating over.
152 PYITER is the python iterator actually responsible for the
155 static struct py_varobj_iter
* CPYCHECKER_STEALS_REFERENCE_TO_ARG (2)
156 py_varobj_iter_new (struct varobj
*var
, PyObject
*pyiter
)
158 struct py_varobj_iter
*self
;
160 self
= XNEW (struct py_varobj_iter
);
161 py_varobj_iter_ctor (self
, var
, pyiter
);
165 /* Return a new pretty-printed varobj iterator suitable to iterate
166 over VAR's children. */
169 py_varobj_get_iterator (struct varobj
*var
, PyObject
*printer
)
172 struct py_varobj_iter
*py_iter
;
174 gdbpy_enter_varobj
enter_py (var
);
176 if (!PyObject_HasAttr (printer
, gdbpy_children_cst
))
179 gdbpy_ref
children (PyObject_CallMethodObjArgs (printer
, gdbpy_children_cst
,
181 if (children
== NULL
)
183 gdbpy_print_stack ();
184 error (_("Null value returned for children"));
187 iter
= PyObject_GetIter (children
.get ());
190 gdbpy_print_stack ();
191 error (_("Could not get children iterator"));
194 py_iter
= py_varobj_iter_new (var
, iter
);
196 return &py_iter
->base
;