Add basic Python API for convenience variables

This adds a basic Python API for accessing convenience variables.
With this, convenience variables can be read and set from Python.
Although gdb supports convenience variables whose value changes at
each call, this is not exposed to Python; it could be, but I think
it's just as good to write a convenience function in this situation.

This is PR python/23080.

Tested on x86-64 Fedora 26.

2018-04-22  Tom Tromey  <tom@tromey.com>

	PR python/23080:
	* NEWS: Update for new functions.
	* python/py-value.c (gdbpy_set_convenience_variable)
	(gdbpy_convenience_variable): New functions.
	* python/python-internal.h (gdbpy_convenience_variable)
	(gdbpy_set_convenience_variable): Declare.
	* python/python.c (python_GdbMethods): Add convenience_variable,
	set_convenience_variable.

doc/ChangeLog
2018-04-22  Tom Tromey  <tom@tromey.com>

	PR python/23080:
	* python.texi (Basic Python): Document gdb.convenience_variable,
	gdb.set_convenience_variable.

testsuite/ChangeLog
2018-04-22  Tom Tromey  <tom@tromey.com>

	PR python/23080:
	* gdb.python/python.exp: Add convenience variable tests.
This commit is contained in:
Tom Tromey 2018-04-22 15:13:09 -06:00
parent 8a60efe714
commit 7729052b53
6 changed files with 132 additions and 2 deletions

View file

@ -1746,6 +1746,83 @@ gdbpy_history (PyObject *self, PyObject *args)
return value_to_value_object (res_val);
}
/* Return the value of a convenience variable. */
PyObject *
gdbpy_convenience_variable (PyObject *self, PyObject *args)
{
const char *varname;
struct value *res_val = NULL;
if (!PyArg_ParseTuple (args, "s", &varname))
return NULL;
TRY
{
struct internalvar *var = lookup_only_internalvar (varname);
if (var != NULL)
{
res_val = value_of_internalvar (python_gdbarch, var);
if (TYPE_CODE (value_type (res_val)) == TYPE_CODE_VOID)
res_val = NULL;
}
}
CATCH (except, RETURN_MASK_ALL)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
END_CATCH
if (res_val == NULL)
Py_RETURN_NONE;
return value_to_value_object (res_val);
}
/* Set the value of a convenience variable. */
PyObject *
gdbpy_set_convenience_variable (PyObject *self, PyObject *args)
{
const char *varname;
PyObject *value_obj;
struct value *value = NULL;
if (!PyArg_ParseTuple (args, "sO", &varname, &value_obj))
return NULL;
/* None means to clear the variable. */
if (value_obj != Py_None)
{
value = convert_value_from_python (value_obj);
if (value == NULL)
return NULL;
}
TRY
{
if (value == NULL)
{
struct internalvar *var = lookup_only_internalvar (varname);
if (var != NULL)
clear_internalvar (var);
}
else
{
struct internalvar *var = lookup_internalvar (varname);
set_internalvar (var, value);
}
}
CATCH (except, RETURN_MASK_ALL)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
END_CATCH
Py_RETURN_NONE;
}
/* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
int