Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* Patch: add gdb.parse_and_eval
@ 2009-06-11 20:49 Tom Tromey
  2009-06-11 22:40 ` Eli Zaretskii
  0 siblings, 1 reply; 4+ messages in thread
From: Tom Tromey @ 2009-06-11 20:49 UTC (permalink / raw)
  To: gdb-patches

This adds the function "gdb.parse_and_eval" to the Python API.
This is handy when writing new commands, among other things.

It needs a doc review.

Tom

2009-06-11  Tom Tromey  <tromey@redhat.com>

	* python/python.c (gdbpy_parse_and_eval): New function.
	(GdbMethods): Add "parse_and_eval".

2009-06-11  Tom Tromey  <tromey@redhat.com>

	* gdb.texinfo (Basic Python): Document gdb.parse_and_eval.

2009-06-11  Tom Tromey  <tromey@redhat.com>

	* gdb.python/python-value.exp (test_parse_and_eval): New
	function.

Index: doc/gdb.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v
retrieving revision 1.599
diff -u -r1.599 gdb.texinfo
--- doc/gdb.texinfo	11 Jun 2009 11:57:46 -0000	1.599
+++ doc/gdb.texinfo	11 Jun 2009 20:48:30 -0000
@@ -18611,6 +18611,13 @@
 @code{gdb.Value} (@pxref{Values From Inferior}).
 @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.
Index: python/python.c
===================================================================
RCS file: /cvs/src/src/gdb/python/python.c,v
retrieving revision 1.18
diff -u -r1.18 python.c
--- python/python.c	28 May 2009 16:49:55 -0000	1.18
+++ python/python.c	11 Jun 2009 20:48:30 -0000
@@ -275,6 +275,26 @@
   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);
+}
+
 \f
 
 /* Printing.  */
@@ -635,6 +655,11 @@
     "lookup_type (name [, block]) -> type\n\
 Return a Type corresponding to the given name." },
 
+  { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS,
+    "parse_and_eval (String) -> Value.\n\
+Parse String as an expression, evaluate it, and return the result as a Value."
+  },
+
   { "write", gdbpy_write, METH_VARARGS,
     "Write a string using gdb's filtered stream." },
   { "flush", gdbpy_flush, METH_NOARGS,
Index: testsuite/gdb.python/python-value.exp
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.python/python-value.exp,v
retrieving revision 1.8
diff -u -r1.8 python-value.exp
--- testsuite/gdb.python/python-value.exp	28 May 2009 00:47:20 -0000	1.8
+++ testsuite/gdb.python/python-value.exp	11 Jun 2009 20:48:32 -0000
@@ -273,6 +273,17 @@
     "print value's type"
 }
 
+# A few tests of gdb.parse_and_eval.
+proc test_parse_and_eval {} {
+  gdb_test "python print gdb.parse_and_eval ('23')" "23" \
+    "parse_and_eval constant test"
+  gdb_test "python print gdb.parse_and_eval ('5 + 7')" "12" \
+    "parse_and_eval simple expression test"
+  gdb_test "python print type(gdb.parse_and_eval ('5 + 7'))" \
+    ".type 'gdb.Value'."\
+    "parse_and_eval type test"
+}
+
 # Start with a fresh gdb.
 
 gdb_exit
@@ -293,6 +304,7 @@
 test_value_boolean
 test_value_compare
 test_objfiles
+test_parse_and_eval
 
 # The following tests require execution.
 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Patch: add gdb.parse_and_eval
  2009-06-11 20:49 Patch: add gdb.parse_and_eval Tom Tromey
@ 2009-06-11 22:40 ` Eli Zaretskii
  2009-12-02 21:57   ` Tom Tromey
  0 siblings, 1 reply; 4+ messages in thread
From: Eli Zaretskii @ 2009-06-11 22:40 UTC (permalink / raw)
  To: tromey; +Cc: gdb-patches

> From: Tom Tromey <tromey@redhat.com>
> Reply-To: tromey@redhat.com
> Date: Thu, 11 Jun 2009 14:49:16 -0600
> 
> +@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

Thanks.  This is okay, but given the recent discussions which
indicated that this feature is necessary for a broad class of scripts,
I would expect a bit more text in the manual about it, describing when
and how this would be useful.

The user manual is not just a reference list of existing commands and
options.  It contains, in addition, the glue: explanations of how
these features fit together to support important use-cases one meets
while debugging programs.  Without this glue, the manual would be a
much less useful document.

TIA


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Patch: add gdb.parse_and_eval
  2009-06-11 22:40 ` Eli Zaretskii
