This is the mail archive of the mailing list for the Archer project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[python] add parse_and_eval

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!


2008-12-01  Tom Tromey  <>

	* python/python.c (GdbMethods) <parse_and_eval>: New.
	(gdbpy_parse_and_eval): New function.

2008-12-01  Tom Tromey  <>

	* 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)
+/* 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;
+    {
+      result = parse_and_eval (expr_str);
+    }
+  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,

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]