From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 31154 invoked by alias); 11 Jun 2009 20:49:28 -0000 Received: (qmail 31144 invoked by uid 22791); 11 Jun 2009 20:49:27 -0000 X-SWARE-Spam-Status: No, hits=-1.7 required=5.0 tests=AWL,BAYES_00,J_CHICKENPOX_35,J_CHICKENPOX_37,SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: sourceware.org Received: from mx2.redhat.com (HELO mx2.redhat.com) (66.187.237.31) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 11 Jun 2009 20:49:21 +0000 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n5BKnJKa032177 for ; Thu, 11 Jun 2009 16:49:19 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n5BKnI6b002287; Thu, 11 Jun 2009 16:49:18 -0400 Received: from opsy.redhat.com (vpn-12-124.rdu.redhat.com [10.11.12.124]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n5BKnH9s025790; Thu, 11 Jun 2009 16:49:17 -0400 Received: by opsy.redhat.com (Postfix, from userid 500) id 72797486A4; Thu, 11 Jun 2009 14:49:16 -0600 (MDT) To: gdb-patches@sourceware.org Subject: Patch: add gdb.parse_and_eval From: Tom Tromey Reply-To: tromey@redhat.com Date: Thu, 11 Jun 2009 20:49:00 -0000 Message-ID: User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2009-06/txt/msg00326.txt.bz2 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 * python/python.c (gdbpy_parse_and_eval): New function. (GdbMethods): Add "parse_and_eval". 2009-06-11 Tom Tromey * gdb.texinfo (Basic Python): Document gdb.parse_and_eval. 2009-06-11 Tom Tromey * 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); +} + /* 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.