binutils-gdb/gdb/python
Andrew Burgess 4b68d4ac98 gdb/python: allow empty gdb.Parameter.__doc__ string
I was recently attempting to create some parameters via the Python
API.  I wanted these parameters to appear similar to how GDB handles
the existing 'style' parameters.

Specifically, I was interested in this behaviour:

  (gdb) help show style filename foreground
  Show the foreground color for this property.
  (gdb) help set style filename foreground
  Set the foreground color for this property.
  (gdb)

Notice how each 'help' command only gets a single line of output.

I tried to reproduce this behaviour via the Python API and was unable.

The problem is that, in order to get just a single line of output like
this, the style parameters are registered with a call to
add_setshow_color_cmd with the 'help_doc' being passed as nullptr.

On the Python side, when parameters are created, the 'help_doc' is
obtained with a call to get_doc_string (python/py-param.c).  This
function either returns the __doc__ string, or a default string: "This
command is not documented.".

To avoid returning the default we could try setting __doc__ to an
empty string, but setting this field to any string means that GDB
prints a line for that string, like this:

  class test_param(gdb.Parameter):
     __doc__ = ""
     def __init__(self, name):
        super ().__init__(name, gdb.COMMAND_NONE, gdb.PARAM_BOOLEAN)
        self.value = True

  test_param('print test')

Then in GDB:

  (gdb) help set print test
  Set the current value of 'print test'.

  (gdb)

The blank line is the problem I'd like to solve.

This commit makes a couple of changes to how parameter doc strings are
handled.

If the doc string is set to an empty string, then GDB now converts
this to nullptr, which removes the blank line problem, the new
behaviour in GDB (for the above `test_param`) is:

  (gdb) help set print test
  Set the current value of 'print test'.
  (gdb)

Next, I noticed that if the set/show docs are set to empty strings,
then the results are less than ideal:

  class test_param(gdb.Parameter):
     set_doc = ""
     def __init__(self, name):
        super ().__init__(name, gdb.COMMAND_NONE, gdb.PARAM_BOOLEAN)
        self.value = True

  test_param('print test')

And in GDB:

  (gdb) help set print test

  This command is not documented.
  (gdb)

So, if the set/show docs are the empty string, GDB now forces these to
be the default string instead, the new behaviour in GDB is:

  (gdb) help set print test
  Set the current value of 'print test'.
  This command is not documented.
  (gdb)

I've added some additional asserts; the set/show docs should always be
non-empty strings, which I believe is the case after this commit.  And
the 'doc' string returned from get_doc_string should never nullptr,
but could be empty.

There are new tests to cover all these changes.
2025-05-13 14:33:20 +01:00
..
lib/gdb gdb/dap: fix decode_source 2025-05-12 11:47:31 -06:00
py-all-events.def Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-arch.c gdb/python: add void_type () method to gdb.Architecture object 2025-02-04 13:56:49 +00:00
py-auto-load.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-block.c gdb/python: add subblocks property to gdb.Block 2025-02-04 13:56:49 +00:00
py-bpevent.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-breakpoint.c gdb/python: remove stray white space in error message 2025-04-22 17:50:55 +01:00
py-cmd.c gdb/python/guile: check for invalid prefixes in Command/Parameter creation 2025-05-13 14:22:22 +01:00
py-color.c gdb/python/guile: check if styling is disabled in Color.escape_sequence 2025-05-06 11:24:28 +01:00
py-color.h Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-connection.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-continueevent.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-dap.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-disasm.c gdb/python: stop using PyObject_IsInstance in py-disasm.c 2025-04-23 15:41:35 +01:00
py-event-types.def Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-event.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-event.h Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-events.h Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-evtregistry.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-evts.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-exitedevent.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-finishbreakpoint.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-frame.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-framefilter.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-function.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-gdb-readline.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-inferior.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-infevents.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-infthread.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-instruction.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-instruction.h Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-lazy-string.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-linetable.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-membuf.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-mi.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-micmd.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-newobjfileevent.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-objfile.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-param.c gdb/python: allow empty gdb.Parameter.__doc__ string 2025-05-13 14:33:20 +01:00
py-prettyprint.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-progspace.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-record-btrace.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-record-btrace.h Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-record-full.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-record-full.h Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-record.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-record.h Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-ref.h Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-registers.c gdb/python: don't use PyObject_IsInstance in py-registers.c 2025-04-23 23:50:22 +01:00
py-signalevent.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-stopevent.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-stopevent.h Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-symbol.c gdb/python: convert gdb.Symbol to use gdbpy_registry 2025-03-19 21:12:53 +00:00
py-symtab.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-threadevent.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-tui.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-type.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-uiout.h Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-unwind.c gdb/python: don't use PyObject_IsInstance in py-unwind.c 2025-04-23 23:54:02 +01:00
py-utils.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-value.c Clean up value_struct_elt_bitpos 2025-04-17 09:25:55 -06:00
py-varobj.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
py-xmethods.c Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
python-config.py Run isort 2024-04-02 11:21:39 -06:00
python-internal.h Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00
python.c [gdb] Handle nullptr gdb_std{err,out} in {gdbpy,ioscm}_flush 2025-04-29 17:01:55 +02:00
python.h Update copyright dates to include 2025 2025-04-08 10:54:39 -06:00