1 /* Copyright (C) 2013-2024 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. */
25 struct py_varobj_iter
: public varobj_iter
27 py_varobj_iter (struct varobj
*var
, gdbpy_ref
<> &&pyiter
,
28 const value_print_options
*opts
);
29 ~py_varobj_iter () override
;
31 std::unique_ptr
<varobj_item
> next () override
;
35 /* The varobj this iterator is listing children for. */
38 /* The next raw index we will try to check is available. If it is
39 equal to number_of_children, then we've already iterated the
41 int m_next_raw_index
= 0;
43 /* The python iterator returned by the printer's 'children' method,
44 or NULL if not available. */
47 /* The print options to use. */
48 value_print_options m_opts
;
51 /* Implementation of the 'dtor' method of pretty-printed varobj
54 py_varobj_iter::~py_varobj_iter ()
56 gdbpy_enter_varobj
enter_py (m_var
);
60 /* Implementation of the 'next' method of pretty-printed varobj
63 std::unique_ptr
<varobj_item
>
64 py_varobj_iter::next ()
68 const char *name
= NULL
;
70 if (!gdb_python_initialized
)
73 gdbpy_enter_varobj
enter_py (m_var
);
75 scoped_restore set_options
= make_scoped_restore (&gdbpy_current_print_options
,
78 gdbpy_ref
<> item (PyIter_Next (m_iter
));
82 /* Normal end of iteration. */
83 if (!PyErr_Occurred ())
86 /* If we got a memory error, just use the text as the item. */
87 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error
))
89 gdbpy_err_fetch fetched_error
;
90 gdb::unique_xmalloc_ptr
<char> value_str
= fetched_error
.to_string ();
91 if (value_str
== NULL
)
97 std::string name_str
= string_printf ("<error at %d>",
99 item
.reset (Py_BuildValue ("(ss)", name_str
.c_str (),
103 gdbpy_print_stack ();
109 /* Any other kind of error. */
110 gdbpy_print_stack ();
115 if (!PyArg_ParseTuple (item
.get (), "sO", &name
, &py_v
))
117 gdbpy_print_stack ();
118 error (_("Invalid item from the child list"));
121 vitem
= new varobj_item ();
122 vitem
->value
= release_value (convert_value_from_python (py_v
));
123 if (vitem
->value
== NULL
)
124 gdbpy_print_stack ();
128 return std::unique_ptr
<varobj_item
> (vitem
);
131 /* Constructor of pretty-printed varobj iterators. VAR is the varobj
132 whose children the iterator will be iterating over. PYITER is the
133 python iterator actually responsible for the iteration. */
135 py_varobj_iter::py_varobj_iter (struct varobj
*var
, gdbpy_ref
<> &&pyiter
,
136 const value_print_options
*opts
)
138 m_iter (pyiter
.release ()),
143 /* Return a new pretty-printed varobj iterator suitable to iterate
144 over VAR's children. */
146 std::unique_ptr
<varobj_iter
>
147 py_varobj_get_iterator (struct varobj
*var
, PyObject
*printer
,
148 const value_print_options
*opts
)
150 gdbpy_enter_varobj
enter_py (var
);
152 if (!PyObject_HasAttr (printer
, gdbpy_children_cst
))
155 scoped_restore set_options
= make_scoped_restore (&gdbpy_current_print_options
,
158 gdbpy_ref
<> children (PyObject_CallMethodObjArgs (printer
, gdbpy_children_cst
,
160 if (children
== NULL
)
162 gdbpy_print_stack ();
163 error (_("Null value returned for children"));
166 gdbpy_ref
<> iter (PyObject_GetIter (children
.get ()));
169 gdbpy_print_stack ();
170 error (_("Could not get children iterator"));
173 return std::make_unique
<py_varobj_iter
> (var
, std::move (iter
), opts
);