This is the mail archive of the
archer@sourceware.org
mailing list for the Archer project.
[python] allow set- and show- specific help for Parameters
- From: Tom Tromey <tromey at redhat dot com>
- To: Project Archer <archer at sourceware dot org>
- Date: Tue, 18 Nov 2008 08:53:38 -0700
- Subject: [python] allow set- and show- specific help for Parameters
- Reply-to: Tom Tromey <tromey at redhat dot com>
This changes the Parameter code to allow set- and show-specific help.
I also made a new "_cst" global and fixed the *_object_type
initializers.
Tom
2008-11-18 Tom Tromey <tromey@redhat.com>
* python/python-internal.h (gdbpy_doc_cst): Declare.
* python/python-cmd.c (cmdpy_init): Use gdbpy_doc_cst. Don't set
tp_new or tp_init.
(cmdpy_object_type): Add extra entries. Set tp_init and tp_new.
* python/python.c (gdbpy_doc_cst): New global.
(_initialize_python): Initialize it.
* python/python-param.c (set_doc_cst, show_doc_cst): New globals.
(gdbpy_initialize_parameters): Initialize them. Don't set
tp_new or tp_init.
(parmpy_init): Use gdbpy_doc_cst. Get set and show help. Don't
examine __doc__.
(get_doc_string): New function.
(parmpy_object_type): Add extra entries. Set tp_init and tp_new.
2008-11-18 Tom Tromey <tromey@redhat.com>
* gdb.texinfo (Parameters In Python): Document set_doc, show_doc.
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index eb24ece..9446ce4 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -18553,6 +18553,20 @@ documentation string for the parameter's class, if there is one. If
there is no documentation string, a default value is used.
@end defmethod
+@defivar Parameter set_doc
+If this attribute exists, and is a string, then its value is used as
+the help text for this parameter's @code{set} command. The value is
+examined when @code{Parameter.__init__} is invoked; subsequent changes
+have no effect.
+@end defivar
+
+@defivar Parameter show_doc
+If this attribute exists, and is a string, then its value is used as
+the help text for this parameter's @code{show} command. The value is
+examined when @code{Parameter.__init__} is invoked; subsequent changes
+have no effect.
+@end defivar
+
@defivar Parameter value
The @code{value} attribute holds the underlying value of the
parameter. It can be read and assigned to just as any other
diff --git a/gdb/python/python-cmd.c b/gdb/python/python-cmd.c
index 63b5432..91cdc7f 100644
--- a/gdb/python/python-cmd.c
+++ b/gdb/python/python-cmd.c
@@ -389,9 +389,9 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject *kwds)
if (! cmd_name)
return -1;
- if (PyObject_HasAttrString (self, "__doc__"))
+ if (PyObject_HasAttr (self, gdbpy_doc_cst))
{
- PyObject *ds_obj = PyObject_GetAttrString (self, "__doc__");
+ PyObject *ds_obj = PyObject_GetAttr (self, gdbpy_doc_cst);
if (ds_obj && gdbpy_is_string (ds_obj))
docstring = python_string_to_host_string (ds_obj);
}
@@ -437,8 +437,6 @@ gdbpy_initialize_commands (void)
{
int i;
- cmdpy_object_type.tp_new = PyType_GenericNew;
- cmdpy_object_type.tp_init = cmdpy_init;
if (PyType_Ready (&cmdpy_object_type) < 0)
return;
@@ -515,5 +513,15 @@ static PyTypeObject cmdpy_object_type =
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
- cmdpy_object_methods /* tp_methods */
+ cmdpy_object_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ cmdpy_init, /* tp_init */
+ 0, /* tp_alloc */
+ PyType_GenericNew /* tp_new */
};
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 19f39cb..a03d889 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -136,5 +136,6 @@ char *gdbpy_get_display_hint (PyObject *printer);
extern PyObject *gdbpy_children_cst;
extern PyObject *gdbpy_to_string_cst;
extern PyObject *gdbpy_display_hint_cst;
+extern PyObject *gdbpy_doc_cst;
#endif /* GDB_PYTHON_INTERNAL_H */
diff --git a/gdb/python/python-param.c b/gdb/python/python-param.c
index cf1ceaa..96c92c8 100644
--- a/gdb/python/python-param.c
+++ b/gdb/python/python-param.c
@@ -89,6 +89,10 @@ typedef struct parmpy_object parmpy_object;
static PyTypeObject parmpy_object_type;
+/* Some handy string constants. */
+static PyObject *set_doc_cst;
+static PyObject *show_doc_cst;
+
/* Get an attribute. */
@@ -374,6 +378,23 @@ compute_enum_values (parmpy_object *self, PyObject *enum_values)
return 1;
}
+/* A helper function which returns a documentation string for an
+ object. */
+static char *
+get_doc_string (PyObject *object, PyObject *attr)
+{
+ char *result = NULL;
+ if (PyObject_HasAttr (object, attr))
+ {
+ PyObject *ds_obj = PyObject_GetAttr (object, attr);
+ if (ds_obj && gdbpy_is_string (ds_obj))
+ result = python_string_to_host_string (ds_obj);
+ }
+ if (! result)
+ result = xstrdup ("This command is not documented.");
+ return result;
+}
+
/* Object initializer; sets up gdb-side structures for command.
Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
@@ -401,7 +422,7 @@ parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
{
parmpy_object *obj = (parmpy_object *) self;
char *name;
- char *docstring = NULL;
+ char *set_doc, *show_doc;
char *cmd_name;
int parmclass, cmdtype;
PyObject *enum_values = NULL;
@@ -458,15 +479,10 @@ parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
if (! cmd_name)
return -1;
- /* FIXME: need set and show docs. */
- if (PyObject_HasAttrString (self, "__doc__"))
- {
- PyObject *ds_obj = PyObject_GetAttrString (self, "__doc__");
- if (ds_obj && gdbpy_is_string (ds_obj))
- docstring = python_string_to_host_string (ds_obj);
- }
- if (! docstring)
- docstring = xstrdup ("This command is not documented.");
+ /* FIXME: there is no way to register a destructor function for
+ set/show commands. So, these are leaked. */
+ set_doc = get_doc_string (self, set_doc_cst);
+ show_doc = get_doc_string (self, show_doc_cst);
Py_INCREF (self);
@@ -474,13 +490,14 @@ parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
{
add_setshow_generic (parmclass, (enum command_class) cmdtype,
cmd_name, obj,
- "FIXME: set doc", "FIXME: show doc",
- docstring, set_list, show_list);
+ set_doc, show_doc,
+ NULL, set_list, show_list);
}
if (except.reason < 0)
{
xfree (cmd_name);
- xfree (docstring);
+ xfree (set_doc);
+ xfree (show_doc);
Py_DECREF (self);
PyErr_Format (except.reason == RETURN_QUIT
? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
@@ -498,11 +515,16 @@ gdbpy_initialize_parameters (void)
{
int i;
- parmpy_object_type.tp_new = PyType_GenericNew;
- parmpy_object_type.tp_init = parmpy_init;
if (PyType_Ready (&parmpy_object_type) < 0)
return;
+ set_doc_cst = PyString_FromString ("set_doc");
+ if (! set_doc_cst)
+ return;
+ show_doc_cst = PyString_FromString ("show_doc");
+ if (! show_doc_cst)
+ return;
+
for (i = 0; parm_constants[i].name; ++i)
{
if (PyModule_AddIntConstant (gdb_module,
@@ -548,5 +570,15 @@ static PyTypeObject parmpy_object_type =
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
- 0 /* tp_methods */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ parmpy_init, /* tp_init */
+ 0, /* tp_alloc */
+ PyType_GenericNew /* tp_new */
};
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 1ff3c25..340213f 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -66,6 +66,7 @@ PyObject *gdb_module;
PyObject *gdbpy_to_string_cst;
PyObject *gdbpy_children_cst;
PyObject *gdbpy_display_hint_cst;
+PyObject *gdbpy_doc_cst;
/* Given a command_line, return a command string suitable for passing
to Python. Lines in the string are separated by newlines. The
@@ -1276,6 +1277,7 @@ Enables or disables auto-loading of Python code when an object is opened."),
gdbpy_to_string_cst = PyString_FromString ("to_string");
gdbpy_children_cst = PyString_FromString ("children");
gdbpy_display_hint_cst = PyString_FromString ("display_hint");
+ gdbpy_doc_cst = PyString_FromString ("__doc__");
/* Create a couple objects which are used for Python's stdout and
stderr. */