]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/python: allow Value.format_string to return styled output
authorAndrew Burgess <aburgess@redhat.com>
Mon, 24 Jan 2022 15:29:49 +0000 (15:29 +0000)
committerAndrew Burgess <aburgess@redhat.com>
Mon, 7 Feb 2022 16:52:47 +0000 (16:52 +0000)
Add a new argument to the gdb.Value.format_string method, 'styling'.
This argument is False by default.

When this argument is True, then the returned string can contain output
styling escape sequences.

When this argument is False, then the returned string will not contain
any styling escape sequences.

If the returned string is going to be printed to the user, then it is
often nice to retain the GDB styling.

For the testing, we need to adjust the TERM environment variable, as
we do for all the styling tests.  I'm now running all of the C tests
in gdb.python/py-format-string.exp in an environment where styling
could be generated, but only my new test should actually produce
styled output, hopefully this will catch the case where a bug might
cause format_string to always produce styled output.

gdb/NEWS
gdb/doc/python.texi
gdb/python/py-value.c
gdb/testsuite/gdb.python/py-format-string.exp

index c739930bfc7a665275e0f54d00a28706a0728279..b4a515120db4950b5692d429c5294d9a23091f95 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -168,6 +168,13 @@ info win
      manager that temporarily sets the gdb parameter NAME to VALUE,
      then resets it when the context is exited.
 
+  ** The gdb.Value.format_string method now takes a 'styling'
+     argument, which is a boolean.  When true, the returned string can
+     include escape sequences to apply styling.  The styling will only
+     be present if styling is otherwise turned on in GDB (see 'help
+     set styling').  When false, which is the default if the argument
+     is not given, then no styling is applied to the returned string.
+
 * New features in the GDB remote stub, GDBserver
 
   ** GDBserver is now supported on OpenRISC GNU/Linux.
index da88b8a7690f85a6ac9fa9f07763d9c98c693d76..c1a3f5f2a7eca65e9cec61594d14735bd6c757bb 100644 (file)
@@ -1079,6 +1079,16 @@ A string containing a single character representing the format to use for
 the returned string.  For instance, @code{'x'} is equivalent to using the
 @value{GDBN} command @code{print} with the @code{/x} option and formats
 the value as a hexadecimal number.
+
+@item styling
+@code{True} if @value{GDBN} should apply styling to the returned
+string.  When styling is applied, the returned string might contain
+ANSI terminal escape sequences.  Escape sequences will only be
+included if styling is turned on, see @ref{Output Styling}.
+Additionally, @value{GDBN} only styles some value contents, so not
+every output string will contain escape sequences.
+
+When @code{False}, which is the default, no output styling is applied.
 @end table
 @end defun
 
index 6401d96897f40d9b8e1477856bed3fcf8f8eea73..b546344da955054f750151ab3e3e364dd1ea9aae 100644 (file)
@@ -639,6 +639,7 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
       "symbols",               /* See set print symbol on|off.  */
       "unions",                        /* See set print union on|off.  */
       "address",               /* See set print address on|off.  */
+      "styling",               /* Should we apply styling.  */
       /* C++ options.  */
       "deref_refs",            /* No corresponding setting.  */
       "actual_objects",                /* See set print object on|off.  */
@@ -683,13 +684,14 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
   PyObject *symbols_obj = NULL;
   PyObject *unions_obj = NULL;
   PyObject *address_obj = NULL;
+  PyObject *styling_obj = Py_False;
   PyObject *deref_refs_obj = NULL;
   PyObject *actual_objects_obj = NULL;
   PyObject *static_members_obj = NULL;
   char *format = NULL;
   if (!gdb_PyArg_ParseTupleAndKeywords (args,
                                        kw,
-                                       "|O!O!O!O!O!O!O!O!O!O!IIIs",
+                                       "|O!O!O!O!O!O!O!O!O!O!O!IIIs",
                                        keywords,
                                        &PyBool_Type, &raw_obj,
                                        &PyBool_Type, &pretty_arrays_obj,
@@ -698,6 +700,7 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
                                        &PyBool_Type, &symbols_obj,
                                        &PyBool_Type, &unions_obj,
                                        &PyBool_Type, &address_obj,
+                                       &PyBool_Type, &styling_obj,
                                        &PyBool_Type, &deref_refs_obj,
                                        &PyBool_Type, &actual_objects_obj,
                                        &PyBool_Type, &static_members_obj,
@@ -752,7 +755,7 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
        }
     }
 
-  string_file stb;
+  string_file stb (PyObject_IsTrue (styling_obj));
 
   try
     {
index 4c78bed120324489d4ba6fa954924edba0e9b66a..ac1353ea2e3b69cf75a69577b35f7258d25c55e4 100644 (file)
@@ -991,6 +991,13 @@ proc_with_prefix test_invalid_args {} {
     "ValueError: a single character is required.*"
 }
 
+# Check the styling argument to format_string.  This function needs to
+# be called with TERM set such that styling can be applied.
+proc test_styling {} {
+    gdb_test "python print(gdb.parse_and_eval(\"a_point_t\").format_string(styling=True, raw=True))" \
+       "{[style x variable] = 42, [style y variable] = 12}"
+}
+
 # Run all the tests in common for both C and C++.
 proc_with_prefix test_all_common {} {
   # No options.
@@ -1023,9 +1030,16 @@ with_test_prefix "format_string" {
   # Perform C Tests.
   if { [build_inferior "${binfile}" "c"] == 0 } {
     with_test_prefix "lang_c" {
-      set current_lang "c"
-      prepare_gdb "${binfile}"
-      test_all_common
+      save_vars { env(TERM) } {
+       # We run all of these tests in an environment where styling
+       # could work, but we only expect the final call to
+       # test_styling to actually produce any styled output.
+       setenv TERM ansi
+       set current_lang "c"
+       prepare_gdb "${binfile}"
+       test_all_common
+       test_styling
+      }
     }
   }