summaryrefslogtreecommitdiff
path: root/gdb/python/py-varobj.c
diff options
context:
space:
mode:
authorTom Tromey <tromey@adacore.com>2022-06-06 09:54:45 -0600
committerTom Tromey <tromey@adacore.com>2022-07-15 09:25:33 -0600
commitc4a3dbaf1132105586586617a59d0e7566eefd41 (patch)
tree60fdbcc109b1a55a76ce8070f6b4b7c81dc6c38f /gdb/python/py-varobj.c
parentaa63b0a77e193b5181390f19465f33c314d0490e (diff)
downloadbinutils-gdb-c4a3dbaf1132105586586617a59d0e7566eefd41.tar.gz
Expose current 'print' settings to Python
PR python/17291 asks for access to the current print options. While I think this need is largely satisfied by the existence of Value.format_string, it seemed to me that a bit more could be done. First, while Value.format_string uses the user's settings, it does not react to temporary settings such as "print/x". This patch changes this. Second, there is no good way to examine the current settings (in particular the temporary ones in effect for just a single "print"). This patch adds this as well. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=17291
Diffstat (limited to 'gdb/python/py-varobj.c')
-rw-r--r--gdb/python/py-varobj.c25
1 files changed, 20 insertions, 5 deletions
diff --git a/gdb/python/py-varobj.c b/gdb/python/py-varobj.c
index 372c91125d9..9e4fb6c58bb 100644
--- a/gdb/python/py-varobj.c
+++ b/gdb/python/py-varobj.c
@@ -17,13 +17,15 @@
#include "python-internal.h"
#include "varobj.h"
#include "varobj-iter.h"
+#include "valprint.h"
/* A dynamic varobj iterator "class" for python pretty-printed
varobjs. This inherits struct varobj_iter. */
struct py_varobj_iter : public varobj_iter
{
- py_varobj_iter (struct varobj *var, gdbpy_ref<> &&pyiter);
+ py_varobj_iter (struct varobj *var, gdbpy_ref<> &&pyiter,
+ const value_print_options *opts);
~py_varobj_iter () override;
std::unique_ptr<varobj_item> next () override;
@@ -41,6 +43,9 @@ private:
/* The python iterator returned by the printer's 'children' method,
or NULL if not available. */
PyObject *m_iter;
+
+ /* The print options to use. */
+ value_print_options m_opts;
};
/* Implementation of the 'dtor' method of pretty-printed varobj
@@ -67,6 +72,9 @@ py_varobj_iter::next ()
gdbpy_enter_varobj enter_py (m_var);
+ scoped_restore set_options = make_scoped_restore (&gdbpy_current_print_options,
+ &m_opts);
+
gdbpy_ref<> item (PyIter_Next (m_iter));
if (item == NULL)
@@ -124,9 +132,11 @@ py_varobj_iter::next ()
whose children the iterator will be iterating over. PYITER is the
python iterator actually responsible for the iteration. */
-py_varobj_iter::py_varobj_iter (struct varobj *var, gdbpy_ref<> &&pyiter)
+py_varobj_iter::py_varobj_iter (struct varobj *var, gdbpy_ref<> &&pyiter,
+ const value_print_options *opts)
: m_var (var),
- m_iter (pyiter.release ())
+ m_iter (pyiter.release ()),
+ m_opts (*opts)
{
}
@@ -134,13 +144,17 @@ py_varobj_iter::py_varobj_iter (struct varobj *var, gdbpy_ref<> &&pyiter)
over VAR's children. */
std::unique_ptr<varobj_iter>
-py_varobj_get_iterator (struct varobj *var, PyObject *printer)
+py_varobj_get_iterator (struct varobj *var, PyObject *printer,
+ const value_print_options *opts)
{
gdbpy_enter_varobj enter_py (var);
if (!PyObject_HasAttr (printer, gdbpy_children_cst))
return NULL;
+ scoped_restore set_options = make_scoped_restore (&gdbpy_current_print_options,
+ opts);
+
gdbpy_ref<> children (PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
NULL));
if (children == NULL)
@@ -157,5 +171,6 @@ py_varobj_get_iterator (struct varobj *var, PyObject *printer)
}
return std::unique_ptr<varobj_iter> (new py_varobj_iter (var,
- std::move (iter)));
+ std::move (iter),
+ opts));
}