This is the mail archive of the
archer@sourceware.org
mailing list for the Archer project.
[python] add parse_and_eval
- From: Tom Tromey <tromey at redhat dot com>
- To: Project Archer <archer at sourceware dot org>
- Date: Mon, 01 Dec 2008 12:09:30 -0700
- Subject: [python] add parse_and_eval
- Reply-to: tromey at redhat dot com
This adds a gdb.parse_and_eval function. This parses a string
as an expression, according to the current language, and then
evaluates it.
Before this you had to do a hack involving 'print' to construct a
Value from an expression. Weird!
Tom
2008-12-01 Tom Tromey <tromey@redhat.com>
* python/python.c (GdbMethods) <parse_and_eval>: New.
(gdbpy_parse_and_eval): New function.
2008-12-01 Tom Tromey <tromey@redhat.com>
* gdb.texinfo (Basic Python): Document parse_and_eval.
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 38d3f1e..dfc19aa 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -18099,6 +18099,13 @@ If the named parameter does not exist, this function throws a
a Python value of the appropriate type, and returned.
@end defun
+@findex gdb.parse_and_eval
+@defun parse_and_eval expression
+Parse @var{expression} as an expression in the current language,
+evaluate it, and return the result as a @code{gdb.Value}.
+@var{expression} must be a string.
+@end defun
+
@findex gdb.write
@defun write string
Print a string to @value{GDBN}'s paginated standard output stream.
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 7b46f0d..bb0b428 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -394,6 +394,26 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
Py_RETURN_NONE;
}
+/* Parse a string and evaluate it as an expression. */
+static PyObject *
+gdbpy_parse_and_eval (PyObject *self, PyObject *args)
+{
+ char *expr_str;
+ struct value *result = NULL;
+ volatile struct gdb_exception except;
+
+ if (!PyArg_ParseTuple (args, "s", &expr_str))
+ return NULL;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ result = parse_and_eval (expr_str);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
+ return value_to_value_object (result);
+}
+
/* Threads. */
@@ -1374,6 +1394,9 @@ Note: may later change to return an object." },
{ "switch_to_thread", gdbpy_switch_to_thread, METH_VARARGS,
"Switch to a thread, given the thread ID." },
+ { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS,
+ "Parse a string as an expression, evaluate it, and return the result." },
+
{ "write", gdbpy_write, METH_VARARGS,
"Write a string using gdb's filtered stream." },
{ "flush", gdbpy_flush, METH_NOARGS,