@ 2009-12-02 21:57   ` Tom Tromey
  2009-12-03  3:51     ` Eli Zaretskii
  0 siblings, 1 reply; 4+ messages in thread
From: Tom Tromey @ 2009-12-02 21:57 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: gdb-patches

>>>>> "Eli" == Eli Zaretskii <eliz@gnu.org> writes:

>> +@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

Eli> Thanks.  This is okay, but given the recent discussions which
Eli> indicated that this feature is necessary for a broad class of scripts,
Eli> I would expect a bit more text in the manual about it, describing when
Eli> and how this would be useful.

How about this?

Tom

2009-12-02  Tom Tromey  <tromey@redhat.com>

	* python/python.c (gdbpy_parse_and_eval): New function.
	(GdbMethods): Add "parse_and_eval".

2009-12-02  Tom Tromey  <tromey@redhat.com>

	* gdb.texinfo (Basic Python): Document gdb.parse_and_eval.

2009-12-02  Tom Tromey  <tromey@redhat.com>

	* gdb.python/py-value.exp (test_parse_and_eval): New
	function.

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 74cbedf..7e51690 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -19328,6 +19328,19 @@ If no exception is raised, the return value is always an instance of
 @code{gdb.Value} (@pxref{Values From Inferior}).
 @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.
+
+This function can be useful when implementing a new command
+(@pxref{Commands In Python}), as it provides a way to parse the
+command's argument as an expression.  It is also useful simply to
+compute values, for example, it is the only way to get the value of a
+convenience variable (@pxref{Convenience Vars}) as a @code{gdb.Value}.
+@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 77a0069..23e94a5 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -323,6 +323,26 @@ execute_gdb_command (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);
+}
+
 \f
 
 /* Printing.  */
@@ -680,6 +700,11 @@ Return a string explaining unwind stop reason." },
     "lookup_type (name [, block]) -> type\n\
 Return a Type corresponding to the given name." },
 
+  { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS,
+    "parse_and_eval (String) -> Value.\n\
+Parse String as an expression, evaluate it, and return the result as a Value."
+  },
+
   { "write", gdbpy_write, METH_VARARGS,
     "Write a string using gdb's filtered stream." },
   { "flush", gdbpy_flush, METH_NOARGS,
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
index 2958233..01f6023 100644
--- a/gdb/testsuite/gdb.python/py-value.exp
+++ b/gdb/testsuite/gdb.python/py-value.exp
@@ -361,6 +361,17 @@ proc test_subscript_regression {lang} {
  gdb_test "python print marray\[1\]\[2\]" "o." "Test multiple subscript"
 }
 
+# A few tests of gdb.parse_and_eval.
+proc test_parse_and_eval {} {
+  gdb_test "python print gdb.parse_and_eval ('23')" "23" \
+    "parse_and_eval constant test"
+  gdb_test "python print gdb.parse_and_eval ('5 + 7')" "12" \
+    "parse_and_eval simple expression test"
+  gdb_test "python print type(gdb.parse_and_eval ('5 + 7'))" \
+    ".type 'gdb.Value'."\
+    "parse_and_eval type test"
+}
+
 # Start with a fresh gdb.
 
 gdb_exit
@@ -381,6 +392,7 @@ test_value_numeric_ops
 test_value_boolean
 test_value_compare
 test_objfiles
+test_parse_and_eval
 
 # The following tests require execution.
 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Patch: add gdb.parse_and_eval
  2009-12-02 21:57   ` Tom Tromey
@ 2009-12-03  3:51     ` Eli Zaretskii
  0 siblings, 0 replies; 4+ messages in thread
From: Eli Zaretskii @ 2009-12-03  3:51 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

> From: Tom Tromey <tromey@redhat.com>
> Cc: gdb-patches@sourceware.org
> Date: Wed, 02 Dec 2009 14:57:40 -0700
> 
> >>>>> "Eli" == Eli Zaretskii <eliz@gnu.org> writes:
> 
> >> +@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
> 
> Eli> Thanks.  This is okay, but given the recent discussions which
> Eli> indicated that this feature is necessary for a broad class of scripts,
> Eli> I would expect a bit more text in the manual about it, describing when
> Eli> and how this would be useful.
> 
> How about this?

Fine with me, thanks.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-12-03  3:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-06-11 20:49 Patch: add gdb.parse_and_eval Tom Tromey
2009-06-11 22:40 ` Eli Zaretskii
2009-12-02 21:57   ` Tom Tromey
2009-12-03  3:51     ` Eli Zaretskii

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox