Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / python / py-varobj.c
CommitLineData
88b9d363 1/* Copyright (C) 2013-2022 Free Software Foundation, Inc.
e5250216
YQ
2
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.
7
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.
12
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/>. */
15
16#include "defs.h"
17#include "python-internal.h"
18#include "varobj.h"
19#include "varobj-iter.h"
20
21/* A dynamic varobj iterator "class" for python pretty-printed
22 varobjs. This inherits struct varobj_iter. */
23
54746ce3 24struct py_varobj_iter : public varobj_iter
e5250216 25{
54746ce3
TT
26 py_varobj_iter (struct varobj *var, gdbpy_ref<> &&pyiter);
27 ~py_varobj_iter () override;
28
60ee72f6 29 std::unique_ptr<varobj_item> next () override;
54746ce3
TT
30
31private:
32
33 /* The varobj this iterator is listing children for. */
34 struct varobj *m_var;
35
36 /* The next raw index we will try to check is available. If it is
37 equal to number_of_children, then we've already iterated the
38 whole set. */
39 int m_next_raw_index = 0;
e5250216
YQ
40
41 /* The python iterator returned by the printer's 'children' method,
42 or NULL if not available. */
54746ce3 43 PyObject *m_iter;
e5250216
YQ
44};
45
46/* Implementation of the 'dtor' method of pretty-printed varobj
47 iterators. */
48
54746ce3 49py_varobj_iter::~py_varobj_iter ()
e5250216 50{
54746ce3
TT
51 gdbpy_enter_varobj enter_py (m_var);
52 Py_XDECREF (m_iter);
e5250216
YQ
53}
54
55/* Implementation of the 'next' method of pretty-printed varobj
56 iterators. */
57
60ee72f6 58std::unique_ptr<varobj_item>
54746ce3 59py_varobj_iter::next ()
e5250216 60{
827f100c
YQ
61 PyObject *py_v;
62 varobj_item *vitem;
63 const char *name = NULL;
64
65 if (!gdb_python_initialized)
66 return NULL;
e5250216 67
54746ce3 68 gdbpy_enter_varobj enter_py (m_var);
e5250216 69
54746ce3 70 gdbpy_ref<> item (PyIter_Next (m_iter));
e5250216
YQ
71
72 if (item == NULL)
73 {
74 /* Normal end of iteration. */
75 if (!PyErr_Occurred ())
76 return NULL;
77
78 /* If we got a memory error, just use the text as the item. */
79 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
80 {
5c329e6a
TT
81 gdbpy_err_fetch fetched_error;
82 gdb::unique_xmalloc_ptr<char> value_str = fetched_error.to_string ();
e5250216
YQ
83 if (value_str == NULL)
84 {
85 gdbpy_print_stack ();
86 return NULL;
87 }
88
7f968c89 89 std::string name_str = string_printf ("<error at %d>",
54746ce3 90 m_next_raw_index++);
7f968c89
TT
91 item.reset (Py_BuildValue ("(ss)", name_str.c_str (),
92 value_str.get ()));
e5250216
YQ
93 if (item == NULL)
94 {
95 gdbpy_print_stack ();
96 return NULL;
97 }
98 }
99 else
100 {
101 /* Any other kind of error. */
102 gdbpy_print_stack ();
103 return NULL;
104 }
105 }
106
788f2586 107 if (!PyArg_ParseTuple (item.get (), "sO", &name, &py_v))
827f100c
YQ
108 {
109 gdbpy_print_stack ();
110 error (_("Invalid item from the child list"));
111 }
112
2f408ecb 113 vitem = new varobj_item ();
11106495 114 vitem->value = release_value (convert_value_from_python (py_v));
827f100c
YQ
115 if (vitem->value == NULL)
116 gdbpy_print_stack ();
2f408ecb 117 vitem->name = name;
827f100c 118
54746ce3 119 m_next_raw_index++;
60ee72f6 120 return std::unique_ptr<varobj_item> (vitem);
e5250216
YQ
121}
122
e5250216
YQ
123/* Constructor of pretty-printed varobj iterators. VAR is the varobj
124 whose children the iterator will be iterating over. PYITER is the
125 python iterator actually responsible for the iteration. */
126
54746ce3
TT
127py_varobj_iter::py_varobj_iter (struct varobj *var, gdbpy_ref<> &&pyiter)
128 : m_var (var),
129 m_iter (pyiter.release ())
e5250216 130{
e5250216
YQ
131}
132
133/* Return a new pretty-printed varobj iterator suitable to iterate
134 over VAR's children. */
135
24fd95b4 136std::unique_ptr<varobj_iter>
e5250216
YQ
137py_varobj_get_iterator (struct varobj *var, PyObject *printer)
138{
6cd67bea 139 gdbpy_enter_varobj enter_py (var);
e5250216
YQ
140
141 if (!PyObject_HasAttr (printer, gdbpy_children_cst))
6cd67bea 142 return NULL;
e5250216 143
7780f186
TT
144 gdbpy_ref<> children (PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
145 NULL));
e5250216
YQ
146 if (children == NULL)
147 {
148 gdbpy_print_stack ();
149 error (_("Null value returned for children"));
150 }
151
1a338907 152 gdbpy_ref<> iter (PyObject_GetIter (children.get ()));
e5250216
YQ
153 if (iter == NULL)
154 {
155 gdbpy_print_stack ();
156 error (_("Could not get children iterator"));
157 }
158
24fd95b4
TT
159 return std::unique_ptr<varobj_iter> (new py_varobj_iter (var,
160 std::move (iter)));
e5250216 161}
This page took 0.870885 seconds and 4 git commands to generate.