aboutsummaryrefslogtreecommitdiff
path: root/gdb/python
diff options
context:
space:
mode:
Diffstat (limited to 'gdb/python')
-rw-r--r--gdb/python/py-style.c21
-rw-r--r--gdb/python/python-internal.h14
-rw-r--r--gdb/python/python.c30
3 files changed, 60 insertions, 5 deletions
diff --git a/gdb/python/py-style.c b/gdb/python/py-style.c
index 51b35f2..cf65e31 100644
--- a/gdb/python/py-style.c
+++ b/gdb/python/py-style.c
@@ -353,6 +353,15 @@ stylepy_init (PyObject *self, PyObject *args, PyObject *kwargs)
+/* See python-internal.h. */
+
+bool
+gdbpy_is_style (PyObject *obj)
+{
+ gdb_assert (obj != nullptr);
+ return PyObject_TypeCheck (obj, &style_object_type);
+}
+
/* Return the ui_file_style for STYLEPY. If the style cannot be found,
then return an empty optional, and set a Python error. */
@@ -369,6 +378,18 @@ stylepy_to_style (style_object *stylepy)
return style;
}
+/* See python-internal.h. */
+
+std::optional<ui_file_style>
+gdbpy_style_object_to_ui_file_style (PyObject *obj)
+{
+ gdb_assert (obj != nullptr);
+ gdb_assert (gdbpy_is_style (obj));
+
+ style_object *style_obj = (style_object *) obj;
+ return stylepy_to_style (style_obj);
+}
+
/* Implementation of gdb.Style.escape_sequence(). Return the escape
sequence to apply Style. If styling is turned off, then this returns
the empty string. Can raise an exception if a named style can no longer
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index f61a175..dbb2d7e 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -570,6 +570,20 @@ struct symtab_and_line *sal_object_to_symtab_and_line (PyObject *obj);
frame_info_ptr frame_object_to_frame_info (PyObject *frame_obj);
struct gdbarch *arch_object_to_gdbarch (PyObject *obj);
+/* Return true if OBJ is a gdb.Style object. OBJ must not be NULL. */
+
+extern bool gdbpy_is_style (PyObject *obj);
+
+/* Return the ui_file_style from OBJ, a gdb.Style object. OBJ must not be
+ NULL.
+
+ It is possible that OBJ is a gdb.Style object, but the underlying style
+ cannot be fetched for some reason. If this happens then a Python error
+ is set and an empty optional is returned. */
+
+extern std::optional<ui_file_style>
+ gdbpy_style_object_to_ui_file_style (PyObject *obj);
+
extern PyObject *gdbpy_execute_mi_command (PyObject *self, PyObject *args,
PyObject *kw);
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 740b196..51e7a0a 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1567,12 +1567,22 @@ static PyObject *
gdbpy_write (PyObject *self, PyObject *args, PyObject *kw)
{
const char *arg;
- static const char *keywords[] = { "text", "stream", NULL };
+ static const char *keywords[] = { "text", "stream", "style", nullptr };
int stream_type = 0;
+ PyObject *style_obj = Py_None;
- if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &arg,
- &stream_type))
- return NULL;
+ if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|iO", keywords, &arg,
+ &stream_type, &style_obj))
+ return nullptr;
+
+ if (style_obj != Py_None && !gdbpy_is_style (style_obj))
+ {
+ PyErr_Format
+ (PyExc_TypeError,
+ _("'style' argument must be gdb.Style or None, not %s."),
+ Py_TYPE (style_obj)->tp_name);
+ return nullptr;
+ }
try
{
@@ -1590,7 +1600,17 @@ gdbpy_write (PyObject *self, PyObject *args, PyObject *kw)
break;
}
- gdb_puts (arg, stream);
+ if (style_obj == Py_None)
+ gdb_puts (arg, stream);
+ else
+ {
+ std::optional<ui_file_style> style
+ = gdbpy_style_object_to_ui_file_style (style_obj);
+ if (!style.has_value ())
+ return nullptr;
+
+ fputs_styled (arg, style.value (), stream);
+ }
}
catch (const gdb_exception &except)
{